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