Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / dtl / _base.js @ 12

History | View | Annotate | Download (13.3 KB)

1 9 andrej.cim
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6
7
8
if(!dojo._hasResource["dojox.dtl._base"]){
9
dojo._hasResource["dojox.dtl._base"]=true;
10
dojo.provide("dojox.dtl._base");
11
dojo.require("dojox.string.Builder");
12
dojo.require("dojox.string.tokenize");
13
dojo.experimental("dojox.dtl");
14
(function(){
15
var dd=dojox.dtl;
16
dd.TOKEN_BLOCK=-1;
17
dd.TOKEN_VAR=-2;
18
dd.TOKEN_COMMENT=-3;
19
dd.TOKEN_TEXT=3;
20
dd._Context=dojo.extend(function(_1){
21
if(_1){
22
dojo._mixin(this,_1);
23
if(_1.get){
24
this._getter=_1.get;
25
delete this.get;
26
}
27
}
28
},{push:function(){
29
var _2=this;
30
var _3=dojo.delegate(this);
31
_3.pop=function(){
32
return _2;
33
};
34
return _3;
35
},pop:function(){
36
throw new Error("pop() called on empty Context");
37
},get:function(_4,_5){
38
var n=this._normalize;
39
if(this._getter){
40
var _6=this._getter(_4);
41
if(typeof _6!="undefined"){
42
return n(_6);
43
}
44
}
45
if(typeof this[_4]!="undefined"){
46
return n(this[_4]);
47
}
48
return _5;
49
},_normalize:function(_7){
50
if(_7 instanceof Date){
51
_7.year=_7.getFullYear();
52
_7.month=_7.getMonth()+1;
53
_7.day=_7.getDate();
54
_7.date=_7.year+"-"+("0"+_7.month).slice(-2)+"-"+("0"+_7.day).slice(-2);
55
_7.hour=_7.getHours();
56
_7.minute=_7.getMinutes();
57
_7.second=_7.getSeconds();
58
_7.microsecond=_7.getMilliseconds();
59
}
60
return _7;
61
},update:function(_8){
62
var _9=this.push();
63
if(_8){
64
dojo._mixin(this,_8);
65
}
66
return _9;
67
}});
68
var _a=/("(?:[^"\\]*(?:\\.[^"\\]*)*)"|'(?:[^'\\]*(?:\\.[^'\\]*)*)'|[^\s]+)/g;
69
var _b=/\s+/g;
70
var _c=function(_d,_e){
71
_d=_d||_b;
72
if(!(_d instanceof RegExp)){
73
_d=new RegExp(_d,"g");
74
}
75
if(!_d.global){
76
throw new Error("You must use a globally flagged RegExp with split "+_d);
77
}
78
_d.exec("");
79
var _f,_10=[],_11=0,i=0;
80
while(_f=_d.exec(this)){
81
_10.push(this.slice(_11,_d.lastIndex-_f[0].length));
82
_11=_d.lastIndex;
83
if(_e&&(++i>_e-1)){
84
break;
85
}
86
}
87
_10.push(this.slice(_11));
88
return _10;
89
};
90
dd.Token=function(_12,_13){
91
this.token_type=_12;
92
this.contents=new String(dojo.trim(_13));
93
this.contents.split=_c;
94
this.split=function(){
95
return String.prototype.split.apply(this.contents,arguments);
96
};
97
};
98
dd.Token.prototype.split_contents=function(_14){
99
var bit,_15=[],i=0;
100
_14=_14||999;
101
while(i++<_14&&(bit=_a.exec(this.contents))){
102
bit=bit[0];
103
if(bit.charAt(0)=="\""&&bit.slice(-1)=="\""){
104
_15.push("\""+bit.slice(1,-1).replace("\\\"","\"").replace("\\\\","\\")+"\"");
105
}else{
106
if(bit.charAt(0)=="'"&&bit.slice(-1)=="'"){
107
_15.push("'"+bit.slice(1,-1).replace("\\'","'").replace("\\\\","\\")+"'");
108
}else{
109
_15.push(bit);
110
}
111
}
112
}
113
return _15;
114
};
115
var ddt=dd.text={_get:function(_16,_17,_18){
116
var _19=dd.register.get(_16,_17.toLowerCase(),_18);
117
if(!_19){
118
if(!_18){
119
throw new Error("No tag found for "+_17);
120
}
121
return null;
122
}
123
var fn=_19[1];
124
var _1a=_19[2];
125
var _1b;
126
if(fn.indexOf(":")!=-1){
127
_1b=fn.split(":");
128
fn=_1b.pop();
129
}
130
dojo["require"](_1a);
131
var _1c=dojo.getObject(_1a);
132
return _1c[fn||_17]||_1c[_17+"_"]||_1c[fn+"_"];
133
},getTag:function(_1d,_1e){
134
return ddt._get("tag",_1d,_1e);
135
},getFilter:function(_1f,_20){
136
return ddt._get("filter",_1f,_20);
137
},getTemplate:function(_21){
138
return new dd.Template(ddt.getTemplateString(_21));
139
},getTemplateString:function(_22){
140
return dojo._getText(_22.toString())||"";
141
},_resolveLazy:function(_23,_24,_25){
142
if(_24){
143
if(_25){
144
return dojo.fromJson(dojo._getText(_23))||{};
145
}else{
146
return dd.text.getTemplateString(_23);
147
}
148
}else{
149
return dojo.xhrGet({handleAs:(_25)?"json":"text",url:_23});
150
}
151
},_resolveTemplateArg:function(arg,_26){
152
if(ddt._isTemplate(arg)){
153
if(!_26){
154
var d=new dojo.Deferred();
155
d.callback(arg);
156
return d;
157
}
158
return arg;
159
}
160
return ddt._resolveLazy(arg,_26);
161
},_isTemplate:function(arg){
162
return (typeof arg=="undefined")||(typeof arg=="string"&&(arg.match(/^\s*[<{]/)||arg.indexOf(" ")!=-1));
163
},_resolveContextArg:function(arg,_27){
164
if(arg.constructor==Object){
165
if(!_27){
166
var d=new dojo.Deferred;
167
d.callback(arg);
168
return d;
169
}
170
return arg;
171
}
172
return ddt._resolveLazy(arg,_27,true);
173
},_re:/(?:\{\{\s*(.+?)\s*\}\}|\{%\s*(load\s*)?(.+?)\s*%\})/g,tokenize:function(str){
174
return dojox.string.tokenize(str,ddt._re,ddt._parseDelims);
175
},_parseDelims:function(_28,_29,tag){
176
if(_28){
177
return [dd.TOKEN_VAR,_28];
178
}else{
179
if(_29){
180
var _2a=dojo.trim(tag).split(/\s+/g);
181
for(var i=0,_2b;_2b=_2a[i];i++){
182
dojo["require"](_2b);
183
}
184
}else{
185
return [dd.TOKEN_BLOCK,tag];
186
}
187
}
188
}};
189
dd.Template=dojo.extend(function(_2c,_2d){
190
var str=_2d?_2c:ddt._resolveTemplateArg(_2c,true)||"";
191
var _2e=ddt.tokenize(str);
192
var _2f=new dd._Parser(_2e);
193
this.nodelist=_2f.parse();
194
},{update:function(_30,_31){
195
return ddt._resolveContextArg(_31).addCallback(this,function(_32){
196
var _33=this.render(new dd._Context(_32));
197
if(_30.forEach){
198
_30.forEach(function(_34){
199
_34.innerHTML=_33;
200
});
201
}else{
202
dojo.byId(_30).innerHTML=_33;
203
}
204
return this;
205
});
206
},render:function(_35,_36){
207
_36=_36||this.getBuffer();
208
_35=_35||new dd._Context({});
209
return this.nodelist.render(_35,_36)+"";
210
},getBuffer:function(){
211
dojo.require("dojox.string.Builder");
212
return new dojox.string.Builder();
213
}});
214
var _37=/\{\{\s*(.+?)\s*\}\}/g;
215
dd.quickFilter=function(str){
216
if(!str){
217
return new dd._NodeList();
218
}
219
if(str.indexOf("{%")==-1){
220
return new dd._QuickNodeList(dojox.string.tokenize(str,_37,function(_38){
221
return new dd._Filter(_38);
222
}));
223
}
224
};
225
dd._QuickNodeList=dojo.extend(function(_39){
226
this.contents=_39;
227
},{render:function(_3a,_3b){
228
for(var i=0,l=this.contents.length;i<l;i++){
229
if(this.contents[i].resolve){
230
_3b=_3b.concat(this.contents[i].resolve(_3a));
231
}else{
232
_3b=_3b.concat(this.contents[i]);
233
}
234
}
235
return _3b;
236
},dummyRender:function(_3c){
237
return this.render(_3c,dd.Template.prototype.getBuffer()).toString();
238
},clone:function(_3d){
239
return this;
240
}});
241
dd._Filter=dojo.extend(function(_3e){
242
if(!_3e){
243
throw new Error("Filter must be called with variable name");
244
}
245
this.contents=_3e;
246
var _3f=this._cache[_3e];
247
if(_3f){
248
this.key=_3f[0];
249
this.filters=_3f[1];
250
}else{
251
this.filters=[];
252
dojox.string.tokenize(_3e,this._re,this._tokenize,this);
253
this._cache[_3e]=[this.key,this.filters];
254
}
255
},{_cache:{},_re:/(?:^_\("([^\\"]*(?:\\.[^\\"])*)"\)|^"([^\\"]*(?:\\.[^\\"]*)*)"|^([a-zA-Z0-9_.]+)|\|(\w+)(?::(?:_\("([^\\"]*(?:\\.[^\\"])*)"\)|"([^\\"]*(?:\\.[^\\"]*)*)"|([a-zA-Z0-9_.]+)|'([^\\']*(?:\\.[^\\']*)*)'))?|^'([^\\']*(?:\\.[^\\']*)*)')/g,_values:{0:"\"",1:"\"",2:"",8:"\""},_args:{4:"\"",5:"\"",6:"",7:"'"},_tokenize:function(){
256
var pos,arg;
257
for(var i=0,has=[];i<arguments.length;i++){
258
has[i]=(typeof arguments[i]!="undefined"&&typeof arguments[i]=="string"&&arguments[i]);
259
}
260
if(!this.key){
261
for(pos in this._values){
262
if(has[pos]){
263
this.key=this._values[pos]+arguments[pos]+this._values[pos];
264
break;
265
}
266
}
267
}else{
268
for(pos in this._args){
269
if(has[pos]){
270
var _40=arguments[pos];
271
if(this._args[pos]=="'"){
272
_40=_40.replace(/\\'/g,"'");
273
}else{
274
if(this._args[pos]=="\""){
275
_40=_40.replace(/\\"/g,"\"");
276
}
277
}
278
arg=[!this._args[pos],_40];
279
break;
280
}
281
}
282
var fn=ddt.getFilter(arguments[3]);
283
if(!dojo.isFunction(fn)){
284
throw new Error(arguments[3]+" is not registered as a filter");
285
}
286
this.filters.push([fn,arg]);
287
}
288
},getExpression:function(){
289
return this.contents;
290
},resolve:function(_41){
291
if(typeof this.key=="undefined"){
292
return "";
293
}
294
var str=this.resolvePath(this.key,_41);
295
for(var i=0,_42;_42=this.filters[i];i++){
296
if(_42[1]){
297
if(_42[1][0]){
298
str=_42[0](str,this.resolvePath(_42[1][1],_41));
299
}else{
300
str=_42[0](str,_42[1][1]);
301
}
302
}else{
303
str=_42[0](str);
304
}
305
}
306
return str;
307
},resolvePath:function(_43,_44){
308
var _45,_46;
309
var _47=_43.charAt(0);
310
var _48=_43.slice(-1);
311
if(!isNaN(parseInt(_47))){
312
_45=(_43.indexOf(".")==-1)?parseInt(_43):parseFloat(_43);
313
}else{
314
if(_47=="\""&&_47==_48){
315
_45=_43.slice(1,-1);
316
}else{
317
if(_43=="true"){
318
return true;
319
}
320
if(_43=="false"){
321
return false;
322
}
323
if(_43=="null"||_43=="None"){
324
return null;
325
}
326
_46=_43.split(".");
327
_45=_44.get(_46[0]);
328
if(dojo.isFunction(_45)){
329
var _49=_44.getThis&&_44.getThis();
330
if(_45.alters_data){
331
_45="";
332
}else{
333
if(_49){
334
_45=_45.call(_49);
335
}else{
336
_45="";
337
}
338
}
339
}
340
for(var i=1;i<_46.length;i++){
341
var _4a=_46[i];
342
if(_45){
343
var _4b=_45;
344
if(dojo.isObject(_45)&&_4a=="items"&&typeof _45[_4a]=="undefined"){
345
var _4c=[];
346
for(var key in _45){
347
_4c.push([key,_45[key]]);
348
}
349
_45=_4c;
350
continue;
351
}
352
if(_45.get&&dojo.isFunction(_45.get)&&_45.get.safe){
353
_45=_45.get(_4a);
354
}else{
355
if(typeof _45[_4a]=="undefined"){
356
_45=_45[_4a];
357
break;
358
}else{
359
_45=_45[_4a];
360
}
361
}
362
if(dojo.isFunction(_45)){
363
if(_45.alters_data){
364
_45="";
365
}else{
366
_45=_45.call(_4b);
367
}
368
}else{
369
if(_45 instanceof Date){
370
_45=dd._Context.prototype._normalize(_45);
371
}
372
}
373
}else{
374
return "";
375
}
376
}
377
}
378
}
379
return _45;
380
}});
381
dd._TextNode=dd._Node=dojo.extend(function(obj){
382
this.contents=obj;
383
},{set:function(_4d){
384
this.contents=_4d;
385
return this;
386
},render:function(_4e,_4f){
387
return _4f.concat(this.contents);
388
},isEmpty:function(){
389
return !dojo.trim(this.contents);
390
},clone:function(){
391
return this;
392
}});
393
dd._NodeList=dojo.extend(function(_50){
394
this.contents=_50||[];
395
this.last="";
396
},{push:function(_51){
397
this.contents.push(_51);
398
return this;
399
},concat:function(_52){
400
this.contents=this.contents.concat(_52);
401
return this;
402
},render:function(_53,_54){
403
for(var i=0;i<this.contents.length;i++){
404
_54=this.contents[i].render(_53,_54);
405
if(!_54){
406
throw new Error("Template must return buffer");
407
}
408
}
409
return _54;
410
},dummyRender:function(_55){
411
return this.render(_55,dd.Template.prototype.getBuffer()).toString();
412
},unrender:function(){
413
return arguments[1];
414
},clone:function(){
415
return this;
416
},rtrim:function(){
417
while(1){
418
i=this.contents.length-1;
419
if(this.contents[i] instanceof dd._TextNode&&this.contents[i].isEmpty()){
420
this.contents.pop();
421
}else{
422
break;
423
}
424
}
425
return this;
426
}});
427
dd._VarNode=dojo.extend(function(str){
428
this.contents=new dd._Filter(str);
429
},{render:function(_56,_57){
430
var str=this.contents.resolve(_56);
431
if(!str.safe){
432
str=dd._base.escape(""+str);
433
}
434
return _57.concat(str);
435
}});
436
dd._noOpNode=new function(){
437
this.render=this.unrender=function(){
438
return arguments[1];
439
};
440
this.clone=function(){
441
return this;
442
};
443
};
444
dd._Parser=dojo.extend(function(_58){
445
this.contents=_58;
446
},{i:0,parse:function(_59){
447
var _5a={},_5b;
448
_59=_59||[];
449
for(var i=0;i<_59.length;i++){
450
_5a[_59[i]]=true;
451
}
452
var _5c=new dd._NodeList();
453
while(this.i<this.contents.length){
454
_5b=this.contents[this.i++];
455
if(typeof _5b=="string"){
456
_5c.push(new dd._TextNode(_5b));
457
}else{
458
var _5d=_5b[0];
459
var _5e=_5b[1];
460
if(_5d==dd.TOKEN_VAR){
461
_5c.push(new dd._VarNode(_5e));
462
}else{
463
if(_5d==dd.TOKEN_BLOCK){
464
if(_5a[_5e]){
465
--this.i;
466
return _5c;
467
}
468
var cmd=_5e.split(/\s+/g);
469
if(cmd.length){
470
cmd=cmd[0];
471
var fn=ddt.getTag(cmd);
472
if(fn){
473
_5c.push(fn(this,new dd.Token(_5d,_5e)));
474
}
475
}
476
}
477
}
478
}
479
}
480
if(_59.length){
481
throw new Error("Could not find closing tag(s): "+_59.toString());
482
}
483
this.contents.length=0;
484
return _5c;
485
},next_token:function(){
486
var _5f=this.contents[this.i++];
487
return new dd.Token(_5f[0],_5f[1]);
488
},delete_first_token:function(){
489
this.i++;
490
},skip_past:function(_60){
491
while(this.i<this.contents.length){
492
var _61=this.contents[this.i++];
493
if(_61[0]==dd.TOKEN_BLOCK&&_61[1]==_60){
494
return;
495
}
496
}
497
throw new Error("Unclosed tag found when looking for "+_60);
498
},create_variable_node:function(_62){
499
return new dd._VarNode(_62);
500
},create_text_node:function(_63){
501
return new dd._TextNode(_63||"");
502
},getTemplate:function(_64){
503
return new dd.Template(_64);
504
}});
505
dd.register={_registry:{attributes:[],tags:[],filters:[]},get:function(_65,_66){
506
var _67=dd.register._registry[_65+"s"];
507
for(var i=0,_68;_68=_67[i];i++){
508
if(typeof _68[0]=="string"){
509
if(_68[0]==_66){
510
return _68;
511
}
512
}else{
513
if(_66.match(_68[0])){
514
return _68;
515
}
516
}
517
}
518
},getAttributeTags:function(){
519
var _69=[];
520
var _6a=dd.register._registry.attributes;
521
for(var i=0,_6b;_6b=_6a[i];i++){
522
if(_6b.length==3){
523
_69.push(_6b);
524
}else{
525
var fn=dojo.getObject(_6b[1]);
526
if(fn&&dojo.isFunction(fn)){
527
_6b.push(fn);
528
_69.push(_6b);
529
}
530
}
531
}
532
return _69;
533
},_any:function(_6c,_6d,_6e){
534
for(var _6f in _6e){
535
for(var i=0,fn;fn=_6e[_6f][i];i++){
536
var key=fn;
537
if(dojo.isArray(fn)){
538
key=fn[0];
539
fn=fn[1];
540
}
541
if(typeof key=="string"){
542
if(key.substr(0,5)=="attr:"){
543
var _70=fn;
544
if(_70.substr(0,5)=="attr:"){
545
_70=_70.slice(5);
546
}
547
dd.register._registry.attributes.push([_70.toLowerCase(),_6d+"."+_6f+"."+_70]);
548
}
549
key=key.toLowerCase();
550
}
551
dd.register._registry[_6c].push([key,fn,_6d+"."+_6f]);
552
}
553
}
554
},tags:function(_71,_72){
555
dd.register._any("tags",_71,_72);
556
},filters:function(_73,_74){
557
dd.register._any("filters",_73,_74);
558
}};
559
var _75=/&/g;
560
var _76=/</g;
561
var _77=/>/g;
562
var _78=/'/g;
563
var _79=/"/g;
564
dd._base.escape=function(_7a){
565
return dd.mark_safe(_7a.replace(_75,"&amp;").replace(_76,"&lt;").replace(_77,"&gt;").replace(_79,"&quot;").replace(_78,"&#39;"));
566
};
567
dd._base.safe=function(_7b){
568
if(typeof _7b=="string"){
569
_7b=new String(_7b);
570
}
571
if(typeof _7b=="object"){
572
_7b.safe=true;
573
}
574
return _7b;
575
};
576
dd.mark_safe=dd._base.safe;
577
dd.register.tags("dojox.dtl.tag",{"date":["now"],"logic":["if","for","ifequal","ifnotequal"],"loader":["extends","block","include","load","ssi"],"misc":["comment","debug","filter","firstof","spaceless","templatetag","widthratio","with"],"loop":["cycle","ifchanged","regroup"]});
578
dd.register.filters("dojox.dtl.filter",{"dates":["date","time","timesince","timeuntil"],"htmlstrings":["linebreaks","linebreaksbr","removetags","striptags"],"integers":["add","get_digit"],"lists":["dictsort","dictsortreversed","first","join","length","length_is","random","slice","unordered_list"],"logic":["default","default_if_none","divisibleby","yesno"],"misc":["filesizeformat","pluralize","phone2numeric","pprint"],"strings":["addslashes","capfirst","center","cut","fix_ampersands","floatformat","iriencode","linenumbers","ljust","lower","make_list","rjust","slugify","stringformat","title","truncatewords","truncatewords_html","upper","urlencode","urlize","urlizetrunc","wordcount","wordwrap"]});
579
dd.register.filters("dojox.dtl",{"_base":["escape","safe"]});
580
})();
581
}