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

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/ext/source/widgets/tree/TreeLoader.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.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.tree.TreeLoader
 
11
 * @extends Ext.util.Observable
 
12
 * A TreeLoader provides for lazy loading of an {@link Ext.tree.TreeNode}'s child
 
13
 * nodes from a specified URL. The response must be a JavaScript Array definition
 
14
 * whose elements are node definition objects. eg:
 
15
 * <pre><code>
 
16
    [{
 
17
        id: 1,
 
18
        text: 'A leaf Node',
 
19
        leaf: true
 
20
    },{
 
21
        id: 2,
 
22
        text: 'A folder Node',
 
23
        children: [{
 
24
            id: 3,
 
25
            text: 'A child Node',
 
26
            leaf: true
 
27
        }]
 
28
   }]
 
29
</code></pre>
 
30
 * <br><br>
 
31
 * A server request is sent, and child nodes are loaded only when a node is expanded.
 
32
 * The loading node's id is passed to the server under the parameter name "node" to
 
33
 * enable the server to produce the correct child nodes.
 
34
 * <br><br>
 
35
 * To pass extra parameters, an event handler may be attached to the "beforeload"
 
36
 * event, and the parameters specified in the TreeLoader's baseParams property:
 
37
 * <pre><code>
 
38
    myTreeLoader.on("beforeload", function(treeLoader, node) {
 
39
        this.baseParams.category = node.attributes.category;
 
40
    }, this);
 
41
</code></pre>
 
42
 * This would pass an HTTP parameter called "category" to the server containing
 
43
 * the value of the Node's "category" attribute.
 
44
 * @constructor
 
45
 * Creates a new Treeloader.
 
46
 * @param {Object} config A config object containing config properties.
 
47
 */
 
48
Ext.tree.TreeLoader = function(config){
 
49
    this.baseParams = {};
 
50
    Ext.apply(this, config);
 
51
 
 
52
    this.addEvents(
 
53
        /**
 
54
         * @event beforeload
 
55
         * Fires before a network request is made to retrieve the Json text which specifies a node's children.
 
56
         * @param {Object} This TreeLoader object.
 
57
         * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
 
58
         * @param {Object} callback The callback function specified in the {@link #load} call.
 
59
         */
 
60
        "beforeload",
 
61
        /**
 
62
         * @event load
 
63
         * Fires when the node has been successfuly loaded.
 
64
         * @param {Object} This TreeLoader object.
 
65
         * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
 
66
         * @param {Object} response The response object containing the data from the server.
 
67
         */
 
68
        "load",
 
69
        /**
 
70
         * @event loadexception
 
71
         * Fires if the network request failed.
 
72
         * @param {Object} This TreeLoader object.
 
73
         * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
 
74
         * @param {Object} response The response object containing the data from the server.
 
75
         */
 
76
        "loadexception"
 
77
    );
 
78
 
 
79
    Ext.tree.TreeLoader.superclass.constructor.call(this);
 
80
};
 
