source: trunk/web/addons/job_monarch/lib/extjs-30/src/widgets/tree/TreeSelectionModel.js @ 625

Last change on this file since 625 was 625, checked in by ramonb, 15 years ago

lib/extjs-30:

  • new ExtJS 3.0
File size: 9.1 KB
Line 
1/*!
2 * Ext JS Library 3.0.0
3 * Copyright(c) 2006-2009 Ext JS, LLC
4 * licensing@extjs.com
5 * http://www.extjs.com/license
6 */
7/**
8 * @class Ext.tree.DefaultSelectionModel
9 * @extends Ext.util.Observable
10 * The default single selection for a TreePanel.
11 */
12Ext.tree.DefaultSelectionModel = function(config){
13   this.selNode = null;
14   
15   this.addEvents(
16       /**
17        * @event selectionchange
18        * Fires when the selected node changes
19        * @param {DefaultSelectionModel} this
20        * @param {TreeNode} node the new selection
21        */
22       "selectionchange",
23
24       /**
25        * @event beforeselect
26        * Fires before the selected node changes, return false to cancel the change
27        * @param {DefaultSelectionModel} this
28        * @param {TreeNode} node the new selection
29        * @param {TreeNode} node the old selection
30        */
31       "beforeselect"
32   );
33
34    Ext.apply(this, config);
35    Ext.tree.DefaultSelectionModel.superclass.constructor.call(this);
36};
37
38Ext.extend(Ext.tree.DefaultSelectionModel, Ext.util.Observable, {
39    init : function(tree){
40        this.tree = tree;
41        tree.getTreeEl().on("keydown", this.onKeyDown, this);
42        tree.on("click", this.onNodeClick, this);
43    },
44   
45    onNodeClick : function(node, e){
46        this.select(node);
47    },
48   
49    /**
50     * Select a node.
51     * @param {TreeNode} node The node to select
52     * @return {TreeNode} The selected node
53     */
54    select : function(node){
55        var last = this.selNode;
56        if(node == last){
57            node.ui.onSelectedChange(true);
58        }else if(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 */
205Ext.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
221Ext.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        if(e.ctrlKey && this.isSelected(node)){
230            this.unselect(node);
231        }else{
232            this.select(node, e, e.ctrlKey);
233        }
234    },
235   
236    /**
237     * Select a node.
238     * @param {TreeNode} node The node to select
239     * @param {EventObject} e (optional) An event associated with the selection
240     * @param {Boolean} keepExisting True to retain existing selections
241     * @return {TreeNode} The selected node
242     */
243    select : function(node, e, keepExisting){
244        if(keepExisting !== true){
245            this.clearSelections(true);
246        }
247        if(this.isSelected(node)){
248            this.lastSelNode = node;
249            return node;
250        }
251        this.selNodes.push(node);
252        this.selMap[node.id] = node;
253        this.lastSelNode = node;
254        node.ui.onSelectedChange(true);
255        this.fireEvent("selectionchange", this, this.selNodes);
256        return node;
257    },
258   
259    /**
260     * Deselect a node.
261     * @param {TreeNode} node The node to unselect
262     */
263    unselect : function(node){
264        if(this.selMap[node.id]){
265            node.ui.onSelectedChange(false);
266            var sn = this.selNodes;
267            var index = sn.indexOf(node);
268            if(index != -1){
269                this.selNodes.splice(index, 1);
270            }
271            delete this.selMap[node.id];
272            this.fireEvent("selectionchange", this, this.selNodes);
273        }
274    },
275   
276    /**
277     * Clear all selections
278     */
279    clearSelections : function(suppressEvent){
280        var sn = this.selNodes;
281        if(sn.length > 0){
282            for(var i = 0, len = sn.length; i < len; i++){
283                sn[i].ui.onSelectedChange(false);
284            }
285            this.selNodes = [];
286            this.selMap = {};
287            if(suppressEvent !== true){
288                this.fireEvent("selectionchange", this, this.selNodes);
289            }
290        }
291    },
292   
293    /**
294     * Returns true if the node is selected
295     * @param {TreeNode} node The node to check
296     * @return {Boolean}
297     */
298    isSelected : function(node){
299        return this.selMap[node.id] ? true : false; 
300    },
301   
302    /**
303     * Returns an array of the selected nodes
304     * @return {Array}
305     */
306    getSelectedNodes : function(){
307        return this.selNodes;   
308    },
309
310    onKeyDown : Ext.tree.DefaultSelectionModel.prototype.onKeyDown,
311
312    selectNext : Ext.tree.DefaultSelectionModel.prototype.selectNext,
313
314    selectPrevious : Ext.tree.DefaultSelectionModel.prototype.selectPrevious
315});
Note: See TracBrowser for help on using the repository browser.