DistanceGuides.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /**
  2. * Copyright (c) 2017, CTI LOGIC
  3. * Copyright (c) 2006-2017, JGraph Ltd
  4. * Copyright (c) 2006-2017, Gaudenz Alder
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  9. *
  10. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  11. *
  12. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  15. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  17. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18. */
  19. //TODO integrate this code in mxGuide (Especially as this is now affecting the other guides)
  20. (function()
  21. {
  22. var guideMove = mxGuide.prototype.move;
  23. mxGuide.prototype.move = function (bounds, delta, gridEnabled, clone)
  24. {
  25. var yShift = delta.y;
  26. var xShift = delta.x;
  27. var hasHorGuides = false;
  28. var hasVerGuides = false;
  29. if (this.states != null && bounds != null && delta != null)
  30. {
  31. var guide = this;
  32. var newState = new mxCellState();
  33. var scale = this.graph.getView().scale;
  34. var tolerance = Math.max(2, this.getGuideTolerance() / 2);
  35. newState.x = bounds.x + xShift;
  36. newState.y = bounds.y + yShift;
  37. newState.width = bounds.width;
  38. newState.height = bounds.height;
  39. var verticalCells = [];
  40. var horizontalCells = [];
  41. //although states are defined as cellState, it has some mxRectangles!
  42. var states = [];
  43. for (var i = 0; i < this.states.length; i++)
  44. {
  45. var state = this.states[i];
  46. var found = false;
  47. if (state instanceof mxCellState)
  48. {
  49. if (clone || !this.graph.isCellSelected(state.cell))
  50. {
  51. if (((newState.x >= state.x && newState.x <= (state.x + state.width))
  52. || (state.x >= newState.x && state.x <= (newState.x + newState.width)))
  53. && (newState.y > state.y + state.height + 4|| newState.y + newState.height + 4 < state.y)) // + 4 to avoid having dy = 0 considered which cause a bug with 3 cells case
  54. {
  55. verticalCells.push(state);
  56. }
  57. else if (((newState.y >= state.y && newState.y <= (state.y + state.height))
  58. || (state.y >= newState.y && state.y <= (newState.y + newState.height)))
  59. && (newState.x > state.x + state.width + 4 || newState.x + newState.width + 4 < state.x)) // + 4 to avoid having dy = 0 considered which cause a bug with 3 cells case
  60. {
  61. horizontalCells.push(state);
  62. }
  63. }
  64. }
  65. }
  66. var eqCy = 0;
  67. var dy = 0;
  68. var fixedDy = 0;
  69. var midDy = 0;
  70. var eqCx = 0;
  71. var dx = 0;
  72. var fixedDx = 0;
  73. var midDx = 0;
  74. var shift = 5 * scale;
  75. if (verticalCells.length > 1)
  76. {
  77. verticalCells.push(newState);
  78. verticalCells.sort(function(s1, s2)
  79. {
  80. return s1.y - s2.y;
  81. });
  82. var newStatePassed = false;
  83. var firstMoving = newState == verticalCells[0];
  84. var lastMoving = newState == verticalCells[verticalCells.length - 1];
  85. //find the mid space and use it as dy and fixedDy
  86. if (!firstMoving && !lastMoving)
  87. {
  88. for (var i = 1; i < verticalCells.length - 1; i++)
  89. {
  90. if (newState == verticalCells[i])
  91. {
  92. var s1 = verticalCells[i - 1];
  93. var s3 = verticalCells[i + 1];
  94. midDy = (s3.y - s1.y - s1.height - newState.height) / 2;
  95. dy = midDy;
  96. fixedDy = dy;
  97. break;
  98. }
  99. }
  100. }
  101. for (var i = 0; i < verticalCells.length - 1; i++)
  102. {
  103. var s1 = verticalCells[i];
  104. var s2 = verticalCells[i + 1];
  105. var isMovingOne = newState == s1 || newState == s2;
  106. var curDy = s2.y - s1.y - s1.height;
  107. newStatePassed |= newState == s1;
  108. if (dy == 0 && eqCy == 0)
  109. {
  110. dy = curDy;
  111. eqCy = 1;
  112. }
  113. else if (Math.abs(dy - curDy) <= (isMovingOne || (i == 1 && newStatePassed)? tolerance : 0)) //non-moving cells must have exact same dy, must handle the case of having the first cell moving so we allow tolerance for second cell (until fixedDy is non-zero)
  114. {
  115. eqCy += 1;
  116. }
  117. else if (eqCy > 1 && newStatePassed) //stop and ignore the following cells
  118. {
  119. verticalCells = verticalCells.slice(0, i + 1);
  120. break;
  121. }
  122. else if (verticalCells.length - i >= 3 && !newStatePassed) //reset and start counting again
  123. {
  124. eqCy = 0;
  125. dy = midDy != 0? midDy : 0;
  126. fixedDy = dy;
  127. verticalCells.splice(0, i == 0? 1 : i);
  128. i = -1;
  129. }
  130. else
  131. {
  132. break;
  133. }
  134. if (fixedDy == 0 && !isMovingOne)
  135. {
  136. fixedDy = curDy;
  137. //Update dy such that following cells shows equal distance guides without tolerance
  138. dy = fixedDy;
  139. }
  140. }
  141. if (verticalCells.length == 3 && verticalCells[1] == newState)
  142. {
  143. fixedDy = 0;
  144. }
  145. }
  146. if (horizontalCells.length > 1)
  147. {
  148. horizontalCells.push(newState)
  149. horizontalCells.sort(function(s1, s2)
  150. {
  151. return s1.x - s2.x;
  152. });
  153. var newStatePassed = false;
  154. var firstMoving = newState == horizontalCells[0];
  155. var lastMoving = newState == horizontalCells[horizontalCells.length - 1];
  156. //find the mid space and use it as dx and fixedDx
  157. if (!firstMoving && !lastMoving)
  158. {
  159. for (var i = 1; i < horizontalCells.length - 1; i++)
  160. {
  161. if (newState == horizontalCells[i])
  162. {
  163. var s1 = horizontalCells[i - 1];
  164. var s3 = horizontalCells[i + 1];
  165. midDx = (s3.x - s1.x - s1.width - newState.width) / 2;
  166. dx = midDx;
  167. fixedDx = dx;
  168. break;
  169. }
  170. }
  171. }
  172. for (var i = 0; i < horizontalCells.length - 1; i++)
  173. {
  174. var s1 = horizontalCells[i];
  175. var s2 = horizontalCells[i + 1];
  176. var isMovingOne = newState == s1 || newState == s2;
  177. var curDx = s2.x - s1.x - s1.width;
  178. newStatePassed |= newState == s1;
  179. if (dx == 0 && eqCx == 0)
  180. {
  181. dx = curDx;
  182. eqCx = 1;
  183. }
  184. else if (Math.abs(dx - curDx) <= (isMovingOne || (i == 1 && newStatePassed)? tolerance : 0))
  185. {
  186. eqCx += 1;
  187. }
  188. else if (eqCx > 1 && newStatePassed) //stop and ignore the following cells
  189. {
  190. horizontalCells = horizontalCells.slice(0, i + 1);
  191. break;
  192. }
  193. else if (horizontalCells.length - i >= 3 && !newStatePassed) //reset and start counting again
  194. {
  195. eqCx = 0;
  196. dx = midDx != 0? midDx : 0;
  197. fixedDx = dx;
  198. horizontalCells.splice(0, i == 0? 1 : i);
  199. i = -1;
  200. }
  201. else
  202. {
  203. break;
  204. }
  205. if (fixedDx == 0 && !isMovingOne)
  206. {
  207. fixedDx = curDx;
  208. //Update dx such that following cells shows equal distance guides without tolerance
  209. dx = fixedDx;
  210. }
  211. }
  212. if (horizontalCells.length == 3 && horizontalCells[1] == newState)
  213. {
  214. fixedDx = 0;
  215. }
  216. }
  217. var createEqGuide = function(p1, p2, curGuide, isVer)
  218. {
  219. var points = [];
  220. var dx = 0
  221. var dy = 0;
  222. if (isVer)
  223. {
  224. dx = shift;
  225. dy = 0;
  226. }
  227. else
  228. {
  229. dx = 0;
  230. dy = shift;
  231. }
  232. points.push(new mxPoint(p1.x - dx, p1.y - dy));
  233. points.push(new mxPoint(p1.x + dx, p1.y + dy));
  234. points.push(p1);
  235. points.push(p2);
  236. points.push(new mxPoint(p2.x - dx, p2.y - dy));
  237. points.push(new mxPoint(p2.x + dx, p2.y + dy));
  238. if (curGuide != null)
  239. {
  240. curGuide.points = points;
  241. return curGuide;
  242. }
  243. else
  244. {
  245. var guideEq = new mxPolyline(points, mxConstants.GUIDE_COLOR, mxConstants.GUIDE_STROKEWIDTH);
  246. guideEq.dialect = mxConstants.DIALECT_SVG;
  247. guideEq.pointerEvents = false;
  248. guideEq.init(guide.graph.getView().getOverlayPane());
  249. return guideEq;
  250. }
  251. };
  252. var hideEqGuides = function(horizontal, vertical)
  253. {
  254. if (horizontal && guide.guidesArrHor != null)
  255. {
  256. for (var i = 0; i < guide.guidesArrHor.length; i++)
  257. {
  258. guide.guidesArrHor[i].node.style.visibility = "hidden";
  259. }
  260. }
  261. if (vertical && guide.guidesArrVer != null)
  262. {
  263. for (var i = 0; i < guide.guidesArrVer.length; i++)
  264. {
  265. guide.guidesArrVer[i].node.style.visibility = "hidden";
  266. }
  267. }
  268. };
  269. if (eqCx > 1 && eqCx == horizontalCells.length - 1)
  270. {
  271. var guidesArr = [];
  272. var curArr = guide.guidesArrHor;
  273. var hPoints = [];
  274. var newX = 0;
  275. //If the newState (moving cell) is the first one, use the next one for x coordinate such that the guide doesn't move with the cell
  276. var firstI = horizontalCells[0] == newState? 1 : 0;
  277. var firstY = horizontalCells[firstI].y + horizontalCells[firstI].height;
  278. if (fixedDx > 0)
  279. {
  280. for (var i = 0; i < horizontalCells.length - 1; i++)
  281. {
  282. var s1 = horizontalCells[i];
  283. var s2 = horizontalCells[i + 1];
  284. if (newState == s1)
  285. {
  286. newX = s2.x - s1.width - fixedDx;
  287. hPoints.push(new mxPoint(newX + s1.width + shift, firstY));
  288. hPoints.push(new mxPoint(s2.x - shift, firstY));
  289. }
  290. else if (newState == s2)
  291. {
  292. hPoints.push(new mxPoint(s1.x + s1.width + shift, firstY));
  293. newX = s1.x + s1.width + fixedDx;
  294. hPoints.push(new mxPoint(newX - shift, firstY));
  295. }
  296. else
  297. {
  298. hPoints.push(new mxPoint(s1.x + s1.width + shift, firstY));
  299. hPoints.push(new mxPoint(s2.x - shift, firstY));
  300. }
  301. }
  302. }
  303. else //this is the case when there are 3 cells and the middle one is moving
  304. {
  305. var s1 = horizontalCells[0];
  306. var s3 = horizontalCells[2];
  307. newX = s1.x + s1.width + (s3.x - s1.x - s1.width - newState.width) / 2;
  308. hPoints.push(new mxPoint(s1.x + s1.width + shift, firstY));
  309. hPoints.push(new mxPoint(newX - shift, firstY));
  310. hPoints.push(new mxPoint(newX + newState.width + shift, firstY));
  311. hPoints.push(new mxPoint(s3.x - shift, firstY));
  312. }
  313. for (var i = 0; i < hPoints.length; i += 2)
  314. {
  315. var p1 = hPoints[i];
  316. var p2 = hPoints[i+1];
  317. var guideEq = createEqGuide(p1, p2, curArr != null ? curArr[i/2] : null);
  318. guideEq.node.style.visibility = "visible";
  319. guideEq.redraw();
  320. guidesArr.push(guideEq);
  321. }
  322. //destroy old non-recycled guides
  323. for (var i = hPoints.length / 2; curArr != null && i < curArr.length; i ++)
  324. {
  325. curArr[i].destroy();
  326. }
  327. guide.guidesArrHor = guidesArr;
  328. xShift = newX - bounds.x;
  329. hasHorGuides = true;
  330. }
  331. else
  332. {
  333. hideEqGuides(true);
  334. }
  335. if (eqCy > 1 && eqCy == verticalCells.length - 1)
  336. {
  337. var guidesArr = [];
  338. var curArr = guide.guidesArrVer;
  339. var vPoints = [];
  340. var newY = 0;
  341. //If the newState (moving cell) is the first one, use the next one for x coordinate such that the guide doesn't move with the cell
  342. var firstI = verticalCells[0] == newState? 1 : 0;
  343. var firstX = verticalCells[firstI].x + verticalCells[firstI].width;
  344. if (fixedDy > 0)
  345. {
  346. for (var i = 0; i < verticalCells.length - 1; i++)
  347. {
  348. var s1 = verticalCells[i];
  349. var s2 = verticalCells[i + 1];
  350. if (newState == s1)
  351. {
  352. newY = s2.y - s1.height - fixedDy;
  353. vPoints.push(new mxPoint(firstX, newY + s1.height + shift));
  354. vPoints.push(new mxPoint(firstX, s2.y - shift));
  355. }
  356. else if (newState == s2)
  357. {
  358. vPoints.push(new mxPoint(firstX, s1.y + s1.height + shift));
  359. newY = s1.y + s1.height + fixedDy;
  360. vPoints.push(new mxPoint(firstX, newY - shift));
  361. }
  362. else
  363. {
  364. vPoints.push(new mxPoint(firstX, s1.y + s1.height + shift));
  365. vPoints.push(new mxPoint(firstX, s2.y - shift));
  366. }
  367. }
  368. }
  369. else //this is the case when there are 3 cells and the middle one is moving
  370. {
  371. var s1 = verticalCells[0];
  372. var s3 = verticalCells[2];
  373. newY = s1.y + s1.height + (s3.y - s1.y - s1.height - newState.height) / 2;
  374. vPoints.push(new mxPoint(firstX, s1.y + s1.height + shift));
  375. vPoints.push(new mxPoint(firstX, newY - shift));
  376. vPoints.push(new mxPoint(firstX, newY + newState.height + shift));
  377. vPoints.push(new mxPoint(firstX, s3.y - shift));
  378. }
  379. for (var i = 0; i < vPoints.length; i += 2)
  380. {
  381. var p1 = vPoints[i];
  382. var p2 = vPoints[i+1];
  383. var guideEq = createEqGuide(p1, p2, curArr != null ? curArr[i/2] : null, true);
  384. guideEq.node.style.visibility = "visible";
  385. guideEq.redraw();
  386. guidesArr.push(guideEq);
  387. }
  388. //destroy old non-recycled guides
  389. for (var i = vPoints.length / 2; curArr != null && i < curArr.length; i ++)
  390. {
  391. curArr[i].destroy();
  392. }
  393. guide.guidesArrVer = guidesArr;
  394. yShift = newY - bounds.y;
  395. hasVerGuides = true;
  396. }
  397. else
  398. {
  399. hideEqGuides(false, true);
  400. }
  401. }
  402. if (hasHorGuides || hasVerGuides)
  403. {
  404. var eqPoint = new mxPoint(xShift, yShift);
  405. var newPoint = guideMove.call(this, bounds, eqPoint, gridEnabled, clone);
  406. //Adjust our point to match non-conflicting other guides
  407. if (hasHorGuides && !hasVerGuides)
  408. {
  409. eqPoint.y = newPoint.y;
  410. }
  411. else if (hasVerGuides && !hasHorGuides)
  412. {
  413. eqPoint.x = newPoint.x;
  414. }
  415. //Hide other guide if this guide overrides them
  416. if (newPoint.y != eqPoint.y)
  417. {
  418. if (this.guideY != null && this.guideY.node != null)
  419. {
  420. this.guideY.node.style.visibility = 'hidden';
  421. }
  422. }
  423. if (newPoint.x != eqPoint.x)
  424. {
  425. if (this.guideX != null && this.guideX.node != null)
  426. {
  427. this.guideX.node.style.visibility = 'hidden';
  428. }
  429. }
  430. return eqPoint;
  431. }
  432. else
  433. {
  434. hideEqGuides(true, true);
  435. return guideMove.apply(this, arguments);
  436. }
  437. };
  438. var guideSetVisible = mxGuide.prototype.setVisible;
  439. mxGuide.prototype.setVisible = function (visible)
  440. {
  441. var guide = this;
  442. guideSetVisible.call(guide, visible);
  443. var guidesArrVer = guide.guidesArrVer;
  444. var guidesArrHor = guide.guidesArrHor;
  445. if (guidesArrVer != null)
  446. {
  447. for (var i = 0; i < guidesArrVer.length; i++)
  448. {
  449. guidesArrVer[i].node.style.visibility = visible? "visible" : "hidden";
  450. }
  451. }
  452. if (guidesArrHor != null)
  453. {
  454. for (var i = 0; i < guidesArrHor.length; i++)
  455. {
  456. guidesArrHor[i].node.style.visibility = visible? "visible" : "hidden";
  457. }
  458. }
  459. };
  460. var guideDestroy = mxGuide.prototype.destroy;
  461. mxGuide.prototype.destroy = function()
  462. {
  463. guideDestroy.call(this);
  464. var guidesArrVer = this.guidesArrVer;
  465. var guidesArrHor = this.guidesArrHor;
  466. if (guidesArrVer != null)
  467. {
  468. for (var i = 0; i < guidesArrVer.length; i++)
  469. {
  470. guidesArrVer[i].destroy();
  471. }
  472. this.guidesArrVer = null;
  473. }
  474. if (guidesArrHor != null)
  475. {
  476. for (var i = 0; i < guidesArrHor.length; i++)
  477. {
  478. guidesArrHor[i].destroy();
  479. }
  480. this.guidesArrHor = null;
  481. }
  482. };
  483. })();