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