3
* Copyright(c) 2006-2008, Ext JS, LLC.
6
* http://extjs.com/license
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:
22
text: 'A folder Node',
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.
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:
38
myTreeLoader.on("beforeload", function(treeLoader, node) {
39
this.baseParams.category = node.attributes.category;
42
* This would pass an HTTP parameter called "category" to the server containing
43
* the value of the Node's "category" attribute.
45
* Creates a new Treeloader.
46
* @param {Object} config A config object containing config properties.
48
Ext.tree.TreeLoader = function(config){
50
Ext.apply(this, config);
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.
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.
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.
79
Ext.tree.TreeLoader.superclass.constructor.call(this);
82
Ext.extend(Ext.tree.TreeLoader, Ext.util.Observable, {
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
89
* @cfg {String} requestMethod The HTTP request method for loading data (defaults to the value of {@link Ext.Ajax#method}).
92
* @cfg {String} url Equivalent to {@link #dataUrl}.
95
* @cfg {Boolean} preloadChildren If set to true, the loader recursively loads "children" attributes when doing the first load on nodes.
98
* @cfg {Object} baseParams (optional) An object containing properties which
99
* specify HTTP parameters to be passed to each request for child nodes.
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.
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.
116
* @cfg {Boolean} clearOnLoad (optional) Default to true. Remove previously existing
117
* child nodes before loading.
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
128
load : function(node, callback){
129
if(this.clearOnLoad){
130
while(node.firstChild){
131
node.removeChild(node.firstChild);
134
if(this.doPreload(node)){ // preloaded json children
135
if(typeof callback == "function"){
138
}else if(this.dataUrl||this.url){
139
this.requestData(node, callback);
143
doPreload : function(node){
144
if(node.attributes.children){
145
if(node.childNodes.length < 1){ // preloaded?
146
var cs = node.attributes.children;
148
for(var i = 0, len = cs.length; i < len; i++){
149
var cn = node.appendChild(this.createNode(cs[i]));
150
if(this.preloadChildren){
162
getParams: function(node){
163
var buf = [], bp = this.baseParams;
165
if(typeof bp[key] != "function"){
166
buf.push(encodeURIComponent(key), "=", encodeURIComponent(bp[key]), "&");
169
buf.push("node=", encodeURIComponent(node.id));
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,
181
argument: {callback: callback, node: node},
182
params: this.getParams(node)
185
// if the load is cancelled, make sure we notify
186
// the node that we are done
187
if(typeof callback == "function"){
193
isLoading : function(){
194
return !!this.transId;
198
if(this.isLoading()){
199
Ext.Ajax.abort(this.transId);
204
* Override this function for custom TreeNode node implementation
206
createNode : function(attr){
207
// apply baseAttrs, nice idea Corey!
209
Ext.applyIf(attr, this.baseAttrs);
211
if(this.applyLoader !== false){
214
if(typeof attr.uiProvider == 'string'){
215
attr.uiProvider = this.uiProviders[attr.uiProvider] || eval(attr.uiProvider);
218
return new Ext.tree.TreePanel.nodeTypes[attr.nodeType](attr);
221
new Ext.tree.TreeNode(attr) :
222
new Ext.tree.AsyncTreeNode(attr);
226
processResponse : function(response, node, callback){
227
var json = response.responseText;
229
var o = eval("("+json+")");
231
for(var i = 0, len = o.length; i < len; i++){
232
var n = this.createNode(o[i]);
238
if(typeof callback == "function"){
239
callback(this, node);
242
this.handleFailure(response);
246
handleResponse : function(response){
247
this.transId = false;
248
var a = response.argument;
249
this.processResponse(response, a.node, a.callback);
250
this.fireEvent("load", this, a.node, response);
253
handleFailure : function(response){
254
this.transId = false;
255
var a = response.argument;
256
this.fireEvent("loadexception", this, a.node, response);
257
if(typeof a.callback == "function"){
258
a.callback(this, a.node);
b'\\ No newline at end of file'