blob: e3bed9d6e9285a63ab2f0dec2d9533ff251fa719 [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
Akron0988d882017-11-10 16:13:12 +0100530 /**
531 * Center the viewport of the canvas
532 * TODO:
533 * This is identical to tree
534 */
535 center : function () {
536 if (this._element === undefined)
537 return;
538
539 var treeDiv = this._element.parentNode;
540
541 var cWidth = parseFloat(window.getComputedStyle(this._element).width);
542 var treeWidth = parseFloat(window.getComputedStyle(treeDiv).width);
543 // Reposition:
544 if (cWidth > treeWidth) {
545 var scrollValue = (cWidth - treeWidth) / 2;
546 treeDiv.scrollLeft = scrollValue;
547 };
548 },
549
Akron6f1302b2017-09-13 12:46:02 +0200550
551 // Show the element
Akronf5dc5102017-05-16 20:32:57 +0200552 show : function () {
553 var svg = this._element;
Akron3a4a08e2017-05-23 22:34:18 +0200554 var height = this.maxArc;
555
Akron3a4a08e2017-05-23 22:34:18 +0200556 // Delete old group
557 if (svg.getElementsByTagName("g")[0] !== undefined) {
558 var group = svg.getElementsByTagName("g")[0];
559 svg.removeChild(group);
560 this._tokenElements = [];
561 };
562
563 var g = svg.appendChild(this._c("g"));
Akronf5dc5102017-05-16 20:32:57 +0200564
Akron6f1302b2017-09-13 12:46:02 +0200565 // Draw token list
Akron3a4a08e2017-05-23 22:34:18 +0200566 var text = g.appendChild(this._c("text"));
Akron3d204282017-09-07 18:24:18 +0200567 text.setAttribute('class', 'leaf');
Akron3a4a08e2017-05-23 22:34:18 +0200568 text.setAttribute("text-anchor", "start");
569 text.setAttribute("y", height);
Akronf5dc5102017-05-16 20:32:57 +0200570
Akron6f1302b2017-09-13 12:46:02 +0200571 // Calculate the start position
Akron3a4a08e2017-05-23 22:34:18 +0200572 this._y = height - (this.anchorStart);
573
Akron6f1302b2017-09-13 12:46:02 +0200574 // Introduce some prepending whitespace (yeah - I know ...)
Akron3a4a08e2017-05-23 22:34:18 +0200575 var ws = text.appendChild(this._c("tspan"));
576 ws.appendChild(document.createTextNode('\u00A0'));
577 ws.style.textAnchor = "start";
578
Akronf5dc5102017-05-16 20:32:57 +0200579 var lastRight = 0;
580 for (var node_i in this._tokens) {
581 // Append svg
582 var tspan = text.appendChild(this._c("tspan"));
583 tspan.appendChild(document.createTextNode(this._tokens[node_i]));
Akron3a4a08e2017-05-23 22:34:18 +0200584 tspan.setAttribute("text-anchor", "middle");
585
Akronf5dc5102017-05-16 20:32:57 +0200586 this._tokenElements.push(tspan);
587
588 // Add whitespace!
Akron3a4a08e2017-05-23 22:34:18 +0200589 tspan.setAttribute("dx", this.tokenSep);
Akronf5dc5102017-05-16 20:32:57 +0200590 };
591
Akron6f1302b2017-09-13 12:46:02 +0200592 // Get some global position data that may change on resize
Akron15175132017-09-07 18:12:55 +0200593 var globalBoundingBox = g.getBoundingClientRect();
594 this.offsetLeft = globalBoundingBox.left;
595
Akron6f1302b2017-09-13 12:46:02 +0200596 // The group of arcs
Akron3a4a08e2017-05-23 22:34:18 +0200597 var arcs = g.appendChild(this._c("g"));
Akron65d31082017-09-08 16:23:40 +0200598 this._arcsElement = arcs;
Akron3a4a08e2017-05-23 22:34:18 +0200599 arcs.classList.add("arcs");
Akron15175132017-09-07 18:12:55 +0200600
Akronc9f23022017-09-13 15:43:52 +0200601 var labels = g.appendChild(this._c("g"));
602 this._labelsElement = labels;
603 labels.classList.add("labels");
604
Akron15175132017-09-07 18:12:55 +0200605 // Sort arcs if not sorted yet
Akron6f1302b2017-09-13 12:46:02 +0200606 if (this._sortedArcs === undefined)
Akron3a4a08e2017-05-23 22:34:18 +0200607 this._sortArcs();
Akrond67d45b2017-05-18 21:47:38 +0200608
Akron6f1302b2017-09-13 12:46:02 +0200609 // 1. Draw all anchors
Akrond67d45b2017-05-18 21:47:38 +0200610 var i;
611 for (i in this._sortedAnchors) {
Akron65d31082017-09-08 16:23:40 +0200612 this._drawAnchor(this._sortedAnchors[i]);
Akrond67d45b2017-05-18 21:47:38 +0200613 };
Akron15175132017-09-07 18:12:55 +0200614
Akron6f1302b2017-09-13 12:46:02 +0200615 // 2. Draw all arcs
Akrond67d45b2017-05-18 21:47:38 +0200616 for (i in this._sortedArcs) {
Akron65d31082017-09-08 16:23:40 +0200617 this._drawArc(this._sortedArcs[i]);
Akronf5dc5102017-05-16 20:32:57 +0200618 };
Akron3a4a08e2017-05-23 22:34:18 +0200619
Akron6f1302b2017-09-13 12:46:02 +0200620 // Resize the svg with some reasonable margins
Akron3a4a08e2017-05-23 22:34:18 +0200621 var width = text.getBoundingClientRect().width;
Akronf3d7d8e2017-05-23 22:52:54 +0200622 svg.setAttribute("width", width + 20);
623 svg.setAttribute("height", height + 20);
Akron3a4a08e2017-05-23 22:34:18 +0200624 svg.setAttribute("class", "relTree");
Akronf5dc5102017-05-16 20:32:57 +0200625 }
Akrond67d45b2017-05-18 21:47:38 +0200626 };
627
Akron6f1302b2017-09-13 12:46:02 +0200628 // Sort relations regarding their span
Akrond67d45b2017-05-18 21:47:38 +0200629 function lengthSort (list, inclusive) {
630
631 /*
632 * The "inclusive" flag allows to
633 * modify the behaviour for inclusivity check,
634 * e.g. if identical start or endpoints mean overlap or not.
635 */
636
637 var stack = [];
638
639 // Iterate over all definitions
640 for (var i = 0; i < list.length; i++) {
641 var current = list[i];
642
643 // Check the stack order
644 var overlaps = 0;
Akrond67d45b2017-05-18 21:47:38 +0200645 for (var j = (stack.length - 1); j >= 0; j--) {
646 var check = stack[j];
647
648 // (a..(b..b)..a)
649 if (current.first <= check.first && current.last >= check.last) {
650 overlaps = check.overlaps + 1;
651 break;
652 }
653
654 // (a..(b..a)..b)
655 else if (current.first <= check.first && current.last >= check.first) {
656
657 if (inclusive || (current.first != check.first && current.last != check.first)) {
658 overlaps = check.overlaps + (current.length == check.length ? 0 : 1);
659 };
660 }
661
662 // (b..(a..b)..a)
663 else if (current.first <= check.last && current.last >= check.last) {
664
665 if (inclusive || (current.first != check.last && current.last != check.last)) {
666 overlaps = check.overlaps + (current.length == check.length ? 0 : 1);
667 };
668 };
669 };
670
671 // Set overlaps
672 current.overlaps = overlaps;
673
674 stack.push(current);
675
676 // Although it is already sorted,
677 // the new item has to be put at the correct place
Akron6f1302b2017-09-13 12:46:02 +0200678 // TODO:
679 // Use something like splice() instead
Akrond67d45b2017-05-18 21:47:38 +0200680 stack.sort(function (a,b) {
681 b.overlaps - a.overlaps
682 });
683 };
684
685 return stack;
686 };
Akronf5dc5102017-05-16 20:32:57 +0200687});