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