Diffstat (limited to 'frontend/beta/js/YUI-extensions/tree/TreeLoader.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/YUI-extensions/tree/TreeLoader.js | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/tree/TreeLoader.js b/frontend/beta/js/YUI-extensions/tree/TreeLoader.js new file mode 100644 index 0000000..34989bd --- a/dev/null +++ b/frontend/beta/js/YUI-extensions/tree/TreeLoader.js | |||
@@ -0,0 +1,107 @@ | |||
1 | YAHOO.ext.tree.TreeLoader = function(config){ | ||
2 | this.baseParams = {}; | ||
3 | this.requestMethod = 'POST'; | ||
4 | YAHOO.ext.util.Config.apply(this, config); | ||
5 | |||
6 | this.events = { | ||
7 | 'beforeload' : true, | ||
8 | 'load' : true, | ||
9 | 'loadexception' : true | ||
10 | }; | ||
11 | }; | ||
12 | |||
13 | YAHOO.extendX(YAHOO.ext.tree.TreeLoader, YAHOO.ext.util.Observable, { | ||
14 | load : function(node, callback){ | ||
15 | if(node.attributes.children){ // preloaded json children | ||
16 | var cs = node.attributes.children; | ||
17 | for(var i = 0, len = cs.length; i < len; i++){ | ||
18 | node.appendChild(this.createNode(cs[i])); | ||
19 | } | ||
20 | if(typeof callback == 'function'){ | ||
21 | callback(); | ||
22 | } | ||
23 | }else if(this.dataUrl){ | ||
24 | this.requestData(node, callback); | ||
25 | } | ||
26 | }, | ||
27 | |||
28 | getParams: function(node){ | ||
29 | var buf = [], bp = this.baseParams; | ||
30 | for(var key in bp){ | ||
31 | if(typeof bp[key] != 'function'){ | ||
32 | buf.push(encodeURIComponent(key), '=', encodeURIComponent(bp[key]), '&'); | ||
33 | } | ||
34 | } | ||
35 | buf.push('node=', encodeURIComponent(node.id)); | ||
36 | return buf.join(''); | ||
37 | }, | ||
38 | |||
39 | requestData : function(node, callback){ | ||
40 | if(this.fireEvent('beforeload', this, node, callback) !== false){ | ||
41 | var params = this.getParams(node); | ||
42 | var cb = { | ||
43 | success: this.handleResponse, | ||
44 | failure: this.handleFailure, | ||
45 | scope: this, | ||
46 | argument: {callback: callback, node: node} | ||
47 | }; | ||
48 | this.transId = YAHOO.util.Connect.asyncRequest(this.requestMethod, this.dataUrl, cb, params); | ||
49 | }else{ | ||
50 | // if the load is cancelled, make sure we notify | ||
51 | // the node that we are done | ||
52 | if(typeof callback == 'function'){ | ||
53 | callback(); | ||
54 | } | ||
55 | } | ||
56 | }, | ||
57 | |||
58 | isLoading : function(){ | ||
59 | return this.transId ? true : false; | ||
60 | }, | ||
61 | |||
62 | abort : function(){ | ||
63 | if(this.isLoading()){ | ||
64 | YAHOO.util.Connect.abort(this.transId); | ||
65 | } | ||
66 | }, | ||
67 | |||
68 | createNode : function(attr){ | ||
69 | if(this.applyLoader !== false){ | ||
70 | attr.loader = this; | ||
71 | } | ||
72 | return(attr.leaf ? | ||
73 | new YAHOO.ext.tree.TreeNode(attr) : | ||
74 | new YAHOO.ext.tree.AsyncTreeNode(attr)); | ||
75 | }, | ||
76 | |||
77 | processResponse : function(response, node, callback){ | ||
78 | var json = response.responseText; | ||
79 | try { | ||
80 | var o = eval('('+json+')'); | ||
81 | for(var i = 0, len = o.length; i < len; i++){ | ||
82 | node.appendChild(this.createNode(o[i])); | ||
83 | } | ||
84 | if(typeof callback == 'function'){ | ||
85 | callback(); | ||
86 | } | ||
87 | }catch(e){ | ||
88 | this.handleFailure(response); | ||
89 | } | ||
90 | }, | ||
91 | |||
92 | handleResponse : function(response){ | ||
93 | this.transId = false; | ||
94 | var a = response.argument; | ||
95 | this.processResponse(response, a.node, a.callback); | ||
96 | this.fireEvent('load', this, a.node, response); | ||
97 | }, | ||
98 | |||
99 | handleFailure : function(response){ | ||
100 | this.transId = false; | ||
101 | var a = response.argument; | ||
102 | this.fireEvent('loadexception', this, a.node, response); | ||
103 | if(typeof a.callback == 'function'){ | ||
104 | a.callback(); | ||
105 | } | ||
106 | } | ||
107 | }); | ||