81
 
 
82
Ext.extend(Ext.tree.TreeLoader, Ext.util.Observable, {
 
83
    /**
 
84
    * @cfg {String} dataUrl The URL from which to request a Json string which
 
85
    * specifies an array of node definition objects representing the child nodes
 
86
    * to be loaded.
 
87
    */
 
88
    /**
 
89
     * @cfg {String} requestMethod The HTTP request method for loading data (defaults to the value of {@link Ext.Ajax#method}).
 
90
     */
 
91
    /**
 
92
     * @cfg {String} url Equivalent to {@link #dataUrl}.
 
93
     */
 
94
    /**
 
95
     * @cfg {Boolean} preloadChildren If set to true, the loader recursively loads "children" attributes when doing the first load on nodes.
 
96
     */
 
97
    /**
 
98
    * @cfg {Object} baseParams (optional) An object containing properties which
 
99
    * specify HTTP parameters to be passed to each request for child nodes.
 
100
    */
 
101
    /**
 
102
    * @cfg {Object} baseAttrs (optional) An object containing attributes to be added to all nodes
 
103
    * created by this loader. If the attributes sent by the server have an attribute in this object,
 
104
    * they take priority.
 
105
    */
 
106
    /**
 
107
    * @cfg {Object} uiProviders (optional) An object containing properties which
 
108
    * specify custom {@link Ext.tree.TreeNodeUI} implementations. If the optional
 
109
    * <i>uiProvider</i> attribute of a returned child node is a string rather
 
110
    * than a reference to a TreeNodeUI implementation, then that string value
 
111
    * is used as a property name in the uiProviders object.
 
112
    */
 
113
    uiProviders : {},
 
114
 
 
115
    /**
 
116
    * @cfg {Boolean} clearOnLoad (optional) Default to true. Remove previously existing
 
117
    * child nodes before loading.
 
118
    */
 
119
    clearOnLoad : true,
 
120
 
 
121
    /**
 
122
     * Load an {@link Ext.tree.TreeNode} from the URL specified in the constructor.
 
123
     * This is called automatically when a node is expanded, but may be used to reload
 
124
     * a node (or append new children if the {@link #clearOnLoad} option is false.)
 
125
     * @param {Ext.tree.TreeNode} node
 
126
     * @param {Function} callback
 
127
     */
 
128
    load : function(node, callback){
 
129
        if(this.clearOnLoad){
 
130
            while(node.firstChild){
 
131
                node.removeChild(node.firstChild);
 
132
            }
 
133
        }
 
134
        if(this.doPreload(node)){ // preloaded json children
 
135
            if(typeof callback == "function"){
 
136
                callback();
 
137
            }
 
138
        }else if(this.dataUrl||this.url){
 
139
            this.requestData(node, callback);
 
140
        }
 
141
    },
 
142
 
 
143
    doPreload : function(node){
 
144
        if(node.attributes.children){
 
145
            if(node.childNodes.length < 1){ // preloaded?
 
146
                var cs = node.attributes.children;
 
147
                node.beginUpdate();
 
148
                for(var i = 0, len = cs.length; i < len; i++){
 
149
                    var cn = node.appendChild(this.createNode(cs[i]));
 
150
                    if(this.preloadChildren){
 
151
                        this.doPreload(cn);
 
152
                    }
 
153
                }
 
154
                node.endUpdate();
 
155
            }
 
156
            return true;
 
157
        }else {
 
158
            return false;
 
159
        }
 
160
    },
 
161
 
 
162
    getParams: function(node){
 
163
        var buf = [], bp = this.baseParams;
 
164
        for(var key in bp){
 
165
            if(typeof bp[key] != "function"){
 
166
                buf.push(encodeURIComponent(key), "=", encodeURIComponent(bp[key]), "&");
 
167
            }
 
168
        }
 
169
        buf.push("node=", encodeURIComponent(node.id));
 
170
        return buf.join("");
 
171
    },
 
172
 
 
173
    requestData : function(node, callback){
 
174
        if(this.fireEvent("beforeload", this, node, callback) !== false){
 
175
            this.transId = Ext.Ajax.request({
 
176
                method:this.requestMethod,
 
177
                url: this.dataUrl||this.url,
 
178
                success: this.handleResponse,
 
179
                failure: this.handleFailure,
 
180
                scope: this,
 
181
                argument: {callback: callback, node: node},
 
182
                params: this.getParams(node)
 
183
            });
 
184
        }else{
 
185
            // if the load is cancelled, make sure we notify
 
186
            // the node that we are done
 
187
            if(typeof callback == "function"){
 
188
                callback();
 
189
            }
 
190
        }
 
191
    },
 
192
 
 
193
    isLoading : function(){
 
194
        return !!this.transId;
 
195
    },
 
196
 
 
197
    abort : function(){
 
198
        if(this.isLoading()){
 
199
            Ext.Ajax.abort(this.transId);
 
200
        }
 
201
    },
 
202
 
 
203
    /**
 
204
    * Override this function for custom TreeNode node implementation
 
205
    */
 
206
    createNode : function(attr){
 
207
        // apply baseAttrs, nice idea Corey!
 
208
        if(this.baseAttrs){
 
209
            Ext.applyIf(attr, this.baseAttrs);
 
210
        }
 
211
        if(this.applyLoader !== false){
 
212
            attr.loader = this;
 
213
        }
 
214
        if(typeof attr.uiProvider == 'string'){
 
215
           attr.uiProvider = this.uiProviders[attr.uiProvider] || eval(attr.uiProvider);
 
216
        }
 
217
        if(attr.nodeType){
 
218
            return new Ext.tree.TreePanel.nodeTypes[attr.nodeType](attr);
 
219
        }else{
 
220
            return attr.leaf ?
 
221
                        new Ext.tree.TreeNode(attr) :
 
222
                        new Ext.tree.AsyncTreeNode(attr);
 
223
        }
 
224
    },
 
225
 
 
226
    processResponse : function(response, node, callback){
 
227
        var json = response.responseText;
 
228
        try {
 
229
            var o = eval("("+json+")");
 
230
            node.beginUpdate();
 
231
            for(var i = 0, len = o.length; i < len; i++){
 
232
                var n = this.createNode(o[i]);
 
233
                if(n){
 
234
                    node.appendChild(n);
 
235
                }
 
236
            }
 
237
            node.endUpdate();
 
238
            if(typeof callback == "function"){
 
239
                callback(this, node);
 
240
            }
 
241
        }catch(e){
 
242
            this.handleFailure(response);
 
243
        }
 
244
    },
 
245
 
 
246
    handleResponse : function(response){
 
247
        this.transId = false;
 
248
        var a = response.argument;
 
249
        this.processResponse(response, a.node, a.callback);
 
250
        this.fireEvent("load", this, a.node, response);
 
251
    },
 
252
 
 
253
    handleFailure : function(response){
 
254
        this.transId = false;
 
255
        var a = response.argument;
 
256
        this.fireEvent("loadexception", this, a.node, response);
 
257
        if(typeof a.callback == "function"){
 
258
            a.callback(this, a.node);
 
259
        }
 
260
    }
 
261
});
 
 
b'\\ No newline at end of file'