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