~dongpo-deng/sahana-eden/test

« back to all changes in this revision

Viewing changes to static/scripts/ext-2.2.1/source/widgets/tree/TreeSelectionModel.js

  • Committer: Deng Dongpo
  • Date: 2010-08-01 09:29:44 UTC
  • Revision ID: dongpo@dhcp-21193.iis.sinica.edu.tw-20100801092944-8t9obt4xtl7otesb
initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Ext JS Library 2.2.1
 
3
 * Copyright(c) 2006-2009, Ext JS, LLC.
 
4
 * licensing@extjs.com
 
5
 * 
 
6
 * http://extjs.com/license
 
7
 */
 
8
 
 
9
/**
 
10
 * @class Ext.tree.DefaultSelectionModel
 
11
 * @extends Ext.util.Observable
 
12
 * The default single selection for a TreePanel.
 
13
 */
 
14
Ext.tree.DefaultSelectionModel = function(config){
 
15
   this.selNode = null;
 
16
   
 
17
   this.addEvents(
 
18
       /**
 
19
        * @event selectionchange
 
20
        * Fires when the selected node changes
 
21
        * @param {DefaultSelectionModel} this
 
22
        * @param {TreeNode} node the new selection
 
23
        */
 
24
       "selectionchange",
 
25
 
 
26
       /**
 
27
        * @event beforeselect
 
28
        * Fires before the selected node changes, return false to cancel the change
 
29
        * @param {DefaultSelectionModel} this
 
30
        * @param {TreeNode} node the new selection
 
31
        * @param {TreeNode} node the old selection
 
32
        */
 
33
       "beforeselect"
 
34
   );
 
35
 
 
36
    Ext.apply(this, config);
 
37
    Ext.tree.DefaultSelectionModel.superclass.constructor.call(this);
 
38
};
 
