summaryrefslogtreecommitdiff
path: root/frontend/beta/js/YUI-extensions/tree/TreeLoader.js
blob: 34989bd2e020c66a56568e090e757047328d1fa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
YAHOO.ext.tree.TreeLoader = function(config){
    this.baseParams = {};
    this.requestMethod = 'POST';
    YAHOO.ext.util.Config.apply(this, config);
    
    this.events = {
        'beforeload' : true,
        'load' : true,
        'loadexception' : true  
    };
};

YAHOO.extendX(YAHOO.ext.tree.TreeLoader, YAHOO.ext.util.Observable, {
    load : function(node, callback){
        if(node.attributes.children){ // preloaded json children
            var cs = node.attributes.children;
            for(var i = 0, len = cs.length; i < len; i++){
                node.appendChild(this.createNode(cs[i]));
            }
            if(typeof callback == 'function'){
                callback();
            }
        }else if(this.dataUrl){
            this.requestData(node, callback);
        }
    },
    
    getParams: function(node){
        var buf = [], bp = this.baseParams;
        for(var key in bp){
            if(typeof bp[key] != 'function'){
                buf.push(encodeURIComponent(key), '=', encodeURIComponent(bp[key]), '&');
            }
        }
        buf.push('node=', encodeURIComponent(node.id));
        return buf.join('');
    },
    
    requestData : function(node, callback){
        if(this.fireEvent('beforeload', this, node, callback) !== false){
            var params = this.getParams(node);
            var cb = {
                success: this.handleResponse,
                failure: this.handleFailure,
                scope: this,
        		argument: {callback: callback, node: node}
            };
            this.transId = YAHOO.util.Connect.asyncRequest(this.requestMethod, this.dataUrl, cb, params);
        }else{
            // if the load is cancelled, make sure we notify 
            // the node that we are done
            if(typeof callback == 'function'){
                callback();
            }
        }
    },
    
    isLoading : function(){
        return this.transId ? true : false;  
    },
    
    abort : function(){
        if(this.isLoading()){
            YAHOO.util.Connect.abort(this.transId);
        }
    },
    
    createNode : function(attr){
        if(this.applyLoader !== false){
            attr.loader = this;
        }
        return(attr.leaf ? 
                        new YAHOO.ext.tree.TreeNode(attr) : 
                        new YAHOO.ext.tree.AsyncTreeNode(attr));  
    },
    
    processResponse : function(response, node, callback){
        var json = response.responseText;
        try {
            var o = eval('('+json+')');
	        for(var i = 0, len = o.length; i < len; i++){
	            node.appendChild(this.createNode(o[i])); 
	        }
	        if(typeof callback == 'function'){
                callback();
            }
        }catch(e){
            this.handleFailure(response);
        }
    },
    
    handleResponse : function(response){
        this.transId = false;
        var a = response.argument;
        this.processResponse(response, a.node, a.callback);
        this.fireEvent('load', this, a.node, response);
    },
    
    handleFailure : function(response){
        this.transId = false;
        var a = response.argument;
        this.fireEvent('loadexception', this, a.node, response);
        if(typeof a.callback == 'function'){
            a.callback();
        }
    }
});