Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / TreeModel.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-selection-TreeModel'>/**
19 </span> * @class Ext.selection.TreeModel
20  * @extends Ext.selection.RowModel
21  *
22  * Adds custom behavior for left/right keyboard navigation for use with a tree.
23  * Depends on the view having an expand and collapse method which accepts a
24  * record.
25  * 
26  * @private
27  */
28 Ext.define('Ext.selection.TreeModel', {
29     extend: 'Ext.selection.RowModel',
30     alias: 'selection.treemodel',
31     
32     // typically selection models prune records from the selection
33     // model when they are removed, because the TreeView constantly
34     // adds/removes records as they are expanded/collapsed
35     pruneRemoved: false,
36     
37     onKeyRight: function(e, t) {
38         var focused = this.getLastFocused(),
39             view    = this.view;
40             
41         if (focused) {
42             // tree node is already expanded, go down instead
43             // this handles both the case where we navigate to firstChild and if
44             // there are no children to the nextSibling
45             if (focused.isExpanded()) {
46                 this.onKeyDown(e, t);
47             // if its not a leaf node, expand it
48             } else if (!focused.isLeaf()) {
49                 view.expand(focused);
50             }
51         }
52     },
53     
54     onKeyLeft: function(e, t) {
55         var focused = this.getLastFocused(),
56             view    = this.view,
57             viewSm  = view.getSelectionModel(),
58             parentNode, parentRecord;
59
60         if (focused) {
61             parentNode = focused.parentNode;
62             // if focused node is already expanded, collapse it
63             if (focused.isExpanded()) {
64                 view.collapse(focused);
65             // has a parentNode and its not root
66             // TODO: this needs to cover the case where the root isVisible
67             } else if (parentNode &amp;&amp; !parentNode.isRoot()) {
68                 // Select a range of records when doing multiple selection.
69                 if (e.shiftKey) {
70                     viewSm.selectRange(parentNode, focused, e.ctrlKey, 'up');
71                     viewSm.setLastFocused(parentNode);
72                 // just move focus, not selection
73                 } else if (e.ctrlKey) {
74                     viewSm.setLastFocused(parentNode);
75                 // select it
76                 } else {
77                     viewSm.select(parentNode);
78                 }
79             }
80         }
81     },
82     
83     onKeyPress: function(e, t) {
84         var selected, checked;
85         
86         if (e.getKey() === e.SPACE || e.getKey() === e.ENTER) {
87             e.stopEvent();
88             selected = this.getLastSelected();
89             if (selected &amp;&amp; selected.isLeaf()) {
90                 checked = selected.get('checked');
91                 if (Ext.isBoolean(checked)) {
92                     selected.set('checked', !checked);
93                 }
94             }
95         } else {
96             this.callParent(arguments);
97         }
98     }
99 });
100 </pre>
101 </body>
102 </html>