UNPKG

10.8 kBJavaScriptView Raw
1import { D as curveBasis, x as utils, l as log, E as parseGenericTypes } from "./mermaid-4b4b971d.js";
2import { l as line } from "./line-53c588d2.js";
3let edgeCount = 0;
4const drawEdge = function(elem, path, relation, conf, diagObj) {
5 const getRelationType = function(type) {
6 switch (type) {
7 case diagObj.db.relationType.AGGREGATION:
8 return "aggregation";
9 case diagObj.db.relationType.EXTENSION:
10 return "extension";
11 case diagObj.db.relationType.COMPOSITION:
12 return "composition";
13 case diagObj.db.relationType.DEPENDENCY:
14 return "dependency";
15 case diagObj.db.relationType.LOLLIPOP:
16 return "lollipop";
17 }
18 };
19 path.points = path.points.filter((p) => !Number.isNaN(p.y));
20 const lineData = path.points;
21 const lineFunction = line().x(function(d) {
22 return d.x;
23 }).y(function(d) {
24 return d.y;
25 }).curve(curveBasis);
26 const svgPath = elem.append("path").attr("d", lineFunction(lineData)).attr("id", "edge" + edgeCount).attr("class", "relation");
27 let url = "";
28 if (conf.arrowMarkerAbsolute) {
29 url = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search;
30 url = url.replace(/\(/g, "\\(");
31 url = url.replace(/\)/g, "\\)");
32 }
33 if (relation.relation.lineType == 1) {
34 svgPath.attr("class", "relation dashed-line");
35 }
36 if (relation.relation.lineType == 10) {
37 svgPath.attr("class", "relation dotted-line");
38 }
39 if (relation.relation.type1 !== "none") {
40 svgPath.attr(
41 "marker-start",
42 "url(" + url + "#" + getRelationType(relation.relation.type1) + "Start)"
43 );
44 }
45 if (relation.relation.type2 !== "none") {
46 svgPath.attr(
47 "marker-end",
48 "url(" + url + "#" + getRelationType(relation.relation.type2) + "End)"
49 );
50 }
51 let x, y;
52 const l = path.points.length;
53 let labelPosition = utils.calcLabelPosition(path.points);
54 x = labelPosition.x;
55 y = labelPosition.y;
56 let p1_card_x, p1_card_y;
57 let p2_card_x, p2_card_y;
58 if (l % 2 !== 0 && l > 1) {
59 let cardinality_1_point = utils.calcCardinalityPosition(
60 relation.relation.type1 !== "none",
61 path.points,
62 path.points[0]
63 );
64 let cardinality_2_point = utils.calcCardinalityPosition(
65 relation.relation.type2 !== "none",
66 path.points,
67 path.points[l - 1]
68 );
69 log.debug("cardinality_1_point " + JSON.stringify(cardinality_1_point));
70 log.debug("cardinality_2_point " + JSON.stringify(cardinality_2_point));
71 p1_card_x = cardinality_1_point.x;
72 p1_card_y = cardinality_1_point.y;
73 p2_card_x = cardinality_2_point.x;
74 p2_card_y = cardinality_2_point.y;
75 }
76 if (relation.title !== void 0) {
77 const g = elem.append("g").attr("class", "classLabel");
78 const label = g.append("text").attr("class", "label").attr("x", x).attr("y", y).attr("fill", "red").attr("text-anchor", "middle").text(relation.title);
79 window.label = label;
80 const bounds = label.node().getBBox();
81 g.insert("rect", ":first-child").attr("class", "box").attr("x", bounds.x - conf.padding / 2).attr("y", bounds.y - conf.padding / 2).attr("width", bounds.width + conf.padding).attr("height", bounds.height + conf.padding);
82 }
83 log.info("Rendering relation " + JSON.stringify(relation));
84 if (relation.relationTitle1 !== void 0 && relation.relationTitle1 !== "none") {
85 const g = elem.append("g").attr("class", "cardinality");
86 g.append("text").attr("class", "type1").attr("x", p1_card_x).attr("y", p1_card_y).attr("fill", "black").attr("font-size", "6").text(relation.relationTitle1);
87 }
88 if (relation.relationTitle2 !== void 0 && relation.relationTitle2 !== "none") {
89 const g = elem.append("g").attr("class", "cardinality");
90 g.append("text").attr("class", "type2").attr("x", p2_card_x).attr("y", p2_card_y).attr("fill", "black").attr("font-size", "6").text(relation.relationTitle2);
91 }
92 edgeCount++;
93};
94const drawClass = function(elem, classDef, conf, diagObj) {
95 log.debug("Rendering class ", classDef, conf);
96 const id = classDef.id;
97 const classInfo = {
98 id,
99 label: classDef.id,
100 width: 0,
101 height: 0
102 };
103 const g = elem.append("g").attr("id", diagObj.db.lookUpDomId(id)).attr("class", "classGroup");
104 let title;
105 if (classDef.link) {
106 title = g.append("svg:a").attr("xlink:href", classDef.link).attr("target", classDef.linkTarget).append("text").attr("y", conf.textHeight + conf.padding).attr("x", 0);
107 } else {
108 title = g.append("text").attr("y", conf.textHeight + conf.padding).attr("x", 0);
109 }
110 let isFirst = true;
111 classDef.annotations.forEach(function(member) {
112 const titleText2 = title.append("tspan").text("«" + member + "»");
113 if (!isFirst) {
114 titleText2.attr("dy", conf.textHeight);
115 }
116 isFirst = false;
117 });
118 let classTitleString = getClassTitleString(classDef);
119 const classTitle = title.append("tspan").text(classTitleString).attr("class", "title");
120 if (!isFirst) {
121 classTitle.attr("dy", conf.textHeight);
122 }
123 const titleHeight = title.node().getBBox().height;
124 const membersLine = g.append("line").attr("x1", 0).attr("y1", conf.padding + titleHeight + conf.dividerMargin / 2).attr("y2", conf.padding + titleHeight + conf.dividerMargin / 2);
125 const members = g.append("text").attr("x", conf.padding).attr("y", titleHeight + conf.dividerMargin + conf.textHeight).attr("fill", "white").attr("class", "classText");
126 isFirst = true;
127 classDef.members.forEach(function(member) {
128 addTspan(members, member, isFirst, conf);
129 isFirst = false;
130 });
131 const membersBox = members.node().getBBox();
132 const methodsLine = g.append("line").attr("x1", 0).attr("y1", conf.padding + titleHeight + conf.dividerMargin + membersBox.height).attr("y2", conf.padding + titleHeight + conf.dividerMargin + membersBox.height);
133 const methods = g.append("text").attr("x", conf.padding).attr("y", titleHeight + 2 * conf.dividerMargin + membersBox.height + conf.textHeight).attr("fill", "white").attr("class", "classText");
134 isFirst = true;
135 classDef.methods.forEach(function(method) {
136 addTspan(methods, method, isFirst, conf);
137 isFirst = false;
138 });
139 const classBox = g.node().getBBox();
140 var cssClassStr = " ";
141 if (classDef.cssClasses.length > 0) {
142 cssClassStr = cssClassStr + classDef.cssClasses.join(" ");
143 }
144 const rect = g.insert("rect", ":first-child").attr("x", 0).attr("y", 0).attr("width", classBox.width + 2 * conf.padding).attr("height", classBox.height + conf.padding + 0.5 * conf.dividerMargin).attr("class", cssClassStr);
145 const rectWidth = rect.node().getBBox().width;
146 title.node().childNodes.forEach(function(x) {
147 x.setAttribute("x", (rectWidth - x.getBBox().width) / 2);
148 });
149 if (classDef.tooltip) {
150 title.insert("title").text(classDef.tooltip);
151 }
152 membersLine.attr("x2", rectWidth);
153 methodsLine.attr("x2", rectWidth);
154 classInfo.width = rectWidth;
155 classInfo.height = classBox.height + conf.padding + 0.5 * conf.dividerMargin;
156 return classInfo;
157};
158const getClassTitleString = function(classDef) {
159 let classTitleString = classDef.id;
160 if (classDef.type) {
161 classTitleString += "<" + classDef.type + ">";
162 }
163 return classTitleString;
164};
165const drawNote = function(elem, note, conf, diagObj) {
166 log.debug("Rendering note ", note, conf);
167 const id = note.id;
168 const noteInfo = {
169 id,
170 text: note.text,
171 width: 0,
172 height: 0
173 };
174 const g = elem.append("g").attr("id", id).attr("class", "classGroup");
175 let text = g.append("text").attr("y", conf.textHeight + conf.padding).attr("x", 0);
176 const lines = JSON.parse(`"${note.text}"`).split("\n");
177 lines.forEach(function(line2) {
178 log.debug(`Adding line: ${line2}`);
179 text.append("tspan").text(line2).attr("class", "title").attr("dy", conf.textHeight);
180 });
181 const noteBox = g.node().getBBox();
182 const rect = g.insert("rect", ":first-child").attr("x", 0).attr("y", 0).attr("width", noteBox.width + 2 * conf.padding).attr(
183 "height",
184 noteBox.height + lines.length * conf.textHeight + conf.padding + 0.5 * conf.dividerMargin
185 );
186 const rectWidth = rect.node().getBBox().width;
187 text.node().childNodes.forEach(function(x) {
188 x.setAttribute("x", (rectWidth - x.getBBox().width) / 2);
189 });
190 noteInfo.width = rectWidth;
191 noteInfo.height = noteBox.height + lines.length * conf.textHeight + conf.padding + 0.5 * conf.dividerMargin;
192 return noteInfo;
193};
194const parseMember = function(text) {
195 let displayText = "";
196 let cssStyle = "";
197 let returnType = "";
198 let visibility = "";
199 let firstChar = text.substring(0, 1);
200 let lastChar = text.substring(text.length - 1, text.length);
201 if (firstChar.match(/[#+~-]/)) {
202 visibility = firstChar;
203 }
204 let noClassifierRe = /[\s\w)~]/;
205 if (!lastChar.match(noClassifierRe)) {
206 cssStyle = parseClassifier(lastChar);
207 }
208 const startIndex = visibility === "" ? 0 : 1;
209 let endIndex = cssStyle === "" ? text.length : text.length - 1;
210 text = text.substring(startIndex, endIndex);
211 const methodStart = text.indexOf("(");
212 const methodEnd = text.indexOf(")");
213 const isMethod = methodStart > 1 && methodEnd > methodStart && methodEnd <= text.length;
214 if (isMethod) {
215 let methodName = text.substring(0, methodStart).trim();
216 const parameters = text.substring(methodStart + 1, methodEnd);
217 displayText = visibility + methodName + "(" + parseGenericTypes(parameters.trim()) + ")";
218 if (methodEnd < text.length) {
219 let potentialClassifier = text.substring(methodEnd + 1, methodEnd + 2);
220 if (cssStyle === "" && !potentialClassifier.match(noClassifierRe)) {
221 cssStyle = parseClassifier(potentialClassifier);
222 returnType = text.substring(methodEnd + 2).trim();
223 } else {
224 returnType = text.substring(methodEnd + 1).trim();
225 }
226 if (returnType !== "") {
227 if (returnType.charAt(0) === ":") {
228 returnType = returnType.substring(1).trim();
229 }
230 returnType = " : " + parseGenericTypes(returnType);
231 displayText += returnType;
232 }
233 }
234 } else {
235 displayText = visibility + parseGenericTypes(text);
236 }
237 return {
238 displayText,
239 cssStyle
240 };
241};
242const addTspan = function(textEl, txt, isFirst, conf) {
243 let member = parseMember(txt);
244 const tSpan = textEl.append("tspan").attr("x", conf.padding).text(member.displayText);
245 if (member.cssStyle !== "") {
246 tSpan.attr("style", member.cssStyle);
247 }
248 if (!isFirst) {
249 tSpan.attr("dy", conf.textHeight);
250 }
251};
252const parseClassifier = function(classifier) {
253 switch (classifier) {
254 case "*":
255 return "font-style:italic;";
256 case "$":
257 return "text-decoration:underline;";
258 default:
259 return "";
260 }
261};
262const svgDraw = {
263 getClassTitleString,
264 drawClass,
265 drawEdge,
266 drawNote,
267 parseMember
268};
269export {
270 parseMember as p,
271 svgDraw as s
272};