mxSelectionCellsHandler.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxSelectionCellsHandler
  7. *
  8. * An event handler that manages cell handlers and invokes their mouse event
  9. * processing functions.
  10. *
  11. * Group: Events
  12. *
  13. * Event: mxEvent.ADD
  14. *
  15. * Fires if a cell has been added to the selection. The <code>state</code>
  16. * property contains the <mxCellState> that has been added.
  17. *
  18. * Event: mxEvent.REMOVE
  19. *
  20. * Fires if a cell has been remove from the selection. The <code>state</code>
  21. * property contains the <mxCellState> that has been removed.
  22. *
  23. * Parameters:
  24. *
  25. * graph - Reference to the enclosing <mxGraph>.
  26. */
  27. function mxSelectionCellsHandler(graph)
  28. {
  29. mxEventSource.call(this);
  30. this.graph = graph;
  31. this.handlers = new mxDictionary();
  32. this.graph.addMouseListener(this);
  33. this.refreshHandler = mxUtils.bind(this, function(sender, evt)
  34. {
  35. if (this.isEnabled())
  36. {
  37. this.refresh();
  38. }
  39. });
  40. this.graph.getSelectionModel().addListener(mxEvent.CHANGE, this.refreshHandler);
  41. this.graph.getModel().addListener(mxEvent.CHANGE, this.refreshHandler);
  42. this.graph.getView().addListener(mxEvent.SCALE, this.refreshHandler);
  43. this.graph.getView().addListener(mxEvent.TRANSLATE, this.refreshHandler);
  44. this.graph.getView().addListener(mxEvent.SCALE_AND_TRANSLATE, this.refreshHandler);
  45. this.graph.getView().addListener(mxEvent.DOWN, this.refreshHandler);
  46. this.graph.getView().addListener(mxEvent.UP, this.refreshHandler);
  47. };
  48. /**
  49. * Extends mxEventSource.
  50. */
  51. mxUtils.extend(mxSelectionCellsHandler, mxEventSource);
  52. /**
  53. * Variable: graph
  54. *
  55. * Reference to the enclosing <mxGraph>.
  56. */
  57. mxSelectionCellsHandler.prototype.graph = null;
  58. /**
  59. * Variable: enabled
  60. *
  61. * Specifies if events are handled. Default is true.
  62. */
  63. mxSelectionCellsHandler.prototype.enabled = true;
  64. /**
  65. * Variable: refreshHandler
  66. *
  67. * Keeps a reference to an event listener for later removal.
  68. */
  69. mxSelectionCellsHandler.prototype.refreshHandler = null;
  70. /**
  71. * Variable: maxHandlers
  72. *
  73. * Defines the maximum number of handlers to paint individually. Default is 100.
  74. */
  75. mxSelectionCellsHandler.prototype.maxHandlers = 100;
  76. /**
  77. * Variable: handlers
  78. *
  79. * <mxDictionary> that maps from cells to handlers.
  80. */
  81. mxSelectionCellsHandler.prototype.handlers = null;
  82. /**
  83. * Function: isEnabled
  84. *
  85. * Returns <enabled>.
  86. */
  87. mxSelectionCellsHandler.prototype.isEnabled = function()
  88. {
  89. return this.enabled;
  90. };
  91. /**
  92. * Function: setEnabled
  93. *
  94. * Sets <enabled>.
  95. */
  96. mxSelectionCellsHandler.prototype.setEnabled = function(value)
  97. {
  98. this.enabled = value;
  99. };
  100. /**
  101. * Function: getHandler
  102. *
  103. * Returns the handler for the given cell.
  104. */
  105. mxSelectionCellsHandler.prototype.getHandler = function(cell)
  106. {
  107. return this.handlers.get(cell);
  108. };
  109. /**
  110. * Function: isHandled
  111. *
  112. * Returns true if the given cell has a handler.
  113. */
  114. mxSelectionCellsHandler.prototype.isHandled = function(cell)
  115. {
  116. return this.getHandler(cell) != null;
  117. };
  118. /**
  119. * Function: reset
  120. *
  121. * Resets all handlers.
  122. */
  123. mxSelectionCellsHandler.prototype.reset = function()
  124. {
  125. this.handlers.visit(function(key, handler)
  126. {
  127. handler.reset.apply(handler);
  128. });
  129. };
  130. /**
  131. * Function: getHandledSelectionCells
  132. *
  133. * Reloads or updates all handlers.
  134. */
  135. mxSelectionCellsHandler.prototype.getHandledSelectionCells = function()
  136. {
  137. return this.graph.getSelectionCells();
  138. };
  139. /**
  140. * Function: refresh
  141. *
  142. * Reloads or updates all handlers.
  143. */
  144. mxSelectionCellsHandler.prototype.refresh = function()
  145. {
  146. // Removes all existing handlers
  147. var oldHandlers = this.handlers;
  148. this.handlers = new mxDictionary();
  149. // Creates handles for all selection cells
  150. var tmp = mxUtils.sortCells(this.getHandledSelectionCells(), false);
  151. // Destroys or updates old handlers
  152. for (var i = 0; i < tmp.length; i++)
  153. {
  154. var state = this.graph.view.getState(tmp[i]);
  155. if (state != null)
  156. {
  157. var handler = oldHandlers.remove(tmp[i]);
  158. if (handler != null)
  159. {
  160. if (handler.state != state)
  161. {
  162. handler.destroy();
  163. handler = null;
  164. }
  165. else if (!this.isHandlerActive(handler))
  166. {
  167. if (handler.refresh != null)
  168. {
  169. handler.refresh();
  170. }
  171. handler.redraw();
  172. }
  173. }
  174. if (handler != null)
  175. {
  176. this.handlers.put(tmp[i], handler);
  177. }
  178. }
  179. }
  180. // Destroys unused handlers
  181. oldHandlers.visit(mxUtils.bind(this, function(key, handler)
  182. {
  183. this.fireEvent(new mxEventObject(mxEvent.REMOVE, 'state', handler.state));
  184. handler.destroy();
  185. }));
  186. // Creates new handlers and updates parent highlight on existing handlers
  187. for (var i = 0; i < tmp.length; i++)
  188. {
  189. var state = this.graph.view.getState(tmp[i]);
  190. if (state != null)
  191. {
  192. var handler = this.handlers.get(tmp[i]);
  193. if (handler == null)
  194. {
  195. handler = this.graph.createHandler(state);
  196. this.fireEvent(new mxEventObject(mxEvent.ADD, 'state', state));
  197. this.handlers.put(tmp[i], handler);
  198. }
  199. else
  200. {
  201. handler.updateParentHighlight();
  202. }
  203. }
  204. }
  205. };
  206. /**
  207. * Function: isHandlerActive
  208. *
  209. * Returns true if the given handler is active and should not be redrawn.
  210. */
  211. mxSelectionCellsHandler.prototype.isHandlerActive = function(handler)
  212. {
  213. return handler.index != null;
  214. };
  215. /**
  216. * Function: updateHandler
  217. *
  218. * Updates the handler for the given shape if one exists.
  219. */
  220. mxSelectionCellsHandler.prototype.updateHandler = function(state)
  221. {
  222. var handler = this.handlers.remove(state.cell);
  223. if (handler != null)
  224. {
  225. // Transfers the current state to the new handler
  226. var index = handler.index;
  227. var x = handler.startX;
  228. var y = handler.startY;
  229. handler.destroy();
  230. handler = this.graph.createHandler(state);
  231. if (handler != null)
  232. {
  233. this.handlers.put(state.cell, handler);
  234. if (index != null && x != null && y != null)
  235. {
  236. handler.start(x, y, index);
  237. }
  238. }
  239. }
  240. };
  241. /**
  242. * Function: mouseDown
  243. *
  244. * Redirects the given event to the handlers.
  245. */
  246. mxSelectionCellsHandler.prototype.mouseDown = function(sender, me)
  247. {
  248. if (this.graph.isEnabled() && this.isEnabled())
  249. {
  250. var args = [sender, me];
  251. this.handlers.visit(function(key, handler)
  252. {
  253. handler.mouseDown.apply(handler, args);
  254. });
  255. }
  256. };
  257. /**
  258. * Function: mouseMove
  259. *
  260. * Redirects the given event to the handlers.
  261. */
  262. mxSelectionCellsHandler.prototype.mouseMove = function(sender, me)
  263. {
  264. if (this.graph.isEnabled() && this.isEnabled())
  265. {
  266. var args = [sender, me];
  267. this.handlers.visit(function(key, handler)
  268. {
  269. handler.mouseMove.apply(handler, args);
  270. });
  271. }
  272. };
  273. /**
  274. * Function: mouseUp
  275. *
  276. * Redirects the given event to the handlers.
  277. */
  278. mxSelectionCellsHandler.prototype.mouseUp = function(sender, me)
  279. {
  280. if (this.graph.isEnabled() && this.isEnabled())
  281. {
  282. var args = [sender, me];
  283. this.handlers.visit(function(key, handler)
  284. {
  285. handler.mouseUp.apply(handler, args);
  286. });
  287. }
  288. };
  289. /**
  290. * Function: destroy
  291. *
  292. * Destroys the handler and all its resources and DOM nodes.
  293. */
  294. mxSelectionCellsHandler.prototype.destroy = function()
  295. {
  296. this.graph.removeMouseListener(this);
  297. if (this.refreshHandler != null)
  298. {
  299. this.graph.getSelectionModel().removeListener(this.refreshHandler);
  300. this.graph.getModel().removeListener(this.refreshHandler);
  301. this.graph.getView().removeListener(this.refreshHandler);
  302. this.refreshHandler = null;
  303. }
  304. };
  305. __mxOutput.mxSelectionCellsHandler = typeof mxSelectionCellsHandler !== 'undefined' ? mxSelectionCellsHandler : undefined;