mxPopupMenuHandler.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxPopupMenuHandler
  7. *
  8. * Event handler that creates popupmenus.
  9. *
  10. * Constructor: mxPopupMenuHandler
  11. *
  12. * Constructs an event handler that creates a <mxPopupMenu>.
  13. */
  14. function mxPopupMenuHandler(graph, factoryMethod)
  15. {
  16. if (graph != null)
  17. {
  18. this.graph = graph;
  19. this.factoryMethod = factoryMethod;
  20. this.graph.addMouseListener(this);
  21. // Does not show menu if any touch gestures take place after the trigger
  22. this.gestureHandler = mxUtils.bind(this, function(sender, eo)
  23. {
  24. this.inTolerance = false;
  25. });
  26. this.graph.addListener(mxEvent.GESTURE, this.gestureHandler);
  27. this.init();
  28. }
  29. };
  30. /**
  31. * Extends mxPopupMenu.
  32. */
  33. mxPopupMenuHandler.prototype = new mxPopupMenu();
  34. mxPopupMenuHandler.prototype.constructor = mxPopupMenuHandler;
  35. /**
  36. * Variable: graph
  37. *
  38. * Reference to the enclosing <mxGraph>.
  39. */
  40. mxPopupMenuHandler.prototype.graph = null;
  41. /**
  42. * Variable: selectOnPopup
  43. *
  44. * Specifies if cells should be selected if a popupmenu is displayed for
  45. * them. Default is true.
  46. */
  47. mxPopupMenuHandler.prototype.selectOnPopup = true;
  48. /**
  49. * Variable: clearSelectionOnBackground
  50. *
  51. * Specifies if cells should be deselected if a popupmenu is displayed for
  52. * the diagram background. Default is true.
  53. */
  54. mxPopupMenuHandler.prototype.clearSelectionOnBackground = true;
  55. /**
  56. * Variable: triggerX
  57. *
  58. * X-coordinate of the mouse down event.
  59. */
  60. mxPopupMenuHandler.prototype.triggerX = null;
  61. /**
  62. * Variable: triggerY
  63. *
  64. * Y-coordinate of the mouse down event.
  65. */
  66. mxPopupMenuHandler.prototype.triggerY = null;
  67. /**
  68. * Variable: screenX
  69. *
  70. * Screen X-coordinate of the mouse down event.
  71. */
  72. mxPopupMenuHandler.prototype.screenX = null;
  73. /**
  74. * Variable: screenY
  75. *
  76. * Screen Y-coordinate of the mouse down event.
  77. */
  78. mxPopupMenuHandler.prototype.screenY = null;
  79. /**
  80. * Function: init
  81. *
  82. * Initializes the shapes required for this vertex handler.
  83. */
  84. mxPopupMenuHandler.prototype.init = function()
  85. {
  86. // Supercall
  87. mxPopupMenu.prototype.init.apply(this);
  88. // Hides the tooltip if the mouse is over
  89. // the context menu
  90. mxEvent.addGestureListeners(this.div, mxUtils.bind(this, function(evt)
  91. {
  92. this.graph.tooltipHandler.hide();
  93. }));
  94. };
  95. /**
  96. * Function: isSelectOnPopup
  97. *
  98. * Hook for returning if a cell should be selected for a given <mxMouseEvent>.
  99. * This implementation returns <selectOnPopup>.
  100. */
  101. mxPopupMenuHandler.prototype.isSelectOnPopup = function(me)
  102. {
  103. return this.selectOnPopup;
  104. };
  105. /**
  106. * Function: mouseDown
  107. *
  108. * Handles the event by initiating the panning. By consuming the event all
  109. * subsequent events of the gesture are redirected to this handler.
  110. */
  111. mxPopupMenuHandler.prototype.mouseDown = function(sender, me)
  112. {
  113. if (this.isEnabled() && !mxEvent.isMultiTouchEvent(me.getEvent()))
  114. {
  115. // Hides the popupmenu if is is being displayed
  116. this.hideMenu();
  117. this.triggerX = me.getGraphX();
  118. this.triggerY = me.getGraphY();
  119. this.screenX = mxEvent.getMainEvent(me.getEvent()).screenX;
  120. this.screenY = mxEvent.getMainEvent(me.getEvent()).screenY;
  121. this.popupTrigger = this.isPopupTrigger(me);
  122. this.inTolerance = true;
  123. }
  124. };
  125. /**
  126. * Function: mouseMove
  127. *
  128. * Handles the event by updating the panning on the graph.
  129. */
  130. mxPopupMenuHandler.prototype.mouseMove = function(sender, me)
  131. {
  132. // Popup trigger may change on mouseUp so ignore it
  133. if (this.inTolerance && this.screenX != null && this.screenY != null)
  134. {
  135. if (Math.abs(mxEvent.getMainEvent(me.getEvent()).screenX - this.screenX) > this.graph.tolerance ||
  136. Math.abs(mxEvent.getMainEvent(me.getEvent()).screenY - this.screenY) > this.graph.tolerance)
  137. {
  138. this.inTolerance = false;
  139. }
  140. }
  141. };
  142. /**
  143. * Function: mouseUp
  144. *
  145. * Handles the event by setting the translation on the view or showing the
  146. * popupmenu.
  147. */
  148. mxPopupMenuHandler.prototype.mouseUp = function(sender, me)
  149. {
  150. if (this.popupTrigger && this.inTolerance && this.triggerX != null && this.triggerY != null)
  151. {
  152. var cell = this.getCellForPopupEvent(me);
  153. // Selects the cell for which the context menu is being displayed
  154. if (this.graph.isEnabled() && this.isSelectOnPopup(me) &&
  155. cell != null && !this.graph.isCellSelected(cell))
  156. {
  157. this.graph.setSelectionCell(cell);
  158. }
  159. else if (this.clearSelectionOnBackground && cell == null)
  160. {
  161. this.graph.clearSelection();
  162. }
  163. // Hides the tooltip if there is one
  164. this.graph.tooltipHandler.hide();
  165. // Menu is shifted by 1 pixel so that the mouse up event
  166. // is routed via the underlying shape instead of the DIV
  167. var origin = mxUtils.getScrollOrigin();
  168. this.popup(me.getX() + origin.x + 1, me.getY() + origin.y + 1, cell, me.getEvent());
  169. me.consume();
  170. }
  171. this.popupTrigger = false;
  172. this.inTolerance = false;
  173. };
  174. /**
  175. * Function: getCellForPopupEvent
  176. *
  177. * Hook to return the cell for the mouse up popup trigger handling.
  178. */
  179. mxPopupMenuHandler.prototype.getCellForPopupEvent = function(me)
  180. {
  181. return me.getCell();
  182. };
  183. /**
  184. * Function: destroy
  185. *
  186. * Destroys the handler and all its resources and DOM nodes.
  187. */
  188. mxPopupMenuHandler.prototype.destroy = function()
  189. {
  190. this.graph.removeMouseListener(this);
  191. this.graph.removeListener(this.gestureHandler);
  192. // Supercall
  193. mxPopupMenu.prototype.destroy.apply(this);
  194. };
  195. __mxOutput.mxPopupMenuHandler = typeof mxPopupMenuHandler !== 'undefined' ? mxPopupMenuHandler : undefined;