mxConstraintHandler.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxConstraintHandler
  7. *
  8. * Handles constraints on connection targets. This class is in charge of
  9. * showing fixed points when the mouse is over a vertex and handles constraints
  10. * to establish new connections.
  11. *
  12. * Constructor: mxConstraintHandler
  13. *
  14. * Constructs an new constraint handler.
  15. *
  16. * Parameters:
  17. *
  18. * graph - Reference to the enclosing <mxGraph>.
  19. * factoryMethod - Optional function to create the edge. The function takes
  20. * the source and target <mxCell> as the first and second argument and
  21. * returns the <mxCell> that represents the new edge.
  22. */
  23. function mxConstraintHandler(graph)
  24. {
  25. this.graph = graph;
  26. // Adds a graph model listener to update the current focus on changes
  27. this.resetHandler = mxUtils.bind(this, function(sender, evt)
  28. {
  29. if (this.currentFocus != null && this.graph.view.getState(this.currentFocus.cell) == null)
  30. {
  31. this.reset();
  32. }
  33. else
  34. {
  35. this.redraw();
  36. }
  37. });
  38. this.graph.model.addListener(mxEvent.CHANGE, this.resetHandler);
  39. this.graph.view.addListener(mxEvent.SCALE_AND_TRANSLATE, this.resetHandler);
  40. this.graph.view.addListener(mxEvent.TRANSLATE, this.resetHandler);
  41. this.graph.view.addListener(mxEvent.SCALE, this.resetHandler);
  42. this.graph.addListener(mxEvent.ROOT, this.resetHandler);
  43. };
  44. /**
  45. * Variable: pointImage
  46. *
  47. * <mxImage> to be used as the image for fixed connection points.
  48. */
  49. mxConstraintHandler.prototype.pointImage = new mxImage(mxClient.imageBasePath + '/point.gif', 5, 5);
  50. /**
  51. * Variable: graph
  52. *
  53. * Reference to the enclosing <mxGraph>.
  54. */
  55. mxConstraintHandler.prototype.graph = null;
  56. /**
  57. * Variable: enabled
  58. *
  59. * Specifies if events are handled. Default is true.
  60. */
  61. mxConstraintHandler.prototype.enabled = true;
  62. /**
  63. * Variable: highlightColor
  64. *
  65. * Specifies the color for the highlight. Default is <mxConstants.DEFAULT_VALID_COLOR>.
  66. */
  67. mxConstraintHandler.prototype.highlightColor = mxConstants.DEFAULT_VALID_COLOR;
  68. /**
  69. * Function: isEnabled
  70. *
  71. * Returns true if events are handled. This implementation
  72. * returns <enabled>.
  73. */
  74. mxConstraintHandler.prototype.isEnabled = function()
  75. {
  76. return this.enabled;
  77. };
  78. /**
  79. * Function: setEnabled
  80. *
  81. * Enables or disables event handling. This implementation
  82. * updates <enabled>.
  83. *
  84. * Parameters:
  85. *
  86. * enabled - Boolean that specifies the new enabled state.
  87. */
  88. mxConstraintHandler.prototype.setEnabled = function(enabled)
  89. {
  90. this.enabled = enabled;
  91. };
  92. /**
  93. * Function: reset
  94. *
  95. * Resets the state of this handler.
  96. */
  97. mxConstraintHandler.prototype.reset = function()
  98. {
  99. if (this.focusIcons != null)
  100. {
  101. for (var i = 0; i < this.focusIcons.length; i++)
  102. {
  103. this.focusIcons[i].destroy();
  104. }
  105. this.focusIcons = null;
  106. }
  107. if (this.focusHighlight != null)
  108. {
  109. this.focusHighlight.destroy();
  110. this.focusHighlight = null;
  111. }
  112. this.currentConstraint = null;
  113. this.currentFocusArea = null;
  114. this.currentPoint = null;
  115. this.currentFocus = null;
  116. this.focusPoints = null;
  117. };
  118. /**
  119. * Function: getTolerance
  120. *
  121. * Returns the tolerance to be used for intersecting connection points. This
  122. * implementation returns <mxGraph.tolerance>.
  123. *
  124. * Parameters:
  125. *
  126. * me - <mxMouseEvent> whose tolerance should be returned.
  127. */
  128. mxConstraintHandler.prototype.getTolerance = function(me)
  129. {
  130. return this.graph.getTolerance();
  131. };
  132. /**
  133. * Function: getImageForConstraint
  134. *
  135. * Returns the tolerance to be used for intersecting connection points.
  136. */
  137. mxConstraintHandler.prototype.getImageForConstraint = function(state, constraint, point)
  138. {
  139. return this.pointImage;
  140. };
  141. /**
  142. * Function: isEventIgnored
  143. *
  144. * Returns true if the given <mxMouseEvent> should be ignored in <update>. This
  145. * implementation always returns false.
  146. */
  147. mxConstraintHandler.prototype.isEventIgnored = function(me, source)
  148. {
  149. return false;
  150. };
  151. /**
  152. * Function: isStateIgnored
  153. *
  154. * Returns true if the given state should be ignored. This always returns false.
  155. */
  156. mxConstraintHandler.prototype.isStateIgnored = function(state, source)
  157. {
  158. return false;
  159. };
  160. /**
  161. * Function: destroyIcons
  162. *
  163. * Destroys the <focusIcons> if they exist.
  164. */
  165. mxConstraintHandler.prototype.destroyIcons = function()
  166. {
  167. if (this.focusIcons != null)
  168. {
  169. for (var i = 0; i < this.focusIcons.length; i++)
  170. {
  171. this.focusIcons[i].destroy();
  172. }
  173. this.focusIcons = null;
  174. this.focusPoints = null;
  175. }
  176. };
  177. /**
  178. * Function: destroyFocusHighlight
  179. *
  180. * Destroys the <focusHighlight> if one exists.
  181. */
  182. mxConstraintHandler.prototype.destroyFocusHighlight = function()
  183. {
  184. if (this.focusHighlight != null)
  185. {
  186. this.focusHighlight.destroy();
  187. this.focusHighlight = null;
  188. }
  189. };
  190. /**
  191. * Function: isKeepFocusEvent
  192. *
  193. * Returns true if the current focused state should not be changed for the given event.
  194. * This returns true if shift and alt are pressed.
  195. */
  196. mxConstraintHandler.prototype.isKeepFocusEvent = function(me)
  197. {
  198. return mxEvent.isShiftDown(me.getEvent());
  199. };
  200. /**
  201. * Function: getCellForEvent
  202. *
  203. * Returns the cell for the given event.
  204. */
  205. mxConstraintHandler.prototype.getCellForEvent = function(me, point)
  206. {
  207. var cell = me.getCell();
  208. // Gets cell under actual point if different from event location
  209. if (cell == null && point != null && (me.getGraphX() != point.x || me.getGraphY() != point.y))
  210. {
  211. cell = this.graph.getCellAt(point.x, point.y);
  212. }
  213. // Uses connectable parent vertex if one exists
  214. if (cell != null && !this.graph.isCellConnectable(cell))
  215. {
  216. var parent = this.graph.getModel().getParent(cell);
  217. if (this.graph.getModel().isVertex(parent) && this.graph.isCellConnectable(parent))
  218. {
  219. cell = parent;
  220. }
  221. }
  222. return (this.graph.isCellLocked(cell)) ? null : cell;
  223. };
  224. /**
  225. * Function: update
  226. *
  227. * Updates the state of this handler based on the given <mxMouseEvent>.
  228. * Source is a boolean indicating if the cell is a source or target.
  229. */
  230. mxConstraintHandler.prototype.update = function(me, source, existingEdge, point)
  231. {
  232. if (this.isEnabled() && !this.isEventIgnored(me))
  233. {
  234. // Lazy installation of mouseleave handler
  235. if (this.mouseleaveHandler == null && this.graph.container != null)
  236. {
  237. this.mouseleaveHandler = mxUtils.bind(this, function()
  238. {
  239. this.reset();
  240. });
  241. mxEvent.addListener(this.graph.container, 'mouseleave', this.resetHandler);
  242. }
  243. var tol = this.getTolerance(me);
  244. var x = (point != null) ? point.x : me.getGraphX();
  245. var y = (point != null) ? point.y : me.getGraphY();
  246. var grid = new mxRectangle(x - tol, y - tol, 2 * tol, 2 * tol);
  247. var mouse = new mxRectangle(me.getGraphX() - tol, me.getGraphY() - tol, 2 * tol, 2 * tol);
  248. var state = this.graph.view.getState(this.getCellForEvent(me, point));
  249. // Keeps focus icons visible while over vertex bounds and no other cell under mouse or shift is pressed
  250. if (!this.isKeepFocusEvent(me) && (this.currentFocusArea == null || this.currentFocus == null ||
  251. (state != null) || !this.graph.getModel().isVertex(this.currentFocus.cell) ||
  252. !mxUtils.intersects(this.currentFocusArea, mouse)) && (state != this.currentFocus))
  253. {
  254. this.currentFocusArea = null;
  255. this.currentFocus = null;
  256. this.setFocus(me, state, source);
  257. }
  258. this.currentConstraint = null;
  259. this.currentPoint = null;
  260. var minDistSq = null;
  261. if (this.focusIcons != null && this.constraints != null &&
  262. (state == null || this.currentFocus == state))
  263. {
  264. var cx = mouse.getCenterX();
  265. var cy = mouse.getCenterY();
  266. for (var i = 0; i < this.focusIcons.length; i++)
  267. {
  268. var dx = cx - this.focusIcons[i].bounds.getCenterX();
  269. var dy = cy - this.focusIcons[i].bounds.getCenterY();
  270. var tmp = dx * dx + dy * dy;
  271. if ((this.intersects(this.focusIcons[i], mouse, source, existingEdge) || (point != null &&
  272. this.intersects(this.focusIcons[i], grid, source, existingEdge))) &&
  273. (minDistSq == null || tmp < minDistSq))
  274. {
  275. this.currentConstraint = this.constraints[i];
  276. this.currentPoint = this.focusPoints[i];
  277. minDistSq = tmp;
  278. var tmp = this.focusIcons[i].bounds.clone();
  279. tmp.grow(mxConstants.HIGHLIGHT_SIZE + 1);
  280. tmp.width -= 1;
  281. tmp.height -= 1;
  282. if (this.focusHighlight == null)
  283. {
  284. var hl = this.createHighlightShape();
  285. hl.dialect = (this.graph.dialect == mxConstants.DIALECT_SVG) ?
  286. mxConstants.DIALECT_SVG : mxConstants.DIALECT_VML;
  287. hl.pointerEvents = false;
  288. hl.init(this.graph.getView().getOverlayPane());
  289. this.focusHighlight = hl;
  290. var getState = mxUtils.bind(this, function()
  291. {
  292. return (this.currentFocus != null) ? this.currentFocus : state;
  293. });
  294. mxEvent.redirectMouseEvents(hl.node, this.graph, getState);
  295. }
  296. this.focusHighlight.bounds = tmp;
  297. this.focusHighlight.redraw();
  298. }
  299. }
  300. }
  301. if (this.currentConstraint == null)
  302. {
  303. this.destroyFocusHighlight();
  304. }
  305. }
  306. else
  307. {
  308. this.currentConstraint = null;
  309. this.currentFocus = null;
  310. this.currentPoint = null;
  311. }
  312. };
  313. /**
  314. * Function: redraw
  315. *
  316. * Transfers the focus to the given state as a source or target terminal. If
  317. * the handler is not enabled then the outline is painted, but the constraints
  318. * are ignored.
  319. */
  320. mxConstraintHandler.prototype.redraw = function()
  321. {
  322. if (this.currentFocus != null && this.constraints != null && this.focusIcons != null)
  323. {
  324. var state = this.graph.view.getState(this.currentFocus.cell);
  325. this.currentFocus = state;
  326. this.currentFocusArea = new mxRectangle(state.x, state.y, state.width, state.height);
  327. for (var i = 0; i < this.constraints.length; i++)
  328. {
  329. var cp = this.graph.getConnectionPoint(state, this.constraints[i]);
  330. var img = this.getImageForConstraint(state, this.constraints[i], cp);
  331. var bounds = new mxRectangle(Math.round(cp.x - img.width / 2),
  332. Math.round(cp.y - img.height / 2), img.width, img.height);
  333. this.focusIcons[i].bounds = bounds;
  334. this.focusIcons[i].redraw();
  335. this.currentFocusArea.add(this.focusIcons[i].bounds);
  336. this.focusPoints[i] = cp;
  337. }
  338. }
  339. };
  340. /**
  341. * Function: setFocus
  342. *
  343. * Transfers the focus to the given state as a source or target terminal. If
  344. * the handler is not enabled then the outline is painted, but the constraints
  345. * are ignored.
  346. */
  347. mxConstraintHandler.prototype.setFocus = function(me, state, source)
  348. {
  349. this.constraints = (state != null && !this.isStateIgnored(state, source) &&
  350. this.graph.isCellConnectable(state.cell)) ? ((this.isEnabled()) ?
  351. (this.graph.getAllConnectionConstraints(state, source) || []) : []) : null;
  352. // Only uses cells which have constraints
  353. if (this.constraints != null)
  354. {
  355. this.currentFocus = state;
  356. this.currentFocusArea = new mxRectangle(state.x, state.y, state.width, state.height);
  357. if (this.focusIcons != null)
  358. {
  359. for (var i = 0; i < this.focusIcons.length; i++)
  360. {
  361. this.focusIcons[i].destroy();
  362. }
  363. this.focusIcons = null;
  364. this.focusPoints = null;
  365. }
  366. this.focusPoints = [];
  367. this.focusIcons = [];
  368. for (var i = 0; i < this.constraints.length; i++)
  369. {
  370. var cp = this.graph.getConnectionPoint(state, this.constraints[i]);
  371. var img = this.getImageForConstraint(state, this.constraints[i], cp);
  372. var src = img.src;
  373. var bounds = new mxRectangle(Math.round(cp.x - img.width / 2),
  374. Math.round(cp.y - img.height / 2), img.width, img.height);
  375. var icon = new mxImageShape(bounds, src);
  376. icon.dialect = (this.graph.dialect != mxConstants.DIALECT_SVG) ?
  377. mxConstants.DIALECT_MIXEDHTML : mxConstants.DIALECT_SVG;
  378. icon.preserveImageAspect = false;
  379. icon.init(this.graph.getView().getDecoratorPane());
  380. // Fixes lost event tracking for images in quirks / IE8 standards
  381. if (mxClient.IS_QUIRKS || document.documentMode == 8)
  382. {
  383. mxEvent.addListener(icon.node, 'dragstart', function(evt)
  384. {
  385. mxEvent.consume(evt);
  386. return false;
  387. });
  388. }
  389. // Move the icon behind all other overlays
  390. if (icon.node.previousSibling != null)
  391. {
  392. icon.node.parentNode.insertBefore(icon.node, icon.node.parentNode.firstChild);
  393. }
  394. var getState = mxUtils.bind(this, function()
  395. {
  396. return (this.currentFocus != null) ? this.currentFocus : state;
  397. });
  398. icon.redraw();
  399. mxEvent.redirectMouseEvents(icon.node, this.graph, getState);
  400. this.currentFocusArea.add(icon.bounds);
  401. this.focusIcons.push(icon);
  402. this.focusPoints.push(cp);
  403. }
  404. this.currentFocusArea.grow(this.getTolerance(me));
  405. }
  406. else
  407. {
  408. this.destroyIcons();
  409. this.destroyFocusHighlight();
  410. }
  411. };
  412. /**
  413. * Function: createHighlightShape
  414. *
  415. * Create the shape used to paint the highlight.
  416. *
  417. * Returns true if the given icon intersects the given point.
  418. */
  419. mxConstraintHandler.prototype.createHighlightShape = function()
  420. {
  421. var hl = new mxRectangleShape(null, this.highlightColor, this.highlightColor, mxConstants.HIGHLIGHT_STROKEWIDTH);
  422. hl.opacity = mxConstants.HIGHLIGHT_OPACITY;
  423. return hl;
  424. };
  425. /**
  426. * Function: intersects
  427. *
  428. * Returns true if the given icon intersects the given rectangle.
  429. */
  430. mxConstraintHandler.prototype.intersects = function(icon, mouse, source, existingEdge)
  431. {
  432. return mxUtils.intersects(icon.bounds, mouse);
  433. };
  434. /**
  435. * Function: destroy
  436. *
  437. * Destroy this handler.
  438. */
  439. mxConstraintHandler.prototype.destroy = function()
  440. {
  441. this.reset();
  442. if (this.resetHandler != null)
  443. {
  444. this.graph.model.removeListener(this.resetHandler);
  445. this.graph.view.removeListener(this.resetHandler);
  446. this.graph.removeListener(this.resetHandler);
  447. this.resetHandler = null;
  448. }
  449. if (this.mouseleaveHandler != null && this.graph.container != null)
  450. {
  451. mxEvent.removeListener(this.graph.container, 'mouseleave', this.mouseleaveHandler);
  452. this.mouseleaveHandler = null;
  453. }
  454. };
  455. __mxOutput.mxConstraintHandler = typeof mxConstraintHandler !== 'undefined' ? mxConstraintHandler : undefined;