4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
12 * @class Roo.tree.TreeLoader
13 * @extends Roo.util.Observable
14 * A TreeLoader provides for lazy loading of an {@link Roo.tree.TreeNode}'s child
15 * nodes from a specified URL. The response must be a javascript Array definition
16 * who's elements are node definition objects. eg:
21 { 'id': 1, 'text': 'A folder Node', 'leaf': false },
22 { 'id': 2, 'text': 'A leaf Node', 'leaf': true }
29 * The old style respose with just an array is still supported, but not recommended.
32 * A server request is sent, and child nodes are loaded only when a node is expanded.
33 * The loading node's id is passed to the server under the parameter name "node" to
34 * enable the server to produce the correct child nodes.
36 * To pass extra parameters, an event handler may be attached to the "beforeload"
37 * event, and the parameters specified in the TreeLoader's baseParams property:
39 myTreeLoader.on("beforeload", function(treeLoader, node) {
40 this.baseParams.category = node.attributes.category;
43 * This would pass an HTTP parameter called "category" to the server containing
44 * the value of the Node's "category" attribute.
46 * Creates a new Treeloader.
47 * @param {Object} config A config object containing config properties.
49 Roo.tree.TreeLoader = function(config){
51 this.requestMethod = "POST";
52 Roo.apply(this, config);
58 * Fires before a network request is made to retrieve the Json text which specifies a node's children.
59 * @param {Object} This TreeLoader object.
60 * @param {Object} node The {@link Roo.tree.TreeNode} object being loaded.
61 * @param {Object} callback The callback function specified in the {@link #load} call.
66 * Fires when the node has been successfuly loaded.
67 * @param {Object} This TreeLoader object.
68 * @param {Object} node The {@link Roo.tree.TreeNode} object being loaded.
69 * @param {Object} response The response object containing the data from the server.
73 * @event loadexception
74 * Fires if the network request failed.
75 * @param {Object} This TreeLoader object.
76 * @param {Object} node The {@link Roo.tree.TreeNode} object being loaded.
77 * @param {Object} response The response object containing the data from the server.
82 * Fires before a node is created, enabling you to return custom Node types
83 * @param {Object} This TreeLoader object.
84 * @param {Object} attr - the data returned from the AJAX call (modify it to suit)
89 Roo.tree.TreeLoader.superclass.constructor.call(this);
92 Roo.extend(Roo.tree.TreeLoader, Roo.util.Observable, {
94 * @cfg {String} dataUrl The URL from which to request a Json string which
95 * specifies an array of node definition object representing the child nodes
99 * @cfg {Object} baseParams (optional) An object containing properties which
100 * specify HTTP parameters to be passed to each request for child nodes.
103 * @cfg {Object} baseAttrs (optional) An object containing attributes to be added to all nodes
104 * created by this loader. If the attributes sent by the server have an attribute in this object,
105 * they take priority.
108 * @cfg {Object} uiProviders (optional) An object containing properties which
110 * DEPRECATED - use 'create' event handler to modify attributes - which affect creation.
111 * specify custom {@link Roo.tree.TreeNodeUI} implementations. If the optional
112 * <i>uiProvider</i> attribute of a returned child node is a string rather
113 * than a reference to a TreeNodeUI implementation, this that string value
114 * is used as a property name in the uiProviders object. You can define the provider named
115 * 'default' , and this will be used for all nodes (if no uiProvider is delivered by the node data)
120 * @cfg {Boolean} clearOnLoad (optional) Default to true. Remove previously existing
121 * child nodes before loading.
126 * @cfg {String} root (optional) Default to false. Use this to read data from an object
127 * property on loading, rather than expecting an array. (eg. more compatible to a standard
128 * Grid query { data : [ .....] }
133 * @cfg {String} queryParam (optional)
134 * Name of the query as it will be passed on the querystring (defaults to 'node')
135 * eg. the request will be ?node=[id]
142 * Load an {@link Roo.tree.TreeNode} from the URL specified in the constructor.
143 * This is called automatically when a node is expanded, but may be used to reload
144 * a node (or append new children if the {@link #clearOnLoad} option is false.)
145 * @param {Roo.tree.TreeNode} node
146 * @param {Function} callback
148 load : function(node, callback){
149 if(this.clearOnLoad){
150 while(node.firstChild){
151 node.removeChild(node.firstChild);
154 if(node.attributes.children){ // preloaded json children
155 var cs = node.attributes.children;
156 for(var i = 0, len = cs.length; i < len; i++){
157 node.appendChild(this.createNode(cs[i]));
159 if(typeof callback == "function"){
162 }else if(this.dataUrl){
163 this.requestData(node, callback);
167 getParams: function(node){
168 var buf = [], bp = this.baseParams;
170 if(typeof bp[key] != "function"){
171 buf.push(encodeURIComponent(key), "=", encodeURIComponent(bp[key]), "&");
174 var n = this.queryParam === false ? 'node' : this.queryParam;
175 buf.push(n + "=", encodeURIComponent(node.id));
179 requestData : function(node, callback){
180 if(this.fireEvent("beforeload", this, node, callback) !== false){
181 this.transId = Roo.Ajax.request({
182 method:this.requestMethod,
183 url: this.dataUrl||this.url,
184 success: this.handleResponse,
185 failure: this.handleFailure,
187 argument: {callback: callback, node: node},
188 params: this.getParams(node)
191 // if the load is cancelled, make sure we notify
192 // the node that we are done
193 if(typeof callback == "function"){
199 isLoading : function(){
200 return this.transId ? true : false;
204 if(this.isLoading()){
205 Roo.Ajax.abort(this.transId);
210 createNode : function(attr)
212 // apply baseAttrs, nice idea Corey!
214 Roo.applyIf(attr, this.baseAttrs);
216 if(this.applyLoader !== false){
219 // uiProvider = depreciated..
221 if(typeof(attr.uiProvider) == 'string'){
222 attr.uiProvider = this.uiProviders[attr.uiProvider] ||
223 /** eval:var:attr */ eval(attr.uiProvider);
225 if(typeof(this.uiProviders['default']) != 'undefined') {
226 attr.uiProvider = this.uiProviders['default'];
229 this.fireEvent('create', this, attr);
231 attr.leaf = typeof(attr.leaf) == 'string' ? attr.leaf * 1 : attr.leaf;
233 new Roo.tree.TreeNode(attr) :
234 new Roo.tree.AsyncTreeNode(attr));
237 processResponse : function(response, node, callback)
239 var json = response.responseText;
242 var o = Roo.decode(json);
244 if (this.root === false && typeof(o.success) != undefined) {
245 this.root = 'data'; // the default behaviour for list like data..
248 if (this.root !== false && !o.success) {
249 // it's a failure condition.
250 var a = response.argument;
251 this.fireEvent("loadexception", this, a.node, response);
252 Roo.log("Load failed - should have a handler really");
258 if (this.root !== false) {
262 for(var i = 0, len = o.length; i < len; i++){
263 var n = this.createNode(o[i]);
268 if(typeof callback == "function"){
269 callback(this, node);
272 this.handleFailure(response);
276 handleResponse : function(response){
277 this.transId = false;
278 var a = response.argument;
279 this.processResponse(response, a.node, a.callback);
280 this.fireEvent("load", this, a.node, response);
283 handleFailure : function(response)
285 // should handle failure better..
286 this.transId = false;
287 var a = response.argument;
288 this.fireEvent("loadexception", this, a.node, response);
289 if(typeof a.callback == "function"){
290 a.callback(this, a.node);