Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / selection / TreeModel.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.selection.TreeModel
17  * @extends Ext.selection.RowModel
18  *
19  * Adds custom behavior for left/right keyboard navigation for use with a tree.
20  * Depends on the view having an expand and collapse method which accepts a
21  * record.
22  * 
23  * @private
24  */
25 Ext.define('Ext.selection.TreeModel', {
26     extend: 'Ext.selection.RowModel',
27     alias: 'selection.treemodel',
28     
29     // typically selection models prune records from the selection
30     // model when they are removed, because the TreeView constantly
31     // adds/removes records as they are expanded/collapsed
32     pruneRemoved: false,
33     
34     onKeyRight: function(e, t) {
35         var focused = this.getLastFocused(),
36             view    = this.view;
37             
38         if (focused) {
39             // tree node is already expanded, go down instead
40             // this handles both the case where we navigate to firstChild and if
41             // there are no children to the nextSibling
42             if (focused.isExpanded()) {
43                 this.onKeyDown(e, t);
44             // if its not a leaf node, expand it
45             } else if (!focused.isLeaf()) {
46                 view.expand(focused);
47             }
48         }
49     },
50     
51     onKeyLeft: function(e, t) {
52         var focused = this.getLastFocused(),
53             view    = this.view,
54             viewSm  = view.getSelectionModel(),
55             parentNode, parentRecord;
56
57         if (focused) {
58             parentNode = focused.parentNode;
59             // if focused node is already expanded, collapse it
60             if (focused.isExpanded()) {
61                 view.collapse(focused);
62             // has a parentNode and its not root
63             // TODO: this needs to cover the case where the root isVisible
64             } else if (parentNode && !parentNode.isRoot()) {
65                 // Select a range of records when doing multiple selection.
66                 if (e.shiftKey) {
67                     viewSm.selectRange(parentNode, focused, e.ctrlKey, 'up');
68                     viewSm.setLastFocused(parentNode);
69                 // just move focus, not selection
70                 } else if (e.ctrlKey) {
71                     viewSm.setLastFocused(parentNode);
72                 // select it
73                 } else {
74                     viewSm.select(parentNode);
75                 }
76             }
77         }
78     },
79     
80     onKeyPress: function(e, t) {
81         var key = e.getKey(),
82             selected, 
83             checked;
84         
85         if (key === e.SPACE || key === e.ENTER) {
86             e.stopEvent();
87             selected = this.getLastSelected();
88             if (selected) {
89                 this.view.onCheckChange(selected);
90             }
91         } else {
92             this.callParent(arguments);
93         }
94     }
95 });
96