1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-EventObject'>/**
2 </span> * @class Ext.EventObject
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.
11 function handleClick(e, t){ // e is not a standard event object, it is a Ext.EventObject
13 var target = e.getTarget(); // same as t (the target HTMLElement)
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
23 // other methods to do the same:
24 Ext.EventManager.on("myDiv", 'click', handleClick);
25 Ext.EventManager.addListener("myDiv", 'click', handleClick);
30 Ext.define('Ext.EventObjectImpl', {
31 uses: ['Ext.util.Point'],
33 <span id='Ext-EventObject-property-BACKSPACE'> /** Key constant @type Number */
35 <span id='Ext-EventObject-property-TAB'> /** Key constant @type Number */
37 <span id='Ext-EventObject-property-NUM_CENTER'> /** Key constant @type Number */
38 </span> NUM_CENTER: 12,
39 <span id='Ext-EventObject-property-ENTER'> /** Key constant @type Number */
41 <span id='Ext-EventObject-property-RETURN'> /** Key constant @type Number */
43 <span id='Ext-EventObject-property-SHIFT'> /** Key constant @type Number */
45 <span id='Ext-EventObject-property-CTRL'> /** Key constant @type Number */
47 <span id='Ext-EventObject-property-ALT'> /** Key constant @type Number */
49 <span id='Ext-EventObject-property-PAUSE'> /** Key constant @type Number */
51 <span id='Ext-EventObject-property-CAPS_LOCK'> /** Key constant @type Number */
52 </span> CAPS_LOCK: 20,
53 <span id='Ext-EventObject-property-ESC'> /** Key constant @type Number */
55 <span id='Ext-EventObject-property-SPACE'> /** Key constant @type Number */
57 <span id='Ext-EventObject-property-PAGE_UP'> /** Key constant @type Number */
59 <span id='Ext-EventObject-property-PAGE_DOWN'> /** Key constant @type Number */
60 </span> PAGE_DOWN: 34,
61 <span id='Ext-EventObject-property-END'> /** Key constant @type Number */
63 <span id='Ext-EventObject-property-HOME'> /** Key constant @type Number */
65 <span id='Ext-EventObject-property-LEFT'> /** Key constant @type Number */
67 <span id='Ext-EventObject-property-UP'> /** Key constant @type Number */
69 <span id='Ext-EventObject-property-RIGHT'> /** Key constant @type Number */
71 <span id='Ext-EventObject-property-DOWN'> /** Key constant @type Number */
73 <span id='Ext-EventObject-property-PRINT_SCREEN'> /** Key constant @type Number */
74 </span> PRINT_SCREEN: 44,
75 <span id='Ext-EventObject-property-INSERT'> /** Key constant @type Number */
77 <span id='Ext-EventObject-property-DELETE'> /** Key constant @type Number */
79 <span id='Ext-EventObject-property-ZERO'> /** Key constant @type Number */
81 <span id='Ext-EventObject-property-ONE'> /** Key constant @type Number */
83 <span id='Ext-EventObject-property-TWO'> /** Key constant @type Number */
85 <span id='Ext-EventObject-property-THREE'> /** Key constant @type Number */
87 <span id='Ext-EventObject-property-FOUR'> /** Key constant @type Number */
89 <span id='Ext-EventObject-property-FIVE'> /** Key constant @type Number */
91 <span id='Ext-EventObject-property-SIX'> /** Key constant @type Number */
93 <span id='Ext-EventObject-property-SEVEN'> /** Key constant @type Number */
95 <span id='Ext-EventObject-property-EIGHT'> /** Key constant @type Number */
97 <span id='Ext-EventObject-property-NINE'> /** Key constant @type Number */
99 <span id='Ext-EventObject-property-A'> /** Key constant @type Number */
101 <span id='Ext-EventObject-property-B'> /** Key constant @type Number */
103 <span id='Ext-EventObject-property-C'> /** Key constant @type Number */
105 <span id='Ext-EventObject-property-D'> /** Key constant @type Number */
107 <span id='Ext-EventObject-property-E'> /** Key constant @type Number */
109 <span id='Ext-EventObject-property-F'> /** Key constant @type Number */
111 <span id='Ext-EventObject-property-G'> /** Key constant @type Number */
113 <span id='Ext-EventObject-property-H'> /** Key constant @type Number */
115 <span id='Ext-EventObject-property-I'> /** Key constant @type Number */
117 <span id='Ext-EventObject-property-J'> /** Key constant @type Number */
119 <span id='Ext-EventObject-property-K'> /** Key constant @type Number */
121 <span id='Ext-EventObject-property-L'> /** Key constant @type Number */
123 <span id='Ext-EventObject-property-M'> /** Key constant @type Number */
125 <span id='Ext-EventObject-property-N'> /** Key constant @type Number */
127 <span id='Ext-EventObject-property-O'> /** Key constant @type Number */
129 <span id='Ext-EventObject-property-P'> /** Key constant @type Number */
131 <span id='Ext-EventObject-property-Q'> /** Key constant @type Number */
133 <span id='Ext-EventObject-property-R'> /** Key constant @type Number */
135 <span id='Ext-EventObject-property-S'> /** Key constant @type Number */
137 <span id='Ext-EventObject-property-T'> /** Key constant @type Number */
139 <span id='Ext-EventObject-property-U'> /** Key constant @type Number */
141 <span id='Ext-EventObject-property-V'> /** Key constant @type Number */
143 <span id='Ext-EventObject-property-W'> /** Key constant @type Number */
145 <span id='Ext-EventObject-property-X'> /** Key constant @type Number */
147 <span id='Ext-EventObject-property-Y'> /** Key constant @type Number */
149 <span id='Ext-EventObject-property-Z'> /** Key constant @type Number */
151 <span id='Ext-EventObject-property-CONTEXT_MENU'> /** Key constant @type Number */
152 </span> CONTEXT_MENU: 93,
153 <span id='Ext-EventObject-property-NUM_ZERO'> /** Key constant @type Number */
154 </span> NUM_ZERO: 96,
155 <span id='Ext-EventObject-property-NUM_ONE'> /** Key constant @type Number */
157 <span id='Ext-EventObject-property-NUM_TWO'> /** Key constant @type Number */
159 <span id='Ext-EventObject-property-NUM_THREE'> /** Key constant @type Number */
160 </span> NUM_THREE: 99,
161 <span id='Ext-EventObject-property-NUM_FOUR'> /** Key constant @type Number */
162 </span> NUM_FOUR: 100,
163 <span id='Ext-EventObject-property-NUM_FIVE'> /** Key constant @type Number */
164 </span> NUM_FIVE: 101,
165 <span id='Ext-EventObject-property-NUM_SIX'> /** Key constant @type Number */
166 </span> NUM_SIX: 102,
167 <span id='Ext-EventObject-property-NUM_SEVEN'> /** Key constant @type Number */
168 </span> NUM_SEVEN: 103,
169 <span id='Ext-EventObject-property-NUM_EIGHT'> /** Key constant @type Number */
170 </span> NUM_EIGHT: 104,
171 <span id='Ext-EventObject-property-NUM_NINE'> /** Key constant @type Number */
172 </span> NUM_NINE: 105,
173 <span id='Ext-EventObject-property-NUM_MULTIPLY'> /** Key constant @type Number */
174 </span> NUM_MULTIPLY: 106,
175 <span id='Ext-EventObject-property-NUM_PLUS'> /** Key constant @type Number */
176 </span> NUM_PLUS: 107,
177 <span id='Ext-EventObject-property-NUM_MINUS'> /** Key constant @type Number */
178 </span> NUM_MINUS: 109,
179 <span id='Ext-EventObject-property-NUM_PERIOD'> /** Key constant @type Number */
180 </span> NUM_PERIOD: 110,
181 <span id='Ext-EventObject-property-NUM_DIVISION'> /** Key constant @type Number */
182 </span> NUM_DIVISION: 111,
183 <span id='Ext-EventObject-property-F1'> /** Key constant @type Number */
185 <span id='Ext-EventObject-property-F2'> /** Key constant @type Number */
187 <span id='Ext-EventObject-property-F3'> /** Key constant @type Number */
189 <span id='Ext-EventObject-property-F4'> /** Key constant @type Number */
191 <span id='Ext-EventObject-property-F5'> /** Key constant @type Number */
193 <span id='Ext-EventObject-property-F6'> /** Key constant @type Number */
195 <span id='Ext-EventObject-property-F7'> /** Key constant @type Number */
197 <span id='Ext-EventObject-property-F8'> /** Key constant @type Number */
199 <span id='Ext-EventObject-property-F9'> /** Key constant @type Number */
201 <span id='Ext-EventObject-property-F10'> /** Key constant @type Number */
203 <span id='Ext-EventObject-property-F11'> /** Key constant @type Number */
205 <span id='Ext-EventObject-property-F12'> /** Key constant @type Number */
208 <span id='Ext-EventObject-property-clickRe'> /**
209 </span> * Simple click regex
212 clickRe: /(dbl)?click/,
213 // safari keypress events for special keys return bad keycodes
220 63276: 33, // page up
221 63277: 34, // page down
226 // normalize button clicks, don't see any way to feature detect this.
237 constructor: function(event, freezeEvent){
239 this.setEvent(event.browserEvent || event, freezeEvent);
243 setEvent: function(event, freezeEvent){
244 var me = this, button, options;
246 if (event == me || (event && event.browserEvent)) { // already wrapped
249 me.browserEvent = event;
252 button = event.button ? me.btnMap[event.button] : (event.which ? event.which - 1 : -1);
253 if (me.clickRe.test(event.type) && button == -1) {
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)
284 Ext.apply(me, options);
288 <span id='Ext-EventObject-method-stopEvent'> /**
289 </span> * Stop the event (preventDefault and stopPropagation)
291 stopEvent: function(){
292 this.stopPropagation();
293 this.preventDefault();
296 <span id='Ext-EventObject-method-preventDefault'> /**
297 </span> * Prevents the browsers default handling of the event.
299 preventDefault: function(){
300 if (this.browserEvent) {
301 Ext.EventManager.preventDefault(this.browserEvent);
305 <span id='Ext-EventObject-method-stopPropagation'> /**
306 </span> * Cancels bubbling of the event.
308 stopPropagation: function(){
309 var browserEvent = this.browserEvent;
312 if (browserEvent.type == 'mousedown') {
313 Ext.EventManager.stoppedMouseDownEvent.fire(this);
315 Ext.EventManager.stopPropagation(browserEvent);
319 <span id='Ext-EventObject-method-getCharCode'> /**
320 </span> * Gets the character code for the event.
323 getCharCode: function(){
324 return this.charCode || this.keyCode;
327 <span id='Ext-EventObject-method-getKey'> /**
328 </span> * Returns a normalized keyCode for the event.
329 * @return {Number} The key code
332 return this.normalizeKey(this.keyCode || this.charCode);
335 <span id='Ext-EventObject-method-normalizeKey'> /**
336 </span> * Normalize key codes across browsers
338 * @param {Number} key The key code
339 * @return {Number} The normalized code
341 normalizeKey: function(key){
342 // can't feature detect this
343 return Ext.isWebKit ? (this.safariKeys[key] || key) : key;
346 <span id='Ext-EventObject-method-getPageX'> /**
347 </span> * Gets the x coordinate of the event.
349 * @deprecated 4.0 Replaced by {@link #getX}
351 getPageX: function(){
355 <span id='Ext-EventObject-method-getPageY'> /**
356 </span> * Gets the y coordinate of the event.
358 * @deprecated 4.0 Replaced by {@link #getY}
360 getPageY: function(){
364 <span id='Ext-EventObject-method-getX'> /**
365 </span> * Gets the x coordinate of the event.
369 return this.getXY()[0];
372 <span id='Ext-EventObject-method-getY'> /**
373 </span> * Gets the y coordinate of the event.
377 return this.getXY()[1];
380 <span id='Ext-EventObject-method-getXY'> /**
381 </span> * Gets the page coordinates of the event.
382 * @return {Array} The xy values like [x, y]
387 this.xy = Ext.EventManager.getPageXY(this.browserEvent);
392 <span id='Ext-EventObject-method-getTarget'> /**
393 </span> * 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}
399 getTarget : function(selector, maxDepth, returnEl){
401 return Ext.fly(this.target).findParent(selector, maxDepth, returnEl);
403 return returnEl ? Ext.get(this.target) : this.target;
406 <span id='Ext-EventObject-method-getRelatedTarget'> /**
407 </span> * 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}
413 getRelatedTarget : function(selector, maxDepth, returnEl){
415 return Ext.fly(this.relatedTarget).findParent(selector, maxDepth, returnEl);
417 return returnEl ? Ext.get(this.relatedTarget) : this.relatedTarget;
420 <span id='Ext-EventObject-method-getWheelDelta'> /**
421 </span> * Normalizes mouse wheel delta across browsers
422 * @return {Number} The delta
424 getWheelDelta : function(){
425 var event = this.browserEvent,
428 if (event.wheelDelta) { /* IE/Opera. */
429 delta = event.wheelDelta / 120;
430 } else if (event.detail){ /* Mozilla case. */
431 delta = -event.detail / 3;
436 <span id='Ext-EventObject-method-within'> /**
437 </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.
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!');
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!');
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
458 within : function(el, related, allowEl){
460 var t = related ? this.getRelatedTarget() : this.getTarget(),
464 result = Ext.fly(el).contains(t);
465 if (!result && allowEl) {
466 result = t == Ext.getDom(el);
474 <span id='Ext-EventObject-method-isNavKeyPress'> /**
475 </span> * Checks if the key pressed was a "navigation" key
476 * @return {Boolean} True if the press is a navigation keypress
478 isNavKeyPress : function(){
480 k = this.normalizeKey(me.keyCode);
482 return (k >= 33 && k <= 40) || // Page Up/Down, End, Home, Left, Up, Right, Down
488 <span id='Ext-EventObject-method-isSpecialKey'> /**
489 </span> * Checks if the key pressed was a "special" key
490 * @return {Boolean} True if the press is a special keypress
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
501 <span id='Ext-EventObject-method-getPoint'> /**
502 </span> * Returns a point object that consists of the object coordinates.
503 * @return {Ext.util.Point} point
505 getPoint : function(){
506 var xy = this.getXY();
507 return Ext.create('Ext.util.Point', xy[0], xy[1]);
510 <span id='Ext-EventObject-method-hasModifier'> /**
511 </span> * Returns true if the control, meta, shift or alt key was pressed during this event.
514 hasModifier : function(){
515 return this.ctrlKey || this.altKey || this.shiftKey || this.metaKey;
518 <span id='Ext-EventObject-method-injectEvent'> /**
519 </span> * 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>
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>
533 * <p><b>MouseEvents</b></p>
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>
543 * <p><b>UIEvents</b></p>
545 * <li>focusin</li>
546 * <li>focusout</li>
547 * <li>activate</li>
548 * <li>focus</li>
549 * <li>blur</li>
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.
555 injectEvent: function () {
557 dispatchers = {}; // keyed by event type (e.g., 'mousedown')
559 // Good reference: http://developer.yahoo.com/yui/docs/UserAction.js.html
561 // IE9 has createEvent, but this code causes major problems with htmleditor (it
562 // blocks all mouse events and maybe more). TODO
564 if (!Ext.isIE && document.createEvent) { // if (DOM compliant)
566 createHtmlEvent: function (doc, type, bubbles, cancelable) {
567 var event = doc.createEvent('HTMLEvents');
569 event.initEvent(type, bubbles, cancelable);
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;
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);
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;
603 createUIEvent: function (doc, type, bubbles, cancelable, detail) {
604 var event = doc.createEvent('UIEvents'),
605 view = doc.defaultView || window;
607 event.initUIEvent(type, bubbles, cancelable, view, detail);
611 fireEvent: function (target, type, event) {
612 target.dispatchEvent(event);
615 fixTarget: function (target) {
616 // Safari3 doesn't have window.dispatchEvent()
617 if (target == window && !target.dispatchEvent) {
624 } else if (document.createEventObject) { // else if (IE)
625 var crazyIEButtons = { 0: 1, 1: 4, 2: 2 };
628 createHtmlEvent: function (doc, type, bubbles, cancelable) {
629 var event = doc.createEventObject();
630 event.bubbles = bubbles;
631 event.cancelable = cancelable;
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
655 createUIEvent: function (doc, type, bubbles, cancelable, detail) {
656 var event = doc.createEventObject();
657 event.bubbles = bubbles;
658 event.cancelable = cancelable;
662 fireEvent: function (target, type, event) {
663 target.fireEvent('on' + type, event);
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;
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]
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);
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);
714 Ext.each(['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mousemove', 'mouseout'],
715 function (eventName) {
716 dispatchers[eventName] = createMouseEventDispatcher(eventName, 1);
723 focusin: [true, false],
724 focusout: [true, false],
725 activate: [true, true],
726 focus: [false, false],
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);
739 // not even sure what ancient browsers fall into this category...
741 dispatchers = {}; // never mind all those we just built :P
744 fixTarget: function (t) {
750 function cannotInject (target, srcEvent) {
752 // TODO log something
756 return function (target) {
758 dispatcher = dispatchers[me.type] || cannotInject,
759 t = target ? (target.dom || target) : me.getTarget();
761 t = API.fixTarget(t);
764 }() // call to produce method
768 Ext.EventObject = new Ext.EventObjectImpl();
772 </pre></pre></body></html>