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