/ext-4.0.7/src/core/src/EventObject.js

https://bitbucket.org/srogerf/javascript · JavaScript · 883 lines · 468 code · 77 blank · 338 comment · 80 complexity · a30f801da6b45b07c95e9ca2f16e4709 MD5 · raw file

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