3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.tree.TreeLoader"></div>/**
16 * @class Ext.tree.TreeLoader
17 * @extends Ext.util.Observable
18 * A TreeLoader provides for lazy loading of an {@link Ext.tree.TreeNode}'s child
19 * nodes from a specified URL. The response must be a JavaScript Array definition
20 * whose elements are node definition objects. e.g.:
28 text: 'A folder Node',
37 * A server request is sent, and child nodes are loaded only when a node is expanded.
38 * The loading node's id is passed to the server under the parameter name "node" to
39 * enable the server to produce the correct child nodes.
41 * To pass extra parameters, an event handler may be attached to the "beforeload"
42 * event, and the parameters specified in the TreeLoader's baseParams property:
44 myTreeLoader.on("beforeload", function(treeLoader, node) {
45 this.baseParams.category = node.attributes.category;
48 * This would pass an HTTP parameter called "category" to the server containing
49 * the value of the Node's "category" attribute.
51 * Creates a new Treeloader.
52 * @param {Object} config A config object containing config properties.
54 Ext.tree.TreeLoader = function(config){
56 Ext.apply(this, config);
59 <div id="event-Ext.tree.TreeLoader-beforeload"></div>/**
61 * Fires before a network request is made to retrieve the Json text which specifies a node's children.
62 * @param {Object} This TreeLoader object.
63 * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
64 * @param {Object} callback The callback function specified in the {@link #load} call.
67 <div id="event-Ext.tree.TreeLoader-load"></div>/**
69 * Fires when the node has been successfuly loaded.
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.
75 <div id="event-Ext.tree.TreeLoader-loadexception"></div>/**
76 * @event loadexception
77 * Fires if the network request failed.
78 * @param {Object} This TreeLoader object.
79 * @param {Object} node The {@link Ext.tree.TreeNode} object being loaded.
80 * @param {Object} response The response object containing the data from the server.
84 Ext.tree.TreeLoader.superclass.constructor.call(this);
85 if(Ext.isString(this.paramOrder)){
86 this.paramOrder = this.paramOrder.split(/[\s,|]/);
90 Ext.extend(Ext.tree.TreeLoader, Ext.util.Observable, {
91 <div id="cfg-Ext.tree.TreeLoader-dataUrl"></div>/**
92 * @cfg {String} dataUrl The URL from which to request a Json string which
93 * specifies an array of node definition objects representing the child nodes
96 <div id="cfg-Ext.tree.TreeLoader-requestMethod"></div>/**
97 * @cfg {String} requestMethod The HTTP request method for loading data (defaults to the value of {@link Ext.Ajax#method}).
99 <div id="cfg-Ext.tree.TreeLoader-url"></div>/**
100 * @cfg {String} url Equivalent to {@link #dataUrl}.
102 <div id="cfg-Ext.tree.TreeLoader-preloadChildren"></div>/**
103 * @cfg {Boolean} preloadChildren If set to true, the loader recursively loads "children" attributes when doing the first load on nodes.
105 <div id="cfg-Ext.tree.TreeLoader-baseParams"></div>/**
106 * @cfg {Object} baseParams (optional) An object containing properties which
107 * specify HTTP parameters to be passed to each request for child nodes.
109 <div id="cfg-Ext.tree.TreeLoader-baseAttrs"></div>/**
110 * @cfg {Object} baseAttrs (optional) An object containing attributes to be added to all nodes
111 * created by this loader. If the attributes sent by the server have an attribute in this object,
112 * they take priority.
114 <div id="cfg-Ext.tree.TreeLoader-uiProviders"></div>/**
115 * @cfg {Object} uiProviders (optional) An object containing properties which
116 * specify custom {@link Ext.tree.TreeNodeUI} implementations. If the optional
117 * <i>uiProvider</i> attribute of a returned child node is a string rather
118 * than a reference to a TreeNodeUI implementation, then that string value
119 * is used as a property name in the uiProviders object.
123 <div id="cfg-Ext.tree.TreeLoader-clearOnLoad"></div>/**
124 * @cfg {Boolean} clearOnLoad (optional) Default to true. Remove previously existing
125 * child nodes before loading.
129 <div id="cfg-Ext.tree.TreeLoader-paramOrder"></div>/**
130 * @cfg {Array/String} paramOrder Defaults to <tt>undefined</tt>. Only used when using directFn.
131 * Specifies the params in the order in which they must be passed to the server-side Direct method
132 * as either (1) an Array of String values, or (2) a String of params delimited by either whitespace,
133 * comma, or pipe. For example,
134 * any of the following would be acceptable:<pre><code>
135 nodeParameter: 'node',
136 paramOrder: ['param1','param2','param3']
137 paramOrder: 'node param1 param2 param3'
138 paramOrder: 'param1,node,param2,param3'
139 paramOrder: 'param1|param2|param|node'
142 paramOrder: undefined,
144 <div id="cfg-Ext.tree.TreeLoader-paramsAsHash"></div>/**
145 * @cfg {Boolean} paramsAsHash Only used when using directFn.
146 * Send parameters as a collection of named arguments (defaults to <tt>false</tt>). Providing a
147 * <tt>{@link #paramOrder}</tt> nullifies this configuration.
151 <div id="cfg-Ext.tree.TreeLoader-nodeParameter"></div>/**
152 * @cfg {String} nodeParameter The name of the parameter sent to the server which contains
153 * the identifier of the node. Defaults to <tt>'node'</tt>.
155 nodeParameter: 'node',
157 <div id="cfg-Ext.tree.TreeLoader-directFn"></div>/**
158 * @cfg {Function} directFn
159 * Function to call when executing a request.
161 directFn : undefined,
163 <div id="method-Ext.tree.TreeLoader-load"></div>/**
164 * Load an {@link Ext.tree.TreeNode} from the URL specified in the constructor.
165 * This is called automatically when a node is expanded, but may be used to reload
166 * a node (or append new children if the {@link #clearOnLoad} option is false.)
167 * @param {Ext.tree.TreeNode} node
168 * @param {Function} callback Function to call after the node has been loaded. The
169 * function is passed the TreeNode which was requested to be loaded.
170 * @param {Object} scope The scope (<code>this</code> reference) in which the callback is executed.
171 * defaults to the loaded TreeNode.
173 load : function(node, callback, scope){
174 if(this.clearOnLoad){
175 while(node.firstChild){
176 node.removeChild(node.firstChild);
179 if(this.doPreload(node)){ // preloaded json children
180 this.runCallback(callback, scope || node, [node]);
181 }else if(this.directFn || this.dataUrl || this.url){
182 this.requestData(node, callback, scope || node);
186 doPreload : function(node){
187 if(node.attributes.children){
188 if(node.childNodes.length < 1){ // preloaded?
189 var cs = node.attributes.children;
191 for(var i = 0, len = cs.length; i < len; i++){
192 var cn = node.appendChild(this.createNode(cs[i]));
193 if(this.preloadChildren){
204 getParams: function(node){
205 var bp = Ext.apply({}, this.baseParams),
206 np = this.nodeParameter,
207 po = this.paramOrder;
209 np && (bp[ np ] = node.id);
214 // reset 'buf' if the nodeParameter was included in paramOrder
215 if(np && po.indexOf(np) > -1){
219 for(var i = 0, len = po.length; i < len; i++){
220 buf.push(bp[ po[i] ]);
222 }else if(this.paramsAsHash){
231 requestData : function(node, callback, scope){
232 if(this.fireEvent("beforeload", this, node, callback) !== false){
234 var args = this.getParams(node);
235 args.push(this.processDirectResponse.createDelegate(this, [{callback: callback, node: node, scope: scope}], true));
236 this.directFn.apply(window, args);
238 this.transId = Ext.Ajax.request({
239 method:this.requestMethod,
240 url: this.dataUrl||this.url,
241 success: this.handleResponse,
242 failure: this.handleFailure,
244 argument: {callback: callback, node: node, scope: scope},
245 params: this.getParams(node)
249 // if the load is cancelled, make sure we notify
250 // the node that we are done
251 this.runCallback(callback, scope || node, []);
255 processDirectResponse: function(result, response, args){
257 this.handleResponse({
258 responseData: Ext.isArray(result) ? result : null,
259 responseText: result,
270 runCallback: function(cb, scope, args){
271 if(Ext.isFunction(cb)){
272 cb.apply(scope, args);
276 isLoading : function(){
277 return !!this.transId;
281 if(this.isLoading()){
282 Ext.Ajax.abort(this.transId);
286 <div id="method-Ext.tree.TreeLoader-createNode"></div>/**
287 * <p>Override this function for custom TreeNode node implementation, or to
288 * modify the attributes at creation time.</p>
289 * Example:<pre><code>
290 new Ext.tree.TreePanel({
292 loader: new Ext.tree.TreeLoader({
294 createNode: function(attr) {
295 // Allow consolidation consignments to have
296 // consignments dropped into them.
297 if (attr.isConsolidation) {
298 attr.iconCls = 'x-consol',
299 attr.allowDrop = true;
301 return Ext.tree.TreeLoader.prototype.createNode.call(this, attr);
307 * @param attr {Object} The attributes from which to create the new node.
309 createNode : function(attr){
310 // apply baseAttrs, nice idea Corey!
312 Ext.applyIf(attr, this.baseAttrs);
314 if(this.applyLoader !== false && !attr.loader){
317 if(Ext.isString(attr.uiProvider)){
318 attr.uiProvider = this.uiProviders[attr.uiProvider] || eval(attr.uiProvider);
321 return new Ext.tree.TreePanel.nodeTypes[attr.nodeType](attr);
324 new Ext.tree.TreeNode(attr) :
325 new Ext.tree.AsyncTreeNode(attr);
329 processResponse : function(response, node, callback, scope){
330 var json = response.responseText;
332 var o = response.responseData || Ext.decode(json);
334 for(var i = 0, len = o.length; i < len; i++){
335 var n = this.createNode(o[i]);
341 this.runCallback(callback, scope || node, [node]);
343 this.handleFailure(response);
347 handleResponse : function(response){
348 this.transId = false;
349 var a = response.argument;
350 this.processResponse(response, a.node, a.callback, a.scope);
351 this.fireEvent("load", this, a.node, response);
354 handleFailure : function(response){
355 this.transId = false;
356 var a = response.argument;
357 this.fireEvent("loadexception", this, a.node, response);
358 this.runCallback(a.callback, a.scope || a.node, [a.node]);
361 destroy : function(){
363 this.purgeListeners();