39
 
 
40
Ext.extend(Ext.tree.DefaultSelectionModel, Ext.util.Observable, {
 
41
    init : function(tree){
 
42
        this.tree = tree;
 
43
        tree.getTreeEl().on("keydown", this.onKeyDown, this);
 
44
        tree.on("click", this.onNodeClick, this);
 
45
    },
 
46
    
 
47
    onNodeClick : function(node, e){
 
48
        this.select(node);
 
49
    },
 
50
    
 
51
    /**
 
52
     * Select a node.
 
53
     * @param {TreeNode} node The node to select
 
54
     * @return {TreeNode} The selected node
 
55
     */
 
56
    select : function(node){
 
57
        var last = this.selNode;
 
58
        if(last != node && this.fireEvent('beforeselect', this, node, last) !== false){
 
59
            if(last){
 
60
                last.ui.onSelectedChange(false);
 
61
            }
 
62
            this.selNode = node;
 
63
            node.ui.onSelectedChange(true);
 
64
            this.fireEvent("selectionchange", this, node, last);
 
65
        }
 
66
        return node;
 
67
    },
 
68
    
 
69
    /**
 
70
     * Deselect a node.
 
71
     * @param {TreeNode} node The node to unselect
 
72
     */
 
73
    unselect : function(node){
 
74
        if(this.selNode == node){
 
75
            this.clearSelections();
 
76
        }    
 
77
    },
 
78
    
 
79
    /**
 
80
     * Clear all selections
 
81
     */
 
82
    clearSelections : function(){
 
83
        var n = this.selNode;
 
84
        if(n){
 
85
            n.ui.onSelectedChange(false);
 
86
            this.selNode = null;
 
87
            this.fireEvent("selectionchange", this, null);
 
88
        }
 
89
        return n;
 
90
    },
 
91
    
 
92
    /**
 
93
     * Get the selected node
 
94
     * @return {TreeNode} The selected node
 
95
     */
 
96
    getSelectedNode : function(){
 
97
        return this.selNode;    
 
98
    },
 
99
    
 
100
    /**
 
101
     * Returns true if the node is selected
 
102
     * @param {TreeNode} node The node to check
 
103
     * @return {Boolean}
 
104
     */
 
105
    isSelected : function(node){
 
106
        return this.selNode == node;  
 
107
    },
 
108
 
 
109
    /**
 
110
     * Selects the node above the selected node in the tree, intelligently walking the nodes
 
111
     * @return TreeNode The new selection
 
112
     */
 
113
    selectPrevious : function(){
 
114
        var s = this.selNode || this.lastSelNode;
 
115
        if(!s){
 
116
            return null;
 
117
        }
 
118
        var ps = s.previousSibling;
 
119
        if(ps){
 
120
            if(!ps.isExpanded() || ps.childNodes.length < 1){
 
121
                return this.select(ps);
 
122
            } else{
 
123
                var lc = ps.lastChild;
 
124
                while(lc && lc.isExpanded() && lc.childNodes.length > 0){
 
125
                    lc = lc.lastChild;
 
126
                }
 
127
                return this.select(lc);
 
128
            }
 
129
        } else if(s.parentNode && (this.tree.rootVisible || !s.parentNode.isRoot)){
 
130
            return this.select(s.parentNode);
 
131
        }
 
132
        return null;
 
133
    },
 
134
 
 
135
    /**
 
136
     * Selects the node above the selected node in the tree, intelligently walking the nodes
 
137
     * @return TreeNode The new selection
 
138
     */
 
139
    selectNext : function(){
 
140
        var s = this.selNode || this.lastSelNode;
 
141
        if(!s){
 
142
            return null;
 
143
        }
 
144
        if(s.firstChild && s.isExpanded()){
 
145
             return this.select(s.firstChild);
 
146
         }else if(s.nextSibling){
 
147
             return this.select(s.nextSibling);
 
148
         }else if(s.parentNode){
 
149
            var newS = null;
 
150
            s.parentNode.bubble(function(){
 
151
                if(this.nextSibling){
 
152
                    newS = this.getOwnerTree().selModel.select(this.nextSibling);
 
153
                    return false;
 
154
                }
 
155
            });
 
156
            return newS;
 
157
         }
 
158
        return null;
 
159
    },
 
160
 
 
161
    onKeyDown : function(e){
 
162
        var s = this.selNode || this.lastSelNode;
 
163
        // undesirable, but required
 
164
        var sm = this;
 
165
        if(!s){
 
166
            return;
 
167
        }
 
168
        var k = e.getKey();
 
169
        switch(k){
 
170
             case e.DOWN:
 
171
                 e.stopEvent();
 
172
                 this.selectNext();
 
173
             break;
 
174
             case e.UP:
 
175
                 e.stopEvent();
 
176
                 this.selectPrevious();
 
177
             break;
 
178
             case e.RIGHT:
 
179
                 e.preventDefault();
 
180
                 if(s.hasChildNodes()){
 
181
                     if(!s.isExpanded()){
 
182
                         s.expand();
 
183
                     }else if(s.firstChild){
 
184
                         this.select(s.firstChild, e);
 
185
                     }
 
186
                 }
 
187
             break;
 
188
             case e.LEFT:
 
189
                 e.preventDefault();
 
190
                 if(s.hasChildNodes() && s.isExpanded()){
 
191
                     s.collapse();
 
192
                 }else if(s.parentNode && (this.tree.rootVisible || s.parentNode != this.tree.getRootNode())){
 
193
                     this.select(s.parentNode, e);
 
194
                 }
 
195
             break;
 
196
        };
 
197
    }
 
198
});
 
199
 
 
200
/**
 
201
 * @class Ext.tree.MultiSelectionModel
 
202
 * @extends Ext.util.Observable
 
203
 * Multi selection for a TreePanel.
 
204
 */
 
