UNPKG

3.37 kBJavaScriptView Raw
1/*
2* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
3*/
4/*
5* This is an extension and not part of the main GoJS library.
6* Note that the API for this class may change with any version, even point releases.
7* If you intend to use an extension in production, you should copy the code to your own source directory.
8* Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
9* See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
10*/
11import * as go from '../release/go-module.js';
12/**
13 * This custom {@link Link} class customizes its route to go parallel to other links connecting the same ports,
14 * if the link is not orthogonal and is not Bezier curved.
15 *
16 * If you want to experiment with this extension, try the <a href="../../extensionsJSM/ParallelRoute.html">Parallel Route Links</a> sample.
17 * @category Part Extension
18 */
19export class ParallelRouteLink extends go.Link {
20 /**
21 * Constructs the link's route by modifying {@link #points}.
22 * @return {boolean} true if it computed a route of points
23 */
24 computePoints() {
25 const result = super.computePoints();
26 if (!this.isOrthogonal && this.curve !== go.Link.Bezier && this.hasCurviness()) {
27 const curv = this.computeCurviness();
28 if (curv !== 0) {
29 const num = this.pointsCount;
30 let pidx = 0;
31 let qidx = num - 1;
32 if (num >= 4) {
33 pidx++;
34 qidx--;
35 }
36 const frompt = this.getPoint(pidx);
37 const topt = this.getPoint(qidx);
38 const dx = topt.x - frompt.x;
39 const dy = topt.y - frompt.y;
40 let mx = frompt.x + dx * 1 / 8;
41 let my = frompt.y + dy * 1 / 8;
42 let px = mx;
43 let py = my;
44 if (-0.01 < dy && dy < 0.01) {
45 if (dx > 0)
46 py -= curv;
47 else
48 py += curv;
49 }
50 else {
51 const slope = -dx / dy;
52 let e = Math.sqrt(curv * curv / (slope * slope + 1));
53 if (curv < 0)
54 e = -e;
55 px = (dy < 0 ? -1 : 1) * e + mx;
56 py = slope * (px - mx) + my;
57 }
58 mx = frompt.x + dx * 7 / 8;
59 my = frompt.y + dy * 7 / 8;
60 let qx = mx;
61 let qy = my;
62 if (-0.01 < dy && dy < 0.01) {
63 if (dx > 0)
64 qy -= curv;
65 else
66 qy += curv;
67 }
68 else {
69 const slope = -dx / dy;
70 let e = Math.sqrt(curv * curv / (slope * slope + 1));
71 if (curv < 0)
72 e = -e;
73 qx = (dy < 0 ? -1 : 1) * e + mx;
74 qy = slope * (qx - mx) + my;
75 }
76 this.insertPointAt(pidx + 1, px, py);
77 this.insertPointAt(qidx + 1, qx, qy);
78 }
79 }
80 return result;
81 }
82}