fc508554e7f9bea9f70773d48b25e45c33ff94c6.svn-base 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.XTemplate = function() {
  7. Ext.XTemplate.superclass.constructor.apply(this, arguments);
  8. var P = this.html;
  9. P = ["<tpl>", P, "</tpl>"].join("");
  10. var O = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
  11. var N = /^<tpl\b[^>]*?for="(.*?)"/;
  12. var L = /^<tpl\b[^>]*?if="(.*?)"/;
  13. var J = /^<tpl\b[^>]*?exec="(.*?)"/;
  14. var C, B = 0;
  15. var G = [];
  16. while (C = P.match(O)) {
  17. var M = C[0].match(N);
  18. var K = C[0].match(L);
  19. var I = C[0].match(J);
  20. var E = null, H = null, D = null;
  21. var A = M && M[1] ? M[1] : "";
  22. if (K) {
  23. E = K && K[1] ? K[1] : null;
  24. if (E) {
  25. H = new Function("values", "parent", "xindex", "xcount",
  26. "with(values){ return "
  27. + (Ext.util.Format.htmlDecode(E)) + "; }")
  28. }
  29. }
  30. if (I) {
  31. E = I && I[1] ? I[1] : null;
  32. if (E) {
  33. D = new Function("values", "parent", "xindex", "xcount",
  34. "with(values){ " + (Ext.util.Format.htmlDecode(E))
  35. + "; }")
  36. }
  37. }
  38. if (A) {
  39. switch (A) {
  40. case "." :
  41. A = new Function("values", "parent",
  42. "with(values){ return values; }");
  43. break;
  44. case ".." :
  45. A = new Function("values", "parent",
  46. "with(values){ return parent; }");
  47. break;
  48. default :
  49. A = new Function("values", "parent",
  50. "with(values){ return " + A + "; }")
  51. }
  52. }
  53. G.push({
  54. id : B,
  55. target : A,
  56. exec : D,
  57. test : H,
  58. body : C[1] || ""
  59. });
  60. P = P.replace(C[0], "{xtpl" + B + "}");
  61. ++B
  62. }
  63. for (var F = G.length - 1; F >= 0; --F) {
  64. this.compileTpl(G[F])
  65. }
  66. this.master = G[G.length - 1];
  67. this.tpls = G
  68. };
  69. Ext.extend(Ext.XTemplate, Ext.Template, {
  70. re : /\{([\w-\.\#]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?(\s?[\+\-\*\\]\s?[\d\.\+\-\*\\\(\)]+)?\}/g,
  71. codeRe : /\{\[((?:\\\]|.|\n)*?)\]\}/g,
  72. applySubTemplate : function(A, H, G, D, C) {
  73. var J = this.tpls[A];
  74. if (J.test && !J.test.call(this, H, G, D, C)) {
  75. return ""
  76. }
  77. if (J.exec && J.exec.call(this, H, G, D, C)) {
  78. return ""
  79. }
  80. var I = J.target ? J.target.call(this, H, G) : H;
  81. G = J.target ? H : G;
  82. if (J.target && I instanceof Array) {
  83. var B = [];
  84. for (var E = 0, F = I.length; E < F; E++) {
  85. B[B.length] = J.compiled.call(this, I[E], G, E + 1, F)
  86. }
  87. return B.join("")
  88. }
  89. return J.compiled.call(this, I, G, D, C)
  90. },
  91. compileTpl : function(tpl) {
  92. var fm = Ext.util.Format;
  93. var useF = this.disableFormats !== true;
  94. var sep = Ext.isGecko ? "+" : ",";
  95. var fn = function(m, name, format, args, math) {
  96. if (name.substr(0, 4) == "xtpl") {
  97. return "'" + sep + "this.applySubTemplate(" + name.substr(4)
  98. + ", values, parent, xindex, xcount)" + sep + "'"
  99. }
  100. var v;
  101. if (name === ".") {
  102. v = "values"
  103. } else {
  104. if (name === "#") {
  105. v = "xindex"
  106. } else {
  107. if (name.indexOf(".") != -1) {
  108. v = name
  109. } else {
  110. v = "values['" + name + "']"
  111. }
  112. }
  113. }
  114. if (math) {
  115. v = "(" + v + math + ")"
  116. }
  117. if (format && useF) {
  118. args = args ? "," + args : "";
  119. if (format.substr(0, 5) != "this.") {
  120. format = "fm." + format + "("
  121. } else {
  122. format = "this.call(\"" + format.substr(5) + "\", ";
  123. args = ", values"
  124. }
  125. } else {
  126. args = "";
  127. format = "(" + v + " === undefined ? '' : "
  128. }
  129. return "'" + sep + format + v + args + ")" + sep + "'"
  130. };
  131. var codeFn = function(m, code) {
  132. return "'" + sep + "(" + code + ")" + sep + "'"
  133. };
  134. var body;
  135. if (Ext.isGecko) {
  136. body = "tpl.compiled = function(values, parent, xindex, xcount){ return '"
  137. + tpl.body.replace(/(\r\n|\n)/g, "\\n")
  138. .replace(/'/g, "\\'").replace(this.re, fn).replace(
  139. this.codeRe, codeFn) + "';};"
  140. } else {
  141. body = ["tpl.compiled = function(values, parent, xindex, xcount){ return ['"];
  142. body.push(tpl.body.replace(/(\r\n|\n)/g, "\\n")
  143. .replace(/'/g, "\\'").replace(this.re, fn).replace(
  144. this.codeRe, codeFn));
  145. body.push("'].join('');};");
  146. body = body.join("")
  147. }
  148. eval(body);
  149. return this
  150. },
  151. apply : function(A) {
  152. return this.master.compiled.call(this, A, {}, 1, 1)
  153. },
  154. applyTemplate : function(A) {
  155. return this.master.compiled.call(this, A, {}, 1, 1)
  156. },
  157. compile : function() {
  158. return this
  159. }
  160. });
  161. Ext.XTemplate.from = function(A) {
  162. A = Ext.getDom(A);
  163. return new Ext.XTemplate(A.value || A.innerHTML)
  164. };