Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / source / util / XTemplate.js
diff --git a/source/util/XTemplate.js b/source/util/XTemplate.js
deleted file mode 100644 (file)
index 1ce193d..0000000
+++ /dev/null
@@ -1,356 +0,0 @@
-/*\r
- * Ext JS Library 2.2.1\r
- * Copyright(c) 2006-2009, Ext JS, LLC.\r
- * licensing@extjs.com\r
- * \r
- * http://extjs.com/license\r
- */\r
-\r
-/**\r
-* @class Ext.XTemplate\r
-* @extends Ext.Template\r
-* <p>A template class that supports advanced functionality like autofilling arrays, conditional processing with\r
-* basic comparison operators, sub-templates, basic math function support, special built-in template variables,\r
-* inline code execution and more.  XTemplate also provides the templating mechanism built into {@link Ext.DataView}.</p>\r
-* <p>XTemplate supports many special tags and built-in operators that aren't defined as part of the API, but are\r
-* supported in the templates that can be created.  The following examples demonstrate all of the supported features.\r
-* This is the data object used for reference in each code example:</p>\r
-* <pre><code>\r
-var data = {\r
-    name: 'Jack Slocum',\r
-    title: 'Lead Developer',\r
-    company: 'Ext JS, LLC',\r
-    email: 'jack@extjs.com',\r
-    address: '4 Red Bulls Drive',\r
-    city: 'Cleveland',\r
-    state: 'Ohio',\r
-    zip: '44102',\r
-    drinks: ['Red Bull', 'Coffee', 'Water'],\r
-    kids: [{\r
-        name: 'Sara Grace',\r
-        age:3\r
-    },{\r
-        name: 'Zachary',\r
-        age:2\r
-    },{\r
-        name: 'John James',\r
-        age:0\r
-    }]\r
-};\r
-</code></pre>\r
-* <p><b>Auto filling of arrays and scope switching</b><br/>Using the <tt>tpl</tt> tag and the <tt>for</tt> operator,\r
-* you can switch to the scope of the object specified by <tt>for</tt> and access its members to populate the template.\r
-* If the variable in <tt>for</tt> is an array, it will auto-fill, repeating the template block inside the <tt>tpl</tt>\r
-* tag for each item in the array:</p>\r
-* <pre><code>\r
-var tpl = new Ext.XTemplate(\r
-    '&lt;p>Name: {name}&lt;/p>',\r
-    '&lt;p>Title: {title}&lt;/p>',\r
-    '&lt;p>Company: {company}&lt;/p>',\r
-    '&lt;p>Kids: ',\r
-    '&lt;tpl for="kids">',\r
-        '&lt;p>{name}&lt;/p>',\r
-    '&lt;/tpl>&lt;/p>'\r
-);\r
-tpl.overwrite(panel.body, data);\r
-</code></pre>\r
-* <p><b>Access to parent object from within sub-template scope</b><br/>When processing a sub-template, for example while\r
-* looping through a child array, you can access the parent object's members via the <tt>parent</tt> object:</p>\r
-* <pre><code>\r
-var tpl = new Ext.XTemplate(\r
-    '&lt;p>Name: {name}&lt;/p>',\r
-    '&lt;p>Kids: ',\r
-    '&lt;tpl for="kids">',\r
-        '&lt;tpl if="age &amp;gt; 1">',  // <-- Note that the &gt; is encoded\r
-            '&lt;p>{name}&lt;/p>',\r
-            '&lt;p>Dad: {parent.name}&lt;/p>',\r
-        '&lt;/tpl>',\r
-    '&lt;/tpl>&lt;/p>'\r
-);\r
-tpl.overwrite(panel.body, data);\r
-</code></pre>\r
-* <p><b>Array item index and basic math support</b> <br/>While processing an array, the special variable <tt>{#}</tt>\r
-* will provide the current array index + 1 (starts at 1, not 0). Templates also support the basic math operators\r
-* + - * and / that can be applied directly on numeric data values:</p>\r
-* <pre><code>\r
-var tpl = new Ext.XTemplate(\r
-    '&lt;p>Name: {name}&lt;/p>',\r
-    '&lt;p>Kids: ',\r
-    '&lt;tpl for="kids">',\r
-        '&lt;tpl if="age &amp;gt; 1">',  // <-- Note that the &gt; is encoded\r
-            '&lt;p>{#}: {name}&lt;/p>',  // <-- Auto-number each item\r
-            '&lt;p>In 5 Years: {age+5}&lt;/p>',  // <-- Basic math\r
-            '&lt;p>Dad: {parent.name}&lt;/p>',\r
-        '&lt;/tpl>',\r
-    '&lt;/tpl>&lt;/p>'\r
-);\r
-tpl.overwrite(panel.body, data);\r
-</code></pre>\r
-* <p><b>Auto-rendering of flat arrays</b> <br/>Flat arrays that contain values (and not objects) can be auto-rendered\r
-* using the special <tt>{.}</tt> variable inside a loop.  This variable will represent the value of\r
-* the array at the current index:</p>\r
-* <pre><code>\r
-var tpl = new Ext.XTemplate(\r
-    '&lt;p>{name}\'s favorite beverages:&lt;/p>',\r
-    '&lt;tpl for="drinks">',\r
-       '&lt;div> - {.}&lt;/div>',\r
-    '&lt;/tpl>'\r
-);\r
-tpl.overwrite(panel.body, data);\r
-</code></pre>\r
-* <p><b>Basic conditional logic</b> <br/>Using the <tt>tpl</tt> tag and the <tt>if</tt>\r
-* operator you can provide conditional checks for deciding whether or not to render specific parts of the template.\r
-* Note that there is no <tt>else</tt> operator &mdash; if needed, you should use two opposite <tt>if</tt> statements.\r
-* Properly-encoded attributes are required as seen in the following example:</p>\r
-* <pre><code>\r
-var tpl = new Ext.XTemplate(\r
-    '&lt;p>Name: {name}&lt;/p>',\r
-    '&lt;p>Kids: ',\r
-    '&lt;tpl for="kids">',\r
-        '&lt;tpl if="age &amp;gt; 1">',  // <-- Note that the &gt; is encoded\r
-            '&lt;p>{name}&lt;/p>',\r
-        '&lt;/tpl>',\r
-    '&lt;/tpl>&lt;/p>'\r
-);\r
-tpl.overwrite(panel.body, data);\r
-</code></pre>\r
-* <p><b>Ability to execute arbitrary inline code</b> <br/>In an XTemplate, anything between {[ ... ]}  is considered\r
-* code to be executed in the scope of the template. There are some special variables available in that code:\r
-* <ul>\r
-* <li><b><tt>values</tt></b>: The values in the current scope. If you are using scope changing sub-templates, you\r
-* can change what <tt>values</tt> is.</li>\r
-* <li><b><tt>parent</tt></b>: The scope (values) of the ancestor template.</li>\r
-* <li><b><tt>xindex</tt></b>: If you are in a looping template, the index of the loop you are in (1-based).</li>\r
-* <li><b><tt>xcount</tt></b>: If you are in a looping template, the total length of the array you are looping.</li>\r
-* <li><b><tt>fm</tt></b>: An alias for <tt>Ext.util.Format</tt>.</li>\r
-* </ul>\r
-* This example demonstrates basic row striping using an inline code block and the <tt>xindex</tt> variable:</p>\r
-* <pre><code>\r
-var tpl = new Ext.XTemplate(\r
-    '&lt;p>Name: {name}&lt;/p>',\r
-    '&lt;p>Company: {[values.company.toUpperCase() + ", " + values.title]}&lt;/p>',\r
-    '&lt;p>Kids: ',\r
-    '&lt;tpl for="kids">',\r
-       '&lt;div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',\r
-        '{name}',\r
-        '&lt;/div>',\r
-    '&lt;/tpl>&lt;/p>'\r
-);\r
-tpl.overwrite(panel.body, data);\r
-</code></pre>\r
-* <p><b>Template member functions</b> <br/>One or more member functions can be defined directly on the config\r
-* object passed into the XTemplate constructor for more complex processing:</p>\r
-* <pre><code>\r
-var tpl = new Ext.XTemplate(\r
-    '&lt;p>Name: {name}&lt;/p>',\r
-    '&lt;p>Kids: ',\r
-    '&lt;tpl for="kids">',\r
-        '&lt;tpl if="this.isGirl(name)">',\r
-            '&lt;p>Girl: {name} - {age}&lt;/p>',\r
-        '&lt;/tpl>',\r
-        '&lt;tpl if="this.isGirl(name) == false">',\r
-            '&lt;p>Boy: {name} - {age}&lt;/p>',\r
-        '&lt;/tpl>',\r
-        '&lt;tpl if="this.isBaby(age)">',\r
-            '&lt;p>{name} is a baby!&lt;/p>',\r
-        '&lt;/tpl>',\r
-    '&lt;/tpl>&lt;/p>', {\r
-     isGirl: function(name){\r
-         return name == 'Sara Grace';\r
-     },\r
-     isBaby: function(age){\r
-        return age < 1;\r
-     }\r
-});\r
-tpl.overwrite(panel.body, data);\r
-</code></pre>\r
-* @constructor\r
-* @param {String/Array/Object} parts The HTML fragment or an array of fragments to join(""), or multiple arguments\r
-* to join("") that can also include a config object\r
-*/\r
-Ext.XTemplate = function(){\r
-    Ext.XTemplate.superclass.constructor.apply(this, arguments);\r
-    var s = this.html;\r
-\r
-    s = ['<tpl>', s, '</tpl>'].join('');\r
-\r
-    var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;\r
-\r
-    var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;\r
-    var ifRe = /^<tpl\b[^>]*?if="(.*?)"/;\r
-    var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;\r
-    var m, id = 0;\r
-    var tpls = [];\r
-\r
-    while(m = s.match(re)){\r
-       var m2 = m[0].match(nameRe);\r
-       var m3 = m[0].match(ifRe);\r
-       var m4 = m[0].match(execRe);\r
-       var exp = null, fn = null, exec = null;\r
-       var name = m2 && m2[1] ? m2[1] : '';\r
-       if(m3){\r
-           exp = m3 && m3[1] ? m3[1] : null;\r
-           if(exp){\r
-               fn = new Function('values', 'parent', 'xindex', 'xcount', 'with(values){ return '+(Ext.util.Format.htmlDecode(exp))+'; }');\r
-           }\r
-       }\r
-       if(m4){\r
-           exp = m4 && m4[1] ? m4[1] : null;\r
-           if(exp){\r
-               exec = new Function('values', 'parent', 'xindex', 'xcount', 'with(values){ '+(Ext.util.Format.htmlDecode(exp))+'; }');\r
-           }\r
-       }\r
-       if(name){\r
-           switch(name){\r
-               case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;\r
-               case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;\r
-               default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');\r
-           }\r
-       }\r
-       tpls.push({\r
-            id: id,\r
-            target: name,\r
-            exec: exec,\r
-            test: fn,\r
-            body: m[1]||''\r
-        });\r
-       s = s.replace(m[0], '{xtpl'+ id + '}');\r
-       ++id;\r
-    }\r
-    for(var i = tpls.length-1; i >= 0; --i){\r
-        this.compileTpl(tpls[i]);\r
-    }\r
-    this.master = tpls[tpls.length-1];\r
-    this.tpls = tpls;\r
-};\r
-Ext.extend(Ext.XTemplate, Ext.Template, {\r
-    // private\r
-    re : /\{([\w-\.\#]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?(\s?[\+\-\*\\]\s?[\d\.\+\-\*\\\(\)]+)?\}/g,\r
-    // private\r
-    codeRe : /\{\[((?:\\\]|.|\n)*?)\]\}/g,\r
-\r
-    // private\r
-    applySubTemplate : function(id, values, parent, xindex, xcount){\r
-        var t = this.tpls[id];\r
-        if(t.test && !t.test.call(this, values, parent, xindex, xcount)){\r
-            return '';\r
-        }\r
-        if(t.exec && t.exec.call(this, values, parent, xindex, xcount)){\r
-            return '';\r
-        }\r
-        var vs = t.target ? t.target.call(this, values, parent) : values;\r
-        parent = t.target ? values : parent;\r
-        if(t.target && Ext.isArray(vs)){\r
-            var buf = [];\r
-            for(var i = 0, len = vs.length; i < len; i++){\r
-                buf[buf.length] = t.compiled.call(this, vs[i], parent, i+1, len);\r
-            }\r
-            return buf.join('');\r
-        }\r
-        return t.compiled.call(this, vs, parent, xindex, xcount);\r
-    },\r
-\r
-    // private\r
-    compileTpl : function(tpl){\r
-        var fm = Ext.util.Format;\r
-        var useF = this.disableFormats !== true;\r
-        var sep = Ext.isGecko ? "+" : ",";\r
-        var fn = function(m, name, format, args, math){\r
-            if(name.substr(0, 4) == 'xtpl'){\r
-                return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent, xindex, xcount)'+sep+"'";\r
-            }\r
-            var v;\r
-            if(name === '.'){\r
-                v = 'values';\r
-            }else if(name === '#'){\r
-                v = 'xindex';\r
-            }else if(name.indexOf('.') != -1){\r
-                v = name;\r
-            }else{\r
-                v = "values['" + name + "']";\r
-            }\r
-            if(math){\r
-                v = '(' + v + math + ')';\r
-            }\r
-            if(format && useF){\r
-                args = args ? ',' + args : "";\r
-                if(format.substr(0, 5) != "this."){\r
-                    format = "fm." + format + '(';\r
-                }else{\r
-                    format = 'this.call("'+ format.substr(5) + '", ';\r
-                    args = ", values";\r
-                }\r
-            }else{\r
-                args= ''; format = "("+v+" === undefined ? '' : ";\r
-            }\r
-            return "'"+ sep + format + v + args + ")"+sep+"'";\r
-        };\r
-        var codeFn = function(m, code){\r
-            return "'"+ sep +'('+code+')'+sep+"'";\r
-        };\r
-\r
-        var body;\r
-        // branched to use + in gecko and [].join() in others\r
-        if(Ext.isGecko){\r
-            body = "tpl.compiled = function(values, parent, xindex, xcount){ return '" +\r
-                   tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn) +\r
-                    "';};";\r
-        }else{\r
-            body = ["tpl.compiled = function(values, parent, xindex, xcount){ return ['"];\r
-            body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn));\r
-            body.push("'].join('');};");\r
-            body = body.join('');\r
-        }\r
-        eval(body);\r
-        return this;\r
-    },\r
-\r
-    /**\r
-     * Returns an HTML fragment of this template with the specified values applied.\r
-     * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})\r
-     * @return {String} The HTML fragment\r
-     */\r
-    applyTemplate : function(values){\r
-        return this.master.compiled.call(this, values, {}, 1, 1);\r
-    },\r
-\r
-    /**\r
-     * Compile the template to a function for optimized performance.  Recommended if the template will be used frequently.\r
-     * @return {Function} The compiled function\r
-     */\r
-    compile : function(){return this;}\r
-\r
-    /**\r
-     * @property re\r
-     * @hide\r
-     */\r
-    /**\r
-     * @property disableFormats\r
-     * @hide\r
-     */\r
-    /**\r
-     * @method set\r
-     * @hide\r
-     */\r
-\r
-});\r
-/**\r
- * Alias for {@link #applyTemplate}\r
- * Returns an HTML fragment of this template with the specified values applied.\r
- * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})\r
- * @return {String} The HTML fragment\r
- * @member Ext.XTemplate\r
- * @method apply\r
- */\r
-Ext.XTemplate.prototype.apply = Ext.XTemplate.prototype.applyTemplate;\r
-\r
-/**\r
- * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.\r
- * @param {String/HTMLElement} el A DOM element or its id\r
- * @return {Ext.Template} The created template\r
- * @static\r
- */\r
-Ext.XTemplate.from = function(el){\r
-    el = Ext.getDom(el);\r
-    return new Ext.XTemplate(el.value || el.innerHTML);\r
-};
\ No newline at end of file