Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / menu / KeyNav.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.menu.KeyNav
17  * @private
18  */
19 Ext.define('Ext.menu.KeyNav', {
20     extend: 'Ext.util.KeyNav',
21
22     requires: ['Ext.FocusManager'],
23     
24     constructor: function(menu) {
25         var me = this;
26
27         me.menu = menu;
28         me.callParent([menu.el, {
29             down: me.down,
30             enter: me.enter,
31             esc: me.escape,
32             left: me.left,
33             right: me.right,
34             space: me.enter,
35             tab: me.tab,
36             up: me.up
37         }]);
38     },
39
40     down: function(e) {
41         var me = this,
42             fi = me.menu.focusedItem;
43
44         if (fi && e.getKey() == Ext.EventObject.DOWN && me.isWhitelisted(fi)) {
45             return true;
46         }
47         me.focusNextItem(1);
48     },
49
50     enter: function(e) {
51         var menu = this.menu,
52             focused = menu.focusedItem;
53  
54         if (menu.activeItem) {
55             menu.onClick(e);
56         } else if (focused && focused.isFormField) {
57             // prevent stopEvent being called
58             return true;
59         }
60     },
61
62     escape: function(e) {
63         Ext.menu.Manager.hideAll();
64     },
65
66     focusNextItem: function(step) {
67         var menu = this.menu,
68             items = menu.items,
69             focusedItem = menu.focusedItem,
70             startIdx = focusedItem ? items.indexOf(focusedItem) : -1,
71             idx = startIdx + step;
72
73         while (idx != startIdx) {
74             if (idx < 0) {
75                 idx = items.length - 1;
76             } else if (idx >= items.length) {
77                 idx = 0;
78             }
79
80             var item = items.getAt(idx);
81             if (menu.canActivateItem(item)) {
82                 menu.setActiveItem(item);
83                 break;
84             }
85             idx += step;
86         }
87     },
88
89     isWhitelisted: function(item) {
90         return Ext.FocusManager.isWhitelisted(item);
91     },
92
93     left: function(e) {
94         var menu = this.menu,
95             fi = menu.focusedItem,
96             ai = menu.activeItem;
97
98         if (fi && this.isWhitelisted(fi)) {
99             return true;
100         }
101
102         menu.hide();
103         if (menu.parentMenu) {
104             menu.parentMenu.focus();
105         }
106     },
107
108     right: function(e) {
109         var menu = this.menu,
110             fi = menu.focusedItem,
111             ai = menu.activeItem,
112             am;
113
114         if (fi && this.isWhitelisted(fi)) {
115             return true;
116         }
117
118         if (ai) {
119             am = menu.activeItem.menu;
120             if (am) {
121                 ai.expandMenu(0);
122                 Ext.defer(function() {
123                     am.setActiveItem(am.items.getAt(0));
124                 }, 25);
125             }
126         }
127     },
128
129     tab: function(e) {
130         var me = this;
131
132         if (e.shiftKey) {
133             me.up(e);
134         } else {
135             me.down(e);
136         }
137     },
138
139     up: function(e) {
140         var me = this,
141             fi = me.menu.focusedItem;
142
143         if (fi && e.getKey() == Ext.EventObject.UP && me.isWhitelisted(fi)) {
144             return true;
145         }
146         me.focusNextItem(-1);
147     }
148 });