/hippo/src/main/webapp/ext/src/core/Template-more.js

http://hdbc.googlecode.com/ · JavaScript · 115 lines · 72 code · 7 blank · 36 comment · 15 complexity · ca66a774ac025d9475381420659a7bfd MD5 · raw file

  1. /*!
  2. * Ext JS Library 3.0.0
  3. * Copyright(c) 2006-2009 Ext JS, LLC
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. /**
  8. * @class Ext.Template
  9. */
  10. Ext.apply(Ext.Template.prototype, {
  11. /**
  12. * Returns an HTML fragment of this template with the specified values applied.
  13. * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
  14. * @return {String} The HTML fragment
  15. * @hide repeat doc
  16. */
  17. applyTemplate : function(values){
  18. var me = this,
  19. useF = me.disableFormats !== true,
  20. fm = Ext.util.Format,
  21. tpl = me;
  22. if(me.compiled){
  23. return me.compiled(values);
  24. }
  25. function fn(m, name, format, args){
  26. if (format && useF) {
  27. if (format.substr(0, 5) == "this.") {
  28. return tpl.call(format.substr(5), values[name], values);
  29. } else {
  30. if (args) {
  31. // quoted values are required for strings in compiled templates,
  32. // but for non compiled we need to strip them
  33. // quoted reversed for jsmin
  34. var re = /^\s*['"](.*)["']\s*$/;
  35. args = args.split(',');
  36. for(var i = 0, len = args.length; i < len; i++){
  37. args[i] = args[i].replace(re, "$1");
  38. }
  39. args = [values[name]].concat(args);
  40. } else {
  41. args = [values[name]];
  42. }
  43. return fm[format].apply(fm, args);
  44. }
  45. } else {
  46. return values[name] !== undefined ? values[name] : "";
  47. }
  48. }
  49. return me.html.replace(me.re, fn);
  50. },
  51. /**
  52. * <tt>true</tt> to disable format functions (defaults to <tt>false</tt>)
  53. * @type Boolean
  54. * @property
  55. */
  56. disableFormats : false,
  57. /**
  58. * The regular expression used to match template variables
  59. * @type RegExp
  60. * @property
  61. * @hide repeat doc
  62. */
  63. re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
  64. /**
  65. * Compiles the template into an internal function, eliminating the RegEx overhead.
  66. * @return {Ext.Template} this
  67. * @hide repeat doc
  68. */
  69. compile : function(){
  70. var me = this,
  71. fm = Ext.util.Format,
  72. useF = me.disableFormats !== true,
  73. sep = Ext.isGecko ? "+" : ",",
  74. body;
  75. function fn(m, name, format, args){
  76. if(format && useF){
  77. args = args ? ',' + args : "";
  78. if(format.substr(0, 5) != "this."){
  79. format = "fm." + format + '(';
  80. }else{
  81. format = 'this.call("'+ format.substr(5) + '", ';
  82. args = ", values";
  83. }
  84. }else{
  85. args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
  86. }
  87. return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
  88. }
  89. // branched to use + in gecko and [].join() in others
  90. if(Ext.isGecko){
  91. body = "this.compiled = function(values){ return '" +
  92. me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
  93. "';};";
  94. }else{
  95. body = ["this.compiled = function(values){ return ['"];
  96. body.push(me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
  97. body.push("'].join('');};");
  98. body = body.join('');
  99. }
  100. eval(body);
  101. return me;
  102. },
  103. // private function used to call members
  104. call : function(fnName, value, allValues){
  105. return this[fnName](value, allValues);
  106. }
  107. });
  108. Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;