blob: 942aceecc857bfcd421416cfce98f45a33e3cb98 [file] [log] [blame]
Akron6f1302b2017-09-13 12:46:02 +02001/**
2 * Parse a relational tree and visualize using arcs.
3 *
4 * @author Nils Diewald
5 */
6
Akron671fdb92017-09-12 18:09:46 +02007define([], function () {
Akronf5dc5102017-05-16 20:32:57 +02008 "use strict";
9
10 var svgNS = "http://www.w3.org/2000/svg";
Akron671fdb92017-09-12 18:09:46 +020011 var _TermRE = new RegExp("^(?:([^\/]+?)\/)?([^:]+?):(.+?)$");
Akronf5dc5102017-05-16 20:32:57 +020012
13 return {
14 create : function (snippet) {
Akron6f1302b2017-09-13 12:46:02 +020015 return Object.create(this)._init(snippet);
Akronf5dc5102017-05-16 20:32:57 +020016 },
Akron6f1302b2017-09-13 12:46:02 +020017
18 // Initialize the state of the object
Akronf5dc5102017-05-16 20:32:57 +020019 _init : function (snippet) {
Akron671fdb92017-09-12 18:09:46 +020020
Akron6f1302b2017-09-13 12:46:02 +020021 // Predefine some values
Akron71c87de2017-09-15 14:24:21 +020022 this._tokens = [];
23 this._arcs = [];
Akron6f1302b2017-09-13 12:46:02 +020024 this._tokenElements = [];
25 this._y = 0;
Akron671fdb92017-09-12 18:09:46 +020026
Akron6f1302b2017-09-13 12:46:02 +020027 // Some configurations
28 this.maxArc = 200; // maximum height of the bezier control point
Akronfee0b622017-09-13 14:46:43 +020029 this.overlapDiff = 40; // Difference on overlaps and minimum height for self-refernces
Akron6f1302b2017-09-13 12:46:02 +020030 this.arcDiff = 15;
31 this.anchorDiff = 8;
32 this.anchorStart = 15;
33 this.tokenSep = 30;
34 this.xPadding = 10;
35 this.yPadding = 5;
Akron671fdb92017-09-12 18:09:46 +020036
Akron6f1302b2017-09-13 12:46:02 +020037 // No snippet to process
38 if (snippet == undefined || snippet == null)
39 return this;
Akron671fdb92017-09-12 18:09:46 +020040
Akron6f1302b2017-09-13 12:46:02 +020041 // Parse the snippet
42 var html = document.createElement("div");
43 html.innerHTML = snippet;
Akron671fdb92017-09-12 18:09:46 +020044
Akron6f1302b2017-09-13 12:46:02 +020045 // Establish temporary parsing memory
46 this.temp = {
47 target : {}, // Remember the map id => pos
48 edges : [], // Remember edge definitions
49 pos : 0 // Keep track of the current token position
Akron671fdb92017-09-12 18:09:46 +020050 };
51
Akron6f1302b2017-09-13 12:46:02 +020052 // Start parsing from root
53 this._parse(0, html.childNodes, undefined);
54
55 // Establish edge list
56 var targetMap = this.temp['target'];
57 var edges = this.temp['edges'];
58
59 // Iterate over edge lists
60 // TODO:
61 // Support spans for anchors!
62 for (var i in edges) {
63 var edge = edges[i];
64
65 // Check the target identifier
66 var targetID = edge.targetID;
67 var target = targetMap[targetID];
68
69 if (target != undefined) {
70
Akron2c1d9ff2017-09-15 13:57:33 +020071 // Check if the source is a span anchor
Akron0d04a182017-09-15 14:34:50 +020072 /*
Akron2c1d9ff2017-09-15 13:57:33 +020073 var start = edge.srcStart;
74 if (start !== edge.srcEnd) {
75 start = [start, edge.srcEnd];
76 };
Akron0d04a182017-09-15 14:34:50 +020077 */
Akron2c1d9ff2017-09-15 13:57:33 +020078
Akron6f1302b2017-09-13 12:46:02 +020079 // Add relation
Akron2c1d9ff2017-09-15 13:57:33 +020080 var relation = {
Akron0d04a182017-09-15 14:34:50 +020081 start : [edge.srcStart, edge.srcEnd],
Akron6f1302b2017-09-13 12:46:02 +020082 end : target,
83 direction : 'uni',
84 label : edge.label
Akron2c1d9ff2017-09-15 13:57:33 +020085 };
86 // console.log(relation);
87 this.addRel(relation);
Akron6f1302b2017-09-13 12:46:02 +020088 };
89 };
90
91 // Reset parsing memory
92 this.temp = {};
93
Akronf5dc5102017-05-16 20:32:57 +020094 return this;
95 },
96
Akron6f1302b2017-09-13 12:46:02 +020097 // Parse a node of the tree snippet
Akron671fdb92017-09-12 18:09:46 +020098 _parse : function (parent, children, mark) {
Akron6f1302b2017-09-13 12:46:02 +020099
100 // Iterate over all child nodes
Akron671fdb92017-09-12 18:09:46 +0200101 for (var i in children) {
102 var c = children[i];
103
104 // Element node
105 if (c.nodeType == 1) {
106
Akron2c1d9ff2017-09-15 13:57:33 +0200107 var xmlid, target;
108
Akron671fdb92017-09-12 18:09:46 +0200109 // Node is an identifier
110 if (c.hasAttribute('xml:id')) {
111
112 // Remember that pos has this identifier
Akron2c1d9ff2017-09-15 13:57:33 +0200113 xmlid = c.getAttribute('xml:id');
114 this.temp['target'][xmlid] = [this.temp['pos'], this.temp['pos']];
Akron671fdb92017-09-12 18:09:46 +0200115 }
116
117 // Node is a relation
118 else if (c.hasAttribute('xlink:href')) {
Akron6f1302b2017-09-13 12:46:02 +0200119 var label;
Akron671fdb92017-09-12 18:09:46 +0200120
Akron6f1302b2017-09-13 12:46:02 +0200121 // Get target id
Akron2c1d9ff2017-09-15 13:57:33 +0200122 target = c.getAttribute('xlink:href').replace(/^#/, "");
Akron671fdb92017-09-12 18:09:46 +0200123
124 if (c.hasAttribute('xlink:title')) {
125 label = this._clean(c.getAttribute('xlink:title'));
126 };
127
128 // Remember the defined edge
Akron2c1d9ff2017-09-15 13:57:33 +0200129 var edge = {
Akron6f1302b2017-09-13 12:46:02 +0200130 label : label,
Akron2c1d9ff2017-09-15 13:57:33 +0200131 srcStart : this.temp['pos'],
Akron671fdb92017-09-12 18:09:46 +0200132 targetID : target
Akron2c1d9ff2017-09-15 13:57:33 +0200133 };
134 this.temp['edges'].push(edge);
Akron671fdb92017-09-12 18:09:46 +0200135 };
136
Akron6f1302b2017-09-13 12:46:02 +0200137 // Go on with child nodes
Akron671fdb92017-09-12 18:09:46 +0200138 if (c.hasChildNodes()) {
139 this._parse(0, c.childNodes, mark);
140 };
Akron2c1d9ff2017-09-15 13:57:33 +0200141
142 if (xmlid !== undefined) {
143 this.temp['target'][xmlid][1] = this.temp['pos'] -1;
144
145 /*
146 console.log('Target ' + xmlid + ' spans from ' +
147 this.temp['target'][xmlid][0] +
148 ' to ' +
149 this.temp['target'][xmlid][1]
150 );
151 */
152 xmlid = undefined;
153 }
154 else if (target !== undefined) {
155 edge["srcEnd"] = this.temp['pos'] -1;
156
157 /*
158 console.log('Source spans from ' +
159 edge["srcStart"] +
160 ' to ' +
161 edge["srcEnd"]
162 );
163 */
164 target = undefined;
165 };
Akron671fdb92017-09-12 18:09:46 +0200166 }
167
168 // Text node
169 else if (c.nodeType == 3) {
170
171 // Check, if there is a non-whitespace token
172 if (c.nodeValue !== undefined) {
173 var str = c.nodeValue.trim();
174 if (str !== undefined && str.length > 0) {
175
176 // Add token to token list
177 this.addToken(str);
178
179 // Move token position
180 this.temp['pos']++;
181 };
182 };
183 }
184 };
185 },
186
Akron6f1302b2017-09-13 12:46:02 +0200187
Akron671fdb92017-09-12 18:09:46 +0200188 // Remove foundry and layer for labels
189 _clean : function (title) {
190 return title.replace(_TermRE, "$3");
191 },
192
Akron6f1302b2017-09-13 12:46:02 +0200193
194 // Return the number of leaf nodes
195 // (not necessarily part of a relation).
196 // Consecutive nodes that are not part of any
197 // relation are summarized in one node.
Akron671fdb92017-09-12 18:09:46 +0200198 size : function () {
199 return this._tokens.length;
200 },
Akron15175132017-09-07 18:12:55 +0200201
Akron6f1302b2017-09-13 12:46:02 +0200202
Akronf5dc5102017-05-16 20:32:57 +0200203 // This is a shorthand for SVG element creation
204 _c : function (tag) {
205 return document.createElementNS(svgNS, tag);
206 },
207
Akron15175132017-09-07 18:12:55 +0200208
Akronf5dc5102017-05-16 20:32:57 +0200209 // Returns the center point of the requesting token
210 _tokenPoint : function (node) {
211 var box = node.getBoundingClientRect();
212 return box.x + (box.width / 2);
213 },
214
Akron15175132017-09-07 18:12:55 +0200215
216 // Draws an anchor
Akrond67d45b2017-05-18 21:47:38 +0200217 _drawAnchor : function (anchor) {
Akron15175132017-09-07 18:12:55 +0200218
219 // Calculate the span of the first and last token, the anchor spans
Akron3a4a08e2017-05-23 22:34:18 +0200220 var firstBox = this._tokenElements[anchor.first].getBoundingClientRect();
Akron6f1302b2017-09-13 12:46:02 +0200221 var lastBox = this._tokenElements[anchor.last].getBoundingClientRect();
Akrond67d45b2017-05-18 21:47:38 +0200222
Akron15175132017-09-07 18:12:55 +0200223 var startPos = firstBox.left - this.offsetLeft;
Akron6f1302b2017-09-13 12:46:02 +0200224 var endPos = lastBox.right - this.offsetLeft;
Akron15175132017-09-07 18:12:55 +0200225
Akron3a4a08e2017-05-23 22:34:18 +0200226 var y = this._y + (anchor.overlaps * this.anchorDiff) - this.anchorStart;
227
Akrond67d45b2017-05-18 21:47:38 +0200228 var l = this._c('path');
Akron65d31082017-09-08 16:23:40 +0200229 this._arcsElement.appendChild(l);
Akron3a4a08e2017-05-23 22:34:18 +0200230 l.setAttribute("d", "M " + startPos + "," + y + " L " + endPos + "," + y);
Akrond67d45b2017-05-18 21:47:38 +0200231 l.setAttribute("class", "anchor");
232 anchor.element = l;
233 anchor.y = y;
234 return l;
235 },
236
Akron6f1302b2017-09-13 12:46:02 +0200237
238 // Create an arc with an optional label
Akronf5dc5102017-05-16 20:32:57 +0200239 // Potentially needs a height parameter for stacks
Akron63ae00b2017-05-16 22:03:36 +0200240 _drawArc : function (arc) {
Akronf5dc5102017-05-16 20:32:57 +0200241
Akrond67d45b2017-05-18 21:47:38 +0200242 var startPos, endPos;
Akronfee0b622017-09-13 14:46:43 +0200243 var startY = this._y;
244 var endY = this._y;
Akronf5dc5102017-05-16 20:32:57 +0200245
Akrond67d45b2017-05-18 21:47:38 +0200246 if (arc.startAnchor !== undefined) {
Akron3a4a08e2017-05-23 22:34:18 +0200247 startPos = this._tokenPoint(arc.startAnchor.element);
Akrond67d45b2017-05-18 21:47:38 +0200248 startY = arc.startAnchor.y;
249 }
250 else {
251 startPos = this._tokenPoint(this._tokenElements[arc.first]);
252 };
253
254 if (arc.endAnchor !== undefined) {
255 endPos = this._tokenPoint(arc.endAnchor.element)
256 endY = arc.endAnchor.y;
257 }
258 else {
259 endPos = this._tokenPoint(this._tokenElements[arc.last]);
260 };
261
Akron15175132017-09-07 18:12:55 +0200262 startPos -= this.offsetLeft;
263 endPos -= this.offsetLeft;
264
Akronfee0b622017-09-13 14:46:43 +0200265 // Special treatment for self-references
266 var overlaps = arc.overlaps;
267 if (startPos == endPos) {
268 startPos -= this.overlapDiff / 3;
Akronc9f23022017-09-13 15:43:52 +0200269 endPos += this.overlapDiff / 3;
Akronfee0b622017-09-13 14:46:43 +0200270 overlaps += .5;
271 };
272
Akronf5dc5102017-05-16 20:32:57 +0200273 var g = this._c("g");
Akron65d31082017-09-08 16:23:40 +0200274 g.setAttribute("class", "arc");
Akronf5dc5102017-05-16 20:32:57 +0200275 var p = g.appendChild(this._c("path"));
Akron15175132017-09-07 18:12:55 +0200276 p.setAttribute('class', 'edge');
Akron65d31082017-09-08 16:23:40 +0200277
278 // Attach the new arc before drawing, so computed values are available
279 this._arcsElement.appendChild(g);
Akronf5dc5102017-05-16 20:32:57 +0200280
281 // Create arc
282 var middle = Math.abs(endPos - startPos) / 2;
283
Akronfee0b622017-09-13 14:46:43 +0200284 // TODO:
285 // take the number of tokens into account!
286 var cHeight = this.arcDiff + (overlaps * this.overlapDiff) + (middle / 2);
Akronc5b5f742017-05-23 16:04:35 +0200287
288 // Respect the maximum height
289 cHeight = cHeight < this.maxArc ? cHeight : this.maxArc;
Akronf5dc5102017-05-16 20:32:57 +0200290
291 var x = Math.min(startPos, endPos);
Akronc5b5f742017-05-23 16:04:35 +0200292
Akron3a4a08e2017-05-23 22:34:18 +0200293 //var controlY = (startY + endY - cHeight);
294 var controlY = (endY - cHeight);
Akronf5dc5102017-05-16 20:32:57 +0200295
Akron3a4a08e2017-05-23 22:34:18 +0200296 var arcE = "M "+ startPos + "," + startY +
297 " C " + startPos + "," + controlY +
298 " " + endPos + "," + controlY +
299 " " + endPos + "," + endY;
Akrond67d45b2017-05-18 21:47:38 +0200300
Akron63ae00b2017-05-16 22:03:36 +0200301 p.setAttribute("d", arcE);
Akronf5dc5102017-05-16 20:32:57 +0200302
Akron3a4a08e2017-05-23 22:34:18 +0200303 if (arc.direction !== undefined) {
304 p.setAttribute("marker-end", "url(#arr)");
305 if (arc.direction === 'bi') {
306 p.setAttribute("marker-start", "url(#arr)");
307 };
308 };
309
Akron6f1302b2017-09-13 12:46:02 +0200310 if (arc.label === undefined)
311 return g;
312
Akronc5b5f742017-05-23 16:04:35 +0200313 /*
314 * Calculate the top point of the arc for labeling using
315 * de Casteljau's algorithm, see e.g.
316 * http://blog.sklambert.com/finding-the-control-points-of-a-bezier-curve/
317 * of course simplified to symmetric arcs ...
318 */
319 // Interpolate one side of the control polygon
Akronc5b5f742017-05-23 16:04:35 +0200320 var middleY = (((startY + controlY) / 2) + controlY) / 2;
321
Akron6f1302b2017-09-13 12:46:02 +0200322 // Create a boxed label
Akronc9f23022017-09-13 15:43:52 +0200323 g = this._c("g");
324 g.setAttribute("class", "label");
325 this._labelsElement.appendChild(g);
326
327 var that = this;
328 g.addEventListener('mouseenter', function () {
329 that._labelsElement.appendChild(this);
330 });
331
Akron6f1302b2017-09-13 12:46:02 +0200332 var labelE = g.appendChild(this._c("text"));
333 labelE.setAttribute("x", x + middle);
334 labelE.setAttribute("y", middleY + 3);
335 labelE.setAttribute("text-anchor", "middle");
336 var textNode = document.createTextNode(arc.label);
337 labelE.appendChild(textNode);
Akronc5b5f742017-05-23 16:04:35 +0200338
Akron6f1302b2017-09-13 12:46:02 +0200339 var labelBox = labelE.getBBox();
340 var textWidth = labelBox.width; // labelE.getComputedTextLength();
341 var textHeight = labelBox.height; // labelE.getComputedTextLength();
Akron1dc87902017-05-29 16:04:56 +0200342
Akron6f1302b2017-09-13 12:46:02 +0200343 // Add box with padding to left and right
344 var labelR = g.insertBefore(this._c("rect"), labelE);
345 var boxWidth = textWidth + 2 * this.xPadding;
346 labelR.setAttribute("x", x + middle - (boxWidth / 2));
347 labelR.setAttribute("ry", 5);
348 labelR.setAttribute("y", labelBox.y - this.yPadding);
349 labelR.setAttribute("width", boxWidth);
350 labelR.setAttribute("height", textHeight + 2 * this.yPadding);
Akronf5dc5102017-05-16 20:32:57 +0200351 },
352
Akron6f1302b2017-09-13 12:46:02 +0200353 // Get the svg element
Akronf5dc5102017-05-16 20:32:57 +0200354 element : function () {
355 if (this._element !== undefined)
356 return this._element;
357
358 // Create svg
359 var svg = this._c("svg");
Akron3a4a08e2017-05-23 22:34:18 +0200360
361 window.addEventListener("resize", function () {
Akron6f1302b2017-09-13 12:46:02 +0200362 // TODO:
363 // Only if text-size changed!
364 // TODO:
365 // This is currently untested
Akron3a4a08e2017-05-23 22:34:18 +0200366 this.show();
367 }.bind(this));
Akron6f1302b2017-09-13 12:46:02 +0200368
369 // Define marker arrows
Akron3a4a08e2017-05-23 22:34:18 +0200370 var defs = svg.appendChild(this._c("defs"));
371 var marker = defs.appendChild(this._c("marker"));
372 marker.setAttribute("refX", 9);
373 marker.setAttribute("id", "arr");
374 marker.setAttribute("orient", "auto-start-reverse");
375 marker.setAttribute("markerUnits","userSpaceOnUse");
Akron3a4a08e2017-05-23 22:34:18 +0200376 var arrow = this._c("path");
377 arrow.setAttribute("transform", "scale(0.8)");
378 arrow.setAttribute("d", "M 0,-5 0,5 10,0 Z");
379 marker.appendChild(arrow);
380
Akronf5dc5102017-05-16 20:32:57 +0200381 this._element = svg;
382 return this._element;
383 },
384
385 // Add a relation with a start, an end,
Akron6f1302b2017-09-13 12:46:02 +0200386 // a direction value and an optional label text
Akronc5b5f742017-05-23 16:04:35 +0200387 addRel : function (rel) {
388 this._arcs.push(rel);
389 return this;
Akronf5dc5102017-05-16 20:32:57 +0200390 },
391
Akronc5b5f742017-05-23 16:04:35 +0200392
Akron6f1302b2017-09-13 12:46:02 +0200393 // Add a token to the list (this will mostly be a word)
Akronc5b5f742017-05-23 16:04:35 +0200394 addToken : function(token) {
395 this._tokens.push(token);
396 return this;
397 },
398
Akronf5dc5102017-05-16 20:32:57 +0200399 /*
400 * All arcs need to be sorted before shown,
401 * to avoid nesting.
402 */
403 _sortArcs : function () {
404
Akrond67d45b2017-05-18 21:47:38 +0200405 // TODO:
406 // Keep in mind that the arcs may have long anchors!
407 // 1. Iterate over all arcs
408 // 2. Sort all multi
Akron71c87de2017-09-15 14:24:21 +0200409 var anchors = {};
Akrond67d45b2017-05-18 21:47:38 +0200410
Akronf5dc5102017-05-16 20:32:57 +0200411 // 1. Sort by length
412 // 2. Tag all spans with the number of overlaps before
413 // a) Iterate over all spans
414 // b) check the latest preceeding overlapping span (lpos)
415 // -> not found: tag with 0
416 // -> found: Add +1 to the level of the (lpos)
417 // c) If the new tag is smaller than the previous element,
418 // reorder
Akron63ae00b2017-05-16 22:03:36 +0200419
420 // Normalize start and end
421 var sortedArcs = this._arcs.map(function (v) {
Akrond67d45b2017-05-18 21:47:38 +0200422
423 // Check for long anchors
424 if (v.start instanceof Array) {
Akrond67d45b2017-05-18 21:47:38 +0200425
Akron0d04a182017-09-15 14:34:50 +0200426 if (v.start[0] == v.start[1]) {
427 v.start = v.start[0];
428 }
Akrond67d45b2017-05-18 21:47:38 +0200429
Akron0d04a182017-09-15 14:34:50 +0200430 else {
431
432 var middle = Math.ceil(Math.abs(v.start[1] - v.start[0]) / 2) + v.start[0];
433
434 // Calculate signature to avoid multiple anchors
435 var anchorSig = "#" + v.start[0] + "_" + v.start[1];
436 if (v.start[0] > v.start[1]) {
437 anchorSig = "#" + v.start[1] + "_" + v.start[0];
Akron71c87de2017-09-15 14:24:21 +0200438 };
Akron0d04a182017-09-15 14:34:50 +0200439
440 // Check if the anchor already exist
441 var anchor = anchors[anchorSig];
442 if (anchor === undefined) {
443 anchor = {
444 "first": v.start[0],
445 "last" : v.start[1],
446 "length" : v.start[1] - v.start[0]
447 };
448 anchors[anchorSig] = anchor;
449 // anchors.push(v.startAnchor);
450 };
451
452 v.startAnchor = anchor;
453
454 // Add to anchors list
455 v.start = middle;
Akron71c87de2017-09-15 14:24:21 +0200456 };
Akrond67d45b2017-05-18 21:47:38 +0200457 };
458
459 if (v.end instanceof Array) {
Akron71c87de2017-09-15 14:24:21 +0200460
Akron0d04a182017-09-15 14:34:50 +0200461 if (v.end[0] == v.end[1]) {
462 v.end = v.end[0];
463 }
Akrond67d45b2017-05-18 21:47:38 +0200464
Akron0d04a182017-09-15 14:34:50 +0200465 else {
466
467 var middle = Math.abs(v.end[0] - v.end[1]) + v.end[0];
468
469 // Calculate signature to avoid multiple anchors
470 var anchorSig = "#" + v.end[0] + "_" + v.end[1];
471 if (v.end[0] > v.end[1]) {
472 anchorSig = "#" + v.end[1] + "_" + v.end[0];
Akron71c87de2017-09-15 14:24:21 +0200473 };
Akron0d04a182017-09-15 14:34:50 +0200474
475 // Check if the anchor already exist
476 var anchor = anchors[anchorSig];
477 if (anchor === undefined) {
478 anchor = {
479 "first": v.end[0],
480 "last" : v.end[1],
481 "length" : v.end[1] - v.end[0]
482 };
483 anchors[anchorSig] = anchor;
484 // anchors.push(v.startAnchor);
485 };
486
487 v.endAnchor = anchor;
488
489 // Add to anchors list
490 // anchors.push(v.endAnchor);
491 v.end = middle;
Akron71c87de2017-09-15 14:24:21 +0200492 };
Akrond67d45b2017-05-18 21:47:38 +0200493 };
494
Akron2c1d9ff2017-09-15 13:57:33 +0200495 v.first = v.start;
496 v.last = v.end;
497
Akrond67d45b2017-05-18 21:47:38 +0200498 // calculate the arch length
Akron63ae00b2017-05-16 22:03:36 +0200499 if (v.start < v.end) {
Akron63ae00b2017-05-16 22:03:36 +0200500 v.length = v.end - v.start;
501 }
502 else {
Akron2c1d9ff2017-09-15 13:57:33 +0200503 // v.first = v.end;
504 // v.last = v.start;
Akron63ae00b2017-05-16 22:03:36 +0200505 v.length = v.start - v.end;
506 };
Akron2c1d9ff2017-09-15 13:57:33 +0200507
Akron63ae00b2017-05-16 22:03:36 +0200508 return v;
509 });
510
511 // Sort based on length
512 sortedArcs.sort(function (a, b) {
513 if (a.length < b.length)
514 return -1;
515 else
516 return 1;
517 });
518
Akron2c1d9ff2017-09-15 13:57:33 +0200519 // Add sorted arcs and anchors
Akron3a4a08e2017-05-23 22:34:18 +0200520 this._sortedArcs = lengthSort(sortedArcs, false);
Akron71c87de2017-09-15 14:24:21 +0200521
522 // Translate map to array (there is probably a better JS method)
523 var sortedAnchors = [];
524 for (var i in anchors) {
525 sortedAnchors.push(anchors[i]);
526 };
527 this._sortedAnchors = lengthSort(sortedAnchors, true);
Akronf5dc5102017-05-16 20:32:57 +0200528 },
Akron6f1302b2017-09-13 12:46:02 +0200529
530
531 // Show the element
Akronf5dc5102017-05-16 20:32:57 +0200532 show : function () {
533 var svg = this._element;
Akron3a4a08e2017-05-23 22:34:18 +0200534 var height = this.maxArc;
535
Akron3a4a08e2017-05-23 22:34:18 +0200536 // Delete old group
537 if (svg.getElementsByTagName("g")[0] !== undefined) {
538 var group = svg.getElementsByTagName("g")[0];
539 svg.removeChild(group);
540 this._tokenElements = [];
541 };
542
543 var g = svg.appendChild(this._c("g"));
Akronf5dc5102017-05-16 20:32:57 +0200544
Akron6f1302b2017-09-13 12:46:02 +0200545 // Draw token list
Akron3a4a08e2017-05-23 22:34:18 +0200546 var text = g.appendChild(this._c("text"));
Akron3d204282017-09-07 18:24:18 +0200547 text.setAttribute('class', 'leaf');
Akron3a4a08e2017-05-23 22:34:18 +0200548 text.setAttribute("text-anchor", "start");
549 text.setAttribute("y", height);
Akronf5dc5102017-05-16 20:32:57 +0200550
Akron6f1302b2017-09-13 12:46:02 +0200551 // Calculate the start position
Akron3a4a08e2017-05-23 22:34:18 +0200552 this._y = height - (this.anchorStart);
553
Akron6f1302b2017-09-13 12:46:02 +0200554 // Introduce some prepending whitespace (yeah - I know ...)
Akron3a4a08e2017-05-23 22:34:18 +0200555 var ws = text.appendChild(this._c("tspan"));
556 ws.appendChild(document.createTextNode('\u00A0'));
557 ws.style.textAnchor = "start";
558
Akronf5dc5102017-05-16 20:32:57 +0200559 var lastRight = 0;
560 for (var node_i in this._tokens) {
561 // Append svg
562 var tspan = text.appendChild(this._c("tspan"));
563 tspan.appendChild(document.createTextNode(this._tokens[node_i]));
Akron3a4a08e2017-05-23 22:34:18 +0200564 tspan.setAttribute("text-anchor", "middle");
565
Akronf5dc5102017-05-16 20:32:57 +0200566 this._tokenElements.push(tspan);
567
568 // Add whitespace!
Akron3a4a08e2017-05-23 22:34:18 +0200569 tspan.setAttribute("dx", this.tokenSep);
Akronf5dc5102017-05-16 20:32:57 +0200570 };
571
Akron6f1302b2017-09-13 12:46:02 +0200572 // Get some global position data that may change on resize
Akron15175132017-09-07 18:12:55 +0200573 var globalBoundingBox = g.getBoundingClientRect();
574 this.offsetLeft = globalBoundingBox.left;
575
Akron6f1302b2017-09-13 12:46:02 +0200576 // The group of arcs
Akron3a4a08e2017-05-23 22:34:18 +0200577 var arcs = g.appendChild(this._c("g"));
Akron65d31082017-09-08 16:23:40 +0200578 this._arcsElement = arcs;
Akron3a4a08e2017-05-23 22:34:18 +0200579 arcs.classList.add("arcs");
Akron15175132017-09-07 18:12:55 +0200580
Akronc9f23022017-09-13 15:43:52 +0200581 var labels = g.appendChild(this._c("g"));
582 this._labelsElement = labels;
583 labels.classList.add("labels");
584
Akron15175132017-09-07 18:12:55 +0200585 // Sort arcs if not sorted yet
Akron6f1302b2017-09-13 12:46:02 +0200586 if (this._sortedArcs === undefined)
Akron3a4a08e2017-05-23 22:34:18 +0200587 this._sortArcs();
Akrond67d45b2017-05-18 21:47:38 +0200588
Akron6f1302b2017-09-13 12:46:02 +0200589 // 1. Draw all anchors
Akrond67d45b2017-05-18 21:47:38 +0200590 var i;
591 for (i in this._sortedAnchors) {
Akron65d31082017-09-08 16:23:40 +0200592 this._drawAnchor(this._sortedAnchors[i]);
Akrond67d45b2017-05-18 21:47:38 +0200593 };
Akron15175132017-09-07 18:12:55 +0200594
Akron6f1302b2017-09-13 12:46:02 +0200595 // 2. Draw all arcs
Akrond67d45b2017-05-18 21:47:38 +0200596 for (i in this._sortedArcs) {
Akron65d31082017-09-08 16:23:40 +0200597 this._drawArc(this._sortedArcs[i]);
Akronf5dc5102017-05-16 20:32:57 +0200598 };
Akron3a4a08e2017-05-23 22:34:18 +0200599
Akron6f1302b2017-09-13 12:46:02 +0200600 // Resize the svg with some reasonable margins
Akron3a4a08e2017-05-23 22:34:18 +0200601 var width = text.getBoundingClientRect().width;
Akronf3d7d8e2017-05-23 22:52:54 +0200602 svg.setAttribute("width", width + 20);
603 svg.setAttribute("height", height + 20);
Akron3a4a08e2017-05-23 22:34:18 +0200604 svg.setAttribute("class", "relTree");
Akronf5dc5102017-05-16 20:32:57 +0200605 }
Akrond67d45b2017-05-18 21:47:38 +0200606 };
607
Akron6f1302b2017-09-13 12:46:02 +0200608 // Sort relations regarding their span
Akrond67d45b2017-05-18 21:47:38 +0200609 function lengthSort (list, inclusive) {
610
611 /*
612 * The "inclusive" flag allows to
613 * modify the behaviour for inclusivity check,
614 * e.g. if identical start or endpoints mean overlap or not.
615 */
616
617 var stack = [];
618
619 // Iterate over all definitions
620 for (var i = 0; i < list.length; i++) {
621 var current = list[i];
622
623 // Check the stack order
624 var overlaps = 0;
Akrond67d45b2017-05-18 21:47:38 +0200625 for (var j = (stack.length - 1); j >= 0; j--) {
626 var check = stack[j];
627
628 // (a..(b..b)..a)
629 if (current.first <= check.first && current.last >= check.last) {
630 overlaps = check.overlaps + 1;
631 break;
632 }
633
634 // (a..(b..a)..b)
635 else if (current.first <= check.first && current.last >= check.first) {
636
637 if (inclusive || (current.first != check.first && current.last != check.first)) {
638 overlaps = check.overlaps + (current.length == check.length ? 0 : 1);
639 };
640 }
641
642 // (b..(a..b)..a)
643 else if (current.first <= check.last && current.last >= check.last) {
644
645 if (inclusive || (current.first != check.last && current.last != check.last)) {
646 overlaps = check.overlaps + (current.length == check.length ? 0 : 1);
647 };
648 };
649 };
650
651 // Set overlaps
652 current.overlaps = overlaps;
653
654 stack.push(current);
655
656 // Although it is already sorted,
657 // the new item has to be put at the correct place
Akron6f1302b2017-09-13 12:46:02 +0200658 // TODO:
659 // Use something like splice() instead
Akrond67d45b2017-05-18 21:47:38 +0200660 stack.sort(function (a,b) {
661 b.overlaps - a.overlaps
662 });
663 };
664
665 return stack;
666 };
Akronf5dc5102017-05-16 20:32:57 +0200667});