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