mxEvent.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. var mxEvent =
  6. {
  7. /**
  8. * Class: mxEvent
  9. *
  10. * Cross-browser DOM event support. For internal event handling,
  11. * <mxEventSource> and the graph event dispatch loop in <mxGraph> are used.
  12. *
  13. * Memory Leaks:
  14. *
  15. * Use this class for adding and removing listeners to/from DOM nodes. The
  16. * <removeAllListeners> function is provided to remove all listeners that
  17. * have been added using <addListener>. The function should be invoked when
  18. * the last reference is removed in the JavaScript code, typically when the
  19. * referenced DOM node is removed from the DOM.
  20. *
  21. * Function: addListener
  22. *
  23. * Binds the function to the specified event on the given element. Use
  24. * <mxUtils.bind> in order to bind the "this" keyword inside the function
  25. * to a given execution scope.
  26. */
  27. addListener: function()
  28. {
  29. var updateListenerList = function(element, eventName, funct)
  30. {
  31. if (element.mxListenerList == null)
  32. {
  33. element.mxListenerList = [];
  34. }
  35. var entry = {name: eventName, f: funct};
  36. element.mxListenerList.push(entry);
  37. };
  38. if (window.addEventListener)
  39. {
  40. return function(element, eventName, funct)
  41. {
  42. element.addEventListener(eventName, funct, false);
  43. updateListenerList(element, eventName, funct);
  44. };
  45. }
  46. else
  47. {
  48. return function(element, eventName, funct)
  49. {
  50. element.attachEvent('on' + eventName, funct);
  51. updateListenerList(element, eventName, funct);
  52. };
  53. }
  54. }(),
  55. /**
  56. * Function: removeListener
  57. *
  58. * Removes the specified listener from the given element.
  59. */
  60. removeListener: function()
  61. {
  62. var updateListener = function(element, eventName, funct)
  63. {
  64. if (element.mxListenerList != null)
  65. {
  66. var listenerCount = element.mxListenerList.length;
  67. for (var i = 0; i < listenerCount; i++)
  68. {
  69. var entry = element.mxListenerList[i];
  70. if (entry.f == funct)
  71. {
  72. element.mxListenerList.splice(i, 1);
  73. break;
  74. }
  75. }
  76. if (element.mxListenerList.length == 0)
  77. {
  78. element.mxListenerList = null;
  79. }
  80. }
  81. };
  82. if (window.removeEventListener)
  83. {
  84. return function(element, eventName, funct)
  85. {
  86. element.removeEventListener(eventName, funct, false);
  87. updateListener(element, eventName, funct);
  88. };
  89. }
  90. else
  91. {
  92. return function(element, eventName, funct)
  93. {
  94. element.detachEvent('on' + eventName, funct);
  95. updateListener(element, eventName, funct);
  96. };
  97. }
  98. }(),
  99. /**
  100. * Function: removeAllListeners
  101. *
  102. * Removes all listeners from the given element.
  103. */
  104. removeAllListeners: function(element)
  105. {
  106. var list = element.mxListenerList;
  107. if (list != null)
  108. {
  109. while (list.length > 0)
  110. {
  111. var entry = list[0];
  112. mxEvent.removeListener(element, entry.name, entry.f);
  113. }
  114. }
  115. },
  116. /**
  117. * Function: addGestureListeners
  118. *
  119. * Adds the given listeners for touch, mouse and/or pointer events. If
  120. * <mxClient.IS_POINTER> is true then pointer events will be registered,
  121. * else the respective mouse events will be registered. If <mxClient.IS_POINTER>
  122. * is false and <mxClient.IS_TOUCH> is true then the respective touch events
  123. * will be registered as well as the mouse events.
  124. */
  125. addGestureListeners: function(node, startListener, moveListener, endListener)
  126. {
  127. if (startListener != null)
  128. {
  129. mxEvent.addListener(node, (mxClient.IS_POINTER) ? 'pointerdown' : 'mousedown', startListener);
  130. }
  131. if (moveListener != null)
  132. {
  133. mxEvent.addListener(node, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', moveListener);
  134. }
  135. if (endListener != null)
  136. {
  137. mxEvent.addListener(node, (mxClient.IS_POINTER) ? 'pointerup' : 'mouseup', endListener);
  138. }
  139. if (!mxClient.IS_POINTER && mxClient.IS_TOUCH)
  140. {
  141. if (startListener != null)
  142. {
  143. mxEvent.addListener(node, 'touchstart', startListener);
  144. }
  145. if (moveListener != null)
  146. {
  147. mxEvent.addListener(node, 'touchmove', moveListener);
  148. }
  149. if (endListener != null)
  150. {
  151. mxEvent.addListener(node, 'touchend', endListener);
  152. }
  153. }
  154. },
  155. /**
  156. * Function: removeGestureListeners
  157. *
  158. * Removes the given listeners from mousedown, mousemove, mouseup and the
  159. * respective touch events if <mxClient.IS_TOUCH> is true.
  160. */
  161. removeGestureListeners: function(node, startListener, moveListener, endListener)
  162. {
  163. if (startListener != null)
  164. {
  165. mxEvent.removeListener(node, (mxClient.IS_POINTER) ? 'pointerdown' : 'mousedown', startListener);
  166. }
  167. if (moveListener != null)
  168. {
  169. mxEvent.removeListener(node, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', moveListener);
  170. }
  171. if (endListener != null)
  172. {
  173. mxEvent.removeListener(node, (mxClient.IS_POINTER) ? 'pointerup' : 'mouseup', endListener);
  174. }
  175. if (!mxClient.IS_POINTER && mxClient.IS_TOUCH)
  176. {
  177. if (startListener != null)
  178. {
  179. mxEvent.removeListener(node, 'touchstart', startListener);
  180. }
  181. if (moveListener != null)
  182. {
  183. mxEvent.removeListener(node, 'touchmove', moveListener);
  184. }
  185. if (endListener != null)
  186. {
  187. mxEvent.removeListener(node, 'touchend', endListener);
  188. }
  189. }
  190. },
  191. /**
  192. * Function: redirectMouseEvents
  193. *
  194. * Redirects the mouse events from the given DOM node to the graph dispatch
  195. * loop using the event and given state as event arguments. State can
  196. * either be an instance of <mxCellState> or a function that returns an
  197. * <mxCellState>. The down, move, up and dblClick arguments are optional
  198. * functions that take the trigger event as arguments and replace the
  199. * default behaviour.
  200. */
  201. redirectMouseEvents: function(node, graph, state, down, move, up, dblClick)
  202. {
  203. var getState = function(evt)
  204. {
  205. return (typeof(state) == 'function') ? state(evt) : state;
  206. };
  207. mxEvent.addGestureListeners(node, function (evt)
  208. {
  209. if (down != null)
  210. {
  211. down(evt);
  212. }
  213. else if (!mxEvent.isConsumed(evt))
  214. {
  215. graph.fireMouseEvent(mxEvent.MOUSE_DOWN, new mxMouseEvent(evt, getState(evt)));
  216. }
  217. },
  218. function (evt)
  219. {
  220. if (move != null)
  221. {
  222. move(evt);
  223. }
  224. else if (!mxEvent.isConsumed(evt))
  225. {
  226. graph.fireMouseEvent(mxEvent.MOUSE_MOVE, new mxMouseEvent(evt, getState(evt)));
  227. }
  228. },
  229. function (evt)
  230. {
  231. if (up != null)
  232. {
  233. up(evt);
  234. }
  235. else if (!mxEvent.isConsumed(evt))
  236. {
  237. graph.fireMouseEvent(mxEvent.MOUSE_UP, new mxMouseEvent(evt, getState(evt)));
  238. }
  239. });
  240. mxEvent.addListener(node, 'dblclick', function (evt)
  241. {
  242. if (dblClick != null)
  243. {
  244. dblClick(evt);
  245. }
  246. else if (!mxEvent.isConsumed(evt))
  247. {
  248. var tmp = getState(evt);
  249. graph.dblClick(evt, (tmp != null) ? tmp.cell : null);
  250. }
  251. });
  252. },
  253. /**
  254. * Function: release
  255. *
  256. * Removes the known listeners from the given DOM node and its descendants.
  257. *
  258. * Parameters:
  259. *
  260. * element - DOM node to remove the listeners from.
  261. */
  262. release: function(element)
  263. {
  264. try
  265. {
  266. if (element != null)
  267. {
  268. mxEvent.removeAllListeners(element);
  269. var children = element.childNodes;
  270. if (children != null)
  271. {
  272. var childCount = children.length;
  273. for (var i = 0; i < childCount; i += 1)
  274. {
  275. mxEvent.release(children[i]);
  276. }
  277. }
  278. }
  279. }
  280. catch (e)
  281. {
  282. // ignores errors as this is typically called in cleanup code
  283. }
  284. },
  285. /**
  286. * Function: addMouseWheelListener
  287. *
  288. * Installs the given function as a handler for mouse wheel events. The
  289. * function has two arguments: the mouse event and a boolean that specifies
  290. * if the wheel was moved up or down.
  291. *
  292. * This has been tested with IE 6 and 7, Firefox (all versions), Opera and
  293. * Safari. It does currently not work on Safari for Mac.
  294. *
  295. * Example:
  296. *
  297. * (code)
  298. * mxEvent.addMouseWheelListener(function (evt, up, pinch)
  299. * {
  300. * mxLog.show();
  301. * mxLog.debug('mouseWheel: up='+up);
  302. * });
  303. *(end)
  304. *
  305. * Parameters:
  306. *
  307. * funct - Handler function that takes the event argument, a boolean argument
  308. * for the mousewheel direction and a boolean to specify if the underlying
  309. * event was a pinch gesture on a touch device.
  310. * target - Target for installing the listener in Google Chrome. See
  311. * https://www.chromestatus.com/features/6662647093133312.
  312. */
  313. addMouseWheelListener: function(funct, target)
  314. {
  315. if (funct != null)
  316. {
  317. var wheelHandler = function(evt)
  318. {
  319. // IE does not give an event object but the
  320. // global event object is the mousewheel event
  321. // at this point in time.
  322. if (evt == null)
  323. {
  324. evt = window.event;
  325. }
  326. //To prevent window zoom on trackpad pinch
  327. if (evt.ctrlKey)
  328. {
  329. evt.preventDefault();
  330. }
  331. var delta = -evt.deltaY;
  332. // Handles the event using the given function
  333. if (Math.abs(evt.deltaX) > 0.5 || Math.abs(evt.deltaY) > 0.5)
  334. {
  335. funct(evt, (evt.deltaY == 0) ? -evt.deltaX > 0 : -evt.deltaY > 0);
  336. }
  337. };
  338. target = target != null ? target : window;
  339. if (mxClient.IS_SF && !mxClient.IS_TOUCH)
  340. {
  341. var scale = 1;
  342. mxEvent.addListener(target, 'gesturestart', function(evt)
  343. {
  344. mxEvent.consume(evt);
  345. scale = 1;
  346. });
  347. mxEvent.addListener(target, 'gesturechange', function(evt)
  348. {
  349. mxEvent.consume(evt);
  350. var diff = scale - evt.scale;
  351. if (Math.abs(diff) > 0.2)
  352. {
  353. funct(evt, diff < 0, true);
  354. scale = evt.scale;
  355. }
  356. });
  357. mxEvent.addListener(target, 'gestureend', function(evt)
  358. {
  359. mxEvent.consume(evt);
  360. });
  361. }
  362. else
  363. {
  364. var evtCache = [];
  365. var dx0 = 0;
  366. var dy0 = 0;
  367. // Adds basic listeners for graph event dispatching
  368. mxEvent.addGestureListeners(target, mxUtils.bind(this, function(evt)
  369. {
  370. if (!mxEvent.isMouseEvent(evt) && evt.pointerId != null)
  371. {
  372. evtCache.push(evt);
  373. }
  374. }),
  375. mxUtils.bind(this, function(evt)
  376. {
  377. if (!mxEvent.isMouseEvent(evt) && evtCache.length == 2)
  378. {
  379. // Find this event in the cache and update its record with this event
  380. for (var i = 0; i < evtCache.length; i++)
  381. {
  382. if (evt.pointerId == evtCache[i].pointerId)
  383. {
  384. evtCache[i] = evt;
  385. break;
  386. }
  387. }
  388. // Calculate the distance between the two pointers
  389. var dx = Math.abs(evtCache[0].clientX - evtCache[1].clientX);
  390. var dy = Math.abs(evtCache[0].clientY - evtCache[1].clientY);
  391. var tx = Math.abs(dx - dx0);
  392. var ty = Math.abs(dy - dy0);
  393. if (tx > mxEvent.PINCH_THRESHOLD || ty > mxEvent.PINCH_THRESHOLD)
  394. {
  395. var cx = evtCache[0].clientX + (evtCache[1].clientX - evtCache[0].clientX) / 2;
  396. var cy = evtCache[0].clientY + (evtCache[1].clientY - evtCache[0].clientY) / 2;
  397. funct(evtCache[0], (tx > ty) ? dx > dx0 : dy > dy0, true, cx, cy);
  398. // Cache the distance for the next move event
  399. dx0 = dx;
  400. dy0 = dy;
  401. }
  402. }
  403. }),
  404. mxUtils.bind(this, function(evt)
  405. {
  406. evtCache = [];
  407. dx0 = 0;
  408. dy0 = 0;
  409. }));
  410. }
  411. mxEvent.addListener(target, 'wheel', wheelHandler);
  412. }
  413. },
  414. /**
  415. * Function: disableContextMenu
  416. *
  417. * Disables the context menu for the given element.
  418. */
  419. disableContextMenu: function(element)
  420. {
  421. mxEvent.addListener(element, 'contextmenu', function(evt)
  422. {
  423. if (evt.preventDefault)
  424. {
  425. evt.preventDefault();
  426. }
  427. return false;
  428. });
  429. },
  430. /**
  431. * Function: getSource
  432. *
  433. * Returns the event's target or srcElement depending on the browser.
  434. */
  435. getSource: function(evt)
  436. {
  437. return (evt.srcElement != null) ? evt.srcElement : evt.target;
  438. },
  439. /**
  440. * Function: isConsumed
  441. *
  442. * Returns true if the event has been consumed using <consume>.
  443. */
  444. isConsumed: function(evt)
  445. {
  446. return evt.isConsumed != null && evt.isConsumed;
  447. },
  448. /**
  449. * Function: isTouchEvent
  450. *
  451. * Returns true if the event was generated using a touch device (not a pen or mouse).
  452. */
  453. isTouchEvent: function(evt)
  454. {
  455. return (evt.pointerType != null) ? (evt.pointerType == 'touch' || evt.pointerType ===
  456. evt.MSPOINTER_TYPE_TOUCH) : ((evt.mozInputSource != null) ?
  457. evt.mozInputSource == 5 : evt.type.indexOf('touch') == 0);
  458. },
  459. /**
  460. * Function: isPenEvent
  461. *
  462. * Returns true if the event was generated using a pen (not a touch device or mouse).
  463. */
  464. isPenEvent: function(evt)
  465. {
  466. return (evt.pointerType != null) ? (evt.pointerType == 'pen' || evt.pointerType ===
  467. evt.MSPOINTER_TYPE_PEN) : ((evt.mozInputSource != null) ?
  468. evt.mozInputSource == 2 : evt.type.indexOf('pen') == 0);
  469. },
  470. /**
  471. * Function: isMultiTouchEvent
  472. *
  473. * Returns true if the event was generated using a touch device (not a pen or mouse).
  474. */
  475. isMultiTouchEvent: function(evt)
  476. {
  477. return (evt.type != null && evt.type.indexOf('touch') == 0 && evt.touches != null && evt.touches.length > 1);
  478. },
  479. /**
  480. * Function: isMouseEvent
  481. *
  482. * Returns true if the event was generated using a mouse (not a pen or touch device).
  483. */
  484. isMouseEvent: function(evt)
  485. {
  486. return (evt.pointerType != null) ? (evt.pointerType == 'mouse' || evt.pointerType ===
  487. evt.MSPOINTER_TYPE_MOUSE) : ((evt.mozInputSource != null) ?
  488. evt.mozInputSource == 1 : evt.type.indexOf('mouse') == 0);
  489. },
  490. /**
  491. * Function: isLeftMouseButton
  492. *
  493. * Returns true if the left mouse button is pressed for the given event.
  494. * To check if a button is pressed during a mouseMove you should use the
  495. * <mxGraph.isMouseDown> property. Note that this returns true in Firefox
  496. * for control+left-click on the Mac.
  497. */
  498. isLeftMouseButton: function(evt)
  499. {
  500. // Special case for mousemove and mousedown we check the buttons
  501. // if it exists because which is 0 even if no button is pressed
  502. if ('buttons' in evt && (evt.type == 'mousedown' || evt.type == 'mousemove'))
  503. {
  504. return evt.buttons == 1;
  505. }
  506. else if ('which' in evt)
  507. {
  508. return evt.which === 1;
  509. }
  510. else
  511. {
  512. return evt.button === 1;
  513. }
  514. },
  515. /**
  516. * Function: isMiddleMouseButton
  517. *
  518. * Returns true if the middle mouse button is pressed for the given event.
  519. * To check if a button is pressed during a mouseMove you should use the
  520. * <mxGraph.isMouseDown> property.
  521. */
  522. isMiddleMouseButton: function(evt)
  523. {
  524. if ('which' in evt)
  525. {
  526. return evt.which === 2;
  527. }
  528. else
  529. {
  530. return evt.button === 4;
  531. }
  532. },
  533. /**
  534. * Function: isRightMouseButton
  535. *
  536. * Returns true if the right mouse button was pressed. Note that this
  537. * button might not be available on some systems. For handling a popup
  538. * trigger <isPopupTrigger> should be used.
  539. */
  540. isRightMouseButton: function(evt)
  541. {
  542. if ('which' in evt)
  543. {
  544. return evt.which === 3;
  545. }
  546. else
  547. {
  548. return evt.button === 2;
  549. }
  550. },
  551. /**
  552. * Function: isPopupTrigger
  553. *
  554. * Returns true if the event is a popup trigger. This implementation
  555. * returns true if the right button or the left button and control was
  556. * pressed on a Mac.
  557. */
  558. isPopupTrigger: function(evt)
  559. {
  560. return mxEvent.isRightMouseButton(evt) || (mxClient.IS_MAC && mxEvent.isControlDown(evt) &&
  561. !mxEvent.isShiftDown(evt) && !mxEvent.isMetaDown(evt) && !mxEvent.isAltDown(evt));
  562. },
  563. /**
  564. * Function: isShiftDown
  565. *
  566. * Returns true if the shift key is pressed for the given event.
  567. */
  568. isShiftDown: function(evt)
  569. {
  570. return (evt != null) ? evt.shiftKey : false;
  571. },
  572. /**
  573. * Function: isAltDown
  574. *
  575. * Returns true if the alt key is pressed for the given event.
  576. */
  577. isAltDown: function(evt)
  578. {
  579. return (evt != null) ? evt.altKey : false;
  580. },
  581. /**
  582. * Function: isControlDown
  583. *
  584. * Returns true if the control key is pressed for the given event.
  585. */
  586. isControlDown: function(evt)
  587. {
  588. return (evt != null) ? evt.ctrlKey : false;
  589. },
  590. /**
  591. * Function: isMetaDown
  592. *
  593. * Returns true if the meta key is pressed for the given event.
  594. */
  595. isMetaDown: function(evt)
  596. {
  597. return (evt != null) ? evt.metaKey : false;
  598. },
  599. /**
  600. * Function: getMainEvent
  601. *
  602. * Returns the touch or mouse event that contains the mouse coordinates.
  603. */
  604. getMainEvent: function(e)
  605. {
  606. if ((e.type == 'touchstart' || e.type == 'touchmove') && e.touches != null && e.touches[0] != null)
  607. {
  608. e = e.touches[0];
  609. }
  610. else if (e.type == 'touchend' && e.changedTouches != null && e.changedTouches[0] != null)
  611. {
  612. e = e.changedTouches[0];
  613. }
  614. return e;
  615. },
  616. /**
  617. * Function: getClientX
  618. *
  619. * Returns true if the meta key is pressed for the given event.
  620. */
  621. getClientX: function(e)
  622. {
  623. return mxEvent.getMainEvent(e).clientX;
  624. },
  625. /**
  626. * Function: getClientY
  627. *
  628. * Returns true if the meta key is pressed for the given event.
  629. */
  630. getClientY: function(e)
  631. {
  632. return mxEvent.getMainEvent(e).clientY;
  633. },
  634. /**
  635. * Function: consume
  636. *
  637. * Consumes the given event.
  638. *
  639. * Parameters:
  640. *
  641. * evt - Native event to be consumed.
  642. * preventDefault - Optional boolean to prevent the default for the event.
  643. * Default is true.
  644. * stopPropagation - Option boolean to stop event propagation. Default is
  645. * true.
  646. */
  647. consume: function(evt, preventDefault, stopPropagation)
  648. {
  649. preventDefault = (preventDefault != null) ? preventDefault : true;
  650. stopPropagation = (stopPropagation != null) ? stopPropagation : true;
  651. if (preventDefault)
  652. {
  653. if (evt.preventDefault)
  654. {
  655. if (stopPropagation)
  656. {
  657. evt.stopPropagation();
  658. }
  659. evt.preventDefault();
  660. }
  661. else if (stopPropagation)
  662. {
  663. evt.cancelBubble = true;
  664. }
  665. }
  666. // Opera
  667. evt.isConsumed = true;
  668. // Other browsers
  669. if (!evt.preventDefault)
  670. {
  671. evt.returnValue = false;
  672. }
  673. },
  674. //
  675. // Special handles in mouse events
  676. //
  677. /**
  678. * Variable: LABEL_HANDLE
  679. *
  680. * Index for the label handle in an mxMouseEvent. This should be a negative
  681. * value that does not interfere with any possible handle indices. Default
  682. * is -1.
  683. */
  684. LABEL_HANDLE: -1,
  685. /**
  686. * Variable: ROTATION_HANDLE
  687. *
  688. * Index for the rotation handle in an mxMouseEvent. This should be a
  689. * negative value that does not interfere with any possible handle indices.
  690. * Default is -2.
  691. */
  692. ROTATION_HANDLE: -2,
  693. /**
  694. * Variable: CUSTOM_HANDLE
  695. *
  696. * Start index for the custom handles in an mxMouseEvent. This should be a
  697. * negative value and is the start index which is decremented for each
  698. * custom handle. Default is -100.
  699. */
  700. CUSTOM_HANDLE: -100,
  701. /**
  702. * Variable: VIRTUAL_HANDLE
  703. *
  704. * Start index for the virtual handles in an mxMouseEvent. This should be a
  705. * negative value and is the start index which is decremented for each
  706. * virtual handle. Default is -100000. This assumes that there are no more
  707. * than VIRTUAL_HANDLE - CUSTOM_HANDLE custom handles.
  708. *
  709. */
  710. VIRTUAL_HANDLE: -100000,
  711. //
  712. // Event names
  713. //
  714. /**
  715. * Variable: MOUSE_DOWN
  716. *
  717. * Specifies the event name for mouseDown.
  718. */
  719. MOUSE_DOWN: 'mouseDown',
  720. /**
  721. * Variable: MOUSE_MOVE
  722. *
  723. * Specifies the event name for mouseMove.
  724. */
  725. MOUSE_MOVE: 'mouseMove',
  726. /**
  727. * Variable: MOUSE_UP
  728. *
  729. * Specifies the event name for mouseUp.
  730. */
  731. MOUSE_UP: 'mouseUp',
  732. /**
  733. * Variable: ACTIVATE
  734. *
  735. * Specifies the event name for activate.
  736. */
  737. ACTIVATE: 'activate',
  738. /**
  739. * Variable: RESIZE_START
  740. *
  741. * Specifies the event name for resizeStart.
  742. */
  743. RESIZE_START: 'resizeStart',
  744. /**
  745. * Variable: RESIZE
  746. *
  747. * Specifies the event name for resize.
  748. */
  749. RESIZE: 'resize',
  750. /**
  751. * Variable: RESIZE_END
  752. *
  753. * Specifies the event name for resizeEnd.
  754. */
  755. RESIZE_END: 'resizeEnd',
  756. /**
  757. * Variable: MOVE_START
  758. *
  759. * Specifies the event name for moveStart.
  760. */
  761. MOVE_START: 'moveStart',
  762. /**
  763. * Variable: MOVE
  764. *
  765. * Specifies the event name for move.
  766. */
  767. MOVE: 'move',
  768. /**
  769. * Variable: MOVE_END
  770. *
  771. * Specifies the event name for moveEnd.
  772. */
  773. MOVE_END: 'moveEnd',
  774. /**
  775. * Variable: PAN_START
  776. *
  777. * Specifies the event name for panStart.
  778. */
  779. PAN_START: 'panStart',
  780. /**
  781. * Variable: PAN
  782. *
  783. * Specifies the event name for pan.
  784. */
  785. PAN: 'pan',
  786. /**
  787. * Variable: PAN_END
  788. *
  789. * Specifies the event name for panEnd.
  790. */
  791. PAN_END: 'panEnd',
  792. /**
  793. * Variable: MINIMIZE
  794. *
  795. * Specifies the event name for minimize.
  796. */
  797. MINIMIZE: 'minimize',
  798. /**
  799. * Variable: NORMALIZE
  800. *
  801. * Specifies the event name for normalize.
  802. */
  803. NORMALIZE: 'normalize',
  804. /**
  805. * Variable: MAXIMIZE
  806. *
  807. * Specifies the event name for maximize.
  808. */
  809. MAXIMIZE: 'maximize',
  810. /**
  811. * Variable: HIDE
  812. *
  813. * Specifies the event name for hide.
  814. */
  815. HIDE: 'hide',
  816. /**
  817. * Variable: SHOW
  818. *
  819. * Specifies the event name for show.
  820. */
  821. SHOW: 'show',
  822. /**
  823. * Variable: CLOSE
  824. *
  825. * Specifies the event name for close.
  826. */
  827. CLOSE: 'close',
  828. /**
  829. * Variable: DESTROY
  830. *
  831. * Specifies the event name for destroy.
  832. */
  833. DESTROY: 'destroy',
  834. /**
  835. * Variable: REFRESH
  836. *
  837. * Specifies the event name for refresh.
  838. */
  839. REFRESH: 'refresh',
  840. /**
  841. * Variable: SIZE
  842. *
  843. * Specifies the event name for size.
  844. */
  845. SIZE: 'size',
  846. /**
  847. * Variable: SELECT
  848. *
  849. * Specifies the event name for select.
  850. */
  851. SELECT: 'select',
  852. /**
  853. * Variable: FIRED
  854. *
  855. * Specifies the event name for fired.
  856. */
  857. FIRED: 'fired',
  858. /**
  859. * Variable: FIRE_MOUSE_EVENT
  860. *
  861. * Specifies the event name for fireMouseEvent.
  862. */
  863. FIRE_MOUSE_EVENT: 'fireMouseEvent',
  864. /**
  865. * Variable: GESTURE
  866. *
  867. * Specifies the event name for gesture.
  868. */
  869. GESTURE: 'gesture',
  870. /**
  871. * Variable: TAP_AND_HOLD
  872. *
  873. * Specifies the event name for tapAndHold.
  874. */
  875. TAP_AND_HOLD: 'tapAndHold',
  876. /**
  877. * Variable: GET
  878. *
  879. * Specifies the event name for get.
  880. */
  881. GET: 'get',
  882. /**
  883. * Variable: RECEIVE
  884. *
  885. * Specifies the event name for receive.
  886. */
  887. RECEIVE: 'receive',
  888. /**
  889. * Variable: CONNECT
  890. *
  891. * Specifies the event name for connect.
  892. */
  893. CONNECT: 'connect',
  894. /**
  895. * Variable: DISCONNECT
  896. *
  897. * Specifies the event name for disconnect.
  898. */
  899. DISCONNECT: 'disconnect',
  900. /**
  901. * Variable: SUSPEND
  902. *
  903. * Specifies the event name for suspend.
  904. */
  905. SUSPEND: 'suspend',
  906. /**
  907. * Variable: RESUME
  908. *
  909. * Specifies the event name for suspend.
  910. */
  911. RESUME: 'resume',
  912. /**
  913. * Variable: MARK
  914. *
  915. * Specifies the event name for mark.
  916. */
  917. MARK: 'mark',
  918. /**
  919. * Variable: ROOT
  920. *
  921. * Specifies the event name for root.
  922. */
  923. ROOT: 'root',
  924. /**
  925. * Variable: POST
  926. *
  927. * Specifies the event name for post.
  928. */
  929. POST: 'post',
  930. /**
  931. * Variable: OPEN
  932. *
  933. * Specifies the event name for open.
  934. */
  935. OPEN: 'open',
  936. /**
  937. * Variable: SAVE
  938. *
  939. * Specifies the event name for open.
  940. */
  941. SAVE: 'save',
  942. /**
  943. * Variable: BEFORE_ADD_VERTEX
  944. *
  945. * Specifies the event name for beforeAddVertex.
  946. */
  947. BEFORE_ADD_VERTEX: 'beforeAddVertex',
  948. /**
  949. * Variable: ADD_VERTEX
  950. *
  951. * Specifies the event name for addVertex.
  952. */
  953. ADD_VERTEX: 'addVertex',
  954. /**
  955. * Variable: AFTER_ADD_VERTEX
  956. *
  957. * Specifies the event name for afterAddVertex.
  958. */
  959. AFTER_ADD_VERTEX: 'afterAddVertex',
  960. /**
  961. * Variable: DONE
  962. *
  963. * Specifies the event name for done.
  964. */
  965. DONE: 'done',
  966. /**
  967. * Variable: EXECUTE
  968. *
  969. * Specifies the event name for execute.
  970. */
  971. EXECUTE: 'execute',
  972. /**
  973. * Variable: EXECUTED
  974. *
  975. * Specifies the event name for executed.
  976. */
  977. EXECUTED: 'executed',
  978. /**
  979. * Variable: BEGIN_UPDATE
  980. *
  981. * Specifies the event name for beginUpdate.
  982. */
  983. BEGIN_UPDATE: 'beginUpdate',
  984. /**
  985. * Variable: START_EDIT
  986. *
  987. * Specifies the event name for startEdit.
  988. */
  989. START_EDIT: 'startEdit',
  990. /**
  991. * Variable: END_UPDATE
  992. *
  993. * Specifies the event name for endUpdate.
  994. */
  995. END_UPDATE: 'endUpdate',
  996. /**
  997. * Variable: END_EDIT
  998. *
  999. * Specifies the event name for endEdit.
  1000. */
  1001. END_EDIT: 'endEdit',
  1002. /**
  1003. * Variable: BEFORE_UNDO
  1004. *
  1005. * Specifies the event name for beforeUndo.
  1006. */
  1007. BEFORE_UNDO: 'beforeUndo',
  1008. /**
  1009. * Variable: UNDO
  1010. *
  1011. * Specifies the event name for undo.
  1012. */
  1013. UNDO: 'undo',
  1014. /**
  1015. * Variable: REDO
  1016. *
  1017. * Specifies the event name for redo.
  1018. */
  1019. REDO: 'redo',
  1020. /**
  1021. * Variable: CHANGE
  1022. *
  1023. * Specifies the event name for change.
  1024. */
  1025. CHANGE: 'change',
  1026. /**
  1027. * Variable: NOTIFY
  1028. *
  1029. * Specifies the event name for notify.
  1030. */
  1031. NOTIFY: 'notify',
  1032. /**
  1033. * Variable: LAYOUT_CELLS
  1034. *
  1035. * Specifies the event name for layoutCells.
  1036. */
  1037. LAYOUT_CELLS: 'layoutCells',
  1038. /**
  1039. * Variable: CLICK
  1040. *
  1041. * Specifies the event name for click.
  1042. */
  1043. CLICK: 'click',
  1044. /**
  1045. * Variable: SCALE
  1046. *
  1047. * Specifies the event name for scale.
  1048. */
  1049. SCALE: 'scale',
  1050. /**
  1051. * Variable: TRANSLATE
  1052. *
  1053. * Specifies the event name for translate.
  1054. */
  1055. TRANSLATE: 'translate',
  1056. /**
  1057. * Variable: SCALE_AND_TRANSLATE
  1058. *
  1059. * Specifies the event name for scaleAndTranslate.
  1060. */
  1061. SCALE_AND_TRANSLATE: 'scaleAndTranslate',
  1062. /**
  1063. * Variable: UP
  1064. *
  1065. * Specifies the event name for up.
  1066. */
  1067. UP: 'up',
  1068. /**
  1069. * Variable: DOWN
  1070. *
  1071. * Specifies the event name for down.
  1072. */
  1073. DOWN: 'down',
  1074. /**
  1075. * Variable: ADD
  1076. *
  1077. * Specifies the event name for add.
  1078. */
  1079. ADD: 'add',
  1080. /**
  1081. * Variable: REMOVE
  1082. *
  1083. * Specifies the event name for remove.
  1084. */
  1085. REMOVE: 'remove',
  1086. /**
  1087. * Variable: CLEAR
  1088. *
  1089. * Specifies the event name for clear.
  1090. */
  1091. CLEAR: 'clear',
  1092. /**
  1093. * Variable: ADD_CELLS
  1094. *
  1095. * Specifies the event name for addCells.
  1096. */
  1097. ADD_CELLS: 'addCells',
  1098. /**
  1099. * Variable: CELLS_ADDED
  1100. *
  1101. * Specifies the event name for cellsAdded.
  1102. */
  1103. CELLS_ADDED: 'cellsAdded',
  1104. /**
  1105. * Variable: MOVE_CELLS
  1106. *
  1107. * Specifies the event name for moveCells.
  1108. */
  1109. MOVE_CELLS: 'moveCells',
  1110. /**
  1111. * Variable: CELLS_MOVED
  1112. *
  1113. * Specifies the event name for cellsMoved.
  1114. */
  1115. CELLS_MOVED: 'cellsMoved',
  1116. /**
  1117. * Variable: RESIZE_CELLS
  1118. *
  1119. * Specifies the event name for resizeCells.
  1120. */
  1121. RESIZE_CELLS: 'resizeCells',
  1122. /**
  1123. * Variable: CELLS_RESIZED
  1124. *
  1125. * Specifies the event name for cellsResized.
  1126. */
  1127. CELLS_RESIZED: 'cellsResized',
  1128. /**
  1129. * Variable: TOGGLE_CELLS
  1130. *
  1131. * Specifies the event name for toggleCells.
  1132. */
  1133. TOGGLE_CELLS: 'toggleCells',
  1134. /**
  1135. * Variable: CELLS_TOGGLED
  1136. *
  1137. * Specifies the event name for cellsToggled.
  1138. */
  1139. CELLS_TOGGLED: 'cellsToggled',
  1140. /**
  1141. * Variable: ORDER_CELLS
  1142. *
  1143. * Specifies the event name for orderCells.
  1144. */
  1145. ORDER_CELLS: 'orderCells',
  1146. /**
  1147. * Variable: CELLS_ORDERED
  1148. *
  1149. * Specifies the event name for cellsOrdered.
  1150. */
  1151. CELLS_ORDERED: 'cellsOrdered',
  1152. /**
  1153. * Variable: REMOVE_CELLS
  1154. *
  1155. * Specifies the event name for removeCells.
  1156. */
  1157. REMOVE_CELLS: 'removeCells',
  1158. /**
  1159. * Variable: CELLS_REMOVED
  1160. *
  1161. * Specifies the event name for cellsRemoved.
  1162. */
  1163. CELLS_REMOVED: 'cellsRemoved',
  1164. /**
  1165. * Variable: GROUP_CELLS
  1166. *
  1167. * Specifies the event name for groupCells.
  1168. */
  1169. GROUP_CELLS: 'groupCells',
  1170. /**
  1171. * Variable: UNGROUP_CELLS
  1172. *
  1173. * Specifies the event name for ungroupCells.
  1174. */
  1175. UNGROUP_CELLS: 'ungroupCells',
  1176. /**
  1177. * Variable: REMOVE_CELLS_FROM_PARENT
  1178. *
  1179. * Specifies the event name for removeCellsFromParent.
  1180. */
  1181. REMOVE_CELLS_FROM_PARENT: 'removeCellsFromParent',
  1182. /**
  1183. * Variable: FOLD_CELLS
  1184. *
  1185. * Specifies the event name for foldCells.
  1186. */
  1187. FOLD_CELLS: 'foldCells',
  1188. /**
  1189. * Variable: CELLS_FOLDED
  1190. *
  1191. * Specifies the event name for cellsFolded.
  1192. */
  1193. CELLS_FOLDED: 'cellsFolded',
  1194. /**
  1195. * Variable: ALIGN_CELLS
  1196. *
  1197. * Specifies the event name for alignCells.
  1198. */
  1199. ALIGN_CELLS: 'alignCells',
  1200. /**
  1201. * Variable: LABEL_CHANGED
  1202. *
  1203. * Specifies the event name for labelChanged.
  1204. */
  1205. LABEL_CHANGED: 'labelChanged',
  1206. /**
  1207. * Variable: CONNECT_CELL
  1208. *
  1209. * Specifies the event name for connectCell.
  1210. */
  1211. CONNECT_CELL: 'connectCell',
  1212. /**
  1213. * Variable: CELL_CONNECTED
  1214. *
  1215. * Specifies the event name for cellConnected.
  1216. */
  1217. CELL_CONNECTED: 'cellConnected',
  1218. /**
  1219. * Variable: SPLIT_EDGE
  1220. *
  1221. * Specifies the event name for splitEdge.
  1222. */
  1223. SPLIT_EDGE: 'splitEdge',
  1224. /**
  1225. * Variable: FLIP_EDGE
  1226. *
  1227. * Specifies the event name for flipEdge.
  1228. */
  1229. FLIP_EDGE: 'flipEdge',
  1230. /**
  1231. * Variable: START_EDITING
  1232. *
  1233. * Specifies the event name for startEditing.
  1234. */
  1235. START_EDITING: 'startEditing',
  1236. /**
  1237. * Variable: EDITING_STARTED
  1238. *
  1239. * Specifies the event name for editingStarted.
  1240. */
  1241. EDITING_STARTED: 'editingStarted',
  1242. /**
  1243. * Variable: EDITING_STOPPED
  1244. *
  1245. * Specifies the event name for editingStopped.
  1246. */
  1247. EDITING_STOPPED: 'editingStopped',
  1248. /**
  1249. * Variable: ADD_OVERLAY
  1250. *
  1251. * Specifies the event name for addOverlay.
  1252. */
  1253. ADD_OVERLAY: 'addOverlay',
  1254. /**
  1255. * Variable: REMOVE_OVERLAY
  1256. *
  1257. * Specifies the event name for removeOverlay.
  1258. */
  1259. REMOVE_OVERLAY: 'removeOverlay',
  1260. /**
  1261. * Variable: UPDATE_CELL_SIZE
  1262. *
  1263. * Specifies the event name for updateCellSize.
  1264. */
  1265. UPDATE_CELL_SIZE: 'updateCellSize',
  1266. /**
  1267. * Variable: ESCAPE
  1268. *
  1269. * Specifies the event name for escape.
  1270. */
  1271. ESCAPE: 'escape',
  1272. /**
  1273. * Variable: DOUBLE_CLICK
  1274. *
  1275. * Specifies the event name for doubleClick.
  1276. */
  1277. DOUBLE_CLICK: 'doubleClick',
  1278. /**
  1279. * Variable: START
  1280. *
  1281. * Specifies the event name for start.
  1282. */
  1283. START: 'start',
  1284. /**
  1285. * Variable: RESET
  1286. *
  1287. * Specifies the event name for reset.
  1288. */
  1289. RESET: 'reset',
  1290. /**
  1291. * Variable: PINCH_THRESHOLD
  1292. *
  1293. * Threshold for pinch gestures to fire a mouse wheel event.
  1294. * Default value is 10.
  1295. */
  1296. PINCH_THRESHOLD: 10
  1297. };