Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / src / util / KeyNav.js
1 /*!
2  * Ext JS Library 3.0.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.KeyNav
9  * <p>Provides a convenient wrapper for normalized keyboard navigation.  KeyNav allows you to bind
10  * navigation keys to function calls that will get called when the keys are pressed, providing an easy
11  * way to implement custom navigation schemes for any UI component.</p>
12  * <p>The following are all of the possible keys that can be implemented: enter, left, right, up, down, tab, esc,
13  * pageUp, pageDown, del, home, end.  Usage:</p>
14  <pre><code>
15 var nav = new Ext.KeyNav("my-element", {
16     "left" : function(e){
17         this.moveLeft(e.ctrlKey);
18     },
19     "right" : function(e){
20         this.moveRight(e.ctrlKey);
21     },
22     "enter" : function(e){
23         this.save();
24     },
25     scope : this
26 });
27 </code></pre>
28  * @constructor
29  * @param {Mixed} el The element to bind to
30  * @param {Object} config The config
31  */
32 Ext.KeyNav = function(el, config){
33     this.el = Ext.get(el);
34     Ext.apply(this, config);
35     if(!this.disabled){
36         this.disabled = true;
37         this.enable();
38     }
39 };
40
41 Ext.KeyNav.prototype = {
42     /**
43      * @cfg {Boolean} disabled
44      * True to disable this KeyNav instance (defaults to false)
45      */
46     disabled : false,
47     /**
48      * @cfg {String} defaultEventAction
49      * The method to call on the {@link Ext.EventObject} after this KeyNav intercepts a key.  Valid values are
50      * {@link Ext.EventObject#stopEvent}, {@link Ext.EventObject#preventDefault} and
51      * {@link Ext.EventObject#stopPropagation} (defaults to 'stopEvent')
52      */
53     defaultEventAction: "stopEvent",
54     /**
55      * @cfg {Boolean} forceKeyDown
56      * Handle the keydown event instead of keypress (defaults to false).  KeyNav automatically does this for IE since
57      * IE does not propagate special keys on keypress, but setting this to true will force other browsers to also
58      * handle keydown instead of keypress.
59      */
60     forceKeyDown : false,
61
62     // private
63     prepareEvent : function(e){
64         var k = e.getKey();
65         var h = this.keyToHandler[k];
66         if(Ext.isSafari2 && h && k >= 37 && k <= 40){
67             e.stopEvent();
68         }
69     },
70
71     // private
72     relay : function(e){
73         var k = e.getKey();
74         var h = this.keyToHandler[k];
75         if(h && this[h]){
76             if(this.doRelay(e, this[h], h) !== true){
77                 e[this.defaultEventAction]();
78             }
79         }
80     },
81
82     // private
83     doRelay : function(e, h, hname){
84         return h.call(this.scope || this, e);
85     },
86
87     // possible handlers
88     enter : false,
89     left : false,
90     right : false,
91     up : false,
92     down : false,
93     tab : false,
94     esc : false,
95     pageUp : false,
96     pageDown : false,
97     del : false,
98     home : false,
99     end : false,
100
101     // quick lookup hash
102     keyToHandler : {
103         37 : "left",
104         39 : "right",
105         38 : "up",
106         40 : "down",
107         33 : "pageUp",
108         34 : "pageDown",
109         46 : "del",
110         36 : "home",
111         35 : "end",
112         13 : "enter",
113         27 : "esc",
114         9  : "tab"
115     },
116
117         /**
118          * Enable this KeyNav
119          */
120         enable: function(){
121                 if(this.disabled){
122             // ie won't do special keys on keypress, no one else will repeat keys with keydown
123             // the EventObject will normalize Safari automatically
124             if(this.isKeydown()){
125                 this.el.on("keydown", this.relay,  this);
126             }else{
127                 this.el.on("keydown", this.prepareEvent,  this);
128                 this.el.on("keypress", this.relay,  this);
129             }
130                     this.disabled = false;
131                 }
132         },
133
134         /**
135          * Disable this KeyNav
136          */
137         disable: function(){
138                 if(!this.disabled){
139                     if(this.isKeydown()){
140                 this.el.un("keydown", this.relay, this);
141             }else{
142                 this.el.un("keydown", this.prepareEvent, this);
143                 this.el.un("keypress", this.relay, this);
144             }
145                     this.disabled = true;
146                 }
147         },
148     
149     /**
150      * Convenience function for setting disabled/enabled by boolean.
151      * @param {Boolean} disabled
152      */
153     setDisabled : function(disabled){
154         this[disabled ? "disable" : "enable"]();
155     },
156     
157     // private
158     isKeydown: function(){
159         return this.forceKeyDown || Ext.EventManager.useKeydown;
160     }
161 };