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 Roo.tree.TreeLoader.superclass.constructor.call(this);
74 Roo.extend(Roo.tree.TreeLoader, Roo.util.Observable, {
76 * @cfg {String} dataUrl The URL from which to request a Json string which
77 * specifies an array of node definition object representing the child nodes
81 * @cfg {Object} baseParams (optional) An object containing properties which
82 * specify HTTP parameters to be passed to each request for child nodes.
85 * @cfg {Object} baseAttrs (optional) An object containing attributes to be added to all nodes
86 * created by this loader. If the attributes sent by the server have an attribute in this object,
90 * @cfg {Object} uiProviders (optional) An object containing properties which
91 * specify custom {@link Roo.tree.TreeNodeUI} implementations. If the optional
92 * <i>uiProvider</i> attribute of a returned child node is a string rather
93 * than a reference to a TreeNodeUI implementation, this that string value
94 * is used as a property name in the uiProviders object. You can define the provider named
95 * 'default' , and this will be used for all nodes (if no uiProvider is delivered by the node data)
100 * @cfg {Boolean} clearOnLoad (optional) Default to true. Remove previously existing
101 * child nodes before loading.
106 * @cfg {String} root (optional) Default to false. Use this to read data from an object
107 * property on loading, rather than expecting an array. (eg. more compatible to a standard
108 * Grid query { data : [ .....] }
113 * @cfg {String} queryParam (optional)
114 * Name of the query as it will be passed on the querystring (defaults to 'node')
115 * eg. the request will be ?node=[id]
122 * Load an {@link Roo.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 {Roo.tree.TreeNode} node
126 * @param {Function} callback
128 load : function(node, callback){
129 if(this.clearOnLoad){
130 while(node.firstChild){
131 node.removeChild(node.firstChild);
134 if(node.attributes.children){ // preloaded json children
135 var cs = node.attributes.children;
136 for(var i = 0, len = cs.length; i < len; i++){
137 node.appendChild(this.createNode(cs[i]));
139 if(typeof callback == "function"){
142 }else if(this.dataUrl){
143 this.requestData(node, callback);
147 getParams: function(node){
148 var buf = [], bp = this.baseParams;
150 if(typeof bp[key] != "function"){
151 buf.push(encodeURIComponent(key), "=", encodeURIComponent(bp[key]), "&");
154 var n = this.queryParam === false ? 'node' : this.queryParam;
155 buf.push(n + "=", encodeURIComponent(node.id));
159 requestData : function(node, callback){
160 if(this.fireEvent("beforeload", this, node, callback) !== false){
161 this.transId = Roo.Ajax.request({
162 method:this.requestMethod,
163 url: this.dataUrl||this.url,
164 success: this.handleResponse,
165 failure: this.handleFailure,
167 argument: {callback: callback, node: node},
168 params: this.getParams(node)
171 // if the load is cancelled, make sure we notify
172 // the node that we are done
173 if(typeof callback == "function"){
179 isLoading : function(){
180 return this.transId ? true : false;
184 if(this.isLoading()){
185 Roo.Ajax.abort(this.transId);
190 * Override this function for custom TreeNode node implementation
192 createNode : function(attr){
193 // apply baseAttrs, nice idea Corey!
195 Roo.applyIf(attr, this.baseAttrs);
197 if(this.applyLoader !== false){
200 if(typeof(attr.uiProvider) == 'string'){
202 attr.uiProvider = this.uiProviders[attr.uiProvider] ||
203 /** eval:var:attr */ eval(attr.uiProvider);
205 if(typeof(this.uiProviders['default']) != 'undefined') {
206 attr.uiProvider = this.uiProviders['default'];
208 attr.leaf = typeof(attr.leaf) == 'string' ? attr.leaf * 1 : attr.leaf;
210 new Roo.tree.TreeNode(attr) :
211 new Roo.tree.AsyncTreeNode(attr));
214 processResponse : function(response, node, callback){
215 var json = response.responseText;
218 var o = /** eval:var:zzzzzzzzzz */ eval("("+json+")");
219 if (this.root !== false) {
223 for(var i = 0, len = o.length; i < len; i++){
224 var n = this.createNode(o[i]);
229 if(typeof callback == "function"){
230 callback(this, node);
233 this.handleFailure(response);
237 handleResponse : function(response){
238 this.transId = false;
239 var a = response.argument;
240 this.processResponse(response, a.node, a.callback);
241 this.fireEvent("load", this, a.node, response);
244 handleFailure : function(response){
245 this.transId = false;
246 var a = response.argument;
247 this.fireEvent("loadexception", this, a.node, response);
248 if(typeof a.callback == "function"){
249 a.callback(this, a.node);