1 | import RRElement from './rrelement';
|
2 | import RRSequence from './rrsequence';
|
3 | import SvgContent from './svg/svgcontent';
|
4 | import RRBreak from './rrbreak';
|
5 |
|
6 | export default class RRDiagram {
|
7 |
|
8 | |
9 |
|
10 |
|
11 | constructor(rrElement) {
|
12 | this.rrElement = rrElement;
|
13 | }
|
14 |
|
15 | toSVG(rrDiagramToSVG) {
|
16 | const rrElementList = [];
|
17 | if (this.rrElement instanceof RRSequence) {
|
18 | const cursorElementList = [];
|
19 | for (let element of this.rrElement.getRRElements()) {
|
20 | if (element instanceof RRBreak) {
|
21 | if (cursorElementList.length != 0) {
|
22 | rrElementList.push(cursorElementList.length == 1 ? cursorElementList[0] : new RRSequence(cursorElementList.slice()));
|
23 | cursorElementList.length = 0;
|
24 | }
|
25 | } else {
|
26 | cursorElementList.push(element);
|
27 | }
|
28 | }
|
29 | if (cursorElementList.length != 0) {
|
30 | rrElementList.push(cursorElementList.length == 1 ? cursorElementList[0] : new RRSequence(cursorElementList.slice()));
|
31 | }
|
32 | } else {
|
33 | rrElementList.push(this.rrElement);
|
34 | }
|
35 | let width = 5;
|
36 | let height = 5;
|
37 | for (let i = 0; i < rrElementList.length; i++) {
|
38 | if (i > 0) {
|
39 | height += 5;
|
40 | }
|
41 | const rrElement_ = rrElementList[i];
|
42 | rrElement_.computeLayoutInfo(rrDiagramToSVG);
|
43 | const layoutInfo = rrElement_.getLayoutInfo();
|
44 | width = Math.max(width, 5 + layoutInfo.getWidth() + 5);
|
45 | height += layoutInfo.getHeight() + 5;
|
46 | }
|
47 | const svgContent = new SvgContent();
|
48 |
|
49 | const xOffset = 0;
|
50 | let yOffset = 5;
|
51 | for (let rrElement_ of rrElementList) {
|
52 | const layoutInfo2 = rrElement_.getLayoutInfo();
|
53 | const connectorOffset2 = layoutInfo2.getConnectorOffset();
|
54 | const width2 = layoutInfo2.getWidth();
|
55 | const height2 = layoutInfo2.getHeight();
|
56 | const y1 = yOffset + connectorOffset2;
|
57 | svgContent.addLineConnector(xOffset, y1, xOffset + 5, y1);
|
58 |
|
59 | rrElement_.toSVG(rrDiagramToSVG, xOffset + 5, yOffset, svgContent);
|
60 | svgContent.addLineConnector(xOffset + 5 + width2, y1, xOffset + 5 + width2 + 5, y1);
|
61 | yOffset += height2 + 10;
|
62 | }
|
63 | const connectorElement = svgContent.getConnectorElement(rrDiagramToSVG);
|
64 | const elements = svgContent.getElements();
|
65 |
|
66 | const sb = [];
|
67 | sb.push("<svg version=\"1.1\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns=\"http://www.w3.org/2000/svg\" width=\"", width, "\" height=\"", height, "\" viewbox=\"0 0 " + width + " " + height + "\">");
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | sb.push(connectorElement);
|
75 | sb.push(elements);
|
76 | sb.push("</svg>");
|
77 | return sb.join("");
|
78 | }
|
79 |
|
80 | } |
\ | No newline at end of file |