summaryrefslogtreecommitdiff
path: root/frontend/beta/js/YUI-extensions/tree/TreeSelectionModel.js
blob: 4fed88ef4f8ffdaddd62b7d8064664bba7a2c061 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
YAHOO.ext.tree.DefaultSelectionModel = function(){
   this.selNode = null;
   
   this.events = {
       'selectionchange' : true
   };
};

YAHOO.extendX(YAHOO.ext.tree.DefaultSelectionModel, YAHOO.ext.util.Observable, {
    init : function(tree){
        this.tree = tree;
        tree.el.mon('keydown', this.onKeyDown, this, true);
        tree.on('click', this.onNodeClick, this, true);
    },
    
    onNodeClick : function(node, e){
        this.select(node);
    },
    
    select : function(node){
        if(this.selNode && this.selNode != node){
            this.selNode.ui.onSelectedChange(false);
        }
        this.selNode = node;
        node.ui.onSelectedChange(true);
        this.fireEvent('selectionchange', this, node);
        return node;
    },
    
    unselect : function(node){
        if(this.selNode == node){
            this.clearSelections();
        }    
    },
    
    clearSelections : function(){
        var n = this.selNode;
        if(n){
            n.ui.onSelectedChange(false);
            this.selNode = null;
            this.fireEvent('selectionchange', this, null);
        }
        return n;
    },
    
    getSelectedNode : function(){
        return this.selNode;    
    },
    
    isSelected : function(node){
        return this.selNode == node;  
    },
    
    onKeyDown : function(e){
        var s = this.selNode || this.lastSelNode;
        // undesirable, but required
        var sm = this;
        if(!s){
            return;
        }
        var k = e.getKey();
        //alert(k)
        switch(k){
             case e.DOWN:
                 e.preventDefault();
                 if(s.firstChild && s.isExpanded()){
                     this.select(s.firstChild, e);
                 }else if(s.nextSibling){
                     this.select(s.nextSibling, e);
                 }else if(s.parentNode){
                     s.parentNode.bubble(function(){
                         if(this.nextSibling){
                             sm.select(this.nextSibling, e);
                             return false;
                         }
                     });
                 }
             break;
             case e.UP:
                 e.preventDefault();
                 var ps = s.previousSibling;
                 if(ps){
                     if(!ps.isExpanded()){
                         this.select(ps, e);
                     }else{
                         var lc = ps.lastChild;
                         while(lc && lc.isExpanded()){
                             lc = lc.lastChild; 
                         }
                         this.select(lc, e);
                     }
                 }else if(s.parentNode && (this.tree.rootVisible || !s.parentNode.isRoot)){
                     this.select(s.parentNode, e);
                 }
             break;
             case e.RIGHT:
                 e.preventDefault();
                 if(s.hasChildNodes()){
                     if(!s.isExpanded()){
                         s.expand();
                     }else if(s.firstChild){
                         this.select(s.firstChild, e);
                     }
                 }
             break;
             case e.LEFT:
                 e.preventDefault();
                 if(s.hasChildNodes() && s.isExpanded()){
                     s.collapse();
                 }else if(s.parentNode && (this.tree.rootVisible || s.parentNode != this.tree.getRootNode())){
                     this.select(s.parentNode, e);
                 }
             break;
        };
    }
});

YAHOO.ext.tree.MultiSelectionModel = function(){
   this.selNodes = [];
   this.selMap = {};
   this.events = {
       'selectionchange' : true
   };
};

YAHOO.extendX(YAHOO.ext.tree.MultiSelectionModel, YAHOO.ext.util.Observable, {
    init : function(tree){
        this.tree = tree;
        tree.el.mon('keydown', this.onKeyDown, this, true);
        tree.on('click', this.onNodeClick, this, true);
    },
    
    onNodeClick : function(node, e){
        this.select(node, e, e.ctrlKey);
    },
    
    select : function(node, e, keepExisting){
        if(keepExisting !== true){
            this.clearSelections(true);
        }
        this.selNodes.push(node);
        this.selMap[node.id] = node;
        this.lastSelNode = node;
        node.ui.onSelectedChange(true);
        this.fireEvent('selectionchange', this, this.selNodes);
        return node;
    },
    
    unselect : function(node){
        if(this.selMap[node.id]){
            node.ui.onSelectedChange(false);
            var sn = this.selNodes;
            var index = -1;
            if(sn.indexOf){
                index = sn.indexOf(node);
            }else{
                for(var i = 0, len = sn.length; i < len; i++){
                    if(sn[i] == node){
                        index = i;
                        break;
                    }
                }
            }
            if(index != -1){
                this.selNodes.splice(index, 1);
            }
            delete this.selMap[node.id];
            this.fireEvent('selectionchange', this, this.selNodes);
        }
    },
    
    clearSelections : function(suppressEvent){
        var sn = this.selNodes;
        if(sn.length > 0){
            for(var i = 0, len = sn.length; i < len; i++){
                sn[i].ui.onSelectedChange(false);
            }
            this.selNodes = [];
            this.selMap = {};
            if(suppressEvent !== true){
                this.fireEvent('selectionchange', this, this.selNodes);
            }
        }
    },
    
    isSelected : function(node){
        return this.selMap[node.id] ? true : false;  
    },
    
    getSelectedNodes : function(){
        return this.selNodes;    
    },
    
    onKeyDown : YAHOO.ext.tree.DefaultSelectionModel.prototype.onKeyDown
});