@@ -94,20 +94,19 @@ define(function() {
94
94
/**
95
95
* Line constructor. Class to represent the different types of drop zone shapes.
96
96
*
97
- * @param {String } [labelstart] start label of a line.
98
97
* @param {int } [x1] centre X1.
99
98
* @param {int } [y1] centre Y1.
100
99
* @param {int } [startRadius] startRadius.
101
- * @param {String } [labelend] end label of a line.
102
100
* @param {int } [x2] centre X2.
103
101
* @param {int } [y2] centre Y2.
104
102
* @param {int } [endRadius] endRadius.
105
103
* @param {String } [lineType] Line type.
104
+ * @param {String } [labelstart] start label of a line.
105
+ * @param {String } [labelmiddle] middle label of a line.
106
+ * @param {String } [labelend] end label of a line.
106
107
* @constructor
107
108
*/
108
- function Line ( labelstart , x1 , y1 , startRadius , labelend , x2 , y2 , endRadius , lineType ) {
109
- this . labelstart = labelstart ;
110
- this . labelend = labelend ;
109
+ function Line ( x1 , y1 , startRadius , x2 , y2 , endRadius , lineType , labelstart , labelmiddle , labelend ) {
111
110
this . x1 = x1 ;
112
111
this . y1 = y1 ;
113
112
@@ -121,6 +120,10 @@ define(function() {
121
120
this . endRadius = endRadius ;
122
121
123
122
this . lineType = lineType ;
123
+
124
+ this . labelstart = labelstart ;
125
+ this . labelmiddle = labelmiddle ;
126
+ this . labelend = labelend ;
124
127
}
125
128
Line . prototype = new Line ( ) ;
126
129
@@ -178,12 +181,15 @@ define(function() {
178
181
179
182
// Set start and end label attributes.
180
183
svgEl . childNodes [ 3 ] . textContent = this . labelstart ;
181
- svgEl . childNodes [ 3 ] . setAttribute ( 'x' , this . centre1 . x ) ;
182
- svgEl . childNodes [ 3 ] . setAttribute ( 'y' , parseInt ( this . centre1 . y ) + 20 ) ;
184
+ this . adjustTextPosition ( svgEl , svgEl . childNodes [ 3 ] , this . centre1 . x , parseInt ( this . centre1 . y ) ) ;
183
185
184
- svgEl . childNodes [ 4 ] . textContent = this . labelend ;
185
- svgEl . childNodes [ 4 ] . setAttribute ( 'x' , this . centre2 . x ) ;
186
- svgEl . childNodes [ 4 ] . setAttribute ( 'y' , parseInt ( this . centre2 . y ) + 20 ) ;
186
+ svgEl . childNodes [ 4 ] . textContent = this . labelmiddle ;
187
+ let middlex = Math . abs ( ( this . centre1 . x + this . centre2 . x ) / 2 ) ;
188
+ let middley = Math . abs ( ( this . centre1 . y + this . centre2 . y ) / 2 ) ;
189
+ this . adjustTextPosition ( svgEl , svgEl . childNodes [ 4 ] , middlex , middley ) ;
190
+
191
+ svgEl . childNodes [ 5 ] . textContent = this . labelend ;
192
+ this . adjustTextPosition ( svgEl , svgEl . childNodes [ 5 ] , this . centre2 . x , parseInt ( this . centre2 . y ) ) ;
187
193
188
194
// If the svg g element is already placed in dropzone, then add the keyboard support.
189
195
var svgClass = svgEl . getAttribute ( 'class' ) ;
@@ -193,6 +199,41 @@ define(function() {
193
199
}
194
200
} ;
195
201
202
+ /**
203
+ * Update svg line attributes.
204
+ *
205
+ * @param {SVGElement } [svgEl] the g element of the SVG.
206
+ * @param {SVGElement } [svgTextEl] the text node of the SVG.
207
+ * @param {int } [linex] coordinate of the line.
208
+ * @param {int } [liney] coordinate of the line.
209
+ */
210
+ Line . prototype . adjustTextPosition = function ( svgEl , svgTextEl , linex , liney ) {
211
+ // SVG container dimensions
212
+ let parentSVG = svgEl . parentNode ;
213
+ const svgWidth = parentSVG . getAttribute ( 'width' ) ;
214
+ const svgHeight = parentSVG . getAttribute ( 'height' ) ;
215
+ const padding = 20 ;
216
+
217
+ // Text element dimensions.
218
+ const bbox = svgTextEl . getBBox ( ) ;
219
+ const textWidth = bbox . width ;
220
+
221
+ svgTextEl . setAttribute ( 'x' , linex ) ;
222
+ svgTextEl . setAttribute ( 'y' , liney + padding ) ;
223
+
224
+ // Recalculate the position of x and y coordinates of text, to make sure the text content is fully displayed.
225
+ if ( linex < textWidth / 2 ) {
226
+ svgTextEl . setAttribute ( 'x' , Math . abs ( textWidth / 2 ) ) ;
227
+ } else if ( ( linex + ( textWidth / 2 ) ) > svgWidth ) {
228
+ svgTextEl . setAttribute ( 'x' , Math . abs ( svgWidth - ( textWidth / 2 ) ) ) ;
229
+ }
230
+
231
+ if ( liney + padding > svgHeight ) {
232
+ // Adjust if the line is very near to the bottom of the svg.
233
+ svgTextEl . setAttribute ( 'y' , liney - padding ) ;
234
+ }
235
+ } ;
236
+
196
237
/**
197
238
* Update svg line attributes.
198
239
*
@@ -607,6 +648,7 @@ define(function() {
607
648
var endcirleEl = createSvgElement ( svgEl , 'circle' ) ;
608
649
endcirleEl . setAttribute ( 'class' , 'endcircle shape' ) ;
609
650
createSvgElement ( svgEl , 'text' ) . setAttribute ( 'class' , 'labelstart shapeLabel' ) ;
651
+ createSvgElement ( svgEl , 'text' ) . setAttribute ( 'class' , 'labelmiddle shapeLabel' ) ;
610
652
createSvgElement ( svgEl , 'text' ) . setAttribute ( 'class' , 'labelend shapeLabel' ) ;
611
653
return svgEl ;
612
654
}
@@ -627,15 +669,16 @@ define(function() {
627
669
/**
628
670
* Line constructor. Class to represent the different types of drop zone shapes.
629
671
*
630
- * @param {String } [labelstart] start label of a line.
631
672
* @param {int } [x1] centre X1.
632
673
* @param {int } [y1] centre Y1.
633
674
* @param {int } [startRadius] startRadius.
634
- * @param {String } [labelend] end label of a line.
635
675
* @param {int } [x2] centre X2.
636
676
* @param {int } [y2] centre Y2.
637
677
* @param {int } [endRadius] endRadius.
638
678
* @param {String } [lineType] Line type.
679
+ * @param {String } [labelstart] start label of a line.
680
+ * @param {String } [labelmiddle] middle label of a line.
681
+ * @param {String } [labelend] end label of a line.
639
682
* @constructor
640
683
*/
641
684
Line : Line ,
@@ -652,20 +695,21 @@ define(function() {
652
695
/**
653
696
* Make a line of the given type.
654
697
*
655
- * @param {Array } linecoordinates in the format (x,y;radius).
656
- * @param {Array } labels Start and end labels of a line .
657
- * @param {String } lineType The linetype (e.g., linesinglearrow, linedoublearrows, ...) .
698
+ * @param {Array } [ linecoordinates] in the format (x,y;radius).
699
+ * @param {String } [lineType] The linetype (e.g., linesinglearrow, linedoublearrows, ...) .
700
+ * @param {Array } [labels] Start, middle and end labels of a line .
658
701
* @return {Line } the new line.
659
702
*/
660
- make : function ( linecoordinates , labels , lineType ) {
703
+ make : function ( linecoordinates , lineType , labels ) {
661
704
// Line coordinates are in the format (x,y;radius).
662
705
var startcoordinates = linecoordinates [ 0 ] . split ( ';' ) ;
663
706
var endcoordinates = linecoordinates [ 1 ] . split ( ';' ) ;
664
707
var linestartbits = startcoordinates [ 0 ] . split ( ',' ) ;
665
708
var lineendbits = endcoordinates [ 0 ] . split ( ',' ) ;
666
709
667
- return new Line ( labels [ 0 ] , parseInt ( linestartbits [ 0 ] ) , parseInt ( linestartbits [ 1 ] ) , parseInt ( startcoordinates [ 1 ] ) ,
668
- labels [ 1 ] , parseInt ( lineendbits [ 0 ] ) , parseInt ( lineendbits [ 1 ] ) , parseInt ( endcoordinates [ 1 ] ) , lineType ) ;
710
+ return new Line ( parseInt ( linestartbits [ 0 ] ) , parseInt ( linestartbits [ 1 ] ) , parseInt ( startcoordinates [ 1 ] ) ,
711
+ parseInt ( lineendbits [ 0 ] ) , parseInt ( lineendbits [ 1 ] ) , parseInt ( endcoordinates [ 1 ] ) , lineType ,
712
+ labels [ 0 ] , labels [ 1 ] , labels [ 2 ] ) ;
669
713
} ,
670
714
671
715
/**
@@ -676,8 +720,9 @@ define(function() {
676
720
* @return {line } the similar line of a different linetype.
677
721
*/
678
722
getSimilar : function ( lineType , line ) {
679
- return new Line ( line . labelstart , parseInt ( line . x1 ) , parseInt ( line . y1 ) , parseInt ( line . startRadius ) ,
680
- line . labelend , parseInt ( line . x2 ) , parseInt ( line . y2 ) , parseInt ( line . endRadius ) , lineType ) ;
723
+ return new Line ( parseInt ( line . x1 ) , parseInt ( line . y1 ) , parseInt ( line . startRadius ) ,
724
+ parseInt ( line . x2 ) , parseInt ( line . y2 ) , parseInt ( line . endRadius ) , lineType ,
725
+ line . labelstart , line . labelmiddle , line . labelend ) ;
681
726
}
682
727
} ;
683
728
} ) ;
0 commit comments