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