You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

492 lines
17 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.XTemplate
  17. * @extends Ext.Template
  18. * <p>A template class that supports advanced functionality like:<div class="mdetail-params"><ul>
  19. * <li>Autofilling arrays using templates and sub-templates</li>
  20. * <li>Conditional processing with basic comparison operators</li>
  21. * <li>Basic math function support</li>
  22. * <li>Execute arbitrary inline code with special built-in template variables</li>
  23. * <li>Custom member functions</li>
  24. * <li>Many special tags and built-in operators that aren't defined as part of
  25. * the API, but are supported in the templates that can be created</li>
  26. * </ul></div></p>
  27. * <p>XTemplate provides the templating mechanism built into:<div class="mdetail-params"><ul>
  28. * <li>{@link Ext.DataView}</li>
  29. * <li>{@link Ext.ListView}</li>
  30. * <li>{@link Ext.form.ComboBox}</li>
  31. * <li>{@link Ext.grid.TemplateColumn}</li>
  32. * <li>{@link Ext.grid.GroupingView}</li>
  33. * <li>{@link Ext.menu.Item}</li>
  34. * <li>{@link Ext.layout.MenuLayout}</li>
  35. * <li>{@link Ext.ColorPalette}</li>
  36. * </ul></div></p>
  37. *
  38. * <p>For example usage {@link #XTemplate see the constructor}.</p>
  39. *
  40. * @constructor
  41. * The {@link Ext.Template#Template Ext.Template constructor} describes
  42. * the acceptable parameters to pass to the constructor. The following
  43. * examples demonstrate all of the supported features.</p>
  44. *
  45. * <div class="mdetail-params"><ul>
  46. *
  47. * <li><b><u>Sample Data</u></b>
  48. * <div class="sub-desc">
  49. * <p>This is the data object used for reference in each code example:</p>
  50. * <pre><code>
  51. var data = {
  52. name: 'Jack Slocum',
  53. title: 'Lead Developer',
  54. company: 'Ext JS, LLC',
  55. email: 'jack@extjs.com',
  56. address: '4 Red Bulls Drive',
  57. city: 'Cleveland',
  58. state: 'Ohio',
  59. zip: '44102',
  60. drinks: ['Red Bull', 'Coffee', 'Water'],
  61. kids: [{
  62. name: 'Sara Grace',
  63. age:3
  64. },{
  65. name: 'Zachary',
  66. age:2
  67. },{
  68. name: 'John James',
  69. age:0
  70. }]
  71. };
  72. * </code></pre>
  73. * </div>
  74. * </li>
  75. *
  76. *
  77. * <li><b><u>Auto filling of arrays</u></b>
  78. * <div class="sub-desc">
  79. * <p>The <b><tt>tpl</tt></b> tag and the <b><tt>for</tt></b> operator are used
  80. * to process the provided data object:
  81. * <ul>
  82. * <li>If the value specified in <tt>for</tt> is an array, it will auto-fill,
  83. * repeating the template block inside the <tt>tpl</tt> tag for each item in the
  84. * array.</li>
  85. * <li>If <tt>for="."</tt> is specified, the data object provided is examined.</li>
  86. * <li>While processing an array, the special variable <tt>{#}</tt>
  87. * will provide the current array index + 1 (starts at 1, not 0).</li>
  88. * </ul>
  89. * </p>
  90. * <pre><code>
  91. &lt;tpl <b>for</b>=".">...&lt;/tpl> // loop through array at root node
  92. &lt;tpl <b>for</b>="foo">...&lt;/tpl> // loop through array at foo node
  93. &lt;tpl <b>for</b>="foo.bar">...&lt;/tpl> // loop through array at foo.bar node
  94. * </code></pre>
  95. * Using the sample data above:
  96. * <pre><code>
  97. var tpl = new Ext.XTemplate(
  98. '&lt;p>Kids: ',
  99. '&lt;tpl <b>for</b>=".">', // process the data.kids node
  100. '&lt;p>{#}. {name}&lt;/p>', // use current array index to autonumber
  101. '&lt;/tpl>&lt;/p>'
  102. );
  103. tpl.overwrite(panel.body, data.kids); // pass the kids property of the data object
  104. * </code></pre>
  105. * <p>An example illustrating how the <b><tt>for</tt></b> property can be leveraged
  106. * to access specified members of the provided data object to populate the template:</p>
  107. * <pre><code>
  108. var tpl = new Ext.XTemplate(
  109. '&lt;p>Name: {name}&lt;/p>',
  110. '&lt;p>Title: {title}&lt;/p>',
  111. '&lt;p>Company: {company}&lt;/p>',
  112. '&lt;p>Kids: ',
  113. '&lt;tpl <b>for="kids"</b>>', // interrogate the kids property within the data
  114. '&lt;p>{name}&lt;/p>',
  115. '&lt;/tpl>&lt;/p>'
  116. );
  117. tpl.overwrite(panel.body, data); // pass the root node of the data object
  118. * </code></pre>
  119. * <p>Flat arrays that contain values (and not objects) can be auto-rendered
  120. * using the special <b><tt>{.}</tt></b> variable inside a loop. This variable
  121. * will represent the value of the array at the current index:</p>
  122. * <pre><code>
  123. var tpl = new Ext.XTemplate(
  124. '&lt;p>{name}\&#39;s favorite beverages:&lt;/p>',
  125. '&lt;tpl for="drinks">',
  126. '&lt;div> - {.}&lt;/div>',
  127. '&lt;/tpl>'
  128. );
  129. tpl.overwrite(panel.body, data);
  130. * </code></pre>
  131. * <p>When processing a sub-template, for example while looping through a child array,
  132. * you can access the parent object's members via the <b><tt>parent</tt></b> object:</p>
  133. * <pre><code>
  134. var tpl = new Ext.XTemplate(
  135. '&lt;p>Name: {name}&lt;/p>',
  136. '&lt;p>Kids: ',
  137. '&lt;tpl for="kids">',
  138. '&lt;tpl if="age > 1">',
  139. '&lt;p>{name}&lt;/p>',
  140. '&lt;p>Dad: {<b>parent</b>.name}&lt;/p>',
  141. '&lt;/tpl>',
  142. '&lt;/tpl>&lt;/p>'
  143. );
  144. tpl.overwrite(panel.body, data);
  145. * </code></pre>
  146. * </div>
  147. * </li>
  148. *
  149. *
  150. * <li><b><u>Conditional processing with basic comparison operators</u></b>
  151. * <div class="sub-desc">
  152. * <p>The <b><tt>tpl</tt></b> tag and the <b><tt>if</tt></b> operator are used
  153. * to provide conditional checks for deciding whether or not to render specific
  154. * parts of the template. Notes:<div class="sub-desc"><ul>
  155. * <li>Double quotes must be encoded if used within the conditional</li>
  156. * <li>There is no <tt>else</tt> operator &mdash; if needed, two opposite
  157. * <tt>if</tt> statements should be used.</li>
  158. * </ul></div>
  159. * <pre><code>
  160. &lt;tpl if="age &gt; 1 &amp;&amp; age &lt; 10">Child&lt;/tpl>
  161. &lt;tpl if="age >= 10 && age < 18">Teenager&lt;/tpl>
  162. &lt;tpl <b>if</b>="this.isGirl(name)">...&lt;/tpl>
  163. &lt;tpl <b>if</b>="id==\'download\'">...&lt;/tpl>
  164. &lt;tpl <b>if</b>="needsIcon">&lt;img src="{icon}" class="{iconCls}"/>&lt;/tpl>
  165. // no good:
  166. &lt;tpl if="name == "Jack"">Hello&lt;/tpl>
  167. // encode &#34; if it is part of the condition, e.g.
  168. &lt;tpl if="name == &#38;quot;Jack&#38;quot;">Hello&lt;/tpl>
  169. * </code></pre>
  170. * Using the sample data above:
  171. * <pre><code>
  172. var tpl = new Ext.XTemplate(
  173. '&lt;p>Name: {name}&lt;/p>',
  174. '&lt;p>Kids: ',
  175. '&lt;tpl for="kids">',
  176. '&lt;tpl if="age > 1">',
  177. '&lt;p>{name}&lt;/p>',
  178. '&lt;/tpl>',
  179. '&lt;/tpl>&lt;/p>'
  180. );
  181. tpl.overwrite(panel.body, data);
  182. * </code></pre>
  183. * </div>
  184. * </li>
  185. *
  186. *
  187. * <li><b><u>Basic math support</u></b>
  188. * <div class="sub-desc">
  189. * <p>The following basic math operators may be applied directly on numeric
  190. * data values:</p><pre>
  191. * + - * /
  192. * </pre>
  193. * For example:
  194. * <pre><code>
  195. var tpl = new Ext.XTemplate(
  196. '&lt;p>Name: {name}&lt;/p>',
  197. '&lt;p>Kids: ',
  198. '&lt;tpl for="kids">',
  199. '&lt;tpl if="age &amp;gt; 1">', // <-- Note that the &gt; is encoded
  200. '&lt;p>{#}: {name}&lt;/p>', // <-- Auto-number each item
  201. '&lt;p>In 5 Years: {age+5}&lt;/p>', // <-- Basic math
  202. '&lt;p>Dad: {parent.name}&lt;/p>',
  203. '&lt;/tpl>',
  204. '&lt;/tpl>&lt;/p>'
  205. );
  206. tpl.overwrite(panel.body, data);
  207. </code></pre>
  208. * </div>
  209. * </li>
  210. *
  211. *
  212. * <li><b><u>Execute arbitrary inline code with special built-in template variables</u></b>
  213. * <div class="sub-desc">
  214. * <p>Anything between <code>{[ ... ]}</code> is considered code to be executed
  215. * in the scope of the template. There are some special variables available in that code:
  216. * <ul>
  217. * <li><b><tt>values</tt></b>: The values in the current scope. If you are using
  218. * scope changing sub-templates, you can change what <tt>values</tt> is.</li>
  219. * <li><b><tt>parent</tt></b>: The scope (values) of the ancestor template.</li>
  220. * <li><b><tt>xindex</tt></b>: If you are in a looping template, the index of the
  221. * loop you are in (1-based).</li>
  222. * <li><b><tt>xcount</tt></b>: If you are in a looping template, the total length
  223. * of the array you are looping.</li>
  224. * <li><b><tt>fm</tt></b>: An alias for <tt>Ext.util.Format</tt>.</li>
  225. * </ul>
  226. * This example demonstrates basic row striping using an inline code block and the
  227. * <tt>xindex</tt> variable:</p>
  228. * <pre><code>
  229. var tpl = new Ext.XTemplate(
  230. '&lt;p>Name: {name}&lt;/p>',
  231. '&lt;p>Company: {[values.company.toUpperCase() + ", " + values.title]}&lt;/p>',
  232. '&lt;p>Kids: ',
  233. '&lt;tpl for="kids">',
  234. '&lt;div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
  235. '{name}',
  236. '&lt;/div>',
  237. '&lt;/tpl>&lt;/p>'
  238. );
  239. tpl.overwrite(panel.body, data);
  240. * </code></pre>
  241. * </div>
  242. * </li>
  243. *
  244. * <li><b><u>Template member functions</u></b>
  245. * <div class="sub-desc">
  246. * <p>One or more member functions can be specified in a configuration
  247. * object passed into the XTemplate constructor for more complex processing:</p>
  248. * <pre><code>
  249. var tpl = new Ext.XTemplate(
  250. '&lt;p>Name: {name}&lt;/p>',
  251. '&lt;p>Kids: ',
  252. '&lt;tpl for="kids">',
  253. '&lt;tpl if="this.isGirl(name)">',
  254. '&lt;p>Girl: {name} - {age}&lt;/p>',
  255. '&lt;/tpl>',
  256. // use opposite if statement to simulate 'else' processing:
  257. '&lt;tpl if="this.isGirl(name) == false">',
  258. '&lt;p>Boy: {name} - {age}&lt;/p>',
  259. '&lt;/tpl>',
  260. '&lt;tpl if="this.isBaby(age)">',
  261. '&lt;p>{name} is a baby!&lt;/p>',
  262. '&lt;/tpl>',
  263. '&lt;/tpl>&lt;/p>',
  264. {
  265. // XTemplate configuration:
  266. compiled: true,
  267. disableFormats: true,
  268. // member functions:
  269. isGirl: function(name){
  270. return name == 'Sara Grace';
  271. },
  272. isBaby: function(age){
  273. return age < 1;
  274. }
  275. }
  276. );
  277. tpl.overwrite(panel.body, data);
  278. * </code></pre>
  279. * </div>
  280. * </li>
  281. *
  282. * </ul></div>
  283. *
  284. * @param {Mixed} config
  285. */
  286. Ext.XTemplate = function(){
  287. Ext.XTemplate.superclass.constructor.apply(this, arguments);
  288. var me = this,
  289. s = me.html,
  290. re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
  291. nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
  292. ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
  293. execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
  294. m,
  295. id = 0,
  296. tpls = [],
  297. VALUES = 'values',
  298. PARENT = 'parent',
  299. XINDEX = 'xindex',
  300. XCOUNT = 'xcount',
  301. RETURN = 'return ',
  302. WITHVALUES = 'with(values){ ';
  303. s = ['<tpl>', s, '</tpl>'].join('');
  304. while((m = s.match(re))){
  305. var m2 = m[0].match(nameRe),
  306. m3 = m[0].match(ifRe),
  307. m4 = m[0].match(execRe),
  308. exp = null,
  309. fn = null,
  310. exec = null,
  311. name = m2 && m2[1] ? m2[1] : '';
  312. if (m3) {
  313. exp = m3 && m3[1] ? m3[1] : null;
  314. if(exp){
  315. fn = new Function(VALUES, PARENT, XINDEX, XCOUNT, WITHVALUES + RETURN +(Ext.util.Format.htmlDecode(exp))+'; }');
  316. }
  317. }
  318. if (m4) {
  319. exp = m4 && m4[1] ? m4[1] : null;
  320. if(exp){
  321. exec = new Function(VALUES, PARENT, XINDEX, XCOUNT, WITHVALUES +(Ext.util.Format.htmlDecode(exp))+'; }');
  322. }
  323. }
  324. if(name){
  325. switch(name){
  326. case '.': name = new Function(VALUES, PARENT, WITHVALUES + RETURN + VALUES + '; }'); break;
  327. case '..': name = new Function(VALUES, PARENT, WITHVALUES + RETURN + PARENT + '; }'); break;
  328. default: name = new Function(VALUES, PARENT, WITHVALUES + RETURN + name + '; }');
  329. }
  330. }
  331. tpls.push({
  332. id: id,
  333. target: name,
  334. exec: exec,
  335. test: fn,
  336. body: m[1]||''
  337. });
  338. s = s.replace(m[0], '{xtpl'+ id + '}');
  339. ++id;
  340. }
  341. for(var i = tpls.length-1; i >= 0; --i){
  342. me.compileTpl(tpls[i]);
  343. }
  344. me.master = tpls[tpls.length-1];
  345. me.tpls = tpls;
  346. };
  347. Ext.extend(Ext.XTemplate, Ext.Template, {
  348. // private
  349. re : /\{([\w\-\.\#]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?(\s?[\+\-\*\\]\s?[\d\.\+\-\*\\\(\)]+)?\}/g,
  350. // private
  351. codeRe : /\{\[((?:\\\]|.|\n)*?)\]\}/g,
  352. // private
  353. applySubTemplate : function(id, values, parent, xindex, xcount){
  354. var me = this,
  355. len,
  356. t = me.tpls[id],
  357. vs,
  358. buf = [];
  359. if ((t.test && !t.test.call(me, values, parent, xindex, xcount)) ||
  360. (t.exec && t.exec.call(me, values, parent, xindex, xcount))) {
  361. return '';
  362. }
  363. vs = t.target ? t.target.call(me, values, parent) : values;
  364. len = vs.length;
  365. parent = t.target ? values : parent;
  366. if(t.target && Ext.isArray(vs)){
  367. for(var i = 0, len = vs.length; i < len; i++){
  368. buf[buf.length] = t.compiled.call(me, vs[i], parent, i+1, len);
  369. }
  370. return buf.join('');
  371. }
  372. return t.compiled.call(me, vs, parent, xindex, xcount);
  373. },
  374. // private
  375. compileTpl : function(tpl){
  376. var fm = Ext.util.Format,
  377. useF = this.disableFormats !== true,
  378. sep = Ext.isGecko ? "+" : ",",
  379. body;
  380. function fn(m, name, format, args, math){
  381. if(name.substr(0, 4) == 'xtpl'){
  382. return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent, xindex, xcount)'+sep+"'";
  383. }
  384. var v;
  385. if(name === '.'){
  386. v = 'values';
  387. }else if(name === '#'){
  388. v = 'xindex';
  389. }else if(name.indexOf('.') != -1){
  390. v = name;
  391. }else{
  392. v = "values['" + name + "']";
  393. }
  394. if(math){
  395. v = '(' + v + math + ')';
  396. }
  397. if (format && useF) {
  398. args = args ? ',' + args : "";
  399. if(format.substr(0, 5) != "this."){
  400. format = "fm." + format + '(';
  401. }else{
  402. format = 'this.call("'+ format.substr(5) + '", ';
  403. args = ", values";
  404. }
  405. } else {
  406. args= ''; format = "("+v+" === undefined ? '' : ";
  407. }
  408. return "'"+ sep + format + v + args + ")"+sep+"'";
  409. }
  410. function codeFn(m, code){
  411. // Single quotes get escaped when the template is compiled, however we want to undo this when running code.
  412. return "'" + sep + '(' + code.replace(/\\'/g, "'") + ')' + sep + "'";
  413. }
  414. // branched to use + in gecko and [].join() in others
  415. if(Ext.isGecko){
  416. body = "tpl.compiled = function(values, parent, xindex, xcount){ return '" +
  417. tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn) +
  418. "';};";
  419. }else{
  420. body = ["tpl.compiled = function(values, parent, xindex, xcount){ return ['"];
  421. body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn));
  422. body.push("'].join('');};");
  423. body = body.join('');
  424. }
  425. eval(body);
  426. return this;
  427. },
  428. /**
  429. * Returns an HTML fragment of this template with the specified values applied.
  430. * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
  431. * @return {String} The HTML fragment
  432. */
  433. applyTemplate : function(values){
  434. return this.master.compiled.call(this, values, {}, 1, 1);
  435. },
  436. /**
  437. * Compile the template to a function for optimized performance. Recommended if the template will be used frequently.
  438. * @return {Function} The compiled function
  439. */
  440. compile : function(){return this;}
  441. /**
  442. * @property re
  443. * @hide
  444. */
  445. /**
  446. * @property disableFormats
  447. * @hide
  448. */
  449. /**
  450. * @method set
  451. * @hide
  452. */
  453. });
  454. /**
  455. * Alias for {@link #applyTemplate}
  456. * Returns an HTML fragment of this template with the specified values applied.
  457. * @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'})
  458. * @return {String} The HTML fragment
  459. * @member Ext.XTemplate
  460. * @method apply
  461. */
  462. Ext.XTemplate.prototype.apply = Ext.XTemplate.prototype.applyTemplate;
  463. /**
  464. * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
  465. * @param {String/HTMLElement} el A DOM element or its id
  466. * @return {Ext.Template} The created template
  467. * @static
  468. */
  469. Ext.XTemplate.from = function(el){
  470. el = Ext.getDom(el);
  471. return new Ext.XTemplate(el.value || el.innerHTML);
  472. };