UNPKG

4.64 kBPlain TextView Raw
1/*
2* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
3*/
4
5/*
6* This is an extension and not part of the main GoJS library.
7* Note that the API for this class may change with any version, even point releases.
8* If you intend to use an extension in production, you should copy the code to your own source directory.
9* Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
10* See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
11*/
12
13import * as go from '../release/go-module.js';
14
15/**
16 * This custom {@link Link} class customizes its {@link Shape} to surround the comment node (the from node).
17 * If the Shape is filled, it will obscure the comment itself unless the Link is behind the comment node.
18 * Thus the default layer for BalloonLinks is "Background".
19 *
20 * If you want to experiment with this extension, try the <a href="../../extensionsJSM/BalloonLink.html">Balloon Links</a> sample.
21 * @category Part Extension
22 */
23export class BalloonLink extends go.Link {
24 private _base: number = 10;
25
26 /**
27 * Constructs a BalloonLink and sets the {@link Part#layerName} property to "Background".
28 */
29 constructor() {
30 super();
31 this.layerName = 'Background';
32 }
33
34 /**
35 * Copies properties to a cloned BalloonLink.
36 */
37 protected cloneProtected(copy: this): void {
38 super.cloneProtected(copy);
39 copy._base = this._base;
40 }
41
42 /**
43 * Gets or sets width of the base of the triangle at the center point of the {@link Link#fromNode}.
44 *
45 * The default value is 10.
46 */
47 get base(): number { return this._base; }
48 set base(value: number) { this._base = value; }
49
50 /**
51 * Produce a Geometry from the Link's route that draws a "balloon" shape around the {@link Link#fromNode}
52 * and has a triangular shape with the base at the fromNode and the top at the toNode.
53 */
54 public makeGeometry(): go.Geometry {
55 const fromnode = this.fromNode;
56 const tonode = this.toNode;
57 if (fromnode === null || tonode === null) return super.makeGeometry();
58 // assume the fromNode is the comment and the toNode is the commented-upon node
59 const bb = fromnode.actualBounds;
60 const nb = tonode.actualBounds;
61
62 const p0 = bb.center;
63 let pn = this.getPoint(this.pointsCount - 1);
64 if (bb.intersectsRect(nb)) {
65 pn = nb.center;
66 }
67 const pos = this.routeBounds;
68
69 // compute the intersection points for the triangular arrow
70 const ang = pn.directionPoint(p0);
71 const L = new go.Point(this.base, 0).rotate(ang - 90).add(p0);
72 const R = new go.Point(this.base, 0).rotate(ang + 90).add(p0);
73 this.getLinkPointFromPoint(fromnode, fromnode, L, pn, true, L);
74 this.getLinkPointFromPoint(fromnode, fromnode, R, pn, true, R);
75
76 // form a triangular arrow from the comment to the commented node
77 const fig = new go.PathFigure(pn.x - pos.x, pn.y - pos.y, true); // filled; start at arrow point at commented node
78 fig.add(new go.PathSegment(go.PathSegment.Line, R.x - pos.x, R.y - pos.y)); // a triangle base point on comment's edge
79 let side = 0;
80 if (L.y >= bb.bottom || R.y >= bb.bottom) side = 2;
81 else if (L.x <= bb.x && R.x <= bb.x) side = 1;
82 else if (L.x >= bb.right && R.x >= bb.right) side = 3;
83
84 this.pathToCorner(side, bb, fig, pos, L, R);
85 this.pathToCorner(side + 1, bb, fig, pos, L, R);
86 this.pathToCorner(side + 2, bb, fig, pos, L, R);
87 this.pathToCorner(side + 3, bb, fig, pos, L, R);
88 fig.add(new go.PathSegment(go.PathSegment.Line, L.x - pos.x, L.y - pos.y).close()); // the other triangle base point on comment's edge
89
90 // return a Geometry
91 return new go.Geometry().add(fig);
92 }
93
94 /**
95 * Draw a line to a corner, but not if the comment arrow encompasses that corner.
96 */
97 public pathToCorner(side: number, bb: go.Rect, fig: go.PathFigure, pos: go.Rect, L: go.Point, R: go.Point): void {
98 switch (side % 4) {
99 case 0: if (!(L.y <= bb.y && R.x <= bb.x)) fig.add(new go.PathSegment(go.PathSegment.Line, bb.x - pos.x, bb.y - pos.y)); break;
100 case 1: if (!(L.x <= bb.x && R.y >= bb.bottom)) fig.add(new go.PathSegment(go.PathSegment.Line, bb.x - pos.x, bb.bottom - pos.y)); break;
101 case 2: if (!(L.y >= bb.bottom && R.x >= bb.right)) fig.add(new go.PathSegment(go.PathSegment.Line, bb.right - pos.x, bb.bottom - pos.y)); break;
102 case 3: if (!(L.x >= bb.right && R.y <= bb.y)) fig.add(new go.PathSegment(go.PathSegment.Line, bb.right - pos.x, bb.y - pos.y)); break;
103 }
104 }
105}