~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/ext/source/legacy/MasterTemplate.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

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