@@ -2,11 +2,9 @@ class Link {
22 constructor ( eventId , trigger , args , reltype ) {
33 this . eventId = eventId ;
44 this . trigger = trigger ;
5- this . arguments = args ;
5+ this . arguments = args . sort ( ( a , b ) => a . anchor . idx - b . anchor . idx ) ;
66 this . links = [ ] ;
77 this . reltype = reltype ;
8- // console.log('trigger\t\t', this.val);
9- // console.log.apply(null, args);
108
119 if ( this . trigger ) { this . trigger . links . push ( this ) ; }
1210 this . arguments . forEach ( arg => arg . anchor . links . push ( this ) ) ;
@@ -76,41 +74,58 @@ class Link {
7674 }
7775 } )
7876 if ( this . line ) {
79- let d ;
77+ let d = '' ;
8078 if ( this . trigger ) {
81- d = this . arguments . map ( ( arg , i ) => {
82- let tx = this . trigger . cx
83- , ty = this . trigger . absoluteY
84- , ax = arg . anchor . cx
85- , ay = arg . anchor . absoluteY ;
86-
87- let dx = ( tx < ax ) ? 10 : - 10 ;
88- let dy = ty - ay ; // FIXME: this only handles case for trigger.y on lower level than arg.y
89-
90- // get text position
91- let len = this . svgTexts [ i ]
92- . x ( ( ax + tx ) / 2 )
93- . y ( arg . anchor . absoluteY - 10 - 7 )
94- . length ( ) / 2 ;
95-
96- let cx = ( ax - tx ) / 2 - dx ;
97- if ( cx < 0 ) { len = - len ; }
98-
99- // get path string
100- return 'M' + [ tx , ty ]
101- + 'c' + [ 0 , - 10 - dy , 0 , - 10 - dy , dx , - 10 - dy ]
102- + 'l' + [ cx - len , 0 ]
103- + 'm' + [ len * 2 , 0 ]
104- + 'l' + [ cx - len , 0 ]
105- + 'c' + [ dx , 0 , dx , 10 , dx , 10 ] ;
106- } ) . join ( ) ;
79+ // draw a polyline between the trigger and each of its arguments
80+ for ( let i = 0 , il = this . arguments . length ; i < il ; ++ i ) {
81+ let leftOfTrigger = this . arguments [ i ] . anchor . idx < this . trigger . idx ;
82+ let dx = leftOfTrigger ? 5 : - 5 ;
83+ let textlen = leftOfTrigger ? this . svgTexts [ i ] . length ( ) : - this . svgTexts [ i ] . length ( ) ;
84+
85+ // draw a line from the prev arrow segment
86+ if ( i > 0 ) {
87+ if ( leftOfTrigger ) {
88+ d += 'L' + [ this . handles [ i + 1 ] . x + dx , this . handles [ i + 1 ] . y - 10 ]
89+ }
90+ else {
91+ d += 'L' + [ this . handles [ i + 1 ] . x + dx + textlen , this . handles [ i + 1 ] . y - 10 ]
92+ }
93+ }
94+ else if ( ! leftOfTrigger ) {
95+ // start drawing from the trigger
96+ d += 'M' + [ this . handles [ 0 ] . x , this . handles [ 0 ] . y ]
97+ + 'c' + [ 0 , - 10 , 0 , - 10 , - dx , - 10 ]
98+ + 'L' + [ this . handles [ 1 ] . x + dx + textlen , this . handles [ 1 ] . y - 10 ] ;
99+ }
100+
101+ // draw an arrow segment coming from each argument
102+ d += 'M' + [ this . handles [ i + 1 ] . x , this . handles [ i + 1 ] . y ]
103+ + 'c' + [ 0 , - 10 , 0 , - 10 , dx , - 10 ] ;
104+ if ( leftOfTrigger ) {
105+ d += 'm' + [ textlen , 0 ] ;
106+ }
107+
108+ if ( leftOfTrigger && ( i + 2 > il || this . arguments [ i + 1 ] . anchor . idx >= this . trigger . idx ) ) {
109+ // draw trigger to the right of the arrow segment
110+ d += 'L' + [ this . handles [ 0 ] . x - dx , this . handles [ 0 ] . y - 10 ]
111+ + 'c' + [ dx , 0 , dx , 0 , dx , 10 ] ;
112+ if ( i + 1 < il ) {
113+ d += 'c' + [ 0 , - 10 , 0 , - 10 , dx , - 10 ] ;
114+ }
115+ }
116+
117+ // draw the text svg
118+ this . svgTexts [ i ]
119+ . x ( this . handles [ i + 1 ] . x + dx + textlen / 2 )
120+ . y ( this . handles [ i + 1 ] . y - 10 - 7 ) ;
121+ }
107122 }
108- else if ( this . relType ) {
123+ else if ( this . reltype ) {
109124 let avg = this . arguments . reduce ( ( acc , a ) => acc + a . anchor . cx , 0 ) / this . arguments . length ;
110125 this . svgTexts [ 0 ] . x ( avg )
111126 . y ( this . arguments [ 0 ] . anchor . absoluteY - 10 - 7 ) ;
112- d = 'M' + [ this . arguments [ 0 ] . anchor . cx , this . arguments [ 0 ] . anchor . absoluteY ]
113- + 'L' + [ this . arguments [ 1 ] . anchor . cx , this . arguments [ 1 ] . anchor . absoluteY ] ;
127+ // d = 'M' + [this.arguments[0].anchor.cx, this.arguments[0].anchor.absoluteY]
128+ // + 'L' + [this.arguments[1].anchor.cx, this.arguments[1].anchor.absoluteY];
114129 }
115130 this . line . plot ( d ) ;
116131 }
@@ -140,6 +155,13 @@ class Link {
140155 return this . arguments . find ( arg => arg . anchor === a ) ;
141156 }
142157
158+ get idx ( ) {
159+ if ( this . trigger ) {
160+ return this . trigger . idx ;
161+ }
162+ return this . arguments . reduce ( ( acc , arg ) => acc + arg . anchor . idx , 0 ) / this . arguments . length ;
163+ }
164+
143165 get cx ( ) {
144166 if ( this . trigger ) {
145167 // if (this.trigger.links.length > 1) {
@@ -149,7 +171,7 @@ class Link {
149171 return this . trigger . cx ;
150172 }
151173 if ( this . arguments . length > 0 ) {
152- return this . arguments . reduce ( ( acc , arg ) => acc + arg . cx , 0 ) / this . arguments . length ;
174+ return this . arguments . reduce ( ( acc , arg ) => acc + arg . anchor . cx , 0 ) / this . arguments . length ;
153175 }
154176 return 0 ;
155177 }
0 commit comments