205
Ext.tree.MultiSelectionModel = function(config){
 
206
   this.selNodes = [];
 
207
   this.selMap = {};
 
208
   this.addEvents(
 
209
       /**
 
210
        * @event selectionchange
 
211
        * Fires when the selected nodes change
 
212
        * @param {MultiSelectionModel} this
 
213
        * @param {Array} nodes Array of the selected nodes
 
214
        */
 
215
       "selectionchange"
 
216
   );
 
217
    Ext.apply(this, config);
 
218
    Ext.tree.MultiSelectionModel.superclass.constructor.call(this);
 
219
};
 
220
 
 
221
Ext.extend(Ext.tree.MultiSelectionModel, Ext.util.Observable, {
 
222
    init : function(tree){
 
223
        this.tree = tree;
 
224
        tree.getTreeEl().on("keydown", this.onKeyDown, this);
 
225
        tree.on("click", this.onNodeClick, this);
 
226
    },
 
227
    
 
228
    onNodeClick : function(node, e){
 
229
        this.select(node, e, e.ctrlKey);
 
230
    },
 
231
    
 
232
    /**
 
233
     * Select a node.
 
234
     * @param {TreeNode} node The node to select
 
235
     * @param {EventObject} e (optional) An event associated with the selection
 
236
     * @param {Boolean} keepExisting True to retain existing selections
 
237
     * @return {TreeNode} The selected node
 
238
     */
 
239
    select : function(node, e, keepExisting){
 
240
        if(keepExisting !== true){
 
241
            this.clearSelections(true);
 
242
        }
 
243
        if(this.isSelected(node)){
 
244
            this.lastSelNode = node;
 
245
            return node;
 
246
        }
 
247
        this.selNodes.push(node);
 
248
        this.selMap[node.id] = node;
 
249
        this.lastSelNode = node;
 
250
        node.ui.onSelectedChange(true);
 
251
        this.fireEvent("selectionchange", this, this.selNodes);
 
252
        return node;
 
253
    },
 
254
    
 
255
    /**
 
256
     * Deselect a node.
 
257
     * @param {TreeNode} node The node to unselect
 
258
     */
 
259
    unselect : function(node){
 
260
        if(this.selMap[node.id]){
 
261
            node.ui.onSelectedChange(false);
 
262
            var sn = this.selNodes;
 
263
            var index = sn.indexOf(node);
 
264
            if(index != -1){
 
265
                this.selNodes.splice(index, 1);
 
266
            }
 
267
            delete this.selMap[node.id];
 
268
            this.fireEvent("selectionchange", this, this.selNodes);
 
269
        }
 
270
    },
 
271
    
 
272
    /**
 
273
     * Clear all selections
 
274
     */
 
275
    clearSelections : function(suppressEvent){
 
276
        var sn = this.selNodes;
 
277
        if(sn.length > 0){
 
278
            for(var i = 0, len = sn.length; i < len; i++){
 
279
                sn[i].ui.onSelectedChange(false);
 
280
            }
 
281
            this.selNodes = [];
 
282
            this.selMap = {};
 
283
            if(suppressEvent !== true){
 
284
                this.fireEvent("selectionchange", this, this.selNodes);
 
285
            }
 
286
        }
 
287
    },
 
288
    
 
289
    /**
 
290
     * Returns true if the node is selected
 
291
     * @param {TreeNode} node The node to check
 
292
     * @return {Boolean}
 
293
     */
 
294
    isSelected : function(node){
 
295
        return this.selMap[node.id] ? true : false;  
 
296
    },
 
297
    
 
298
    /**
 
299
     * Returns an array of the selected nodes
 
300
     * @return {Array}
 
301
     */
 
302
    getSelectedNodes : function(){
 
303
        return this.selNodes;    
 
304
    },
 
305
 
 
306
    onKeyDown : Ext.tree.DefaultSelectionModel.prototype.onKeyDown,
 
307
 
 
308
    selectNext : Ext.tree.DefaultSelectionModel.prototype.selectNext,
 
309
 
 
310
    selectPrevious : Ext.tree.DefaultSelectionModel.prototype.selectPrevious
 
311
});
 
 
b'\\ No newline at end of file'