Upgrade to ExtJS 3.3.0 - Released 10/06/2010
[extjs.git] / src / core / EventManager-more.js
1 /*!
2  * Ext JS Library 3.3.0
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, short accessor for useKeydown
113        getKeyEvent : function(){
114            return useKeydown ? 'keydown' : 'keypress';
115        },
116
117        // protected for use inside the framework
118        // detects whether we should use keydown or keypress based on the browser.
119        useKeydown: useKeydown
120    };
121 }());
122
123 Ext.EventManager.on = Ext.EventManager.addListener;
124
125
126 Ext.apply(Ext.EventObjectImpl.prototype, {
127    /** Key constant @type Number */
128    BACKSPACE: 8,
129    /** Key constant @type Number */
130    TAB: 9,
131    /** Key constant @type Number */
132    NUM_CENTER: 12,
133    /** Key constant @type Number */
134    ENTER: 13,
135    /** Key constant @type Number */
136    RETURN: 13,
137    /** Key constant @type Number */
138    SHIFT: 16,
139    /** Key constant @type Number */
140    CTRL: 17,
141    CONTROL : 17, // legacy
142    /** Key constant @type Number */
143    ALT: 18,
144    /** Key constant @type Number */
145    PAUSE: 19,
146    /** Key constant @type Number */
147    CAPS_LOCK: 20,
148    /** Key constant @type Number */
149    ESC: 27,
150    /** Key constant @type Number */
151    SPACE: 32,
152    /** Key constant @type Number */
153    PAGE_UP: 33,
154    PAGEUP : 33, // legacy
155    /** Key constant @type Number */
156    PAGE_DOWN: 34,
157    PAGEDOWN : 34, // legacy
158    /** Key constant @type Number */
159    END: 35,
160    /** Key constant @type Number */
161    HOME: 36,
162    /** Key constant @type Number */
163    LEFT: 37,
164    /** Key constant @type Number */
165    UP: 38,
166    /** Key constant @type Number */
167    RIGHT: 39,
168    /** Key constant @type Number */
169    DOWN: 40,
170    /** Key constant @type Number */
171    PRINT_SCREEN: 44,
172    /** Key constant @type Number */
173    INSERT: 45,
174    /** Key constant @type Number */
175    DELETE: 46,
176    /** Key constant @type Number */
177    ZERO: 48,
178    /** Key constant @type Number */
179    ONE: 49,
180    /** Key constant @type Number */
181    TWO: 50,
182    /** Key constant @type Number */
183    THREE: 51,
184    /** Key constant @type Number */
185    FOUR: 52,
186    /** Key constant @type Number */
187    FIVE: 53,
188    /** Key constant @type Number */
189    SIX: 54,
190    /** Key constant @type Number */
191    SEVEN: 55,
192    /** Key constant @type Number */
193    EIGHT: 56,
194    /** Key constant @type Number */
195    NINE: 57,
196    /** Key constant @type Number */
197    A: 65,
198    /** Key constant @type Number */
199    B: 66,
200    /** Key constant @type Number */
201    C: 67,
202    /** Key constant @type Number */
203    D: 68,
204    /** Key constant @type Number */
205    E: 69,
206    /** Key constant @type Number */
207    F: 70,
208    /** Key constant @type Number */
209    G: 71,
210    /** Key constant @type Number */
211    H: 72,
212    /** Key constant @type Number */
213    I: 73,
214    /** Key constant @type Number */
215    J: 74,
216    /** Key constant @type Number */
217    K: 75,
218    /** Key constant @type Number */
219    L: 76,
220    /** Key constant @type Number */
221    M: 77,
222    /** Key constant @type Number */
223    N: 78,
224    /** Key constant @type Number */
225    O: 79,
226    /** Key constant @type Number */
227    P: 80,
228    /** Key constant @type Number */
229    Q: 81,
230    /** Key constant @type Number */
231    R: 82,
232    /** Key constant @type Number */
233    S: 83,
234    /** Key constant @type Number */
235    T: 84,
236    /** Key constant @type Number */
237    U: 85,
238    /** Key constant @type Number */
239    V: 86,
240    /** Key constant @type Number */
241    W: 87,
242    /** Key constant @type Number */
243    X: 88,
244    /** Key constant @type Number */
245    Y: 89,
246    /** Key constant @type Number */
247    Z: 90,
248    /** Key constant @type Number */
249    CONTEXT_MENU: 93,
250    /** Key constant @type Number */
251    NUM_ZERO: 96,
252    /** Key constant @type Number */
253    NUM_ONE: 97,
254    /** Key constant @type Number */
255    NUM_TWO: 98,
256    /** Key constant @type Number */
257    NUM_THREE: 99,
258    /** Key constant @type Number */
259    NUM_FOUR: 100,
260    /** Key constant @type Number */
261    NUM_FIVE: 101,
262    /** Key constant @type Number */
263    NUM_SIX: 102,
264    /** Key constant @type Number */
265    NUM_SEVEN: 103,
266    /** Key constant @type Number */
267    NUM_EIGHT: 104,
268    /** Key constant @type Number */
269    NUM_NINE: 105,
270    /** Key constant @type Number */
271    NUM_MULTIPLY: 106,
272    /** Key constant @type Number */
273    NUM_PLUS: 107,
274    /** Key constant @type Number */
275    NUM_MINUS: 109,
276    /** Key constant @type Number */
277    NUM_PERIOD: 110,
278    /** Key constant @type Number */
279    NUM_DIVISION: 111,
280    /** Key constant @type Number */
281    F1: 112,
282    /** Key constant @type Number */
283    F2: 113,
284    /** Key constant @type Number */
285    F3: 114,
286    /** Key constant @type Number */
287    F4: 115,
288    /** Key constant @type Number */
289    F5: 116,
290    /** Key constant @type Number */
291    F6: 117,
292    /** Key constant @type Number */
293    F7: 118,
294    /** Key constant @type Number */
295    F8: 119,
296    /** Key constant @type Number */
297    F9: 120,
298    /** Key constant @type Number */
299    F10: 121,
300    /** Key constant @type Number */
301    F11: 122,
302    /** Key constant @type Number */
303    F12: 123,
304
305    /** @private */
306    isNavKeyPress : function(){
307        var me = this,
308            k = this.normalizeKey(me.keyCode);
309        return (k >= 33 && k <= 40) ||  // Page Up/Down, End, Home, Left, Up, Right, Down
310        k == me.RETURN ||
311        k == me.TAB ||
312        k == me.ESC;
313    },
314
315    isSpecialKey : function(){
316        var k = this.normalizeKey(this.keyCode);
317        return (this.type == 'keypress' && this.ctrlKey) ||
318        this.isNavKeyPress() ||
319        (k == this.BACKSPACE) || // Backspace
320        (k >= 16 && k <= 20) || // Shift, Ctrl, Alt, Pause, Caps Lock
321        (k >= 44 && k <= 46);   // Print Screen, Insert, Delete
322    },
323
324    getPoint : function(){
325        return new Ext.lib.Point(this.xy[0], this.xy[1]);
326    },
327
328    /**
329     * Returns true if the control, meta, shift or alt key was pressed during this event.
330     * @return {Boolean}
331     */
332    hasModifier : function(){
333        return ((this.ctrlKey || this.altKey) || this.shiftKey);
334    }
335 });