Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / util / 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.util.KeyNav
17  * <p>Provides a convenient wrapper for normalized keyboard navigation.  KeyNav allows you to bind
18  * navigation keys to function calls that will get called when the keys are pressed, providing an easy
19  * way to implement custom navigation schemes for any UI component.</p>
20  * <p>The following are all of the possible keys that can be implemented: enter, space, left, right, up, down, tab, esc,
21  * pageUp, pageDown, del, backspace, home, end.  Usage:</p>
22  <pre><code>
23 var nav = new Ext.util.KeyNav("my-element", {
24     "left" : function(e){
25         this.moveLeft(e.ctrlKey);
26     },
27     "right" : function(e){
28         this.moveRight(e.ctrlKey);
29     },
30     "enter" : function(e){
31         this.save();
32     },
33     scope : this
34 });
35 </code></pre>
36  */
37 Ext.define('Ext.util.KeyNav', {
38     
39     alternateClassName: 'Ext.KeyNav',
40     
41     requires: ['Ext.util.KeyMap'],
42     
43     statics: {
44         keyOptions: {
45             left: 37,
46             right: 39,
47             up: 38,
48             down: 40,
49             space: 32,
50             pageUp: 33,
51             pageDown: 34,
52             del: 46,
53             backspace: 8,
54             home: 36,
55             end: 35,
56             enter: 13,
57             esc: 27,
58             tab: 9
59         }
60     },
61
62     /**
63      * Creates new KeyNav.
64      * @param {String/HTMLElement/Ext.Element} el The element or its ID to bind to
65      * @param {Object} config The config
66      */
67     constructor: function(el, config){
68         this.setConfig(el, config || {});
69     },
70     
71     /**
72      * Sets up a configuration for the KeyNav.
73      * @private
74      * @param {String/HTMLElement/Ext.Element} el The element or its ID to bind to
75      * @param {Object} config A configuration object as specified in the constructor.
76      */
77     setConfig: function(el, config) {
78         if (this.map) {
79             this.map.destroy();
80         }
81         
82         var map = Ext.create('Ext.util.KeyMap', el, null, this.getKeyEvent('forceKeyDown' in config ? config.forceKeyDown : this.forceKeyDown)),
83             keys = Ext.util.KeyNav.keyOptions,
84             scope = config.scope || this,
85             key;
86         
87         this.map = map;
88         for (key in keys) {
89             if (keys.hasOwnProperty(key)) {
90                 if (config[key]) {
91                     map.addBinding({
92                         scope: scope,
93                         key: keys[key],
94                         handler: Ext.Function.bind(this.handleEvent, scope, [config[key]], true),
95                         defaultEventAction: config.defaultEventAction || this.defaultEventAction
96                     });
97                 }
98             }
99         }
100         
101         map.disable();
102         if (!config.disabled) {
103             map.enable();
104         }
105     },
106     
107     /**
108      * Method for filtering out the map argument
109      * @private
110      * @param {Ext.util.KeyMap} map
111      * @param {Ext.EventObject} event
112      * @param {Object} options Contains the handler to call
113      */
114     handleEvent: function(map, event, handler){
115         return handler.call(this, event);
116     },
117     
118     /**
119      * @cfg {Boolean} disabled
120      * True to disable this KeyNav instance.
121      */
122     disabled: false,
123     
124     /**
125      * @cfg {String} defaultEventAction
126      * The method to call on the {@link Ext.EventObject} after this KeyNav intercepts a key.  Valid values are
127      * {@link Ext.EventObject#stopEvent}, {@link Ext.EventObject#preventDefault} and
128      * {@link Ext.EventObject#stopPropagation}.
129      */
130     defaultEventAction: "stopEvent",
131     
132     /**
133      * @cfg {Boolean} forceKeyDown
134      * Handle the keydown event instead of keypress.  KeyNav automatically does this for IE since
135      * IE does not propagate special keys on keypress, but setting this to true will force other browsers to also
136      * handle keydown instead of keypress.
137      */
138     forceKeyDown: false,
139     
140     /**
141      * Destroy this KeyNav (this is the same as calling disable).
142      * @param {Boolean} removeEl True to remove the element associated with this KeyNav.
143      */
144     destroy: function(removeEl){
145         this.map.destroy(removeEl);
146         delete this.map;
147     },
148
149     /**
150      * Enable this KeyNav
151      */
152     enable: function() {
153         this.map.enable();
154         this.disabled = false;
155     },
156
157     /**
158      * Disable this KeyNav
159      */
160     disable: function() {
161         this.map.disable();
162         this.disabled = true;
163     },
164     
165     /**
166      * Convenience function for setting disabled/enabled by boolean.
167      * @param {Boolean} disabled
168      */
169     setDisabled : function(disabled){
170         this.map.setDisabled(disabled);
171         this.disabled = disabled;
172     },
173     
174     /**
175      * Determines the event to bind to listen for keys. Depends on the {@link #forceKeyDown} setting,
176      * as well as the useKeyDown option on the EventManager.
177      * @return {String} The type of event to listen for.
178      */
179     getKeyEvent: function(forceKeyDown){
180         return (forceKeyDown || Ext.EventManager.useKeyDown) ? 'keydown' : 'keypress';
181     }
182 });
183