mxCellHighlight.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxCellHighlight
  7. *
  8. * A helper class to highlight cells. Here is an example for a given cell.
  9. *
  10. * (code)
  11. * var highlight = new mxCellHighlight(graph, '#ff0000', 2);
  12. * highlight.highlight(graph.view.getState(cell)));
  13. * (end)
  14. *
  15. * Constructor: mxCellHighlight
  16. *
  17. * Constructs a cell highlight.
  18. */
  19. function mxCellHighlight(graph, highlightColor, strokeWidth, dashed)
  20. {
  21. if (graph != null)
  22. {
  23. this.graph = graph;
  24. this.highlightColor = (highlightColor != null) ? highlightColor : mxConstants.DEFAULT_VALID_COLOR;
  25. this.strokeWidth = (strokeWidth != null) ? strokeWidth : mxConstants.HIGHLIGHT_STROKEWIDTH;
  26. this.dashed = (dashed != null) ? dashed : false;
  27. this.opacity = mxConstants.HIGHLIGHT_OPACITY;
  28. // Updates the marker if the graph changes
  29. this.repaintHandler = mxUtils.bind(this, function()
  30. {
  31. // Updates reference to state
  32. if (this.state != null)
  33. {
  34. var tmp = this.graph.view.getState(this.state.cell);
  35. if (tmp == null)
  36. {
  37. this.hide();
  38. }
  39. else
  40. {
  41. this.state = tmp;
  42. this.repaint();
  43. }
  44. }
  45. });
  46. this.graph.getView().addListener(mxEvent.SCALE, this.repaintHandler);
  47. this.graph.getView().addListener(mxEvent.TRANSLATE, this.repaintHandler);
  48. this.graph.getView().addListener(mxEvent.SCALE_AND_TRANSLATE, this.repaintHandler);
  49. this.graph.getModel().addListener(mxEvent.CHANGE, this.repaintHandler);
  50. // Hides the marker if the current root changes
  51. this.resetHandler = mxUtils.bind(this, function()
  52. {
  53. this.hide();
  54. });
  55. this.graph.getView().addListener(mxEvent.DOWN, this.resetHandler);
  56. this.graph.getView().addListener(mxEvent.UP, this.resetHandler);
  57. }
  58. };
  59. /**
  60. * Variable: keepOnTop
  61. *
  62. * Specifies if the highlights should appear on top of everything
  63. * else in the overlay pane. Default is false.
  64. */
  65. mxCellHighlight.prototype.keepOnTop = false;
  66. /**
  67. * Variable: graph
  68. *
  69. * Reference to the enclosing <mxGraph>.
  70. */
  71. mxCellHighlight.prototype.graph = null;
  72. /**
  73. * Variable: state
  74. *
  75. * Reference to the <mxCellState>.
  76. */
  77. mxCellHighlight.prototype.state = null;
  78. /**
  79. * Variable: spacing
  80. *
  81. * Specifies the spacing between the highlight for vertices and the vertex.
  82. * Default is 2.
  83. */
  84. mxCellHighlight.prototype.spacing = 2;
  85. /**
  86. * Variable: resetHandler
  87. *
  88. * Holds the handler that automatically invokes reset if the highlight
  89. * should be hidden.
  90. */
  91. mxCellHighlight.prototype.resetHandler = null;
  92. /**
  93. * Function: setHighlightColor
  94. *
  95. * Sets the color of the rectangle used to highlight drop targets.
  96. *
  97. * Parameters:
  98. *
  99. * color - String that represents the new highlight color.
  100. */
  101. mxCellHighlight.prototype.setHighlightColor = function(color)
  102. {
  103. this.highlightColor = color;
  104. if (this.shape != null)
  105. {
  106. this.shape.stroke = color;
  107. }
  108. };
  109. /**
  110. * Function: drawHighlight
  111. *
  112. * Creates and returns the highlight shape for the given state.
  113. */
  114. mxCellHighlight.prototype.drawHighlight = function()
  115. {
  116. this.shape = this.createShape();
  117. this.repaint();
  118. if (!this.keepOnTop && this.shape.node.parentNode.firstChild != this.shape.node)
  119. {
  120. this.shape.node.parentNode.insertBefore(this.shape.node, this.shape.node.parentNode.firstChild);
  121. }
  122. };
  123. /**
  124. * Function: createShape
  125. *
  126. * Creates and returns the highlight shape for the given state.
  127. */
  128. mxCellHighlight.prototype.createShape = function()
  129. {
  130. var shape = this.graph.cellRenderer.createShape(this.state);
  131. shape.svgStrokeTolerance = this.graph.tolerance;
  132. shape.points = this.state.absolutePoints;
  133. shape.apply(this.state);
  134. shape.stroke = this.highlightColor;
  135. shape.opacity = this.opacity;
  136. shape.isDashed = this.dashed;
  137. shape.isShadow = false;
  138. shape.dialect = (this.graph.dialect != mxConstants.DIALECT_SVG) ? mxConstants.DIALECT_VML : mxConstants.DIALECT_SVG;
  139. shape.init(this.graph.getView().getOverlayPane());
  140. mxEvent.redirectMouseEvents(shape.node, this.graph, this.state);
  141. if (this.graph.dialect != mxConstants.DIALECT_SVG)
  142. {
  143. shape.pointerEvents = false;
  144. }
  145. else
  146. {
  147. shape.svgPointerEvents = 'stroke';
  148. }
  149. return shape;
  150. };
  151. /**
  152. * Function: getStrokeWidth
  153. *
  154. * Returns the stroke width.
  155. */
  156. mxCellHighlight.prototype.getStrokeWidth = function(state)
  157. {
  158. return this.strokeWidth;
  159. };
  160. /**
  161. * Function: repaint
  162. *
  163. * Updates the highlight after a change of the model or view.
  164. */
  165. mxCellHighlight.prototype.repaint = function()
  166. {
  167. if (this.state != null && this.shape != null)
  168. {
  169. this.shape.scale = this.state.view.scale;
  170. if (this.graph.model.isEdge(this.state.cell))
  171. {
  172. this.shape.strokewidth = this.getStrokeWidth();
  173. this.shape.points = this.state.absolutePoints;
  174. this.shape.outline = false;
  175. }
  176. else
  177. {
  178. this.shape.bounds = new mxRectangle(this.state.x - this.spacing, this.state.y - this.spacing,
  179. this.state.width + 2 * this.spacing, this.state.height + 2 * this.spacing);
  180. this.shape.rotation = Number(this.state.style[mxConstants.STYLE_ROTATION] || '0');
  181. this.shape.strokewidth = this.getStrokeWidth() / this.state.view.scale;
  182. this.shape.outline = true;
  183. }
  184. // Uses cursor from shape in highlight
  185. if (this.state.shape != null)
  186. {
  187. this.shape.setCursor(this.state.shape.getCursor());
  188. }
  189. // Workaround for event transparency in VML with transparent color
  190. // is to use a non-transparent color with near zero opacity
  191. if (mxClient.IS_QUIRKS || document.documentMode == 8)
  192. {
  193. if (this.shape.stroke == 'transparent')
  194. {
  195. // KNOWN: Quirks mode does not seem to catch events if
  196. // we do not force an update of the DOM via a change such
  197. // as mxLog.debug. Since IE6 is EOL we do not add a fix.
  198. this.shape.stroke = 'white';
  199. this.shape.opacity = 1;
  200. }
  201. else
  202. {
  203. this.shape.opacity = this.opacity;
  204. }
  205. }
  206. this.shape.redraw();
  207. }
  208. };
  209. /**
  210. * Function: hide
  211. *
  212. * Resets the state of the cell marker.
  213. */
  214. mxCellHighlight.prototype.hide = function()
  215. {
  216. this.highlight(null);
  217. };
  218. /**
  219. * Function: mark
  220. *
  221. * Marks the <markedState> and fires a <mark> event.
  222. */
  223. mxCellHighlight.prototype.highlight = function(state)
  224. {
  225. if (this.state != state)
  226. {
  227. if (this.shape != null)
  228. {
  229. this.shape.destroy();
  230. this.shape = null;
  231. }
  232. this.state = state;
  233. if (this.state != null)
  234. {
  235. this.drawHighlight();
  236. }
  237. }
  238. };
  239. /**
  240. * Function: isHighlightAt
  241. *
  242. * Returns true if this highlight is at the given position.
  243. */
  244. mxCellHighlight.prototype.isHighlightAt = function(x, y)
  245. {
  246. var hit = false;
  247. // Quirks mode is currently not supported as it used a different coordinate system
  248. if (this.shape != null && document.elementFromPoint != null && !mxClient.IS_QUIRKS)
  249. {
  250. var elt = document.elementFromPoint(x, y);
  251. while (elt != null)
  252. {
  253. if (elt == this.shape.node)
  254. {
  255. hit = true;
  256. break;
  257. }
  258. elt = elt.parentNode;
  259. }
  260. }
  261. return hit;
  262. };
  263. /**
  264. * Function: destroy
  265. *
  266. * Destroys the handler and all its resources and DOM nodes.
  267. */
  268. mxCellHighlight.prototype.destroy = function()
  269. {
  270. this.graph.getView().removeListener(this.resetHandler);
  271. this.graph.getView().removeListener(this.repaintHandler);
  272. this.graph.getModel().removeListener(this.repaintHandler);
  273. if (this.shape != null)
  274. {
  275. this.shape.destroy();
  276. this.shape = null;
  277. }
  278. };
  279. __mxOutput.mxCellHighlight = typeof mxCellHighlight !== 'undefined' ? mxCellHighlight : undefined;