3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
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>
23 var nav = new Ext.util.KeyNav("my-element", {
25 this.moveLeft(e.ctrlKey);
27 "right" : function(e){
28 this.moveRight(e.ctrlKey);
30 "enter" : function(e){
37 Ext.define('Ext.util.KeyNav', {
39 alternateClassName: 'Ext.KeyNav',
41 requires: ['Ext.util.KeyMap'],
64 * @param {String/HTMLElement/Ext.Element} el The element or its ID to bind to
65 * @param {Object} config The config
67 constructor: function(el, config){
68 this.setConfig(el, config || {});
72 * Sets up a configuration for the KeyNav.
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.
77 setConfig: function(el, config) {
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,
89 if (keys.hasOwnProperty(key)) {
94 handler: Ext.Function.bind(this.handleEvent, scope, [config[key]], true),
95 defaultEventAction: config.defaultEventAction || this.defaultEventAction
102 if (!config.disabled) {
108 * Method for filtering out the map argument
110 * @param {Ext.util.KeyMap} map
111 * @param {Ext.EventObject} event
112 * @param {Object} options Contains the handler to call
114 handleEvent: function(map, event, handler){
115 return handler.call(this, event);
119 * @cfg {Boolean} disabled
120 * True to disable this KeyNav instance.
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}.
130 defaultEventAction: "stopEvent",
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.
141 * Destroy this KeyNav (this is the same as calling disable).
142 * @param {Boolean} removeEl True to remove the element associated with this KeyNav.
144 destroy: function(removeEl){
145 this.map.destroy(removeEl);
154 this.disabled = false;
158 * Disable this KeyNav
160 disable: function() {
162 this.disabled = true;
166 * Convenience function for setting disabled/enabled by boolean.
167 * @param {Boolean} disabled
169 setDisabled : function(disabled){
170 this.map.setDisabled(disabled);
171 this.disabled = disabled;
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.
179 getKeyEvent: function(forceKeyDown){
180 return (forceKeyDown || Ext.EventManager.useKeyDown) ? 'keydown' : 'keypress';