UNPKG

3.46 kBJavaScriptView Raw
1import RRElement from './rrelement';
2import RRSequence from './rrsequence';
3import SvgContent from './svg/svgcontent';
4import RRBreak from './rrbreak';
5
6export default class RRDiagram {
7
8 /**
9 * @param {RRElement} rrElement
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 // First, generate the XML for the elements, to know the usage.
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 // TODO: add decorations (like arrows)?
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 // Then generate the rest (CSS and SVG container tags) based on that usage.
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 /* String styles = svgContent.getCSSStyles();
69 if(styles.length() > 0) {
70 sb.push("<defs><style type=\"text/css\">");
71 sb.push(styles);
72 sb.push("</style></defs>");
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