mxCellMarker.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxCellMarker
  7. *
  8. * A helper class to process mouse locations and highlight cells.
  9. *
  10. * Helper class to highlight cells. To add a cell marker to an existing graph
  11. * for highlighting all cells, the following code is used:
  12. *
  13. * (code)
  14. * var marker = new mxCellMarker(graph);
  15. * graph.addMouseListener({
  16. * mouseDown: function() {},
  17. * mouseMove: function(sender, me)
  18. * {
  19. * marker.process(me);
  20. * },
  21. * mouseUp: function() {}
  22. * });
  23. * (end)
  24. *
  25. * Event: mxEvent.MARK
  26. *
  27. * Fires after a cell has been marked or unmarked. The <code>state</code>
  28. * property contains the marked <mxCellState> or null if no state is marked.
  29. *
  30. * Constructor: mxCellMarker
  31. *
  32. * Constructs a new cell marker.
  33. *
  34. * Parameters:
  35. *
  36. * graph - Reference to the enclosing <mxGraph>.
  37. * validColor - Optional marker color for valid states. Default is
  38. * <mxConstants.DEFAULT_VALID_COLOR>.
  39. * invalidColor - Optional marker color for invalid states. Default is
  40. * <mxConstants.DEFAULT_INVALID_COLOR>.
  41. * hotspot - Portion of the width and hight where a state intersects a
  42. * given coordinate pair. A value of 0 means always highlight. Default is
  43. * <mxConstants.DEFAULT_HOTSPOT>.
  44. */
  45. function mxCellMarker(graph, validColor, invalidColor, hotspot)
  46. {
  47. mxEventSource.call(this);
  48. if (graph != null)
  49. {
  50. this.graph = graph;
  51. this.validColor = (validColor != null) ? validColor : mxConstants.DEFAULT_VALID_COLOR;
  52. this.invalidColor = (invalidColor != null) ? invalidColor : mxConstants.DEFAULT_INVALID_COLOR;
  53. this.hotspot = (hotspot != null) ? hotspot : mxConstants.DEFAULT_HOTSPOT;
  54. this.highlight = new mxCellHighlight(graph);
  55. }
  56. };
  57. /**
  58. * Extends mxEventSource.
  59. */
  60. mxUtils.extend(mxCellMarker, mxEventSource);
  61. /**
  62. * Variable: graph
  63. *
  64. * Reference to the enclosing <mxGraph>.
  65. */
  66. mxCellMarker.prototype.graph = null;
  67. /**
  68. * Variable: enabled
  69. *
  70. * Specifies if the marker is enabled. Default is true.
  71. */
  72. mxCellMarker.prototype.enabled = true;
  73. /**
  74. * Variable: hotspot
  75. *
  76. * Specifies the portion of the width and height that should trigger
  77. * a highlight. The area around the center of the cell to be marked is used
  78. * as the hotspot. Possible values are between 0 and 1. Default is
  79. * mxConstants.DEFAULT_HOTSPOT.
  80. */
  81. mxCellMarker.prototype.hotspot = mxConstants.DEFAULT_HOTSPOT;
  82. /**
  83. * Variable: hotspotEnabled
  84. *
  85. * Specifies if the hotspot is enabled. Default is false.
  86. */
  87. mxCellMarker.prototype.hotspotEnabled = false;
  88. /**
  89. * Variable: validColor
  90. *
  91. * Holds the valid marker color.
  92. */
  93. mxCellMarker.prototype.validColor = null;
  94. /**
  95. * Variable: invalidColor
  96. *
  97. * Holds the invalid marker color.
  98. */
  99. mxCellMarker.prototype.invalidColor = null;
  100. /**
  101. * Variable: currentColor
  102. *
  103. * Holds the current marker color.
  104. */
  105. mxCellMarker.prototype.currentColor = null;
  106. /**
  107. * Variable: validState
  108. *
  109. * Holds the marked <mxCellState> if it is valid.
  110. */
  111. mxCellMarker.prototype.validState = null;
  112. /**
  113. * Variable: markedState
  114. *
  115. * Holds the marked <mxCellState>.
  116. */
  117. mxCellMarker.prototype.markedState = null;
  118. /**
  119. * Function: setEnabled
  120. *
  121. * Enables or disables event handling. This implementation
  122. * updates <enabled>.
  123. *
  124. * Parameters:
  125. *
  126. * enabled - Boolean that specifies the new enabled state.
  127. */
  128. mxCellMarker.prototype.setEnabled = function(enabled)
  129. {
  130. this.enabled = enabled;
  131. };
  132. /**
  133. * Function: isEnabled
  134. *
  135. * Returns true if events are handled. This implementation
  136. * returns <enabled>.
  137. */
  138. mxCellMarker.prototype.isEnabled = function()
  139. {
  140. return this.enabled;
  141. };
  142. /**
  143. * Function: setHotspot
  144. *
  145. * Sets the <hotspot>.
  146. */
  147. mxCellMarker.prototype.setHotspot = function(hotspot)
  148. {
  149. this.hotspot = hotspot;
  150. };
  151. /**
  152. * Function: getHotspot
  153. *
  154. * Returns the <hotspot>.
  155. */
  156. mxCellMarker.prototype.getHotspot = function()
  157. {
  158. return this.hotspot;
  159. };
  160. /**
  161. * Function: setHotspotEnabled
  162. *
  163. * Specifies whether the hotspot should be used in <intersects>.
  164. */
  165. mxCellMarker.prototype.setHotspotEnabled = function(enabled)
  166. {
  167. this.hotspotEnabled = enabled;
  168. };
  169. /**
  170. * Function: isHotspotEnabled
  171. *
  172. * Returns true if hotspot is used in <intersects>.
  173. */
  174. mxCellMarker.prototype.isHotspotEnabled = function()
  175. {
  176. return this.hotspotEnabled;
  177. };
  178. /**
  179. * Function: hasValidState
  180. *
  181. * Returns true if <validState> is not null.
  182. */
  183. mxCellMarker.prototype.hasValidState = function()
  184. {
  185. return this.validState != null;
  186. };
  187. /**
  188. * Function: getValidState
  189. *
  190. * Returns the <validState>.
  191. */
  192. mxCellMarker.prototype.getValidState = function()
  193. {
  194. return this.validState;
  195. };
  196. /**
  197. * Function: getMarkedState
  198. *
  199. * Returns the <markedState>.
  200. */
  201. mxCellMarker.prototype.getMarkedState = function()
  202. {
  203. return this.markedState;
  204. };
  205. /**
  206. * Function: reset
  207. *
  208. * Resets the state of the cell marker.
  209. */
  210. mxCellMarker.prototype.reset = function()
  211. {
  212. this.validState = null;
  213. if (this.markedState != null)
  214. {
  215. this.markedState = null;
  216. this.unmark();
  217. }
  218. };
  219. /**
  220. * Function: process
  221. *
  222. * Processes the given event and cell and marks the state returned by
  223. * <getState> with the color returned by <getMarkerColor>. If the
  224. * markerColor is not null, then the state is stored in <markedState>. If
  225. * <isValidState> returns true, then the state is stored in <validState>
  226. * regardless of the marker color. The state is returned regardless of the
  227. * marker color and valid state.
  228. */
  229. mxCellMarker.prototype.process = function(me)
  230. {
  231. var state = null;
  232. if (this.isEnabled())
  233. {
  234. state = this.getState(me);
  235. this.setCurrentState(state, me);
  236. }
  237. return state;
  238. };
  239. /**
  240. * Function: setCurrentState
  241. *
  242. * Sets and marks the current valid state.
  243. */
  244. mxCellMarker.prototype.setCurrentState = function(state, me, color)
  245. {
  246. var isValid = (state != null) ? this.isValidState(state) : false;
  247. color = (color != null) ? color : this.getMarkerColor(me.getEvent(), state, isValid);
  248. if (isValid)
  249. {
  250. this.validState = state;
  251. }
  252. else
  253. {
  254. this.validState = null;
  255. }
  256. if (state != this.markedState || color != this.currentColor)
  257. {
  258. this.currentColor = color;
  259. if (state != null && this.currentColor != null)
  260. {
  261. this.markedState = state;
  262. this.mark();
  263. }
  264. else if (this.markedState != null)
  265. {
  266. this.markedState = null;
  267. this.unmark();
  268. }
  269. }
  270. };
  271. /**
  272. * Function: markCell
  273. *
  274. * Marks the given cell using the given color, or <validColor> if no color is specified.
  275. */
  276. mxCellMarker.prototype.markCell = function(cell, color)
  277. {
  278. var state = this.graph.getView().getState(cell);
  279. if (state != null)
  280. {
  281. this.currentColor = (color != null) ? color : this.validColor;
  282. this.markedState = state;
  283. this.mark();
  284. }
  285. };
  286. /**
  287. * Function: mark
  288. *
  289. * Marks the <markedState> and fires a <mark> event.
  290. */
  291. mxCellMarker.prototype.mark = function()
  292. {
  293. this.highlight.setHighlightColor(this.currentColor);
  294. this.highlight.highlight(this.markedState);
  295. this.fireEvent(new mxEventObject(mxEvent.MARK, 'state', this.markedState));
  296. };
  297. /**
  298. * Function: unmark
  299. *
  300. * Hides the marker and fires a <mark> event.
  301. */
  302. mxCellMarker.prototype.unmark = function()
  303. {
  304. this.mark();
  305. };
  306. /**
  307. * Function: isValidState
  308. *
  309. * Returns true if the given <mxCellState> is a valid state. If this
  310. * returns true, then the state is stored in <validState>. The return value
  311. * of this method is used as the argument for <getMarkerColor>.
  312. */
  313. mxCellMarker.prototype.isValidState = function(state)
  314. {
  315. return true;
  316. };
  317. /**
  318. * Function: getMarkerColor
  319. *
  320. * Returns the valid- or invalidColor depending on the value of isValid.
  321. * The given <mxCellState> is ignored by this implementation.
  322. */
  323. mxCellMarker.prototype.getMarkerColor = function(evt, state, isValid)
  324. {
  325. return (isValid) ? this.validColor : this.invalidColor;
  326. };
  327. /**
  328. * Function: getState
  329. *
  330. * Uses <getCell>, <getStateToMark> and <intersects> to return the
  331. * <mxCellState> for the given <mxMouseEvent>.
  332. */
  333. mxCellMarker.prototype.getState = function(me)
  334. {
  335. var view = this.graph.getView();
  336. var cell = this.getCell(me);
  337. var state = this.getStateToMark(view.getState(cell));
  338. return (state != null && this.intersects(state, me)) ? state : null;
  339. };
  340. /**
  341. * Function: getCell
  342. *
  343. * Returns the <mxCell> for the given event and cell. This returns the
  344. * given cell.
  345. */
  346. mxCellMarker.prototype.getCell = function(me)
  347. {
  348. return me.getCell();
  349. };
  350. /**
  351. * Function: getStateToMark
  352. *
  353. * Returns the <mxCellState> to be marked for the given <mxCellState> under
  354. * the mouse. This returns the given state.
  355. */
  356. mxCellMarker.prototype.getStateToMark = function(state)
  357. {
  358. return state;
  359. };
  360. /**
  361. * Function: intersects
  362. *
  363. * Returns true if the given coordinate pair intersects the given state.
  364. * This returns true if the <hotspot> is 0 or the coordinates are inside
  365. * the hotspot for the given cell state.
  366. */
  367. mxCellMarker.prototype.intersects = function(state, me)
  368. {
  369. if (this.hotspotEnabled)
  370. {
  371. return mxUtils.intersectsHotspot(state, me.getGraphX(), me.getGraphY(),
  372. this.hotspot, mxConstants.MIN_HOTSPOT_SIZE,
  373. mxConstants.MAX_HOTSPOT_SIZE);
  374. }
  375. return true;
  376. };
  377. /**
  378. * Function: destroy
  379. *
  380. * Destroys the handler and all its resources and DOM nodes.
  381. */
  382. mxCellMarker.prototype.destroy = function()
  383. {
  384. this.graph.getView().removeListener(this.resetHandler);
  385. this.graph.getModel().removeListener(this.resetHandler);
  386. this.highlight.destroy();
  387. };
  388. __mxOutput.mxCellMarker = typeof mxCellMarker !== 'undefined' ? mxCellMarker : undefined;