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