remove debugging code
[roojs1] / Roo / MasterTemplate.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
12
13  
14
15 /**
16  * @class Roo.MasterTemplate
17  * @extends Roo.Template
18  * Provides a template that can have child templates. The syntax is:
19 <pre><code>
20 var t = new Roo.MasterTemplate(
21         '&lt;select name="{name}"&gt;',
22                 '&lt;tpl name="options"&gt;&lt;option value="{value:trim}"&gt;{text:ellipsis(10)}&lt;/option&gt;&lt;/tpl&gt;',
23         '&lt;/select&gt;'
24 );
25 t.add('options', {value: 'foo', text: 'bar'});
26 // or you can add multiple child elements in one shot
27 t.addAll('options', [
28     {value: 'foo', text: 'bar'},
29     {value: 'foo2', text: 'bar2'},
30     {value: 'foo3', text: 'bar3'}
31 ]);
32 // then append, applying the master template values
33 t.append('my-form', {name: 'my-select'});
34 </code></pre>
35 * A name attribute for the child template is not required if you have only one child
36 * template or you want to refer to them by index.
37  */
38 Roo.MasterTemplate = function(){
39     Roo.MasterTemplate.superclass.constructor.apply(this, arguments);
40     this.originalHtml = this.html;
41     var st = {};
42     var m, re = this.subTemplateRe;
43     re.lastIndex = 0;
44     var subIndex = 0;
45     while(m = re.exec(this.html)){
46         var name = m[1], content = m[2];
47         st[subIndex] = {
48             name: name,
49             index: subIndex,
50             buffer: [],
51             tpl : new Roo.Template(content)
52         };
53         if(name){
54             st[name] = st[subIndex];
55         }
56         st[subIndex].tpl.compile();
57         st[subIndex].tpl.call = this.call.createDelegate(this);
58         subIndex++;
59     }
60     this.subCount = subIndex;
61     this.subs = st;
62 };
63 Roo.extend(Roo.MasterTemplate, Roo.Template, {
64     /**
65     * The regular expression used to match sub templates
66     * @type RegExp
67     * @property
68     */
69     subTemplateRe : /<tpl(?:\sname="([\w-]+)")?>((?:.|\n)*?)<\/tpl>/gi,
70
71     /**
72      * Applies the passed values to a child template.
73      * @param {String/Number} name (optional) The name or index of the child template
74      * @param {Array/Object} values The values to be applied to the template
75      * @return {MasterTemplate} this
76      */
77      add : function(name, values){
78         if(arguments.length == 1){
79             values = arguments[0];
80             name = 0;
81         }
82         var s = this.subs[name];
83         s.buffer[s.buffer.length] = s.tpl.apply(values);
84         return this;
85     },
86
87     /**
88      * Applies all the passed values to a child template.
89      * @param {String/Number} name (optional) The name or index of the child template
90      * @param {Array} values The values to be applied to the template, this should be an array of objects.
91      * @param {Boolean} reset (optional) True to reset the template first
92      * @return {MasterTemplate} this
93      */
94     fill : function(name, values, reset){
95         var a = arguments;
96         if(a.length == 1 || (a.length == 2 && typeof a[1] == "boolean")){
97             values = a[0];
98             name = 0;
99             reset = a[1];
100         }
101         if(reset){
102             this.reset();
103         }
104         for(var i = 0, len = values.length; i < len; i++){
105             this.add(name, values[i]);
106         }
107         return this;
108     },
109
110     /**
111      * Resets the template for reuse
112      * @return {MasterTemplate} this
113      */
114      reset : function(){
115         var s = this.subs;
116         for(var i = 0; i < this.subCount; i++){
117             s[i].buffer = [];
118         }
119         return this;
120     },
121
122     applyTemplate : function(values){
123         var s = this.subs;
124         var replaceIndex = -1;
125         this.html = this.originalHtml.replace(this.subTemplateRe, function(m, name){
126             return s[++replaceIndex].buffer.join("");
127         });
128         return Roo.MasterTemplate.superclass.applyTemplate.call(this, values);
129     },
130
131     apply : function(){
132         return this.applyTemplate.apply(this, arguments);
133     },
134
135     compile : function(){return this;}
136 });
137
138 /**
139  * Alias for fill().
140  * @method
141  */
142 Roo.MasterTemplate.prototype.addAll = Roo.MasterTemplate.prototype.fill;
143  /**
144  * Creates a template from the passed element's value (display:none textarea, preferred) or innerHTML. e.g.
145  * var tpl = Roo.MasterTemplate.from('element-id');
146  * @param {String/HTMLElement} el
147  * @param {Object} config
148  * @static
149  */
150 Roo.MasterTemplate.from = function(el, config){
151     el = Roo.getDom(el);
152     return new Roo.MasterTemplate(el.value || el.innerHTML, config || '');
153 };