/** * @class Ext.Template *

Represents an HTML fragment template. Templates may be {@link #compile precompiled} * for greater performance.

* An instance of this class may be created by passing to the constructor either * a single argument, or multiple arguments: *
* @param {Mixed} config */ Ext.define('Ext.Template', { /* Begin Definitions */ requires: ['Ext.core.DomHelper'], statics: {
/** * Creates a template from the passed element's value (display:none textarea, preferred) or innerHTML. * @param {String/HTMLElement} el A DOM element or its id * @param {Object} config A configuration object * @return {Ext.Template} The created template * @static */ from: function(el, config) { el = Ext.getDom(el); return new this(el.value || el.innerHTML, config || ''); } }, /* End Definitions */ constructor: function(html) { var me = this, args = arguments, buffer = [], value, i, length; me.initialConfig = {}; if (Ext.isArray(html)) { html = html.join(""); } else if (args.length > 1) { for (i = 0, length = args.length; i < length; i++) { value = args[i]; if (typeof value == 'object') { Ext.apply(me.initialConfig, value); Ext.apply(me, value); } else { buffer.push(value); } } html = buffer.join(''); } // @private me.html = html; if (me.compiled) { me.compile(); } }, isTemplate: true,
/** * @cfg {Boolean} disableFormats true to disable format functions in the template. If the template doesn't contain format functions, setting * disableFormats to true will reduce apply time (defaults to false) */ disableFormats: false, re: /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
/** * Returns an HTML fragment of this template with the specified values applied. * @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'}) * @return {String} The HTML fragment * @hide repeat doc */ applyTemplate: function(values) { var me = this, useFormat = me.disableFormats !== true, fm = Ext.String, tpl = me; if (me.compiled) { return me.compiled(values); } function fn(m, name, format, args) { if (format && useFormat) { if (args) { args = [values[name]].concat(new Function('return ['+ args +'];')()); } else { args = [values[name]]; } if (format.substr(0, 5) == "this.") { return tpl[format.substr(5)].apply(tpl, args); } else { return fm[format].apply(fm, args); } } else { return values[name] !== undefined ? values[name] : ""; } } return me.html.replace(me.re, fn); },
/** * Sets the HTML used as the template and optionally compiles it. * @param {String} html * @param {Boolean} compile (optional) True to compile the template (defaults to undefined) * @return {Ext.Template} this */ set: function(html, compile) { var me = this; me.html = html; me.compiled = null; return compile ? me.compile() : me; }, compileARe: /\\/g, compileBRe: /(\r\n|\n)/g, compileCRe: /'/g,
/** * Compiles the template into an internal function, eliminating the RegEx overhead. * @return {Ext.Template} this * @hide repeat doc */ compile: function() { var me = this, fm = Ext.String, useFormat = me.disableFormats !== true, body, bodyReturn; function fn(m, name, format, args) { if (format && useFormat) { args = args ? ',' + args: ""; if (format.substr(0, 5) != "this.") { format = "fm." + format + '('; } else { format = 'this.' + format.substr(5) + '('; } } else { args = ''; format = "(values['" + name + "'] == undefined ? '' : "; } return "'," + format + "values['" + name + "']" + args + ") ,'"; } bodyReturn = me.html.replace(me.compileARe, '\\\\').replace(me.compileBRe, '\\n').replace(me.compileCRe, "\\'").replace(me.re, fn); body = "this.compiled = function(values){ return ['" + bodyReturn + "'].join('');};"; eval(body); return me; },
/** * Applies the supplied values to the template and inserts the new node(s) as the first child of el. * @param {Mixed} el The context element * @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'}) * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined) * @return {HTMLElement/Ext.core.Element} The new node or Element */ insertFirst: function(el, values, returnElement) { return this.doInsert('afterBegin', el, values, returnElement); },
/** * Applies the supplied values to the template and inserts the new node(s) before el. * @param {Mixed} el The context element * @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'}) * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined) * @return {HTMLElement/Ext.core.Element} The new node or Element */ insertBefore: function(el, values, returnElement) { return this.doInsert('beforeBegin', el, values, returnElement); },
/** * Applies the supplied values to the template and inserts the new node(s) after el. * @param {Mixed} el The context element * @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'}) * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined) * @return {HTMLElement/Ext.core.Element} The new node or Element */ insertAfter: function(el, values, returnElement) { return this.doInsert('afterEnd', el, values, returnElement); },
/** * Applies the supplied values to the template and appends * the new node(s) to the specified el. *

For example usage {@link #Template see the constructor}.

* @param {Mixed} el The context element * @param {Object/Array} values * The template values. Can be an array if the params are numeric (i.e. {0}) * or an object (i.e. {foo: 'bar'}). * @param {Boolean} returnElement (optional) true to return an Ext.core.Element (defaults to undefined) * @return {HTMLElement/Ext.core.Element} The new node or Element */ append: function(el, values, returnElement) { return this.doInsert('beforeEnd', el, values, returnElement); }, doInsert: function(where, el, values, returnEl) { el = Ext.getDom(el); var newNode = Ext.core.DomHelper.insertHtml(where, el, this.applyTemplate(values)); return returnEl ? Ext.get(newNode, true) : newNode; },
/** * Applies the supplied values to the template and overwrites the content of el with the new node(s). * @param {Mixed} el The context element * @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'}) * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined) * @return {HTMLElement/Ext.core.Element} The new node or Element */ overwrite: function(el, values, returnElement) { el = Ext.getDom(el); el.innerHTML = this.applyTemplate(values); return returnElement ? Ext.get(el.firstChild, true) : el.firstChild; } }, function() { //FIXME: We have to copy these here at the moment because these methods have been split between Ext.util.Format and Ext.String :/ Ext.apply(Ext.util.Format, { ellipsis: Ext.String.ellipsis, htmlEncode: Ext.String.htmlEncode });
/** * Alias for {@link #applyTemplate} * Returns an HTML fragment of this template with the specified values applied. * @param {Object/Array} values * The template values. Can be an array if the params are numeric (i.e. {0}) * or an object (i.e. {foo: 'bar'}). * @return {String} The HTML fragment * @member Ext.Template * @method apply */ this.createAlias('apply', 'applyTemplate'); });