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:
18 [{ 'id': 1, 'text': 'A folder Node', 'leaf': false },
19 { 'id': 2, 'text': 'A leaf Node', 'leaf': true }]
22 * A server request is sent, and child nodes are loaded only when a node is expanded.
23 * The loading node's id is passed to the server under the parameter name "node" to
24 * enable the server to produce the correct child nodes.
26 * To pass extra parameters, an event handler may be attached to the "beforeload"
27 * event, and the parameters specified in the TreeLoader's baseParams property:
29 myTreeLoader.on("beforeload", function(treeLoader, node) {
30 this.baseParams.category = node.attributes.category;
33 * This would pass an HTTP parameter called "category" to the server containing
34 * the value of the Node's "category" attribute.
36 * Creates a new Treeloader.
37 * @param {Object} config A config object containing config properties.
39 Roo.tree.TreeLoader = function(config){
41 this.requestMethod = "POST";
42 Roo.apply(this, config);
47 * Fires before a network request is made to retrieve the Json text which specifies a node's children.
48 * @param {Object} This TreeLoader object.
49 * @param {Object} node The {@link Roo.tree.TreeNode} object being loaded.
50 * @param {Object} callback The callback function specified in the {@link #load} call.
55 * Fires when the node has been successfuly loaded.
56 * @param {Object} This TreeLoader object.
57 * @param {Object} node The {@link Roo.tree.TreeNode} object being loaded.
58 * @param {Object} response The response object containing the data from the server.
62 * @event loadexception
63 * Fires if the network request failed.
64 * @param {Object} This TreeLoader object.
65 * @param {Object} node The {@link Roo.tree.TreeNode} object being loaded.
66 * @param {Object} response The response object containing the data from the server.
68 "loadexception" : true,
71 * Fires before a node is created, enabling you to return custom Node types
72 * @param {Object} This TreeLoader object.
73 * @param {Object} attr - the data returned from the AJAX call (modify it to suit)
78 Roo.tree.TreeLoader.superclass.constructor.call(this);
81 Roo.extend(Roo.tree.TreeLoader, Roo.util.Observable, {
83 * @cfg {String} dataUrl The URL from which to request a Json string which
84 * specifies an array of node definition object representing the child nodes
88 * @cfg {Object} baseParams (optional) An object containing properties which
89 * specify HTTP parameters to be passed to each request for child nodes.
92 * @cfg {Object} baseAttrs (optional) An object containing attributes to be added to all nodes
93 * created by this loader. If the attributes sent by the server have an attribute in this object,
97 * @cfg {Object} uiProviders (optional) An object containing properties which
99 * DEPRECIATED - use 'create' event handler to modify attributes - which affect creation.
100 * specify custom {@link Roo.tree.TreeNodeUI} implementations. If the optional
101 * <i>uiProvider</i> attribute of a returned child node is a string rather
102 * than a reference to a TreeNodeUI implementation, this that string value
103 * is used as a property name in the uiProviders object. You can define the provider named
104 * 'default' , and this will be used for all nodes (if no uiProvider is delivered by the node data)
109 * @cfg {Boolean} clearOnLoad (optional) Default to true. Remove previously existing
110 * child nodes before loading.
115 * @cfg {String} root (optional) Default to false. Use this to read data from an object
116 * property on loading, rather than expecting an array. (eg. more compatible to a standard
117 * Grid query { data : [ .....] }
122 * @cfg {String} queryParam (optional)
123 * Name of the query as it will be passed on the querystring (defaults to 'node')
124 * eg. the request will be ?node=[id]
131 * Load an {@link Roo.tree.TreeNode} from the URL specified in the constructor.
132 * This is called automatically when a node is expanded, but may be used to reload
133 * a node (or append new children if the {@link #clearOnLoad} option is false.)
134 * @param {Roo.tree.TreeNode} node
135 * @param {Function} callback
137 load : function(node, callback){
138 if(this.clearOnLoad){
139 while(node.firstChild){
140 node.removeChild(node.firstChild);
143 if(node.attributes.children){ // preloaded json children
144 var cs = node.attributes.children;
145 for(var i = 0, len = cs.length; i < len; i++){
146 node.appendChild(this.createNode(cs[i]));
148 if(typeof callback == "function"){
151 }else if(this.dataUrl){
152 this.requestData(node, callback);
156 getParams: function(node){
157 var buf = [], bp = this.baseParams;
159 if(typeof bp[key] != "function"){
160 buf.push(encodeURIComponent(key), "=", encodeURIComponent(bp[key]), "&");
163 var n = this.queryParam === false ? 'node' : this.queryParam;
164 buf.push(n + "=", encodeURIComponent(node.id));
168 requestData : function(node, callback){
169 if(this.fireEvent("beforeload", this, node, callback) !== false){
170 this.transId = Roo.Ajax.request({
171 method:this.requestMethod,
172 url: this.dataUrl||this.url,
173 success: this.handleResponse,
174 failure: this.handleFailure,
176 argument: {callback: callback, node: node},
177 params: this.getParams(node)
180 // if the load is cancelled, make sure we notify
181 // the node that we are done
182 if(typeof callback == "function"){
188 isLoading : function(){
189 return this.transId ? true : false;
193 if(this.isLoading()){
194 Roo.Ajax.abort(this.transId);
199 * Override this function for custom TreeNode node implementation
201 createNode : function(attr){
202 // apply baseAttrs, nice idea Corey!
204 Roo.applyIf(attr, this.baseAttrs);
206 if(this.applyLoader !== false){
209 // uiProvider = depreciated..
211 if(typeof(attr.uiProvider) == 'string'){
212 attr.uiProvider = this.uiProviders[attr.uiProvider] ||
213 /** eval:var:attr */ eval(attr.uiProvider);
215 if(typeof(this.uiProviders['default']) != 'undefined') {
216 attr.uiProvider = this.uiProviders['default'];
219 this.fireEvent('create', this, attr);
221 attr.leaf = typeof(attr.leaf) == 'string' ? attr.leaf * 1 : attr.leaf;
223 new Roo.tree.TreeNode(attr) :
224 new Roo.tree.AsyncTreeNode(attr));
227 processResponse : function(response, node, callback){
228 var json = response.responseText;
231 var o = /** eval:var:zzzzzzzzzz */ eval("("+json+")");
232 if (this.root !== false) {
236 for(var i = 0, len = o.length; i < len; i++){
237 var n = this.createNode(o[i]);
242 if(typeof callback == "function"){
243 callback(this, node);
246 this.handleFailure(response);
250 handleResponse : function(response){
251 this.transId = false;
252 var a = response.argument;
253 this.processResponse(response, a.node, a.callback);
254 this.fireEvent("load", this, a.node, response);
257 handleFailure : function(response){
258 this.transId = false;
259 var a = response.argument;
260 this.fireEvent("loadexception", this, a.node, response);
261 if(typeof a.callback == "function"){
262 a.callback(this, a.node);