1 | /* |
---|
2 | * Ext JS Library 2.2.1 |
---|
3 | * Copyright(c) 2006-2009, 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 | * <p>Override this function for custom TreeNode node implementation, or to |
---|
205 | * modify the attributes at creation time.</p> |
---|
206 | * Example:<code><pre> |
---|
207 | new Ext.tree.TreePanel({ |
---|
208 | ... |
---|
209 | new Ext.tree.TreeLoader({ |
---|
210 | url: 'dataUrl', |
---|
211 | createNode: function(attr) { |
---|
212 | // Allow consolidation consignments to have |
---|
213 | // consignments dropped into them. |
---|
214 | if (attr.isConsolidation) { |
---|
215 | attr.iconCls = 'x-consol', |
---|
216 | attr.allowDrop = true; |
---|
217 | } |
---|
218 | return Ext.tree.TreeLoader.prototype.call(this, attr); |
---|
219 | } |
---|
220 | }), |
---|
221 | ... |
---|
222 | }); |
---|
223 | </pre></code> |
---|
224 | * @param attr {Object} The attributes from which to create the new node. |
---|
225 | */ |
---|
226 | createNode : function(attr){ |
---|
227 | // apply baseAttrs, nice idea Corey! |
---|
228 | if(this.baseAttrs){ |
---|
229 | Ext.applyIf(attr, this.baseAttrs); |
---|
230 | } |
---|
231 | if(this.applyLoader !== false){ |
---|
232 | attr.loader = this; |
---|
233 | } |
---|
234 | if(typeof attr.uiProvider == 'string'){ |
---|
235 | attr.uiProvider = this.uiProviders[attr.uiProvider] || eval(attr.uiProvider); |
---|
236 | } |
---|
237 | if(attr.nodeType){ |
---|
238 | return new Ext.tree.TreePanel.nodeTypes[attr.nodeType](attr); |
---|
239 | }else{ |
---|
240 | return attr.leaf ? |
---|
241 | new Ext.tree.TreeNode(attr) : |
---|
242 | new Ext.tree.AsyncTreeNode(attr); |
---|
243 | } |
---|
244 | }, |
---|
245 | |
---|
246 | processResponse : function(response, node, callback){ |
---|
247 | var json = response.responseText; |
---|
248 | try { |
---|
249 | var o = eval("("+json+")"); |
---|
250 | node.beginUpdate(); |
---|
251 | for(var i = 0, len = o.length; i < len; i++){ |
---|
252 | var n = this.createNode(o[i]); |
---|
253 | if(n){ |
---|
254 | node.appendChild(n); |
---|
255 | } |
---|
256 | } |
---|
257 | node.endUpdate(); |
---|
258 | if(typeof callback == "function"){ |
---|
259 | callback(this, node); |
---|
260 | } |
---|
261 | }catch(e){ |
---|
262 | this.handleFailure(response); |
---|
263 | } |
---|
264 | }, |
---|
265 | |
---|
266 | handleResponse : function(response){ |
---|
267 | this.transId = false; |
---|
268 | var a = response.argument; |
---|
269 | this.processResponse(response, a.node, a.callback); |
---|
270 | this.fireEvent("load", this, a.node, response); |
---|
271 | }, |
---|
272 | |
---|
273 | handleFailure : function(response){ |
---|
274 | this.transId = false; |
---|
275 | var a = response.argument; |
---|
276 | this.fireEvent("loadexception", this, a.node, response); |
---|
277 | if(typeof a.callback == "function"){ |
---|
278 | a.callback(this, a.node); |
---|
279 | } |
---|
280 | } |
---|
281 | }); |
---|