UNPKG

3.41 kBJavaScriptView Raw
1import RRElement from './rrelement';
2import LayoutInfo from './layoutinfo';
3
4export default class RRChoice extends RRElement {
5
6 /**
7 * @param {(RRElement[] | RRElement)} rrElements
8 */
9 constructor(rrElements) {
10 super();
11 if(arguments.length == 0) {
12 rrElements = [];
13 } else if(rrElements.constructor !== Array) {
14 rrElements = arguments;
15 }
16 this.rrElements = rrElements;
17 }
18
19 computeLayoutInfo(rrDiagramToSVG) {
20 let width = 0;
21 let height = 0;
22 let connectorOffset = 0;
23 for (let i = 0; i < this.rrElements.length; i++) {
24 const rrElement = this.rrElements[i];
25 rrElement.computeLayoutInfo(rrDiagramToSVG);
26 const layoutInfo = rrElement.getLayoutInfo();
27 if (i == 0) {
28 connectorOffset = layoutInfo.getConnectorOffset();
29 } else {
30 height += 5;
31 }
32 height += layoutInfo.getHeight();
33 width = Math.max(width, layoutInfo.getWidth());
34 }
35 width += 20 + 20;
36 this.setLayoutInfo(new LayoutInfo(width, height, connectorOffset));
37 }
38
39 toSVG(rrDiagramToSVG, xOffset, yOffset, svgContent) {
40 const layoutInfo = this.getLayoutInfo();
41 const y1 = yOffset + layoutInfo.getConnectorOffset();
42 const x1 = xOffset + 10;
43 const x2 = xOffset + layoutInfo.getWidth() - 10;
44 const xOffset2 = xOffset + 20;
45 let y2 = 0;
46 let yOffset2 = yOffset;
47 for (let i = 0; i < this.rrElements.length; i++) {
48 const rrElement = this.rrElements[i];
49 const layoutInfo2 = rrElement.getLayoutInfo();
50 const width = layoutInfo2.getWidth();
51 const height = layoutInfo2.getHeight();
52 y2 = yOffset2 + layoutInfo2.getConnectorOffset();
53 if (i == 0) {
54 // Line to first element
55 svgContent.addLineConnector(x1 - 10, y1, x1 + 10, y1);
56 } else {
57 if (i == this.rrElements.length - 1) {
58 // Curve and vertical down
59 svgContent.addPathConnector(x1 - 5, y1, "q5 0 5 5", x1, y1 + 5);
60 svgContent.addLineConnector(x1, y1 + 5, x1, y2 - 5);
61 }
62 // Curve and horizontal line to element
63 svgContent.addPathConnector(x1, y2 - 5, "q0 5 5 5", x1 + 5, y2);
64 svgContent.addLineConnector(x1 + 5, y2, xOffset2, y2);
65 }
66 rrElement.toSVG(rrDiagramToSVG, xOffset2, yOffset2, svgContent);
67 if (i == 0) {
68 // Line to first element
69 svgContent.addLineConnector(xOffset2 + width, y2, x2 + 10, y2);
70 } else {
71 // Horizontal line to element and curve
72 svgContent.addLineConnector(x2 - 5, y2, xOffset2 + width, y2);
73 svgContent.addPathConnector(x2 - 5, y2, "q5 0 5-5", x2, y2 - 5);
74 if (i == this.rrElements.length - 1) {
75 // Vertical up and curve
76 svgContent.addLineConnector(x2, y2 - 5, x2, y1 + 5);
77 svgContent.addPathConnector(x2, y1 + 5, "q0-5 5-5", x2 + 5, y1);
78 }
79 }
80 yOffset2 += height + 5;
81 }
82 }
83
84}
\No newline at end of file