Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / core / src / EventObject.js
1 /**
2  * @class Ext.EventObject
3
4 Just as {@link Ext.core.Element} wraps around a native DOM node, Ext.EventObject
5 wraps the browser's native event-object normalizing cross-browser differences,
6 such as which mouse button is clicked, keys pressed, mechanisms to stop
7 event-propagation along with a method to prevent default actions from taking place.
8
9 For example:
10
11     function handleClick(e, t){ // e is not a standard event object, it is a Ext.EventObject
12         e.preventDefault();
13         var target = e.getTarget(); // same as t (the target HTMLElement)
14         ...
15     }
16
17     var myDiv = {@link Ext#get Ext.get}("myDiv");  // get reference to an {@link Ext.core.Element}
18     myDiv.on(         // 'on' is shorthand for addListener
19         "click",      // perform an action on click of myDiv
20         handleClick   // reference to the action handler
21     );
22
23     // other methods to do the same:
24     Ext.EventManager.on("myDiv", 'click', handleClick);
25     Ext.EventManager.addListener("myDiv", 'click', handleClick);
26
27  * @singleton
28  * @markdown
29  */
30 Ext.define('Ext.EventObjectImpl', {
31     uses: ['Ext.util.Point'],
32
33     /** Key constant @type Number */
34     BACKSPACE: 8,
35     /** Key constant @type Number */
36     TAB: 9,
37     /** Key constant @type Number */
38     NUM_CENTER: 12,
39     /** Key constant @type Number */
40     ENTER: 13,
41     /** Key constant @type Number */
42     RETURN: 13,
43     /** Key constant @type Number */
44     SHIFT: 16,
45     /** Key constant @type Number */
46     CTRL: 17,
47     /** Key constant @type Number */
48     ALT: 18,
49     /** Key constant @type Number */
50     PAUSE: 19,
51     /** Key constant @type Number */
52     CAPS_LOCK: 20,
53     /** Key constant @type Number */
54     ESC: 27,
55     /** Key constant @type Number */
56     SPACE: 32,
57     /** Key constant @type Number */
58     PAGE_UP: 33,
59     /** Key constant @type Number */
60     PAGE_DOWN: 34,
61     /** Key constant @type Number */
62     END: 35,
63     /** Key constant @type Number */
64     HOME: 36,
65     /** Key constant @type Number */
66     LEFT: 37,
67     /** Key constant @type Number */
68     UP: 38,
69     /** Key constant @type Number */
70     RIGHT: 39,
71     /** Key constant @type Number */
72     DOWN: 40,
73     /** Key constant @type Number */
74     PRINT_SCREEN: 44,
75     /** Key constant @type Number */
76     INSERT: 45,
77     /** Key constant @type Number */
78     DELETE: 46,
79     /** Key constant @type Number */
80     ZERO: 48,
81     /** Key constant @type Number */
82     ONE: 49,
83     /** Key constant @type Number */
84     TWO: 50,
85     /** Key constant @type Number */
86     THREE: 51,
87     /** Key constant @type Number */
88     FOUR: 52,
89     /** Key constant @type Number */
90     FIVE: 53,
91     /** Key constant @type Number */
92     SIX: 54,
93     /** Key constant @type Number */
94     SEVEN: 55,
95     /** Key constant @type Number */
96     EIGHT: 56,
97     /** Key constant @type Number */
98     NINE: 57,
99     /** Key constant @type Number */
100     A: 65,
101     /** Key constant @type Number */
102     B: 66,
103     /** Key constant @type Number */
104     C: 67,
105     /** Key constant @type Number */
106     D: 68,
107     /** Key constant @type Number */
108     E: 69,
109     /** Key constant @type Number */
110     F: 70,
111     /** Key constant @type Number */
112     G: 71,
113     /** Key constant @type Number */
114     H: 72,
115     /** Key constant @type Number */
116     I: 73,
117     /** Key constant @type Number */
118     J: 74,
119     /** Key constant @type Number */
120     K: 75,
121     /** Key constant @type Number */
122     L: 76,
123     /** Key constant @type Number */
124     M: 77,
125     /** Key constant @type Number */
126     N: 78,
127     /** Key constant @type Number */
128     O: 79,
129     /** Key constant @type Number */
130     P: 80,
131     /** Key constant @type Number */
132     Q: 81,
133     /** Key constant @type Number */
134     R: 82,
135     /** Key constant @type Number */
136     S: 83,
137     /** Key constant @type Number */
138     T: 84,
139     /** Key constant @type Number */
140     U: 85,
141     /** Key constant @type Number */
142     V: 86,
143     /** Key constant @type Number */
144     W: 87,
145     /** Key constant @type Number */
146     X: 88,
147     /** Key constant @type Number */
148     Y: 89,
149     /** Key constant @type Number */
150     Z: 90,
151     /** Key constant @type Number */
152     CONTEXT_MENU: 93,
153     /** Key constant @type Number */
154     NUM_ZERO: 96,
155     /** Key constant @type Number */
156     NUM_ONE: 97,
157     /** Key constant @type Number */
158     NUM_TWO: 98,
159     /** Key constant @type Number */
160     NUM_THREE: 99,
161     /** Key constant @type Number */
162     NUM_FOUR: 100,
163     /** Key constant @type Number */
164     NUM_FIVE: 101,
165     /** Key constant @type Number */
166     NUM_SIX: 102,
167     /** Key constant @type Number */
168     NUM_SEVEN: 103,
169     /** Key constant @type Number */
170     NUM_EIGHT: 104,
171     /** Key constant @type Number */
172     NUM_NINE: 105,
173     /** Key constant @type Number */
174     NUM_MULTIPLY: 106,
175     /** Key constant @type Number */
176     NUM_PLUS: 107,
177     /** Key constant @type Number */
178     NUM_MINUS: 109,
179     /** Key constant @type Number */
180     NUM_PERIOD: 110,
181     /** Key constant @type Number */
182     NUM_DIVISION: 111,
183     /** Key constant @type Number */
184     F1: 112,
185     /** Key constant @type Number */
186     F2: 113,
187     /** Key constant @type Number */
188     F3: 114,
189     /** Key constant @type Number */
190     F4: 115,
191     /** Key constant @type Number */
192     F5: 116,
193     /** Key constant @type Number */
194     F6: 117,
195     /** Key constant @type Number */
196     F7: 118,
197     /** Key constant @type Number */
198     F8: 119,
199     /** Key constant @type Number */
200     F9: 120,
201     /** Key constant @type Number */
202     F10: 121,
203     /** Key constant @type Number */
204     F11: 122,
205     /** Key constant @type Number */
206     F12: 123,
207
208     /**
209      * Simple click regex
210      * @private
211      */
212     clickRe: /(dbl)?click/,
213     // safari keypress events for special keys return bad keycodes
214     safariKeys: {
215         3: 13, // enter
216         63234: 37, // left
217         63235: 39, // right
218         63232: 38, // up
219         63233: 40, // down
220         63276: 33, // page up
221         63277: 34, // page down
222         63272: 46, // delete
223         63273: 36, // home
224         63275: 35 // end
225     },
226     // normalize button clicks, don't see any way to feature detect this.
227     btnMap: Ext.isIE ? {
228         1: 0,
229         4: 1,
230         2: 2
231     } : {
232         0: 0,
233         1: 1,
234         2: 2
235     },
236
237     constructor: function(event, freezeEvent){
238         if (event) {
239             this.setEvent(event.browserEvent || event, freezeEvent);
240         }
241     },
242
243     setEvent: function(event, freezeEvent){
244         var me = this, button, options;
245
246         if (event == me || (event && event.browserEvent)) { // already wrapped
247             return event;
248         }
249         me.browserEvent = event;
250         if (event) {
251             // normalize buttons
252             button = event.button ? me.btnMap[event.button] : (event.which ? event.which - 1 : -1);
253             if (me.clickRe.test(event.type) && button == -1) {
254                 button = 0;
255             }
256             options = {
257                 type: event.type,
258                 button: button,
259                 shiftKey: event.shiftKey,
260                 // mac metaKey behaves like ctrlKey
261                 ctrlKey: event.ctrlKey || event.metaKey || false,
262                 altKey: event.altKey,
263                 // in getKey these will be normalized for the mac
264                 keyCode: event.keyCode,
265                 charCode: event.charCode,
266                 // cache the targets for the delayed and or buffered events
267                 target: Ext.EventManager.getTarget(event),
268                 relatedTarget: Ext.EventManager.getRelatedTarget(event),
269                 currentTarget: event.currentTarget,
270                 xy: (freezeEvent ? me.getXY() : null)
271             };
272         } else {
273             options = {
274                 button: -1,
275                 shiftKey: false,
276                 ctrlKey: false,
277                 altKey: false,
278                 keyCode: 0,
279                 charCode: 0,
280                 target: null,
281                 xy: [0, 0]
282             };
283         }
284         Ext.apply(me, options);
285         return me;
286     },
287
288     /**
289      * Stop the event (preventDefault and stopPropagation)
290      */
291     stopEvent: function(){
292         this.stopPropagation();
293         this.preventDefault();
294     },
295
296     /**
297      * Prevents the browsers default handling of the event.
298      */
299     preventDefault: function(){
300         if (this.browserEvent) {
301             Ext.EventManager.preventDefault(this.browserEvent);
302         }
303     },
304
305     /**
306      * Cancels bubbling of the event.
307      */
308     stopPropagation: function(){
309         var browserEvent = this.browserEvent;
310
311         if (browserEvent) {
312             if (browserEvent.type == 'mousedown') {
313                 Ext.EventManager.stoppedMouseDownEvent.fire(this);
314             }
315             Ext.EventManager.stopPropagation(browserEvent);
316         }
317     },
318
319     /**
320      * Gets the character code for the event.
321      * @return {Number}
322      */
323     getCharCode: function(){
324         return this.charCode || this.keyCode;
325     },
326
327     /**
328      * Returns a normalized keyCode for the event.
329      * @return {Number} The key code
330      */
331     getKey: function(){
332         return this.normalizeKey(this.keyCode || this.charCode);
333     },
334
335     /**
336      * Normalize key codes across browsers
337      * @private
338      * @param {Number} key The key code
339      * @return {Number} The normalized code
340      */
341     normalizeKey: function(key){
342         // can't feature detect this
343         return Ext.isWebKit ? (this.safariKeys[key] || key) : key;
344     },
345
346     /**
347      * Gets the x coordinate of the event.
348      * @return {Number}
349      * @deprecated 4.0 Replaced by {@link #getX}
350      */
351     getPageX: function(){
352         return this.getX();
353     },
354
355     /**
356      * Gets the y coordinate of the event.
357      * @return {Number}
358      * @deprecated 4.0 Replaced by {@link #getY}
359      */
360     getPageY: function(){
361         return this.getY();
362     },
363     
364     /**
365      * Gets the x coordinate of the event.
366      * @return {Number}
367      */
368     getX: function() {
369         return this.getXY()[0];
370     },    
371     
372     /**
373      * Gets the y coordinate of the event.
374      * @return {Number}
375      */
376     getY: function() {
377         return this.getXY()[1];
378     },
379         
380     /**
381      * Gets the page coordinates of the event.
382      * @return {Array} The xy values like [x, y]
383      */
384     getXY: function() {
385         if (!this.xy) {
386             // same for XY
387             this.xy = Ext.EventManager.getPageXY(this.browserEvent);
388         }
389         return this.xy;
390     },
391
392     /**
393      * Gets the target for the event.
394      * @param {String} selector (optional) A simple selector to filter the target or look for an ancestor of the target
395      * @param {Number/Mixed} maxDepth (optional) The max depth to search as a number or element (defaults to 10 || document.body)
396      * @param {Boolean} returnEl (optional) True to return a Ext.core.Element object instead of DOM node
397      * @return {HTMLelement}
398      */
399     getTarget : function(selector, maxDepth, returnEl){
400         if (selector) {
401             return Ext.fly(this.target).findParent(selector, maxDepth, returnEl);
402         }
403         return returnEl ? Ext.get(this.target) : this.target;
404     },
405
406     /**
407      * Gets the related target.
408      * @param {String} selector (optional) A simple selector to filter the target or look for an ancestor of the target
409      * @param {Number/Mixed} maxDepth (optional) The max depth to search as a number or element (defaults to 10 || document.body)
410      * @param {Boolean} returnEl (optional) True to return a Ext.core.Element object instead of DOM node
411      * @return {HTMLElement}
412      */
413     getRelatedTarget : function(selector, maxDepth, returnEl){
414         if (selector) {
415             return Ext.fly(this.relatedTarget).findParent(selector, maxDepth, returnEl);
416         }
417         return returnEl ? Ext.get(this.relatedTarget) : this.relatedTarget;
418     },
419
420     /**
421      * Normalizes mouse wheel delta across browsers
422      * @return {Number} The delta
423      */
424     getWheelDelta : function(){
425         var event = this.browserEvent,
426             delta = 0;
427
428         if (event.wheelDelta) { /* IE/Opera. */
429             delta = event.wheelDelta / 120;
430         } else if (event.detail){ /* Mozilla case. */
431             delta = -event.detail / 3;
432         }
433         return delta;
434     },
435
436     /**
437     * Returns true if the target of this event is a child of el.  Unless the allowEl parameter is set, it will return false if if the target is el.
438     * Example usage:<pre><code>
439 // Handle click on any child of an element
440 Ext.getBody().on('click', function(e){
441     if(e.within('some-el')){
442         alert('Clicked on a child of some-el!');
443     }
444 });
445
446 // Handle click directly on an element, ignoring clicks on child nodes
447 Ext.getBody().on('click', function(e,t){
448     if((t.id == 'some-el') && !e.within(t, true)){
449         alert('Clicked directly on some-el!');
450     }
451 });
452 </code></pre>
453      * @param {Mixed} el The id, DOM element or Ext.core.Element to check
454      * @param {Boolean} related (optional) true to test if the related target is within el instead of the target
455      * @param {Boolean} allowEl {optional} true to also check if the passed element is the target or related target
456      * @return {Boolean}
457      */
458     within : function(el, related, allowEl){
459         if(el){
460             var t = related ? this.getRelatedTarget() : this.getTarget(),
461                 result;
462
463             if (t) {
464                 result = Ext.fly(el).contains(t);
465                 if (!result && allowEl) {
466                     result = t == Ext.getDom(el);
467                 }
468                 return result;
469             }
470         }
471         return false;
472     },
473
474     /**
475      * Checks if the key pressed was a "navigation" key
476      * @return {Boolean} True if the press is a navigation keypress
477      */
478     isNavKeyPress : function(){
479         var me = this,
480             k = this.normalizeKey(me.keyCode);
481
482        return (k >= 33 && k <= 40) ||  // Page Up/Down, End, Home, Left, Up, Right, Down
483        k == me.RETURN ||
484        k == me.TAB ||
485        k == me.ESC;
486     },
487
488     /**
489      * Checks if the key pressed was a "special" key
490      * @return {Boolean} True if the press is a special keypress
491      */
492     isSpecialKey : function(){
493         var k = this.normalizeKey(this.keyCode);
494         return (this.type == 'keypress' && this.ctrlKey) ||
495         this.isNavKeyPress() ||
496         (k == this.BACKSPACE) || // Backspace
497         (k >= 16 && k <= 20) || // Shift, Ctrl, Alt, Pause, Caps Lock
498         (k >= 44 && k <= 46);   // Print Screen, Insert, Delete
499     },
500
501     /**
502      * Returns a point object that consists of the object coordinates.
503      * @return {Ext.util.Point} point
504      */
505     getPoint : function(){
506         var xy = this.getXY();
507         return Ext.create('Ext.util.Point', xy[0], xy[1]);
508     },
509
510    /**
511     * Returns true if the control, meta, shift or alt key was pressed during this event.
512     * @return {Boolean}
513     */
514     hasModifier : function(){
515         return this.ctrlKey || this.altKey || this.shiftKey || this.metaKey;
516     },
517
518     /**
519      * Injects a DOM event using the data in this object and (optionally) a new target.
520      * This is a low-level technique and not likely to be used by application code. The
521      * currently supported event types are:
522      * <p><b>HTMLEvents</b></p>
523      * <ul>
524      * <li>load</li>
525      * <li>unload</li>
526      * <li>select</li>
527      * <li>change</li>
528      * <li>submit</li>
529      * <li>reset</li>
530      * <li>resize</li>
531      * <li>scroll</li>
532      * </ul>
533      * <p><b>MouseEvents</b></p>
534      * <ul>
535      * <li>click</li>
536      * <li>dblclick</li>
537      * <li>mousedown</li>
538      * <li>mouseup</li>
539      * <li>mouseover</li>
540      * <li>mousemove</li>
541      * <li>mouseout</li>
542      * </ul>
543      * <p><b>UIEvents</b></p>
544      * <ul>
545      * <li>focusin</li>
546      * <li>focusout</li>
547      * <li>activate</li>
548      * <li>focus</li>
549      * <li>blur</li>
550      * </ul>
551      * @param {Element/HTMLElement} target If specified, the target for the event. This
552      * is likely to be used when relaying a DOM event. If not specified, {@link #getTarget}
553      * is used to determine the target.
554      */
555     injectEvent: function () {
556         var API,
557             dispatchers = {}; // keyed by event type (e.g., 'mousedown')
558
559         // Good reference: http://developer.yahoo.com/yui/docs/UserAction.js.html
560
561         // IE9 has createEvent, but this code causes major problems with htmleditor (it
562         // blocks all mouse events and maybe more). TODO
563
564         if (!Ext.isIE && document.createEvent) { // if (DOM compliant)
565             API = {
566                 createHtmlEvent: function (doc, type, bubbles, cancelable) {
567                     var event = doc.createEvent('HTMLEvents');
568
569                     event.initEvent(type, bubbles, cancelable);
570                     return event;
571                 },
572
573                 createMouseEvent: function (doc, type, bubbles, cancelable, detail,
574                                             clientX, clientY, ctrlKey, altKey, shiftKey, metaKey,
575                                             button, relatedTarget) {
576                     var event = doc.createEvent('MouseEvents'),
577                         view = doc.defaultView || window;
578
579                     if (event.initMouseEvent) {
580                         event.initMouseEvent(type, bubbles, cancelable, view, detail,
581                                     clientX, clientY, clientX, clientY, ctrlKey, altKey,
582                                     shiftKey, metaKey, button, relatedTarget);
583                     } else { // old Safari
584                         event = doc.createEvent('UIEvents');
585                         event.initEvent(type, bubbles, cancelable);
586                         event.view = view;
587                         event.detail = detail;
588                         event.screenX = clientX;
589                         event.screenY = clientY;
590                         event.clientX = clientX;
591                         event.clientY = clientY;
592                         event.ctrlKey = ctrlKey;
593                         event.altKey = altKey;
594                         event.metaKey = metaKey;
595                         event.shiftKey = shiftKey;
596                         event.button = button;
597                         event.relatedTarget = relatedTarget;
598                     }
599
600                     return event;
601                 },
602
603                 createUIEvent: function (doc, type, bubbles, cancelable, detail) {
604                     var event = doc.createEvent('UIEvents'),
605                         view = doc.defaultView || window;
606
607                     event.initUIEvent(type, bubbles, cancelable, view, detail);
608                     return event;
609                 },
610
611                 fireEvent: function (target, type, event) {
612                     target.dispatchEvent(event);
613                 },
614
615                 fixTarget: function (target) {
616                     // Safari3 doesn't have window.dispatchEvent()
617                     if (target == window && !target.dispatchEvent) {
618                         return document;
619                     }
620
621                     return target;
622                 }
623             }
624         } else if (document.createEventObject) { // else if (IE)
625             var crazyIEButtons = { 0: 1, 1: 4, 2: 2 };
626
627             API = {
628                 createHtmlEvent: function (doc, type, bubbles, cancelable) {
629                     var event = doc.createEventObject();
630                     event.bubbles = bubbles;
631                     event.cancelable = cancelable;
632                     return event;
633                 },
634
635                 createMouseEvent: function (doc, type, bubbles, cancelable, detail,
636                                             clientX, clientY, ctrlKey, altKey, shiftKey, metaKey,
637                                             button, relatedTarget) {
638                     var event = doc.createEventObject();
639                     event.bubbles = bubbles;
640                     event.cancelable = cancelable;
641                     event.detail = detail;
642                     event.screenX = clientX;
643                     event.screenY = clientY;
644                     event.clientX = clientX;
645                     event.clientY = clientY;
646                     event.ctrlKey = ctrlKey;
647                     event.altKey = altKey;
648                     event.shiftKey = shiftKey;
649                     event.metaKey = metaKey;
650                     event.button = crazyIEButtons[button] || button;
651                     event.relatedTarget = relatedTarget; // cannot assign to/fromElement
652                     return event;
653                 },
654
655                 createUIEvent: function (doc, type, bubbles, cancelable, detail) {
656                     var event = doc.createEventObject();
657                     event.bubbles = bubbles;
658                     event.cancelable = cancelable;
659                     return event;
660                 },
661
662                 fireEvent: function (target, type, event) {
663                     target.fireEvent('on' + type, event);
664                 },
665
666                 fixTarget: function (target) {
667                     if (target == document) {
668                         // IE6,IE7 thinks window==document and doesn't have window.fireEvent()
669                         // IE6,IE7 cannot properly call document.fireEvent()
670                         return document.documentElement;
671                     }
672
673                     return target;
674                 }
675             };
676         }
677
678         //----------------
679         // HTMLEvents
680
681         Ext.Object.each({
682                 load:   [false, false],
683                 unload: [false, false],
684                 select: [true, false],
685                 change: [true, false],
686                 submit: [true, true],
687                 reset:  [true, false],
688                 resize: [true, false],
689                 scroll: [true, false]
690             },
691             function (name, value) {
692                 var bubbles = value[0], cancelable = value[1];
693                 dispatchers[name] = function (targetEl, srcEvent) {
694                     var e = API.createHtmlEvent(name, bubbles, cancelable);
695                     API.fireEvent(targetEl, name, e);
696                 };
697             });
698
699         //----------------
700         // MouseEvents
701
702         function createMouseEventDispatcher (type, detail) {
703             var cancelable = (type != 'mousemove');
704             return function (targetEl, srcEvent) {
705                 var xy = srcEvent.getXY(),
706                     e = API.createMouseEvent(targetEl.ownerDocument, type, true, cancelable,
707                                 detail, xy[0], xy[1], srcEvent.ctrlKey, srcEvent.altKey,
708                                 srcEvent.shiftKey, srcEvent.metaKey, srcEvent.button,
709                                 srcEvent.relatedTarget);
710                 API.fireEvent(targetEl, type, e);
711             };
712         }
713
714         Ext.each(['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mousemove', 'mouseout'],
715             function (eventName) {
716                 dispatchers[eventName] = createMouseEventDispatcher(eventName, 1);
717             });
718
719         //----------------
720         // UIEvents
721
722         Ext.Object.each({
723                 focusin:  [true, false],
724                 focusout: [true, false],
725                 activate: [true, true],
726                 focus:    [false, false],
727                 blur:     [false, false]
728             },
729             function (name, value) {
730                 var bubbles = value[0], cancelable = value[1];
731                 dispatchers[name] = function (targetEl, srcEvent) {
732                     var e = API.createUIEvent(targetEl.ownerDocument, name, bubbles, cancelable, 1);
733                     API.fireEvent(targetEl, name, e);
734                 };
735             });
736
737         //---------
738         if (!API) {
739             // not even sure what ancient browsers fall into this category...
740
741             dispatchers = {}; // never mind all those we just built :P
742
743             API = {
744                 fixTarget: function (t) {
745                     return t;
746                 }
747             };
748         }
749
750         function cannotInject (target, srcEvent) {
751             //<debug>
752             // TODO log something
753             //</debug>
754         }
755
756         return function (target) {
757             var me = this,
758                 dispatcher = dispatchers[me.type] || cannotInject,
759                 t = target ? (target.dom || target) : me.getTarget();
760
761             t = API.fixTarget(t);
762             dispatcher(t, me);
763         };
764     }() // call to produce method
765
766 }, function() {
767
768 Ext.EventObject = new Ext.EventObjectImpl();
769
770 });
771