4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-EventObject'>/**
19 </span> * @class Ext.EventObject
21 Just as {@link Ext.core.Element} wraps around a native DOM node, Ext.EventObject
22 wraps the browser's native event-object normalizing cross-browser differences,
23 such as which mouse button is clicked, keys pressed, mechanisms to stop
24 event-propagation along with a method to prevent default actions from taking place.
28 function handleClick(e, t){ // e is not a standard event object, it is a Ext.EventObject
30 var target = e.getTarget(); // same as t (the target HTMLElement)
34 var myDiv = {@link Ext#get Ext.get}("myDiv"); // get reference to an {@link Ext.core.Element}
35 myDiv.on( // 'on' is shorthand for addListener
36 "click", // perform an action on click of myDiv
37 handleClick // reference to the action handler
40 // other methods to do the same:
41 Ext.EventManager.on("myDiv", 'click', handleClick);
42 Ext.EventManager.addListener("myDiv", 'click', handleClick);
47 Ext.define('Ext.EventObjectImpl', {
48 uses: ['Ext.util.Point'],
50 <span id='Ext-EventObject-property-BACKSPACE'> /** Key constant @type Number */
52 <span id='Ext-EventObject-property-TAB'> /** Key constant @type Number */
54 <span id='Ext-EventObject-property-NUM_CENTER'> /** Key constant @type Number */
55 </span> NUM_CENTER: 12,
56 <span id='Ext-EventObject-property-ENTER'> /** Key constant @type Number */
58 <span id='Ext-EventObject-property-RETURN'> /** Key constant @type Number */
60 <span id='Ext-EventObject-property-SHIFT'> /** Key constant @type Number */
62 <span id='Ext-EventObject-property-CTRL'> /** Key constant @type Number */
64 <span id='Ext-EventObject-property-ALT'> /** Key constant @type Number */
66 <span id='Ext-EventObject-property-PAUSE'> /** Key constant @type Number */
68 <span id='Ext-EventObject-property-CAPS_LOCK'> /** Key constant @type Number */
69 </span> CAPS_LOCK: 20,
70 <span id='Ext-EventObject-property-ESC'> /** Key constant @type Number */
72 <span id='Ext-EventObject-property-SPACE'> /** Key constant @type Number */
74 <span id='Ext-EventObject-property-PAGE_UP'> /** Key constant @type Number */
76 <span id='Ext-EventObject-property-PAGE_DOWN'> /** Key constant @type Number */
77 </span> PAGE_DOWN: 34,
78 <span id='Ext-EventObject-property-END'> /** Key constant @type Number */
80 <span id='Ext-EventObject-property-HOME'> /** Key constant @type Number */
82 <span id='Ext-EventObject-property-LEFT'> /** Key constant @type Number */
84 <span id='Ext-EventObject-property-UP'> /** Key constant @type Number */
86 <span id='Ext-EventObject-property-RIGHT'> /** Key constant @type Number */
88 <span id='Ext-EventObject-property-DOWN'> /** Key constant @type Number */
90 <span id='Ext-EventObject-property-PRINT_SCREEN'> /** Key constant @type Number */
91 </span> PRINT_SCREEN: 44,
92 <span id='Ext-EventObject-property-INSERT'> /** Key constant @type Number */
94 <span id='Ext-EventObject-property-DELETE'> /** Key constant @type Number */
96 <span id='Ext-EventObject-property-ZERO'> /** Key constant @type Number */
98 <span id='Ext-EventObject-property-ONE'> /** Key constant @type Number */
100 <span id='Ext-EventObject-property-TWO'> /** Key constant @type Number */
102 <span id='Ext-EventObject-property-THREE'> /** Key constant @type Number */
104 <span id='Ext-EventObject-property-FOUR'> /** Key constant @type Number */
106 <span id='Ext-EventObject-property-FIVE'> /** Key constant @type Number */
108 <span id='Ext-EventObject-property-SIX'> /** Key constant @type Number */
110 <span id='Ext-EventObject-property-SEVEN'> /** Key constant @type Number */
112 <span id='Ext-EventObject-property-EIGHT'> /** Key constant @type Number */
114 <span id='Ext-EventObject-property-NINE'> /** Key constant @type Number */
116 <span id='Ext-EventObject-property-A'> /** Key constant @type Number */
118 <span id='Ext-EventObject-property-B'> /** Key constant @type Number */
120 <span id='Ext-EventObject-property-C'> /** Key constant @type Number */
122 <span id='Ext-EventObject-property-D'> /** Key constant @type Number */
124 <span id='Ext-EventObject-property-E'> /** Key constant @type Number */
126 <span id='Ext-EventObject-property-F'> /** Key constant @type Number */
128 <span id='Ext-EventObject-property-G'> /** Key constant @type Number */
130 <span id='Ext-EventObject-property-H'> /** Key constant @type Number */
132 <span id='Ext-EventObject-property-I'> /** Key constant @type Number */
134 <span id='Ext-EventObject-property-J'> /** Key constant @type Number */
136 <span id='Ext-EventObject-property-K'> /** Key constant @type Number */
138 <span id='Ext-EventObject-property-L'> /** Key constant @type Number */
140 <span id='Ext-EventObject-property-M'> /** Key constant @type Number */
142 <span id='Ext-EventObject-property-N'> /** Key constant @type Number */
144 <span id='Ext-EventObject-property-O'> /** Key constant @type Number */
146 <span id='Ext-EventObject-property-P'> /** Key constant @type Number */
148 <span id='Ext-EventObject-property-Q'> /** Key constant @type Number */
150 <span id='Ext-EventObject-property-R'> /** Key constant @type Number */
152 <span id='Ext-EventObject-property-S'> /** Key constant @type Number */
154 <span id='Ext-EventObject-property-T'> /** Key constant @type Number */
156 <span id='Ext-EventObject-property-U'> /** Key constant @type Number */
158 <span id='Ext-EventObject-property-V'> /** Key constant @type Number */
160 <span id='Ext-EventObject-property-W'> /** Key constant @type Number */
162 <span id='Ext-EventObject-property-X'> /** Key constant @type Number */
164 <span id='Ext-EventObject-property-Y'> /** Key constant @type Number */
166 <span id='Ext-EventObject-property-Z'> /** Key constant @type Number */
168 <span id='Ext-EventObject-property-CONTEXT_MENU'> /** Key constant @type Number */
169 </span> CONTEXT_MENU: 93,
170 <span id='Ext-EventObject-property-NUM_ZERO'> /** Key constant @type Number */
171 </span> NUM_ZERO: 96,
172 <span id='Ext-EventObject-property-NUM_ONE'> /** Key constant @type Number */
174 <span id='Ext-EventObject-property-NUM_TWO'> /** Key constant @type Number */
176 <span id='Ext-EventObject-property-NUM_THREE'> /** Key constant @type Number */
177 </span> NUM_THREE: 99,
178 <span id='Ext-EventObject-property-NUM_FOUR'> /** Key constant @type Number */
179 </span> NUM_FOUR: 100,
180 <span id='Ext-EventObject-property-NUM_FIVE'> /** Key constant @type Number */
181 </span> NUM_FIVE: 101,
182 <span id='Ext-EventObject-property-NUM_SIX'> /** Key constant @type Number */
183 </span> NUM_SIX: 102,
184 <span id='Ext-EventObject-property-NUM_SEVEN'> /** Key constant @type Number */
185 </span> NUM_SEVEN: 103,
186 <span id='Ext-EventObject-property-NUM_EIGHT'> /** Key constant @type Number */
187 </span> NUM_EIGHT: 104,
188 <span id='Ext-EventObject-property-NUM_NINE'> /** Key constant @type Number */
189 </span> NUM_NINE: 105,
190 <span id='Ext-EventObject-property-NUM_MULTIPLY'> /** Key constant @type Number */
191 </span> NUM_MULTIPLY: 106,
192 <span id='Ext-EventObject-property-NUM_PLUS'> /** Key constant @type Number */
193 </span> NUM_PLUS: 107,
194 <span id='Ext-EventObject-property-NUM_MINUS'> /** Key constant @type Number */
195 </span> NUM_MINUS: 109,
196 <span id='Ext-EventObject-property-NUM_PERIOD'> /** Key constant @type Number */
197 </span> NUM_PERIOD: 110,
198 <span id='Ext-EventObject-property-NUM_DIVISION'> /** Key constant @type Number */
199 </span> NUM_DIVISION: 111,
200 <span id='Ext-EventObject-property-F1'> /** Key constant @type Number */
202 <span id='Ext-EventObject-property-F2'> /** Key constant @type Number */
204 <span id='Ext-EventObject-property-F3'> /** Key constant @type Number */
206 <span id='Ext-EventObject-property-F4'> /** Key constant @type Number */
208 <span id='Ext-EventObject-property-F5'> /** Key constant @type Number */
210 <span id='Ext-EventObject-property-F6'> /** Key constant @type Number */
212 <span id='Ext-EventObject-property-F7'> /** Key constant @type Number */
214 <span id='Ext-EventObject-property-F8'> /** Key constant @type Number */
216 <span id='Ext-EventObject-property-F9'> /** Key constant @type Number */
218 <span id='Ext-EventObject-property-F10'> /** Key constant @type Number */
220 <span id='Ext-EventObject-property-F11'> /** Key constant @type Number */
222 <span id='Ext-EventObject-property-F12'> /** Key constant @type Number */
225 <span id='Ext-EventObject-property-clickRe'> /**
226 </span> * Simple click regex
229 clickRe: /(dbl)?click/,
230 // safari keypress events for special keys return bad keycodes
237 63276: 33, // page up
238 63277: 34, // page down
243 // normalize button clicks, don't see any way to feature detect this.
254 constructor: function(event, freezeEvent){
256 this.setEvent(event.browserEvent || event, freezeEvent);
260 setEvent: function(event, freezeEvent){
261 var me = this, button, options;
263 if (event == me || (event && event.browserEvent)) { // already wrapped
266 me.browserEvent = event;
269 button = event.button ? me.btnMap[event.button] : (event.which ? event.which - 1 : -1);
270 if (me.clickRe.test(event.type) && button == -1) {
276 shiftKey: event.shiftKey,
277 // mac metaKey behaves like ctrlKey
278 ctrlKey: event.ctrlKey || event.metaKey || false,
279 altKey: event.altKey,
280 // in getKey these will be normalized for the mac
281 keyCode: event.keyCode,
282 charCode: event.charCode,
283 // cache the targets for the delayed and or buffered events
284 target: Ext.EventManager.getTarget(event),
285 relatedTarget: Ext.EventManager.getRelatedTarget(event),
286 currentTarget: event.currentTarget,
287 xy: (freezeEvent ? me.getXY() : null)
301 Ext.apply(me, options);
305 <span id='Ext-EventObject-method-stopEvent'> /**
306 </span> * Stop the event (preventDefault and stopPropagation)
308 stopEvent: function(){
309 this.stopPropagation();
310 this.preventDefault();
313 <span id='Ext-EventObject-method-preventDefault'> /**
314 </span> * Prevents the browsers default handling of the event.
316 preventDefault: function(){
317 if (this.browserEvent) {
318 Ext.EventManager.preventDefault(this.browserEvent);
322 <span id='Ext-EventObject-method-stopPropagation'> /**
323 </span> * Cancels bubbling of the event.
325 stopPropagation: function(){
326 var browserEvent = this.browserEvent;
329 if (browserEvent.type == 'mousedown') {
330 Ext.EventManager.stoppedMouseDownEvent.fire(this);
332 Ext.EventManager.stopPropagation(browserEvent);
336 <span id='Ext-EventObject-method-getCharCode'> /**
337 </span> * Gets the character code for the event.
340 getCharCode: function(){
341 return this.charCode || this.keyCode;
344 <span id='Ext-EventObject-method-getKey'> /**
345 </span> * Returns a normalized keyCode for the event.
346 * @return {Number} The key code
349 return this.normalizeKey(this.keyCode || this.charCode);
352 <span id='Ext-EventObject-method-normalizeKey'> /**
353 </span> * Normalize key codes across browsers
355 * @param {Number} key The key code
356 * @return {Number} The normalized code
358 normalizeKey: function(key){
359 // can't feature detect this
360 return Ext.isWebKit ? (this.safariKeys[key] || key) : key;
363 <span id='Ext-EventObject-method-getPageX'> /**
364 </span> * Gets the x coordinate of the event.
366 * @deprecated 4.0 Replaced by {@link #getX}
368 getPageX: function(){
372 <span id='Ext-EventObject-method-getPageY'> /**
373 </span> * Gets the y coordinate of the event.
375 * @deprecated 4.0 Replaced by {@link #getY}
377 getPageY: function(){
381 <span id='Ext-EventObject-method-getX'> /**
382 </span> * Gets the x coordinate of the event.
386 return this.getXY()[0];
389 <span id='Ext-EventObject-method-getY'> /**
390 </span> * Gets the y coordinate of the event.
394 return this.getXY()[1];
397 <span id='Ext-EventObject-method-getXY'> /**
398 </span> * Gets the page coordinates of the event.
399 * @return {Array} The xy values like [x, y]
404 this.xy = Ext.EventManager.getPageXY(this.browserEvent);
409 <span id='Ext-EventObject-method-getTarget'> /**
410 </span> * Gets the target for the event.
411 * @param {String} selector (optional) A simple selector to filter the target or look for an ancestor of the target
412 * @param {Number/Mixed} maxDepth (optional) The max depth to search as a number or element (defaults to 10 || document.body)
413 * @param {Boolean} returnEl (optional) True to return a Ext.core.Element object instead of DOM node
414 * @return {HTMLelement}
416 getTarget : function(selector, maxDepth, returnEl){
418 return Ext.fly(this.target).findParent(selector, maxDepth, returnEl);
420 return returnEl ? Ext.get(this.target) : this.target;
423 <span id='Ext-EventObject-method-getRelatedTarget'> /**
424 </span> * Gets the related target.
425 * @param {String} selector (optional) A simple selector to filter the target or look for an ancestor of the target
426 * @param {Number/Mixed} maxDepth (optional) The max depth to search as a number or element (defaults to 10 || document.body)
427 * @param {Boolean} returnEl (optional) True to return a Ext.core.Element object instead of DOM node
428 * @return {HTMLElement}
430 getRelatedTarget : function(selector, maxDepth, returnEl){
432 return Ext.fly(this.relatedTarget).findParent(selector, maxDepth, returnEl);
434 return returnEl ? Ext.get(this.relatedTarget) : this.relatedTarget;
437 <span id='Ext-EventObject-method-getWheelDelta'> /**
438 </span> * Normalizes mouse wheel delta across browsers
439 * @return {Number} The delta
441 getWheelDelta : function(){
442 var event = this.browserEvent,
445 if (event.wheelDelta) { /* IE/Opera. */
446 delta = event.wheelDelta / 120;
447 } else if (event.detail){ /* Mozilla case. */
448 delta = -event.detail / 3;
453 <span id='Ext-EventObject-method-within'> /**
454 </span> * 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.
455 * Example usage:<pre><code>
456 // Handle click on any child of an element
457 Ext.getBody().on('click', function(e){
458 if(e.within('some-el')){
459 alert('Clicked on a child of some-el!');
463 // Handle click directly on an element, ignoring clicks on child nodes
464 Ext.getBody().on('click', function(e,t){
465 if((t.id == 'some-el') && !e.within(t, true)){
466 alert('Clicked directly on some-el!');
469 </code></pre>
470 * @param {Mixed} el The id, DOM element or Ext.core.Element to check
471 * @param {Boolean} related (optional) true to test if the related target is within el instead of the target
472 * @param {Boolean} allowEl {optional} true to also check if the passed element is the target or related target
475 within : function(el, related, allowEl){
477 var t = related ? this.getRelatedTarget() : this.getTarget(),
481 result = Ext.fly(el).contains(t);
482 if (!result && allowEl) {
483 result = t == Ext.getDom(el);
491 <span id='Ext-EventObject-method-isNavKeyPress'> /**
492 </span> * Checks if the key pressed was a "navigation" key
493 * @return {Boolean} True if the press is a navigation keypress
495 isNavKeyPress : function(){
497 k = this.normalizeKey(me.keyCode);
499 return (k >= 33 && k <= 40) || // Page Up/Down, End, Home, Left, Up, Right, Down
505 <span id='Ext-EventObject-method-isSpecialKey'> /**
506 </span> * Checks if the key pressed was a "special" key
507 * @return {Boolean} True if the press is a special keypress
509 isSpecialKey : function(){
510 var k = this.normalizeKey(this.keyCode);
511 return (this.type == 'keypress' && this.ctrlKey) ||
512 this.isNavKeyPress() ||
513 (k == this.BACKSPACE) || // Backspace
514 (k >= 16 && k <= 20) || // Shift, Ctrl, Alt, Pause, Caps Lock
515 (k >= 44 && k <= 46); // Print Screen, Insert, Delete
518 <span id='Ext-EventObject-method-getPoint'> /**
519 </span> * Returns a point object that consists of the object coordinates.
520 * @return {Ext.util.Point} point
522 getPoint : function(){
523 var xy = this.getXY();
524 return Ext.create('Ext.util.Point', xy[0], xy[1]);
527 <span id='Ext-EventObject-method-hasModifier'> /**
528 </span> * Returns true if the control, meta, shift or alt key was pressed during this event.
531 hasModifier : function(){
532 return this.ctrlKey || this.altKey || this.shiftKey || this.metaKey;
535 <span id='Ext-EventObject-method-injectEvent'> /**
536 </span> * Injects a DOM event using the data in this object and (optionally) a new target.
537 * This is a low-level technique and not likely to be used by application code. The
538 * currently supported event types are:
539 * <p><b>HTMLEvents</b></p>
541 * <li>load</li>
542 * <li>unload</li>
543 * <li>select</li>
544 * <li>change</li>
545 * <li>submit</li>
546 * <li>reset</li>
547 * <li>resize</li>
548 * <li>scroll</li>
550 * <p><b>MouseEvents</b></p>
552 * <li>click</li>
553 * <li>dblclick</li>
554 * <li>mousedown</li>
555 * <li>mouseup</li>
556 * <li>mouseover</li>
557 * <li>mousemove</li>
558 * <li>mouseout</li>
560 * <p><b>UIEvents</b></p>
562 * <li>focusin</li>
563 * <li>focusout</li>
564 * <li>activate</li>
565 * <li>focus</li>
566 * <li>blur</li>
568 * @param {Element/HTMLElement} target If specified, the target for the event. This
569 * is likely to be used when relaying a DOM event. If not specified, {@link #getTarget}
570 * is used to determine the target.
572 injectEvent: function () {
574 dispatchers = {}; // keyed by event type (e.g., 'mousedown')
576 // Good reference: http://developer.yahoo.com/yui/docs/UserAction.js.html
578 // IE9 has createEvent, but this code causes major problems with htmleditor (it
579 // blocks all mouse events and maybe more). TODO
581 if (!Ext.isIE && document.createEvent) { // if (DOM compliant)
583 createHtmlEvent: function (doc, type, bubbles, cancelable) {
584 var event = doc.createEvent('HTMLEvents');
586 event.initEvent(type, bubbles, cancelable);
590 createMouseEvent: function (doc, type, bubbles, cancelable, detail,
591 clientX, clientY, ctrlKey, altKey, shiftKey, metaKey,
592 button, relatedTarget) {
593 var event = doc.createEvent('MouseEvents'),
594 view = doc.defaultView || window;
596 if (event.initMouseEvent) {
597 event.initMouseEvent(type, bubbles, cancelable, view, detail,
598 clientX, clientY, clientX, clientY, ctrlKey, altKey,
599 shiftKey, metaKey, button, relatedTarget);
600 } else { // old Safari
601 event = doc.createEvent('UIEvents');
602 event.initEvent(type, bubbles, cancelable);
604 event.detail = detail;
605 event.screenX = clientX;
606 event.screenY = clientY;
607 event.clientX = clientX;
608 event.clientY = clientY;
609 event.ctrlKey = ctrlKey;
610 event.altKey = altKey;
611 event.metaKey = metaKey;
612 event.shiftKey = shiftKey;
613 event.button = button;
614 event.relatedTarget = relatedTarget;
620 createUIEvent: function (doc, type, bubbles, cancelable, detail) {
621 var event = doc.createEvent('UIEvents'),
622 view = doc.defaultView || window;
624 event.initUIEvent(type, bubbles, cancelable, view, detail);
628 fireEvent: function (target, type, event) {
629 target.dispatchEvent(event);
632 fixTarget: function (target) {
633 // Safari3 doesn't have window.dispatchEvent()
634 if (target == window && !target.dispatchEvent) {
641 } else if (document.createEventObject) { // else if (IE)
642 var crazyIEButtons = { 0: 1, 1: 4, 2: 2 };
645 createHtmlEvent: function (doc, type, bubbles, cancelable) {
646 var event = doc.createEventObject();
647 event.bubbles = bubbles;
648 event.cancelable = cancelable;
652 createMouseEvent: function (doc, type, bubbles, cancelable, detail,
653 clientX, clientY, ctrlKey, altKey, shiftKey, metaKey,
654 button, relatedTarget) {
655 var event = doc.createEventObject();
656 event.bubbles = bubbles;
657 event.cancelable = cancelable;
658 event.detail = detail;
659 event.screenX = clientX;
660 event.screenY = clientY;
661 event.clientX = clientX;
662 event.clientY = clientY;
663 event.ctrlKey = ctrlKey;
664 event.altKey = altKey;
665 event.shiftKey = shiftKey;
666 event.metaKey = metaKey;
667 event.button = crazyIEButtons[button] || button;
668 event.relatedTarget = relatedTarget; // cannot assign to/fromElement
672 createUIEvent: function (doc, type, bubbles, cancelable, detail) {
673 var event = doc.createEventObject();
674 event.bubbles = bubbles;
675 event.cancelable = cancelable;
679 fireEvent: function (target, type, event) {
680 target.fireEvent('on' + type, event);
683 fixTarget: function (target) {
684 if (target == document) {
685 // IE6,IE7 thinks window==document and doesn't have window.fireEvent()
686 // IE6,IE7 cannot properly call document.fireEvent()
687 return document.documentElement;
699 load: [false, false],
700 unload: [false, false],
701 select: [true, false],
702 change: [true, false],
703 submit: [true, true],
704 reset: [true, false],
705 resize: [true, false],
706 scroll: [true, false]
708 function (name, value) {
709 var bubbles = value[0], cancelable = value[1];
710 dispatchers[name] = function (targetEl, srcEvent) {
711 var e = API.createHtmlEvent(name, bubbles, cancelable);
712 API.fireEvent(targetEl, name, e);
719 function createMouseEventDispatcher (type, detail) {
720 var cancelable = (type != 'mousemove');
721 return function (targetEl, srcEvent) {
722 var xy = srcEvent.getXY(),
723 e = API.createMouseEvent(targetEl.ownerDocument, type, true, cancelable,
724 detail, xy[0], xy[1], srcEvent.ctrlKey, srcEvent.altKey,
725 srcEvent.shiftKey, srcEvent.metaKey, srcEvent.button,
726 srcEvent.relatedTarget);
727 API.fireEvent(targetEl, type, e);
731 Ext.each(['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mousemove', 'mouseout'],
732 function (eventName) {
733 dispatchers[eventName] = createMouseEventDispatcher(eventName, 1);
740 focusin: [true, false],
741 focusout: [true, false],
742 activate: [true, true],
743 focus: [false, false],
746 function (name, value) {
747 var bubbles = value[0], cancelable = value[1];
748 dispatchers[name] = function (targetEl, srcEvent) {
749 var e = API.createUIEvent(targetEl.ownerDocument, name, bubbles, cancelable, 1);
750 API.fireEvent(targetEl, name, e);
756 // not even sure what ancient browsers fall into this category...
758 dispatchers = {}; // never mind all those we just built :P
761 fixTarget: function (t) {
767 function cannotInject (target, srcEvent) {
769 // TODO log something
773 return function (target) {
775 dispatcher = dispatchers[me.type] || cannotInject,
776 t = target ? (target.dom || target) : me.getTarget();
778 t = API.fixTarget(t);
781 }() // call to produce method
785 Ext.EventObject = new Ext.EventObjectImpl();