mxMouseEvent.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxMouseEvent
  7. *
  8. * Base class for all mouse events in mxGraph. A listener for this event should
  9. * implement the following methods:
  10. *
  11. * (code)
  12. * graph.addMouseListener(
  13. * {
  14. * mouseDown: function(sender, evt)
  15. * {
  16. * mxLog.debug('mouseDown');
  17. * },
  18. * mouseMove: function(sender, evt)
  19. * {
  20. * mxLog.debug('mouseMove');
  21. * },
  22. * mouseUp: function(sender, evt)
  23. * {
  24. * mxLog.debug('mouseUp');
  25. * }
  26. * });
  27. * (end)
  28. *
  29. * Constructor: mxMouseEvent
  30. *
  31. * Constructs a new event object for the given arguments.
  32. *
  33. * Parameters:
  34. *
  35. * evt - Native mouse event.
  36. * state - Optional <mxCellState> under the mouse.
  37. *
  38. */
  39. function mxMouseEvent(evt, state)
  40. {
  41. this.evt = evt;
  42. this.state = state;
  43. this.sourceState = state;
  44. };
  45. /**
  46. * Variable: consumed
  47. *
  48. * Holds the consumed state of this event.
  49. */
  50. mxMouseEvent.prototype.consumed = false;
  51. /**
  52. * Variable: evt
  53. *
  54. * Holds the inner event object.
  55. */
  56. mxMouseEvent.prototype.evt = null;
  57. /**
  58. * Variable: graphX
  59. *
  60. * Holds the x-coordinate of the event in the graph. This value is set in
  61. * <mxGraph.fireMouseEvent>.
  62. */
  63. mxMouseEvent.prototype.graphX = null;
  64. /**
  65. * Variable: graphY
  66. *
  67. * Holds the y-coordinate of the event in the graph. This value is set in
  68. * <mxGraph.fireMouseEvent>.
  69. */
  70. mxMouseEvent.prototype.graphY = null;
  71. /**
  72. * Variable: state
  73. *
  74. * Holds the optional <mxCellState> associated with this event.
  75. */
  76. mxMouseEvent.prototype.state = null;
  77. /**
  78. * Variable: sourceState
  79. *
  80. * Holds the <mxCellState> that was passed to the constructor. This can be
  81. * different from <state> depending on the result of <mxGraph.getEventState>.
  82. */
  83. mxMouseEvent.prototype.sourceState = null;
  84. /**
  85. * Function: getEvent
  86. *
  87. * Returns <evt>.
  88. */
  89. mxMouseEvent.prototype.getEvent = function()
  90. {
  91. return this.evt;
  92. };
  93. /**
  94. * Function: getSource
  95. *
  96. * Returns the target DOM element using <mxEvent.getSource> for <evt>.
  97. */
  98. mxMouseEvent.prototype.getSource = function()
  99. {
  100. return mxEvent.getSource(this.evt);
  101. };
  102. /**
  103. * Function: isSource
  104. *
  105. * Returns true if the given <mxShape> is the source of <evt>.
  106. */
  107. mxMouseEvent.prototype.isSource = function(shape)
  108. {
  109. if (shape != null)
  110. {
  111. return mxUtils.isAncestorNode(shape.node, this.getSource());
  112. }
  113. return false;
  114. };
  115. /**
  116. * Function: getX
  117. *
  118. * Returns <evt.clientX>.
  119. */
  120. mxMouseEvent.prototype.getX = function()
  121. {
  122. return mxEvent.getClientX(this.getEvent());
  123. };
  124. /**
  125. * Function: getY
  126. *
  127. * Returns <evt.clientY>.
  128. */
  129. mxMouseEvent.prototype.getY = function()
  130. {
  131. return mxEvent.getClientY(this.getEvent());
  132. };
  133. /**
  134. * Function: getGraphX
  135. *
  136. * Returns <graphX>.
  137. */
  138. mxMouseEvent.prototype.getGraphX = function()
  139. {
  140. return this.graphX;
  141. };
  142. /**
  143. * Function: getGraphY
  144. *
  145. * Returns <graphY>.
  146. */
  147. mxMouseEvent.prototype.getGraphY = function()
  148. {
  149. return this.graphY;
  150. };
  151. /**
  152. * Function: getState
  153. *
  154. * Returns <state>.
  155. */
  156. mxMouseEvent.prototype.getState = function()
  157. {
  158. return this.state;
  159. };
  160. /**
  161. * Function: getCell
  162. *
  163. * Returns the <mxCell> in <state> is not null.
  164. */
  165. mxMouseEvent.prototype.getCell = function()
  166. {
  167. var state = this.getState();
  168. if (state != null)
  169. {
  170. return state.cell;
  171. }
  172. return null;
  173. };
  174. /**
  175. * Function: isPopupTrigger
  176. *
  177. * Returns true if the event is a popup trigger.
  178. */
  179. mxMouseEvent.prototype.isPopupTrigger = function()
  180. {
  181. return mxEvent.isPopupTrigger(this.getEvent());
  182. };
  183. /**
  184. * Function: isConsumed
  185. *
  186. * Returns <consumed>.
  187. */
  188. mxMouseEvent.prototype.isConsumed = function()
  189. {
  190. return this.consumed;
  191. };
  192. /**
  193. * Function: consume
  194. *
  195. * Sets <consumed> to true and invokes preventDefault on the native event
  196. * if such a method is defined. This is used mainly to avoid the cursor from
  197. * being changed to a text cursor in Webkit. You can use the preventDefault
  198. * flag to disable this functionality.
  199. *
  200. * Parameters:
  201. *
  202. * preventDefault - Specifies if the native event should be canceled. Default
  203. * is true.
  204. */
  205. mxMouseEvent.prototype.consume = function(preventDefault)
  206. {
  207. preventDefault = (preventDefault != null) ? preventDefault :
  208. (this.evt.touches != null || mxEvent.isMouseEvent(this.evt));
  209. if (preventDefault && this.evt.preventDefault)
  210. {
  211. this.evt.preventDefault();
  212. }
  213. // Workaround for images being dragged in IE
  214. // Does not change returnValue in Opera
  215. if (mxClient.IS_IE)
  216. {
  217. this.evt.returnValue = true;
  218. }
  219. // Sets local consumed state
  220. this.consumed = true;
  221. };
  222. __mxOutput.mxMouseEvent = typeof mxMouseEvent !== 'undefined' ? mxMouseEvent : undefined;