@@ -6,6 +6,7 @@ class Link {
66 this . links = [ ] ;
77 this . reltype = reltype ;
88 this . top = top ;
9+ this . visible = true ;
910
1011 this . slot = 0 ;
1112
@@ -45,36 +46,21 @@ class Link {
4546 this . recalculateSlots ( words ) ;
4647
4748 // init handles
48- const s = 4 ;
49-
50- // draw trigger
49+ // get location of trigger
5150 if ( this . trigger ) {
52- // draw a diamond at the location of the trigger
5351 let offset = this . trigger . links . filter ( l => l . top == this . top ) . indexOf ( this ) ;
5452 let x = this . trigger . cx + 8 * offset ;
5553 let y = this . top ? this . trigger . absoluteY : this . trigger . absoluteDescent ;
56-
57- let handle = this . svg . path ( `M${ s } ,0L0,${ s } L${ - s } ,0L0,${ - s } Z` )
58- . x ( x - s )
59- . y ( y - s ) ;
60- this . handles . push ( { anchor : this . trigger , handle, x, y, offset } ) ;
54+ this . handles . push ( { anchor : this . trigger , x, y, offset } ) ;
6155 }
6256
6357 // draw arguments
6458 this . arguments . forEach ( arg => {
65- // draw a triangle at the location of the argument
59+ // get location of the argument
6660 let offset = arg . anchor . links . filter ( l => l . top == this . top ) . indexOf ( this ) ;
6761 let x = arg . anchor . cx + 8 * offset ;
6862 let y = this . top ? arg . anchor . absoluteY : arg . anchor . absoluteDescent ;
69-
70- let handle = (
71- this . top ?
72- this . svg . path ( `M${ [ s , - s / 2 ] } L${ [ - s , - s / 2 ] } L0,${ s } ` ) :
73- this . svg . path ( `M0,${ - s / 2 } L${ [ - s , s ] } L${ [ s , s ] } ` )
74- )
75- . x ( x - s )
76- . y ( y - s ) ;
77- this . handles . push ( { anchor : arg . anchor , handle, x, y, offset } ) ;
63+ this . handles . push ( { anchor : arg . anchor , x, y, offset } ) ;
7864
7965 // draw svgText for each trigger-argument relation
8066 if ( this . trigger ) {
@@ -98,18 +84,34 @@ class Link {
9884 this . draw ( ) ;
9985 }
10086
87+ toggle ( ) {
88+ this . visible = ! this . visible ;
89+ if ( this . visible ) { this . show ( ) ; }
90+ else { this . hide ( ) ; }
91+ }
92+ show ( ) {
93+ this . visible = true ;
94+ if ( this . svg ) {
95+ this . svg . show ( ) ;
96+ this . draw ( ) ;
97+ }
98+ }
99+ hide ( ) {
100+ this . visible = false ;
101+ if ( this . svg ) {
102+ this . svg . hide ( ) ;
103+ }
104+ }
105+
101106 draw ( anchor ) {
102- const s = 4 ;
103107 // redraw handles if word or link was moved
104- this . handles . forEach ( h => {
105- if ( anchor === h . anchor ) {
106- h . x = anchor . cx + 8 * h . offset ;
107- h . y = this . top ? anchor . absoluteY : anchor . absoluteDescent ;
108- h . handle
109- . x ( h . x - s )
110- . y ( h . y - s ) ;
111- }
112- } ) ;
108+ let h = this . handles . find ( h => ( anchor === h . anchor ) ) ;
109+ if ( h ) {
110+ h . x = anchor . cx + 8 * h . offset ;
111+ h . y = this . top ? anchor . absoluteY : anchor . absoluteDescent ;
112+ }
113+
114+ if ( ! this . visible ) { return ; }
113115
114116 // redraw line if it exists
115117 if ( this . line ) {
@@ -164,6 +166,10 @@ class Link {
164166 . x ( handle1 . x + dx + textlen / 2 )
165167 . y ( y - 10 ) ;
166168
169+ // draw an arrow at the handle
170+ const s = 4 ;
171+ d += this . arrowhead ( handle1 ) ;
172+
167173 let handlePrecedesTrigger = leftOfTrigger && ( i + 2 > il || this . arguments [ i + 1 ] . anchor . idx >= this . trigger . idx ) ;
168174
169175 // check if crossing over a row
@@ -218,7 +224,9 @@ class Link {
218224 + 'L' + [ avg - textlen / 2 , y ]
219225 + 'm' + [ textlen , 0 ]
220226 + 'L' + [ endHandle . x - 5 , y ]
221- + 'C' + [ endHandle . x , y , endHandle . x , y , endHandle . x , endHandle . y ] ;
227+ + 'C' + [ endHandle . x , y , endHandle . x , y , endHandle . x , endHandle . y ]
228+ + this . arrowhead ( this . handles [ 0 ] )
229+ + this . arrowhead ( endHandle ) ;
222230 }
223231 else {
224232 avg = ( this . handles [ 0 ] . x + width ) / 2 ;
@@ -231,7 +239,9 @@ class Link {
231239 let tempY = this . getY ( endHandle ) ;
232240 d += 'M0,' + tempY
233241 + 'L' + [ endHandle . x - 5 , tempY ]
234- + 'C' + [ endHandle . x , tempY , endHandle . x , tempY , endHandle . x , endHandle . y ] ;
242+ + 'C' + [ endHandle . x , tempY , endHandle . x , tempY , endHandle . x , endHandle . y ]
243+ + this . arrowhead ( this . handles [ 0 ] )
244+ + this . arrowhead ( endHandle ) ;
235245 }
236246 this . svgTexts [ 0 ] . x ( avg )
237247 . y ( y - 10 ) ;
@@ -251,6 +261,14 @@ class Link {
251261 : r . rh + r . ry + 25 - 15 * this . slot ;
252262 }
253263
264+ // helper function to return a path string for an arrowhead
265+ arrowhead ( handle ) {
266+ const s = 4 ;
267+ return this . top ?
268+ 'M' + [ handle . x - s , handle . y - s ] + 'l' + [ s , s * 1.5 ] + 'l' + [ s , - s * 1.5 ] :
269+ 'M' + [ handle . x - s , handle . y + s ] + 'l' + [ s , - s * 1.5 ] + 'l' + [ s , s * 1.5 ] ;
270+ }
271+
254272 remove ( ) {
255273 this . svg . remove ( ) ;
256274
0 commit comments