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