Upgrade to ExtJS 3.2.1 - Released 04/27/2010
[extjs.git] / src / widgets / tree / AsyncTreeNode.js
1 /*!
2  * Ext JS Library 3.2.1
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.tree.AsyncTreeNode
9  * @extends Ext.tree.TreeNode
10  * @cfg {TreeLoader} loader A TreeLoader to be used by this node (defaults to the loader defined on the tree)
11  * @constructor
12  * @param {Object/String} attributes The attributes/config for the node or just a string with the text for the node 
13  */
14  Ext.tree.AsyncTreeNode = function(config){
15     this.loaded = config && config.loaded === true;
16     this.loading = false;
17     Ext.tree.AsyncTreeNode.superclass.constructor.apply(this, arguments);
18     /**
19     * @event beforeload
20     * Fires before this node is loaded, return false to cancel
21     * @param {Node} this This node
22     */
23     this.addEvents('beforeload', 'load');
24     /**
25     * @event load
26     * Fires when this node is loaded
27     * @param {Node} this This node
28     */
29     /**
30      * The loader used by this node (defaults to using the tree's defined loader)
31      * @type TreeLoader
32      * @property loader
33      */
34 };
35 Ext.extend(Ext.tree.AsyncTreeNode, Ext.tree.TreeNode, {
36     expand : function(deep, anim, callback, scope){
37         if(this.loading){ // if an async load is already running, waiting til it's done
38             var timer;
39             var f = function(){
40                 if(!this.loading){ // done loading
41                     clearInterval(timer);
42                     this.expand(deep, anim, callback, scope);
43                 }
44             }.createDelegate(this);
45             timer = setInterval(f, 200);
46             return;
47         }
48         if(!this.loaded){
49             if(this.fireEvent("beforeload", this) === false){
50                 return;
51             }
52             this.loading = true;
53             this.ui.beforeLoad(this);
54             var loader = this.loader || this.attributes.loader || this.getOwnerTree().getLoader();
55             if(loader){
56                 loader.load(this, this.loadComplete.createDelegate(this, [deep, anim, callback, scope]), this);
57                 return;
58             }
59         }
60         Ext.tree.AsyncTreeNode.superclass.expand.call(this, deep, anim, callback, scope);
61     },
62     
63     /**
64      * Returns true if this node is currently loading
65      * @return {Boolean}
66      */
67     isLoading : function(){
68         return this.loading;  
69     },
70     
71     loadComplete : function(deep, anim, callback, scope){
72         this.loading = false;
73         this.loaded = true;
74         this.ui.afterLoad(this);
75         this.fireEvent("load", this);
76         this.expand(deep, anim, callback, scope);
77     },
78     
79     /**
80      * Returns true if this node has been loaded
81      * @return {Boolean}
82      */
83     isLoaded : function(){
84         return this.loaded;
85     },
86     
87     hasChildNodes : function(){
88         if(!this.isLeaf() && !this.loaded){
89             return true;
90         }else{
91             return Ext.tree.AsyncTreeNode.superclass.hasChildNodes.call(this);
92         }
93     },
94
95     /**
96      * Trigger a reload for this node
97      * @param {Function} callback
98      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this Node.
99      */
100     reload : function(callback, scope){
101         this.collapse(false, false);
102         while(this.firstChild){
103             this.removeChild(this.firstChild).destroy();
104         }
105         this.childrenRendered = false;
106         this.loaded = false;
107         if(this.isHiddenRoot()){
108             this.expanded = false;
109         }
110         this.expand(false, false, callback, scope);
111     }
112 });
113
114 Ext.tree.TreePanel.nodeTypes.async = Ext.tree.AsyncTreeNode;