Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / src / core / EventManager-more.js
1 /*!
2  * Ext JS Library 3.2.2
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8 * @class Ext.EventManager
9 */
10 Ext.apply(Ext.EventManager, function(){
11    var resizeEvent,
12        resizeTask,
13        textEvent,
14        textSize,
15        D = Ext.lib.Dom,
16        propRe = /^(?:scope|delay|buffer|single|stopEvent|preventDefault|stopPropagation|normalized|args|delegate)$/,
17        curWidth = 0,
18        curHeight = 0,
19        // note 1: IE fires ONLY the keydown event on specialkey autorepeat
20        // note 2: Safari < 3.1, Gecko (Mac/Linux) & Opera fire only the keypress event on specialkey autorepeat
21        // (research done by @Jan Wolter at http://unixpapa.com/js/key.html)
22        useKeydown = Ext.isWebKit ?
23                    Ext.num(navigator.userAgent.match(/AppleWebKit\/(\d+)/)[1]) >= 525 :
24                    !((Ext.isGecko && !Ext.isWindows) || Ext.isOpera);
25
26    return {
27        // private
28        doResizeEvent: function(){
29            var h = D.getViewHeight(),
30                w = D.getViewWidth();
31
32             //whacky problem in IE where the resize event will fire even though the w/h are the same.
33             if(curHeight != h || curWidth != w){
34                resizeEvent.fire(curWidth = w, curHeight = h);
35             }
36        },
37
38        /**
39         * Adds a listener to be notified when the browser window is resized and provides resize event buffering (100 milliseconds),
40         * passes new viewport width and height to handlers.
41         * @param {Function} fn      The handler function the window resize event invokes.
42         * @param {Object}   scope   The scope (<code>this</code> reference) in which the handler function executes. Defaults to the browser window.
43         * @param {boolean}  options Options object as passed to {@link Ext.Element#addListener}
44         */
45        onWindowResize : function(fn, scope, options){
46            if(!resizeEvent){
47                resizeEvent = new Ext.util.Event();
48                resizeTask = new Ext.util.DelayedTask(this.doResizeEvent);
49                Ext.EventManager.on(window, "resize", this.fireWindowResize, this);
50            }
51            resizeEvent.addListener(fn, scope, options);
52        },
53
54        // exposed only to allow manual firing
55        fireWindowResize : function(){
56            if(resizeEvent){
57                resizeTask.delay(100);
58            }
59        },
60
61        /**
62         * Adds a listener to be notified when the user changes the active text size. Handler gets called with 2 params, the old size and the new size.
63         * @param {Function} fn      The function the event invokes.
64         * @param {Object}   scope   The scope (<code>this</code> reference) in which the handler function executes. Defaults to the browser window.
65         * @param {boolean}  options Options object as passed to {@link Ext.Element#addListener}
66         */
67        onTextResize : function(fn, scope, options){
68            if(!textEvent){
69                textEvent = new Ext.util.Event();
70                var textEl = new Ext.Element(document.createElement('div'));
71                textEl.dom.className = 'x-text-resize';
72                textEl.dom.innerHTML = 'X';
73                textEl.appendTo(document.body);
74                textSize = textEl.dom.offsetHeight;
75                setInterval(function(){
76                    if(textEl.dom.offsetHeight != textSize){
77                        textEvent.fire(textSize, textSize = textEl.dom.offsetHeight);
78                    }
79                }, this.textResizeInterval);
80            }
81            textEvent.addListener(fn, scope, options);
82        },
83
84        /**
85         * Removes the passed window resize listener.
86         * @param {Function} fn        The method the event invokes
87         * @param {Object}   scope    The scope of handler
88         */
89        removeResizeListener : function(fn, scope){
90            if(resizeEvent){
91                resizeEvent.removeListener(fn, scope);
92            }
93        },
94
95        // private
96        fireResize : function(){
97            if(resizeEvent){
98                resizeEvent.fire(D.getViewWidth(), D.getViewHeight());
99            }
100        },
101
102         /**
103         * The frequency, in milliseconds, to check for text resize events (defaults to 50)
104         */
105        textResizeInterval : 50,
106
107        /**
108         * Url used for onDocumentReady with using SSL (defaults to Ext.SSL_SECURE_URL)
109         */
110        ieDeferSrc : false,
111
112        // protected for use inside the framework
113        // detects whether we should use keydown or keypress based on the browser.
114        useKeydown: useKeydown
115    };
116 }());
117
118 Ext.EventManager.on = Ext.EventManager.addListener;
119
120
121 Ext.apply(Ext.EventObjectImpl.prototype, {
122    /** Key constant @type Number */
123    BACKSPACE: 8,
124    /** Key constant @type Number */
125    TAB: 9,
126    /** Key constant @type Number */
127    NUM_CENTER: 12,
128    /** Key constant @type Number */
129    ENTER: 13,
130    /** Key constant @type Number */
131    RETURN: 13,
132    /** Key constant @type Number */
133    SHIFT: 16,
134    /** Key constant @type Number */
135    CTRL: 17,
136    CONTROL : 17, // legacy
137    /** Key constant @type Number */
138    ALT: 18,
139    /** Key constant @type Number */
140    PAUSE: 19,
141    /** Key constant @type Number */
142    CAPS_LOCK: 20,
143    /** Key constant @type Number */
144    ESC: 27,
145    /** Key constant @type Number */
146    SPACE: 32,
147    /** Key constant @type Number */
148    PAGE_UP: 33,
149    PAGEUP : 33, // legacy
150    /** Key constant @type Number */
151    PAGE_DOWN: 34,
152    PAGEDOWN : 34, // legacy
153    /** Key constant @type Number */
154    END: 35,
155    /** Key constant @type Number */
156    HOME: 36,
157    /** Key constant @type Number */
158    LEFT: 37,
159    /** Key constant @type Number */
160    UP: 38,
161    /** Key constant @type Number */
162    RIGHT: 39,
163    /** Key constant @type Number */
164    DOWN: 40,
165    /** Key constant @type Number */
166    PRINT_SCREEN: 44,
167    /** Key constant @type Number */
168    INSERT: 45,
169    /** Key constant @type Number */
170    DELETE: 46,
171    /** Key constant @type Number */
172    ZERO: 48,
173    /** Key constant @type Number */
174    ONE: 49,
175    /** Key constant @type Number */
176    TWO: 50,
177    /** Key constant @type Number */
178    THREE: 51,
179    /** Key constant @type Number */
180    FOUR: 52,
181    /** Key constant @type Number */
182    FIVE: 53,
183    /** Key constant @type Number */
184    SIX: 54,
185    /** Key constant @type Number */
186    SEVEN: 55,
187    /** Key constant @type Number */
188    EIGHT: 56,
189    /** Key constant @type Number */
190    NINE: 57,
191    /** Key constant @type Number */
192    A: 65,
193    /** Key constant @type Number */
194    B: 66,
195    /** Key constant @type Number */
196    C: 67,
197    /** Key constant @type Number */
198    D: 68,
199    /** Key constant @type Number */
200    E: 69,
201    /** Key constant @type Number */
202    F: 70,
203    /** Key constant @type Number */
204    G: 71,
205    /** Key constant @type Number */
206    H: 72,
207    /** Key constant @type Number */
208    I: 73,
209    /** Key constant @type Number */
210    J: 74,
211    /** Key constant @type Number */
212    K: 75,
213    /** Key constant @type Number */
214    L: 76,
215    /** Key constant @type Number */
216    M: 77,
217    /** Key constant @type Number */
218    N: 78,
219    /** Key constant @type Number */
220    O: 79,
221    /** Key constant @type Number */
222    P: 80,
223    /** Key constant @type Number */
224    Q: 81,
225    /** Key constant @type Number */
226    R: 82,
227    /** Key constant @type Number */
228    S: 83,
229    /** Key constant @type Number */
230    T: 84,
231    /** Key constant @type Number */
232    U: 85,
233    /** Key constant @type Number */
234    V: 86,
235    /** Key constant @type Number */
236    W: 87,
237    /** Key constant @type Number */
238    X: 88,
239    /** Key constant @type Number */
240    Y: 89,
241    /** Key constant @type Number */
242    Z: 90,
243    /** Key constant @type Number */
244    CONTEXT_MENU: 93,
245    /** Key constant @type Number */
246    NUM_ZERO: 96,
247    /** Key constant @type Number */
248    NUM_ONE: 97,
249    /** Key constant @type Number */
250    NUM_TWO: 98,
251    /** Key constant @type Number */
252    NUM_THREE: 99,
253    /** Key constant @type Number */
254    NUM_FOUR: 100,
255    /** Key constant @type Number */
256    NUM_FIVE: 101,
257    /** Key constant @type Number */
258    NUM_SIX: 102,
259    /** Key constant @type Number */
260    NUM_SEVEN: 103,
261    /** Key constant @type Number */
262    NUM_EIGHT: 104,
263    /** Key constant @type Number */
264    NUM_NINE: 105,
265    /** Key constant @type Number */
266    NUM_MULTIPLY: 106,
267    /** Key constant @type Number */
268    NUM_PLUS: 107,
269    /** Key constant @type Number */
270    NUM_MINUS: 109,
271    /** Key constant @type Number */
272    NUM_PERIOD: 110,
273    /** Key constant @type Number */
274    NUM_DIVISION: 111,
275    /** Key constant @type Number */
276    F1: 112,
277    /** Key constant @type Number */
278    F2: 113,
279    /** Key constant @type Number */
280    F3: 114,
281    /** Key constant @type Number */
282    F4: 115,
283    /** Key constant @type Number */
284    F5: 116,
285    /** Key constant @type Number */
286    F6: 117,
287    /** Key constant @type Number */
288    F7: 118,
289    /** Key constant @type Number */
290    F8: 119,
291    /** Key constant @type Number */
292    F9: 120,
293    /** Key constant @type Number */
294    F10: 121,
295    /** Key constant @type Number */
296    F11: 122,
297    /** Key constant @type Number */
298    F12: 123,
299
300    /** @private */
301    isNavKeyPress : function(){
302        var me = this,
303            k = this.normalizeKey(me.keyCode);
304        return (k >= 33 && k <= 40) ||  // Page Up/Down, End, Home, Left, Up, Right, Down
305        k == me.RETURN ||
306        k == me.TAB ||
307        k == me.ESC;
308    },
309
310    isSpecialKey : function(){
311        var k = this.normalizeKey(this.keyCode);
312        return (this.type == 'keypress' && this.ctrlKey) ||
313        this.isNavKeyPress() ||
314        (k == this.BACKSPACE) || // Backspace
315        (k >= 16 && k <= 20) || // Shift, Ctrl, Alt, Pause, Caps Lock
316        (k >= 44 && k <= 46);   // Print Screen, Insert, Delete
317    },
318
319    getPoint : function(){
320        return new Ext.lib.Point(this.xy[0], this.xy[1]);
321    },
322
323    /**
324     * Returns true if the control, meta, shift or alt key was pressed during this event.
325     * @return {Boolean}
326     */
327    hasModifier : function(){
328        return ((this.ctrlKey || this.altKey) || this.shiftKey);
329    }
330 });