Template.js
上传用户:dawnssy
上传日期:2022-08-06
资源大小:9345k
文件大小:9k
源码类别:

JavaScript

开发平台:

JavaScript

  1. /*!
  2.  * Ext JS Library 3.1.0
  3.  * Copyright(c) 2006-2009 Ext JS, LLC
  4.  * licensing@extjs.com
  5.  * http://www.extjs.com/license
  6.  */
  7. /**
  8.  * @class Ext.Template
  9.  * <p>Represents an HTML fragment template. Templates may be {@link #compile precompiled}
  10.  * for greater performance.</p>
  11.  * <p>For example usage {@link #Template see the constructor}.</p>
  12.  * 
  13.  * @constructor
  14.  * An instance of this class may be created by passing to the constructor either
  15.  * a single argument, or multiple arguments:
  16.  * <div class="mdetail-params"><ul>
  17.  * <li><b>single argument</b> : String/Array
  18.  * <div class="sub-desc">
  19.  * The single argument may be either a String or an Array:<ul>
  20.  * <li><tt>String</tt> : </li><pre><code>
  21. var t = new Ext.Template("&lt;div>Hello {0}.&lt;/div>");
  22. t.{@link #append}('some-element', ['foo']);
  23.  * </code></pre>
  24.  * <li><tt>Array</tt> : </li>
  25.  * An Array will be combined with <code>join('')</code>.
  26. <pre><code>
  27. var t = new Ext.Template([
  28.     '&lt;div name="{id}"&gt;',
  29.         '&lt;span class="{cls}"&gt;{name:trim} {value:ellipsis(10)}&lt;/span&gt;',
  30.     '&lt;/div&gt;',
  31. ]);
  32. t.{@link #compile}();
  33. t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
  34. </code></pre>
  35.  * </ul></div></li>
  36.  * <li><b>multiple arguments</b> : String, Object, Array, ...
  37.  * <div class="sub-desc">
  38.  * Multiple arguments will be combined with <code>join('')</code>.
  39.  * <pre><code>
  40. var t = new Ext.Template(
  41.     '&lt;div name="{id}"&gt;',
  42.         '&lt;span class="{cls}"&gt;{name} {value}&lt;/span&gt;',
  43.     '&lt;/div&gt;',
  44.     // a configuration object:
  45.     {
  46.         compiled: true,      // {@link #compile} immediately
  47.         disableFormats: true // See Notes below.
  48.     } 
  49. );
  50.  * </code></pre>
  51.  * <p><b>Notes</b>:</p>
  52.  * <div class="mdetail-params"><ul>
  53.  * <li>Formatting and <code>disableFormats</code> are not applicable for Ext Core.</li>
  54.  * <li>For a list of available format functions, see {@link Ext.util.Format}.</li>
  55.  * <li><code>disableFormats</code> reduces <code>{@link #apply}</code> time
  56.  * when no formatting is required.</li>
  57.  * </ul></div>
  58.  * </div></li>
  59.  * </ul></div>
  60.  * @param {Mixed} config
  61.  */
  62. Ext.Template = function(html){
  63.     var me = this,
  64.      a = arguments,
  65.      buf = [];
  66.     if (Ext.isArray(html)) {
  67.         html = html.join("");
  68.     } else if (a.length > 1) {
  69.     Ext.each(a, function(v) {
  70.             if (Ext.isObject(v)) {
  71.                 Ext.apply(me, v);
  72.             } else {
  73.                 buf.push(v);
  74.             }
  75.         });
  76.         html = buf.join('');
  77.     }
  78.     /**@private*/
  79.     me.html = html;
  80.     /**
  81.      * @cfg {Boolean} compiled Specify <tt>true</tt> to compile the template
  82.      * immediately (see <code>{@link #compile}</code>).
  83.      * Defaults to <tt>false</tt>.
  84.      */
  85.     if (me.compiled) {
  86.         me.compile();
  87.     }
  88. };
  89. Ext.Template.prototype = {
  90.     /**
  91.      * @cfg {RegExp} re The regular expression used to match template variables.
  92.      * Defaults to:<pre><code>
  93.      * re : /{([w-]+)}/g                                     // for Ext Core
  94.      * re : /{([w-]+)(?::([w.]*)(?:((.*?)?))?)?}/g      // for Ext JS
  95.      * </code></pre>
  96.      */
  97.     re : /{([w-]+)}/g,
  98.     /**
  99.      * See <code>{@link #re}</code>.
  100.      * @type RegExp
  101.      * @property re
  102.      */
  103.     /**
  104.      * Returns an HTML fragment of this template with the specified <code>values</code> applied.
  105.      * @param {Object/Array} values
  106.      * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
  107.      * or an object (i.e. <code>{foo: 'bar'}</code>).
  108.      * @return {String} The HTML fragment
  109.      */
  110.     applyTemplate : function(values){
  111. var me = this;
  112.         return me.compiled ?
  113.          me.compiled(values) :
  114. me.html.replace(me.re, function(m, name){
  115.          return values[name] !== undefined ? values[name] : "";
  116.         });
  117. },
  118.     /**
  119.      * Sets the HTML used as the template and optionally compiles it.
  120.      * @param {String} html
  121.      * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
  122.      * @return {Ext.Template} this
  123.      */
  124.     set : function(html, compile){
  125.     var me = this;
  126.         me.html = html;
  127.         me.compiled = null;
  128.         return compile ? me.compile() : me;
  129.     },
  130.     /**
  131.      * Compiles the template into an internal function, eliminating the RegEx overhead.
  132.      * @return {Ext.Template} this
  133.      */
  134.     compile : function(){
  135.         var me = this,
  136.          sep = Ext.isGecko ? "+" : ",";
  137.         function fn(m, name){                        
  138.         name = "values['" + name + "']";
  139.         return "'"+ sep + '(' + name + " == undefined ? '' : " + name + ')' + sep + "'";
  140.         }
  141.                 
  142.         eval("this.compiled = function(values){ return " + (Ext.isGecko ? "'" : "['") +
  143.              me.html.replace(/\/g, '\\').replace(/(rn|n)/g, '\n').replace(/'/g, "\'").replace(this.re, fn) +
  144.              (Ext.isGecko ?  "';};" : "'].join('');};"));
  145.         return me;
  146.     },
  147.     /**
  148.      * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
  149.      * @param {Mixed} el The context element
  150.      * @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'})
  151.      * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
  152.      * @return {HTMLElement/Ext.Element} The new node or Element
  153.      */
  154.     insertFirst: function(el, values, returnElement){
  155.         return this.doInsert('afterBegin', el, values, returnElement);
  156.     },
  157.     /**
  158.      * Applies the supplied values to the template and inserts the new node(s) before el.
  159.      * @param {Mixed} el The context element
  160.      * @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'})
  161.      * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
  162.      * @return {HTMLElement/Ext.Element} The new node or Element
  163.      */
  164.     insertBefore: function(el, values, returnElement){
  165.         return this.doInsert('beforeBegin', el, values, returnElement);
  166.     },
  167.     /**
  168.      * Applies the supplied values to the template and inserts the new node(s) after el.
  169.      * @param {Mixed} el The context element
  170.      * @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'})
  171.      * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
  172.      * @return {HTMLElement/Ext.Element} The new node or Element
  173.      */
  174.     insertAfter : function(el, values, returnElement){
  175.         return this.doInsert('afterEnd', el, values, returnElement);
  176.     },
  177.     /**
  178.      * Applies the supplied <code>values</code> to the template and appends
  179.      * the new node(s) to the specified <code>el</code>.
  180.      * <p>For example usage {@link #Template see the constructor}.</p>
  181.      * @param {Mixed} el The context element
  182.      * @param {Object/Array} values
  183.      * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
  184.      * or an object (i.e. <code>{foo: 'bar'}</code>).
  185.      * @param {Boolean} returnElement (optional) true to return an Ext.Element (defaults to undefined)
  186.      * @return {HTMLElement/Ext.Element} The new node or Element
  187.      */
  188.     append : function(el, values, returnElement){
  189.         return this.doInsert('beforeEnd', el, values, returnElement);
  190.     },
  191.     doInsert : function(where, el, values, returnEl){
  192.         el = Ext.getDom(el);
  193.         var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
  194.         return returnEl ? Ext.get(newNode, true) : newNode;
  195.     },
  196.     /**
  197.      * Applies the supplied values to the template and overwrites the content of el with the new node(s).
  198.      * @param {Mixed} el The context element
  199.      * @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'})
  200.      * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
  201.      * @return {HTMLElement/Ext.Element} The new node or Element
  202.      */
  203.     overwrite : function(el, values, returnElement){
  204.         el = Ext.getDom(el);
  205.         el.innerHTML = this.applyTemplate(values);
  206.         return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
  207.     }
  208. };
  209. /**
  210.  * Alias for {@link #applyTemplate}
  211.  * Returns an HTML fragment of this template with the specified <code>values</code> applied.
  212.  * @param {Object/Array} values
  213.  * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
  214.  * or an object (i.e. <code>{foo: 'bar'}</code>).
  215.  * @return {String} The HTML fragment
  216.  * @member Ext.Template
  217.  * @method apply
  218.  */
  219. Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;
  220. /**
  221.  * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
  222.  * @param {String/HTMLElement} el A DOM element or its id
  223.  * @param {Object} config A configuration object
  224.  * @return {Ext.Template} The created template
  225.  * @static
  226.  */
  227. Ext.Template.from = function(el, config){
  228.     el = Ext.getDom(el);
  229.     return new Ext.Template(el.value || el.innerHTML, config || '');
  230. };