Roo/XTemplate.js
[roojs1] / Roo / XTemplate.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11 Roo.XTemplate = function(){
12     Roo.XTemplate.superclass.constructor.apply(this, arguments);
13     if (this.html) {
14         this.preCompile();
15     }
16 };
17
18
19 Roo.extend(Roo.XTemplate, Roo.Template, {
20
21     /**
22      *
23      * basic tag replacing syntax
24      * WORD:WORD()
25      * 
26      */
27     re : new RegExp(
28             '\{' +
29             '([\w-\.]+)' +
30             '(?:\:([\w\.]*)' +
31             '(?:\((.*?)?\))?)?' +
32             '\}', 'g'),
33
34     
35     compile: function()
36     {
37         var s = this.html;
38      
39         s = ['<tpl>', s, '</tpl>'].join('');
40     
41         var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
42     
43         var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;
44         var ifRe   = /^<tpl\b[^>]*?if="(.*?)"/;
45         var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;
46         var m, id = 0;
47         var tpls = [];
48     
49         while(true == !!(m = s.match(re))){
50            var m2 = m[0].match(nameRe);
51            var m3 = m[0].match(ifRe);
52            var m4 = m[0].match(execRe);
53            var exp = null,
54                 fn = null,
55                 exec = null;
56            var name = m2 && m2[1] ? m2[1] : '';
57            if(m3){
58                 // if - puts fn into test..
59                 exp = m3 && m3[1] ? m3[1] : null;
60                 if(exp){
61                    fn = new Function('values', 'parent', 'with(values){ return '+(Roo.util.Format.htmlDecode(exp))+'; }');
62                 }
63            }
64            if(m4){
65                 // exec - calls a function... returns empty if true is  returned.
66                exp = m4 && m4[1] ? m4[1] : null;
67                if(exp){
68                    exec = new Function('values', 'parent', 'with(values){ '+(Roo.util.Format.htmlDecode(exp))+'; }');
69                }
70            }
71            if(name){
72                 // for = 
73                switch(name){
74                    case '.':  name = new Function('values', 'parent', 'with(values){ return values; }'); break;
75                    case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
76                    default:   name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
77                }
78            }
79            tpls.push({
80                 id: id,
81                 target: name,
82                 exec: exec,
83                 test: fn,
84                 body: m[1]||''
85             });
86            s = s.replace(m[0], '{xtpl'+ id + '}');
87            ++id;
88         }
89         for(var i = tpls.length-1; i >= 0; --i){
90             this.compileTpl(tpls[i]);
91         }
92         this.master = tpls[tpls.length-1];
93         this.tpls = tpls;
94         return this;
95     },
96     
97     applySubTemplate : function(id, values, parent){
98         var t = this.tpls[id];
99         if(t.test && !t.test.call(this, values, parent)){
100             return '';
101         }
102         if(t.exec && t.exec.call(this, values, parent)){
103             return '';
104         }
105         var vs = t.target ? t.target.call(this, values, parent) : values;
106         parent = t.target ? values : parent;
107         if(t.target && vs instanceof Array){
108             var buf = [];
109             for(var i = 0, len = vs.length; i < len; i++){
110                 buf[buf.length] = t.compiled.call(this, vs[i], parent);
111             }
112             return buf.join('');
113         }
114         return t.compiled.call(this, vs, parent);
115     },
116
117     compileTpl : function(tpl)
118     {
119         var fm = Roo.util.Format;
120         var useF = this.disableFormats !== true;
121         var sep = Roo.isGecko ? "+" : ",";
122         var fn = function(m, name, format, args){
123             
124             if (!format) {
125                 format= 'htmlEncode';
126             }
127             if (format == 'raw' ) {
128                 format = false;
129             }
130             
131             if(name.substr(0, 4) == 'xtpl'){
132                 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent)'+sep+"'";
133             }
134             
135             var v;
136             //if(name.indexOf('.') != -1){
137                 v = name;
138             //}else{
139             //    v = "values['" + name + "']";
140             //}
141             if(format && useF){
142                 args = args ? ',' + args : "";
143                 if(format.substr(0, 5) != "this."){
144                     format = "fm." + format + '(';
145                 }else{
146                     format = 'this.call("'+ format.substr(5) + '", ';
147                     args = ", values";
148                 }
149             }else{
150                 args= '';
151                 format = "("+v+" === undefined ? '' : ";
152             }
153             return "'"+ sep + format + v + args + ")"+sep+"'";
154         };
155         var body;
156         // branched to use + in gecko and [].join() in others
157         if(Roo.isGecko){
158             body = "tpl.compiled = function(values, parent){ with(values) { return '" +
159                    tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
160                     "';};};";
161         }else{
162             body = ["tpl.compiled = function(values, parent){ with (values) { return ['"];
163             body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
164             body.push("'].join('');};};");
165             body = body.join('');
166         }
167         
168         /** eval:var:zzzzzzz */
169         eval(body);
170         Roo.log(body.replace(/\\n/,'\n'));
171         
172         return this;
173     },
174
175     applyTemplate : function(values){
176         return this.master.compiled.call(this, values, {});
177         //var s = this.subs;
178     },
179
180     apply : function(){
181         return this.applyTemplate.apply(this, arguments);
182     }
183
184  });
185
186 Roo.XTemplate.from = function(el){
187     el = Roo.getDom(el);
188     return new Roo.XTemplate(el.value || el.innerHTML);
189 };