mxHandle.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxHandle
  7. *
  8. * Implements a single custom handle for vertices.
  9. *
  10. * Constructor: mxHandle
  11. *
  12. * Constructs a new handle for the given state.
  13. *
  14. * Parameters:
  15. *
  16. * state - <mxCellState> of the cell to be handled.
  17. */
  18. function mxHandle(state, cursor, image, shape)
  19. {
  20. this.graph = state.view.graph;
  21. this.state = state;
  22. this.cursor = (cursor != null) ? cursor : this.cursor;
  23. this.image = (image != null) ? image : this.image;
  24. this.shape = (shape != null) ? shape : null;
  25. this.init();
  26. };
  27. /**
  28. * Variable: cursor
  29. *
  30. * Specifies the cursor to be used for this handle. Default is 'default'.
  31. */
  32. mxHandle.prototype.cursor = 'default';
  33. /**
  34. * Variable: image
  35. *
  36. * Specifies the <mxImage> to be used to render the handle. Default is null.
  37. */
  38. mxHandle.prototype.image = null;
  39. /**
  40. * Variable: ignoreGrid
  41. *
  42. * Default is false.
  43. */
  44. mxHandle.prototype.ignoreGrid = false;
  45. /**
  46. * Function: getPosition
  47. *
  48. * Hook for subclassers to return the current position of the handle.
  49. */
  50. mxHandle.prototype.getPosition = function(bounds) { };
  51. /**
  52. * Function: setPosition
  53. *
  54. * Hooks for subclassers to update the style in the <state>.
  55. */
  56. mxHandle.prototype.setPosition = function(bounds, pt, me) { };
  57. /**
  58. * Function: execute
  59. *
  60. * Hook for subclassers to execute the handle.
  61. */
  62. mxHandle.prototype.execute = function(me) { };
  63. /**
  64. * Function: copyStyle
  65. *
  66. * Sets the cell style with the given name to the corresponding value in <state>.
  67. */
  68. mxHandle.prototype.copyStyle = function(key)
  69. {
  70. this.graph.setCellStyles(key, this.state.style[key], [this.state.cell]);
  71. };
  72. /**
  73. * Function: processEvent
  74. *
  75. * Processes the given <mxMouseEvent> and invokes <setPosition>.
  76. */
  77. mxHandle.prototype.processEvent = function(me)
  78. {
  79. var scale = this.graph.view.scale;
  80. var tr = this.graph.view.translate;
  81. var pt = new mxPoint(me.getGraphX() / scale - tr.x, me.getGraphY() / scale - tr.y);
  82. // Center shape on mouse cursor
  83. if (this.shape != null && this.shape.bounds != null)
  84. {
  85. pt.x -= this.shape.bounds.width / scale / 4;
  86. pt.y -= this.shape.bounds.height / scale / 4;
  87. }
  88. // Snaps to grid for the rotated position then applies the rotation for the direction after that
  89. var alpha1 = -mxUtils.toRadians(this.getRotation());
  90. var alpha2 = -mxUtils.toRadians(this.getTotalRotation()) - alpha1;
  91. pt = this.flipPoint(this.rotatePoint(this.snapPoint(this.rotatePoint(pt, alpha1),
  92. this.ignoreGrid || !this.graph.isGridEnabledEvent(me.getEvent())), alpha2));
  93. this.setPosition(this.state.getPaintBounds(), pt, me);
  94. this.redraw();
  95. };
  96. /**
  97. * Function: positionChanged
  98. *
  99. * Should be called after <setPosition> in <processEvent>.
  100. * This repaints the state using <mxCellRenderer>.
  101. */
  102. mxHandle.prototype.positionChanged = function()
  103. {
  104. if (this.state.text != null)
  105. {
  106. this.state.text.apply(this.state);
  107. }
  108. if (this.state.shape != null)
  109. {
  110. this.state.shape.apply(this.state);
  111. }
  112. this.graph.cellRenderer.redraw(this.state, true);
  113. };
  114. /**
  115. * Function: getRotation
  116. *
  117. * Returns the rotation defined in the style of the cell.
  118. */
  119. mxHandle.prototype.getRotation = function()
  120. {
  121. if (this.state.shape != null)
  122. {
  123. return this.state.shape.getRotation();
  124. }
  125. return 0;
  126. };
  127. /**
  128. * Function: getTotalRotation
  129. *
  130. * Returns the rotation from the style and the rotation from the direction of
  131. * the cell.
  132. */
  133. mxHandle.prototype.getTotalRotation = function()
  134. {
  135. if (this.state.shape != null)
  136. {
  137. return this.state.shape.getShapeRotation();
  138. }
  139. return 0;
  140. };
  141. /**
  142. * Function: init
  143. *
  144. * Creates and initializes the shapes required for this handle.
  145. */
  146. mxHandle.prototype.init = function()
  147. {
  148. var html = this.isHtmlRequired();
  149. if (this.image != null)
  150. {
  151. this.shape = new mxImageShape(new mxRectangle(0, 0, this.image.width, this.image.height), this.image.src);
  152. this.shape.preserveImageAspect = false;
  153. }
  154. else if (this.shape == null)
  155. {
  156. this.shape = this.createShape(html);
  157. }
  158. this.initShape(html);
  159. };
  160. /**
  161. * Function: createShape
  162. *
  163. * Creates and returns the shape for this handle.
  164. */
  165. mxHandle.prototype.createShape = function(html)
  166. {
  167. var bounds = new mxRectangle(0, 0, mxConstants.HANDLE_SIZE, mxConstants.HANDLE_SIZE);
  168. return new mxRectangleShape(bounds, mxConstants.HANDLE_FILLCOLOR, mxConstants.HANDLE_STROKECOLOR);
  169. };
  170. /**
  171. * Function: initShape
  172. *
  173. * Initializes <shape> and sets its cursor.
  174. */
  175. mxHandle.prototype.initShape = function(html)
  176. {
  177. if (html && this.shape.isHtmlAllowed())
  178. {
  179. this.shape.dialect = mxConstants.DIALECT_STRICTHTML;
  180. this.shape.init(this.graph.container);
  181. }
  182. else
  183. {
  184. this.shape.dialect = (this.graph.dialect != mxConstants.DIALECT_SVG) ?
  185. mxConstants.DIALECT_MIXEDHTML : mxConstants.DIALECT_SVG;
  186. if (this.cursor != null)
  187. {
  188. this.shape.init(this.graph.getView().getOverlayPane());
  189. }
  190. }
  191. mxEvent.redirectMouseEvents(this.shape.node, this.graph, this.state);
  192. this.shape.node.style.cursor = this.cursor;
  193. };
  194. /**
  195. * Function: redraw
  196. *
  197. * Renders the shape for this handle.
  198. */
  199. mxHandle.prototype.redraw = function()
  200. {
  201. if (this.shape != null && this.state.shape != null)
  202. {
  203. var pt = this.getPosition(this.state.getPaintBounds());
  204. if (pt != null)
  205. {
  206. var alpha = mxUtils.toRadians(this.getTotalRotation());
  207. pt = this.rotatePoint(this.flipPoint(pt), alpha);
  208. var scale = this.graph.view.scale;
  209. var tr = this.graph.view.translate;
  210. this.shape.bounds.x = Math.floor((pt.x + tr.x) * scale - this.shape.bounds.width / 2);
  211. this.shape.bounds.y = Math.floor((pt.y + tr.y) * scale - this.shape.bounds.height / 2);
  212. // Needed to force update of text bounds
  213. this.shape.redraw();
  214. }
  215. }
  216. };
  217. /**
  218. * Function: isHtmlRequired
  219. *
  220. * Returns true if this handle should be rendered in HTML. This returns true if
  221. * the text node is in the graph container.
  222. */
  223. mxHandle.prototype.isHtmlRequired = function()
  224. {
  225. return this.state.text != null && this.state.text.node.parentNode == this.graph.container;
  226. };
  227. /**
  228. * Function: rotatePoint
  229. *
  230. * Rotates the point by the given angle.
  231. */
  232. mxHandle.prototype.rotatePoint = function(pt, alpha)
  233. {
  234. var bounds = this.state.getCellBounds();
  235. var cx = new mxPoint(bounds.getCenterX(), bounds.getCenterY());
  236. var cos = Math.cos(alpha);
  237. var sin = Math.sin(alpha);
  238. return mxUtils.getRotatedPoint(pt, cos, sin, cx);
  239. };
  240. /**
  241. * Function: flipPoint
  242. *
  243. * Flips the given point vertically and/or horizontally.
  244. */
  245. mxHandle.prototype.flipPoint = function(pt)
  246. {
  247. if (this.state.shape != null)
  248. {
  249. var bounds = this.state.getCellBounds();
  250. if (this.state.shape.flipH)
  251. {
  252. pt.x = 2 * bounds.x + bounds.width - pt.x;
  253. }
  254. if (this.state.shape.flipV)
  255. {
  256. pt.y = 2 * bounds.y + bounds.height - pt.y;
  257. }
  258. }
  259. return pt;
  260. };
  261. /**
  262. * Function: snapPoint
  263. *
  264. * Snaps the given point to the grid if ignore is false. This modifies
  265. * the given point in-place and also returns it.
  266. */
  267. mxHandle.prototype.snapPoint = function(pt, ignore)
  268. {
  269. if (!ignore)
  270. {
  271. pt.x = this.graph.snap(pt.x);
  272. pt.y = this.graph.snap(pt.y);
  273. }
  274. return pt;
  275. };
  276. /**
  277. * Function: setVisible
  278. *
  279. * Shows or hides this handle.
  280. */
  281. mxHandle.prototype.setVisible = function(visible)
  282. {
  283. if (this.shape != null && this.shape.node != null)
  284. {
  285. this.shape.node.style.display = (visible) ? '' : 'none';
  286. }
  287. };
  288. /**
  289. * Function: reset
  290. *
  291. * Resets the state of this handle by setting its visibility to true.
  292. */
  293. mxHandle.prototype.reset = function()
  294. {
  295. this.setVisible(true);
  296. this.state.style = this.graph.getCellStyle(this.state.cell);
  297. this.positionChanged();
  298. };
  299. /**
  300. * Function: destroy
  301. *
  302. * Destroys this handle.
  303. */
  304. mxHandle.prototype.destroy = function()
  305. {
  306. if (this.shape != null)
  307. {
  308. this.shape.destroy();
  309. this.shape = null;
  310. }
  311. };
  312. __mxOutput.mxHandle = typeof mxHandle !== 'undefined' ? mxHandle : undefined;