PageRenderTime 38ms CodeModel.GetById 13ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/hippo/src/main/webapp/ext/src/core/Template-more.js

http://hdbc.googlecode.com/
JavaScript | 115 lines | 72 code | 7 blank | 36 comment | 15 complexity | ca66a774ac025d9475381420659a7bfd MD5 | raw file
  1/*!
  2 * Ext JS Library 3.0.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 */
 10Ext.apply(Ext.Template.prototype, {
 11    /**
 12     * Returns an HTML fragment of this template with the specified values applied.
 13     * @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'})
 14     * @return {String} The HTML fragment
 15     * @hide repeat doc
 16     */
 17    applyTemplate : function(values){
 18		var me = this,
 19			useF = me.disableFormats !== true,
 20        	fm = Ext.util.Format, 
 21        	tpl = me;	    
 22	    
 23        if(me.compiled){
 24            return me.compiled(values);
 25        }
 26        function fn(m, name, format, args){
 27            if (format && useF) {
 28                if (format.substr(0, 5) == "this.") {
 29                    return tpl.call(format.substr(5), values[name], values);
 30                } else {
 31                    if (args) {
 32                        // quoted values are required for strings in compiled templates,
 33                        // but for non compiled we need to strip them
 34                        // quoted reversed for jsmin
 35                        var re = /^\s*['"](.*)["']\s*$/;
 36                        args = args.split(',');
 37                        for(var i = 0, len = args.length; i < len; i++){
 38                            args[i] = args[i].replace(re, "$1");
 39                        }
 40                        args = [values[name]].concat(args);
 41                    } else {
 42                        args = [values[name]];
 43                    }
 44                    return fm[format].apply(fm, args);
 45                }
 46            } else {
 47                return values[name] !== undefined ? values[name] : "";
 48            }
 49        }
 50        return me.html.replace(me.re, fn);
 51    },
 52		
 53    /**
 54     * <tt>true</tt> to disable format functions (defaults to <tt>false</tt>)
 55     * @type Boolean
 56     * @property
 57     */
 58    disableFormats : false,				
 59	
 60    /**
 61     * The regular expression used to match template variables
 62     * @type RegExp
 63     * @property
 64     * @hide repeat doc
 65     */
 66    re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
 67    
 68    /**
 69     * Compiles the template into an internal function, eliminating the RegEx overhead.
 70     * @return {Ext.Template} this
 71     * @hide repeat doc
 72     */
 73    compile : function(){
 74        var me = this,
 75        	fm = Ext.util.Format,
 76        	useF = me.disableFormats !== true,
 77        	sep = Ext.isGecko ? "+" : ",",
 78        	body;
 79        
 80        function fn(m, name, format, args){
 81            if(format && useF){
 82                args = args ? ',' + args : "";
 83                if(format.substr(0, 5) != "this."){
 84                    format = "fm." + format + '(';
 85                }else{
 86                    format = 'this.call("'+ format.substr(5) + '", ';
 87                    args = ", values";
 88                }
 89            }else{
 90                args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
 91            }
 92            return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
 93        }
 94        
 95        // branched to use + in gecko and [].join() in others
 96        if(Ext.isGecko){
 97            body = "this.compiled = function(values){ return '" +
 98                   me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
 99                    "';};";
100        }else{
101            body = ["this.compiled = function(values){ return ['"];
102            body.push(me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
103            body.push("'].join('');};");
104            body = body.join('');
105        }
106        eval(body);
107        return me;
108    },
109    
110    // private function used to call members
111    call : function(fnName, value, allValues){
112        return this[fnName](value, allValues);
113    }
114});
115Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;