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.tree.TreeLoader |
---|
9 | * @extends Ext.util.Observable |
---|
10 | * A TreeLoader provides for lazy loading of an {@link Ext.tree.TreeNode}'s child |
---|
11 | * nodes from a specified URL. The response must be a JavaScript Array definition |
---|
12 | * whose elements are node definition objects. e.g.: |
---|
13 | * <pre><code> |
---|
14 | [{ |
---|
15 | id: 1, |
---|
16 | text: 'A leaf Node', |
---|
17 | leaf: true |
---|
18 | },{ |
---|
19 | id: 2, |
---|
20 | text: 'A folder Node', |
---|
21 | children: [{ |
---|
22 | id: 3, |
---|
23 | text: 'A child Node', |
---|
24 | leaf: true |
---|
25 | }] |
---|
26 | }] |
---|
27 | </code></pre> |
---|
28 | * <br><br> |
---|
29 | * A server request is sent, and child nodes are loaded only when a node is expanded. |
---|
30 | * The loading node's id is passed to the server under the parameter name "node" to |
---|
31 | * enable the server to produce the correct child nodes. |
---|
32 | * <br><br> |
---|
33 | * To pass extra parameters, an event handler may be attached to the "beforeload" |
---|
34 | * event, and the parameters specified in the TreeLoader's baseParams property: |
---|
35 | * <pre><code> |
---|
36 | myTreeLoader.on("beforeload", function(treeLoader, node) { |
---|
37 | this.baseParams.category = node.attributes.category; |
---|
38 | }, this); |
---|
39 | </code></pre> |
---|
40 | * This would pass an HTTP parameter called "category" to the server containing |
---|
41 | * the value of the Node's "category" attribute. |
---|
42 | * @constructor |
---|
43 | * Creates a new Treeloader. |
---|
44 | * @param {Object} config A config object containing config properties. |
---|
45 | */ |
---|
46 | Ext.tree.TreeLoader = function(config){ |
---|
47 | this.baseParams = {}; |
---|
48 | Ext.apply(this, config); |
---|
49 | |
---|
50 | this.addEvents( |
---|
51 | /** |
---|
52 | * @event beforeload |
---|
53 | * Fires before a network request is made to retrieve the Json text which specifies a node's children. |
---|
54 | * @param {Object} This TreeLoader object. |
---|
55 | * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded. |
---|
56 | * @param {Object} callback The callback function specified in the {@link #load} call. |
---|
57 | */ |
---|
58 | "beforeload", |
---|
59 | /** |
---|
60 | * @event load |
---|
61 | * Fires when the node has been successfuly loaded. |
---|
62 | * @param {Object} This TreeLoader object. |
---|
63 | * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded. |
---|
64 | * @param {Object} response The response object containing the data from the server. |
---|
65 | */ |
---|
66 | "load", |
---|
67 | /** |
---|
68 | * @event loadexception |
---|
69 | * Fires if the network request failed. |
---|
70 | * @param {Object} This TreeLoader object. |
---|
71 | * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded. |
---|
72 | * @param {Object} response The response object containing the data from the server. |
---|
73 | */ |
---|
74 | "loadexception" |
---|
75 | ); |
---|
76 | Ext.tree.TreeLoader.superclass.constructor.call(this); |
---|
77 | if(typeof this.paramOrder == 'string'){ |
---|
78 | this.paramOrder = this.paramOrder.split(/[\s,|]/); |
---|
79 | } |
---|
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 | * @cfg {Array/String} paramOrder Defaults to <tt>undefined</tt>. Only used when using directFn. |
---|
123 | * A list of params to be executed |
---|
124 | * server side. Specify the params in the order in which they must be executed on the server-side |
---|
125 | * as either (1) an Array of String values, or (2) a String of params delimited by either whitespace, |
---|
126 | * comma, or pipe. For example, |
---|
127 | * any of the following would be acceptable:<pre><code> |
---|
128 | paramOrder: ['param1','param2','param3'] |
---|
129 | paramOrder: 'param1 param2 param3' |
---|
130 | paramOrder: 'param1,param2,param3' |
---|
131 | paramOrder: 'param1|param2|param' |
---|
132 | </code></pre> |
---|
133 | */ |
---|
134 | paramOrder: undefined, |
---|
135 | |
---|
136 | /** |
---|
137 | * @cfg {Boolean} paramsAsHash Only used when using directFn. |
---|
138 | * Send parameters as a collection of named arguments (defaults to <tt>false</tt>). Providing a |
---|
139 | * <tt>{@link #paramOrder}</tt> nullifies this configuration. |
---|
140 | */ |
---|
141 | paramsAsHash: false, |
---|
142 | |
---|
143 | /** |
---|
144 | * @cfg {Function} directFn |
---|
145 | * Function to call when executing a request. |
---|
146 | */ |
---|
147 | directFn : undefined, |
---|
148 | |
---|
149 | /** |
---|
150 | * Load an {@link Ext.tree.TreeNode} from the URL specified in the constructor. |
---|
151 | * This is called automatically when a node is expanded, but may be used to reload |
---|
152 | * a node (or append new children if the {@link #clearOnLoad} option is false.) |
---|
153 | * @param {Ext.tree.TreeNode} node |
---|
154 | * @param {Function} callback |
---|
155 | * @param (Object) scope |
---|
156 | */ |
---|
157 | load : function(node, callback, scope){ |
---|
158 | if(this.clearOnLoad){ |
---|
159 | while(node.firstChild){ |
---|
160 | node.removeChild(node.firstChild); |
---|
161 | } |
---|
162 | } |
---|
163 | if(this.doPreload(node)){ // preloaded json children |
---|
164 | this.runCallback(callback, scope || node, []); |
---|
165 | }else if(this.directFn || this.dataUrl || this.url){ |
---|
166 | this.requestData(node, callback, scope || node); |
---|
167 | } |
---|
168 | }, |
---|
169 | |
---|
170 | doPreload : function(node){ |
---|
171 | if(node.attributes.children){ |
---|
172 | if(node.childNodes.length < 1){ // preloaded? |
---|
173 | var cs = node.attributes.children; |
---|
174 | node.beginUpdate(); |
---|
175 | for(var i = 0, len = cs.length; i < len; i++){ |
---|
176 | var cn = node.appendChild(this.createNode(cs[i])); |
---|
177 | if(this.preloadChildren){ |
---|
178 | this.doPreload(cn); |
---|
179 | } |
---|
180 | } |
---|
181 | node.endUpdate(); |
---|
182 | } |
---|
183 | return true; |
---|
184 | } |
---|
185 | return false; |
---|
186 | }, |
---|
187 | |
---|
188 | getParams: function(node){ |
---|
189 | var buf = [], bp = this.baseParams; |
---|
190 | if(this.directFn){ |
---|
191 | buf.push(node.id); |
---|
192 | if(bp){ |
---|
193 | if(this.paramOrder){ |
---|
194 | for(var i = 0, len = this.paramOrder.length; i < len; i++){ |
---|
195 | buf.push(bp[this.paramOrder[i]]); |
---|
196 | } |
---|
197 | }else if(this.paramsAsHash){ |
---|
198 | buf.push(bp); |
---|
199 | } |
---|
200 | } |
---|
201 | return buf; |
---|
202 | }else{ |
---|
203 | for(var key in bp){ |
---|
204 | if(!Ext.isFunction(bp[key])){ |
---|
205 | buf.push(encodeURIComponent(key), "=", encodeURIComponent(bp[key]), "&"); |
---|
206 | } |
---|
207 | } |
---|
208 | buf.push("node=", encodeURIComponent(node.id)); |
---|
209 | return buf.join(""); |
---|
210 | } |
---|
211 | }, |
---|
212 | |
---|
213 | requestData : function(node, callback, scope){ |
---|
214 | if(this.fireEvent("beforeload", this, node, callback) !== false){ |
---|
215 | if(this.directFn){ |
---|
216 | var args = this.getParams(node); |
---|
217 | args.push(this.processDirectResponse.createDelegate(this, [{callback: callback, node: node, scope: scope}], true)); |
---|
218 | this.directFn.apply(window, args); |
---|
219 | }else{ |
---|
220 | this.transId = Ext.Ajax.request({ |
---|
221 | method:this.requestMethod, |
---|
222 | url: this.dataUrl||this.url, |
---|
223 | success: this.handleResponse, |
---|
224 | failure: this.handleFailure, |
---|
225 | scope: this, |
---|
226 | argument: {callback: callback, node: node, scope: scope}, |
---|
227 | params: this.getParams(node) |
---|
228 | }); |
---|
229 | } |
---|
230 | }else{ |
---|
231 | // if the load is cancelled, make sure we notify |
---|
232 | // the node that we are done |
---|
233 | this.runCallback(callback, scope || node, []); |
---|
234 | } |
---|
235 | }, |
---|
236 | |
---|
237 | processDirectResponse: function(result, response, args){ |
---|
238 | if(response.status){ |
---|
239 | this.handleResponse({ |
---|
240 | responseData: Ext.isArray(result) ? result : null, |
---|
241 | responseText: result, |
---|
242 | argument: args |
---|
243 | }); |
---|
244 | }else{ |
---|
245 | this.handleFailure({ |
---|
246 | argument: args |
---|
247 | }); |
---|
248 | } |
---|
249 | }, |
---|
250 | |
---|
251 | // private |
---|
252 | runCallback: function(cb, scope, args){ |
---|
253 | if(Ext.isFunction(cb)){ |
---|
254 | cb.apply(scope, args); |
---|
255 | } |
---|
256 | }, |
---|
257 | |
---|
258 | isLoading : function(){ |
---|
259 | return !!this.transId; |
---|
260 | }, |
---|
261 | |
---|
262 | abort : function(){ |
---|
263 | if(this.isLoading()){ |
---|
264 | Ext.Ajax.abort(this.transId); |
---|
265 | } |
---|
266 | }, |
---|
267 | |
---|
268 | /** |
---|
269 | * <p>Override this function for custom TreeNode node implementation, or to |
---|
270 | * modify the attributes at creation time.</p> |
---|
271 | * Example:<pre><code> |
---|
272 | new Ext.tree.TreePanel({ |
---|
273 | ... |
---|
274 | new Ext.tree.TreeLoader({ |
---|
275 | url: 'dataUrl', |
---|
276 | createNode: function(attr) { |
---|
277 | // Allow consolidation consignments to have |
---|
278 | // consignments dropped into them. |
---|
279 | if (attr.isConsolidation) { |
---|
280 | attr.iconCls = 'x-consol', |
---|
281 | attr.allowDrop = true; |
---|
282 | } |
---|
283 | return Ext.tree.TreeLoader.prototype.call(this, attr); |
---|
284 | } |
---|
285 | }), |
---|
286 | ... |
---|
287 | }); |
---|
288 | </code></pre> |
---|
289 | * @param attr {Object} The attributes from which to create the new node. |
---|
290 | */ |
---|
291 | createNode : function(attr){ |
---|
292 | // apply baseAttrs, nice idea Corey! |
---|
293 | if(this.baseAttrs){ |
---|
294 | Ext.applyIf(attr, this.baseAttrs); |
---|
295 | } |
---|
296 | if(this.applyLoader !== false){ |
---|
297 | attr.loader = this; |
---|
298 | } |
---|
299 | if(typeof attr.uiProvider == 'string'){ |
---|
300 | attr.uiProvider = this.uiProviders[attr.uiProvider] || eval(attr.uiProvider); |
---|
301 | } |
---|
302 | if(attr.nodeType){ |
---|
303 | return new Ext.tree.TreePanel.nodeTypes[attr.nodeType](attr); |
---|
304 | }else{ |
---|
305 | return attr.leaf ? |
---|
306 | new Ext.tree.TreeNode(attr) : |
---|
307 | new Ext.tree.AsyncTreeNode(attr); |
---|
308 | } |
---|
309 | }, |
---|
310 | |
---|
311 | processResponse : function(response, node, callback, scope){ |
---|
312 | var json = response.responseText; |
---|
313 | try { |
---|
314 | var o = response.responseData || Ext.decode(json); |
---|
315 | node.beginUpdate(); |
---|
316 | for(var i = 0, len = o.length; i < len; i++){ |
---|
317 | var n = this.createNode(o[i]); |
---|
318 | if(n){ |
---|
319 | node.appendChild(n); |
---|
320 | } |
---|
321 | } |
---|
322 | node.endUpdate(); |
---|
323 | this.runCallback(callback, scope || node, [node]); |
---|
324 | }catch(e){ |
---|
325 | this.handleFailure(response); |
---|
326 | } |
---|
327 | }, |
---|
328 | |
---|
329 | handleResponse : function(response){ |
---|
330 | this.transId = false; |
---|
331 | var a = response.argument; |
---|
332 | this.processResponse(response, a.node, a.callback, a.scope); |
---|
333 | this.fireEvent("load", this, a.node, response); |
---|
334 | }, |
---|
335 | |
---|
336 | handleFailure : function(response){ |
---|
337 | this.transId = false; |
---|
338 | var a = response.argument; |
---|
339 | this.fireEvent("loadexception", this, a.node, response); |
---|
340 | this.runCallback(a.callback, a.scope || a.node, [a.node]); |
---|
341 | } |
---|
342 | }); |
---|