blob: 22fbc51fa36283ce20edfddce54cbd832e2c8ed0 [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
22 this._tokens = [];
23 this._arcs = []
24 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
29 this.overlapDiff = 40;
30 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
71 // Add relation
72 this.addRel({
73 start : edge.src,
74 end : target,
75 direction : 'uni',
76 label : edge.label
77 });
78 };
79 };
80
81 // Reset parsing memory
82 this.temp = {};
83
Akronf5dc5102017-05-16 20:32:57 +020084 return this;
85 },
86
Akron6f1302b2017-09-13 12:46:02 +020087 // Parse a node of the tree snippet
Akron671fdb92017-09-12 18:09:46 +020088 _parse : function (parent, children, mark) {
Akron6f1302b2017-09-13 12:46:02 +020089
90 // Iterate over all child nodes
Akron671fdb92017-09-12 18:09:46 +020091 for (var i in children) {
92 var c = children[i];
93
94 // Element node
95 if (c.nodeType == 1) {
96
97 // Node is an identifier
98 if (c.hasAttribute('xml:id')) {
99
100 // Remember that pos has this identifier
Akron6f1302b2017-09-13 12:46:02 +0200101 // TODO:
102 // Target may be a span!
Akron671fdb92017-09-12 18:09:46 +0200103 this.temp['target'][c.getAttribute('xml:id')] = this.temp['pos'];
104 }
105
106 // Node is a relation
107 else if (c.hasAttribute('xlink:href')) {
Akron6f1302b2017-09-13 12:46:02 +0200108 var label;
Akron671fdb92017-09-12 18:09:46 +0200109
Akron6f1302b2017-09-13 12:46:02 +0200110 // Get target id
111 var target = c.getAttribute('xlink:href').replace(/^#/, "");
Akron671fdb92017-09-12 18:09:46 +0200112
113 if (c.hasAttribute('xlink:title')) {
114 label = this._clean(c.getAttribute('xlink:title'));
115 };
116
117 // Remember the defined edge
Akron6f1302b2017-09-13 12:46:02 +0200118 // TODO:
119 // src may be a span!
Akron671fdb92017-09-12 18:09:46 +0200120 this.temp['edges'].push({
Akron6f1302b2017-09-13 12:46:02 +0200121 label : label,
122 src : this.temp['pos'],
Akron671fdb92017-09-12 18:09:46 +0200123 targetID : target
124 });
125 };
126
Akron6f1302b2017-09-13 12:46:02 +0200127 // Go on with child nodes
Akron671fdb92017-09-12 18:09:46 +0200128 if (c.hasChildNodes()) {
129 this._parse(0, c.childNodes, mark);
130 };
131 }
132
133 // Text node
134 else if (c.nodeType == 3) {
135
136 // Check, if there is a non-whitespace token
137 if (c.nodeValue !== undefined) {
138 var str = c.nodeValue.trim();
139 if (str !== undefined && str.length > 0) {
140
141 // Add token to token list
142 this.addToken(str);
143
144 // Move token position
145 this.temp['pos']++;
146 };
147 };
148 }
149 };
150 },
151
Akron6f1302b2017-09-13 12:46:02 +0200152
Akron671fdb92017-09-12 18:09:46 +0200153 // Remove foundry and layer for labels
154 _clean : function (title) {
155 return title.replace(_TermRE, "$3");
156 },
157
Akron6f1302b2017-09-13 12:46:02 +0200158
159 // Return the number of leaf nodes
160 // (not necessarily part of a relation).
161 // Consecutive nodes that are not part of any
162 // relation are summarized in one node.
Akron671fdb92017-09-12 18:09:46 +0200163 size : function () {
164 return this._tokens.length;
165 },
Akron15175132017-09-07 18:12:55 +0200166
Akron6f1302b2017-09-13 12:46:02 +0200167
Akronf5dc5102017-05-16 20:32:57 +0200168 // This is a shorthand for SVG element creation
169 _c : function (tag) {
170 return document.createElementNS(svgNS, tag);
171 },
172
Akron15175132017-09-07 18:12:55 +0200173
Akronf5dc5102017-05-16 20:32:57 +0200174 // Returns the center point of the requesting token
175 _tokenPoint : function (node) {
176 var box = node.getBoundingClientRect();
177 return box.x + (box.width / 2);
178 },
179
Akron15175132017-09-07 18:12:55 +0200180
181 // Draws an anchor
Akrond67d45b2017-05-18 21:47:38 +0200182 _drawAnchor : function (anchor) {
Akron15175132017-09-07 18:12:55 +0200183
184 // Calculate the span of the first and last token, the anchor spans
Akron3a4a08e2017-05-23 22:34:18 +0200185 var firstBox = this._tokenElements[anchor.first].getBoundingClientRect();
Akron6f1302b2017-09-13 12:46:02 +0200186 var lastBox = this._tokenElements[anchor.last].getBoundingClientRect();
Akrond67d45b2017-05-18 21:47:38 +0200187
Akron15175132017-09-07 18:12:55 +0200188 var startPos = firstBox.left - this.offsetLeft;
Akron6f1302b2017-09-13 12:46:02 +0200189 var endPos = lastBox.right - this.offsetLeft;
Akron15175132017-09-07 18:12:55 +0200190
Akron3a4a08e2017-05-23 22:34:18 +0200191 var y = this._y + (anchor.overlaps * this.anchorDiff) - this.anchorStart;
192
Akrond67d45b2017-05-18 21:47:38 +0200193 var l = this._c('path');
Akron65d31082017-09-08 16:23:40 +0200194 this._arcsElement.appendChild(l);
Akron3a4a08e2017-05-23 22:34:18 +0200195 l.setAttribute("d", "M " + startPos + "," + y + " L " + endPos + "," + y);
Akrond67d45b2017-05-18 21:47:38 +0200196 l.setAttribute("class", "anchor");
197 anchor.element = l;
198 anchor.y = y;
199 return l;
200 },
201
Akron6f1302b2017-09-13 12:46:02 +0200202
203 // Create an arc with an optional label
Akronf5dc5102017-05-16 20:32:57 +0200204 // Potentially needs a height parameter for stacks
Akron63ae00b2017-05-16 22:03:36 +0200205 _drawArc : function (arc) {
Akronf5dc5102017-05-16 20:32:57 +0200206
Akrond67d45b2017-05-18 21:47:38 +0200207 var startPos, endPos;
Akron3a4a08e2017-05-23 22:34:18 +0200208 var startY = this._y, endY = this._y;
Akronf5dc5102017-05-16 20:32:57 +0200209
Akrond67d45b2017-05-18 21:47:38 +0200210 if (arc.startAnchor !== undefined) {
Akron3a4a08e2017-05-23 22:34:18 +0200211 startPos = this._tokenPoint(arc.startAnchor.element);
Akrond67d45b2017-05-18 21:47:38 +0200212 startY = arc.startAnchor.y;
213 }
214 else {
215 startPos = this._tokenPoint(this._tokenElements[arc.first]);
216 };
217
218 if (arc.endAnchor !== undefined) {
219 endPos = this._tokenPoint(arc.endAnchor.element)
220 endY = arc.endAnchor.y;
221 }
222 else {
223 endPos = this._tokenPoint(this._tokenElements[arc.last]);
224 };
225
Akron15175132017-09-07 18:12:55 +0200226 startPos -= this.offsetLeft;
227 endPos -= this.offsetLeft;
228
Akronf5dc5102017-05-16 20:32:57 +0200229 var g = this._c("g");
Akron65d31082017-09-08 16:23:40 +0200230 g.setAttribute("class", "arc");
Akronf5dc5102017-05-16 20:32:57 +0200231 var p = g.appendChild(this._c("path"));
Akron15175132017-09-07 18:12:55 +0200232 p.setAttribute('class', 'edge');
Akron65d31082017-09-08 16:23:40 +0200233
234 // Attach the new arc before drawing, so computed values are available
235 this._arcsElement.appendChild(g);
Akronf5dc5102017-05-16 20:32:57 +0200236
237 // Create arc
238 var middle = Math.abs(endPos - startPos) / 2;
239
Akron63ae00b2017-05-16 22:03:36 +0200240 // TODO: take the number of tokens into account!
Akronc5b5f742017-05-23 16:04:35 +0200241 var cHeight = this.arcDiff + arc.overlaps * this.overlapDiff + (middle / 2);
242
243 // Respect the maximum height
244 cHeight = cHeight < this.maxArc ? cHeight : this.maxArc;
Akronf5dc5102017-05-16 20:32:57 +0200245
246 var x = Math.min(startPos, endPos);
Akronc5b5f742017-05-23 16:04:35 +0200247
Akron3a4a08e2017-05-23 22:34:18 +0200248 //var controlY = (startY + endY - cHeight);
249 var controlY = (endY - cHeight);
Akronf5dc5102017-05-16 20:32:57 +0200250
Akron3a4a08e2017-05-23 22:34:18 +0200251 var arcE = "M "+ startPos + "," + startY +
252 " C " + startPos + "," + controlY +
253 " " + endPos + "," + controlY +
254 " " + endPos + "," + endY;
Akrond67d45b2017-05-18 21:47:38 +0200255
Akron63ae00b2017-05-16 22:03:36 +0200256 p.setAttribute("d", arcE);
Akronf5dc5102017-05-16 20:32:57 +0200257
Akron3a4a08e2017-05-23 22:34:18 +0200258 if (arc.direction !== undefined) {
259 p.setAttribute("marker-end", "url(#arr)");
260 if (arc.direction === 'bi') {
261 p.setAttribute("marker-start", "url(#arr)");
262 };
263 };
264
Akron6f1302b2017-09-13 12:46:02 +0200265 if (arc.label === undefined)
266 return g;
267
Akronc5b5f742017-05-23 16:04:35 +0200268 /*
269 * Calculate the top point of the arc for labeling using
270 * de Casteljau's algorithm, see e.g.
271 * http://blog.sklambert.com/finding-the-control-points-of-a-bezier-curve/
272 * of course simplified to symmetric arcs ...
273 */
274 // Interpolate one side of the control polygon
Akronc5b5f742017-05-23 16:04:35 +0200275 var middleY = (((startY + controlY) / 2) + controlY) / 2;
276
Akron6f1302b2017-09-13 12:46:02 +0200277 // Create a boxed label
278 var labelE = g.appendChild(this._c("text"));
279 labelE.setAttribute("x", x + middle);
280 labelE.setAttribute("y", middleY + 3);
281 labelE.setAttribute("text-anchor", "middle");
282 var textNode = document.createTextNode(arc.label);
283 labelE.appendChild(textNode);
Akronc5b5f742017-05-23 16:04:35 +0200284
Akron6f1302b2017-09-13 12:46:02 +0200285 var labelBox = labelE.getBBox();
286 var textWidth = labelBox.width; // labelE.getComputedTextLength();
287 var textHeight = labelBox.height; // labelE.getComputedTextLength();
Akron1dc87902017-05-29 16:04:56 +0200288
Akron6f1302b2017-09-13 12:46:02 +0200289 // Add box with padding to left and right
290 var labelR = g.insertBefore(this._c("rect"), labelE);
291 var boxWidth = textWidth + 2 * this.xPadding;
292 labelR.setAttribute("x", x + middle - (boxWidth / 2));
293 labelR.setAttribute("ry", 5);
294 labelR.setAttribute("y", labelBox.y - this.yPadding);
295 labelR.setAttribute("width", boxWidth);
296 labelR.setAttribute("height", textHeight + 2 * this.yPadding);
Akronf5dc5102017-05-16 20:32:57 +0200297 },
298
Akron6f1302b2017-09-13 12:46:02 +0200299 // Get the svg element
Akronf5dc5102017-05-16 20:32:57 +0200300 element : function () {
301 if (this._element !== undefined)
302 return this._element;
303
304 // Create svg
305 var svg = this._c("svg");
Akron3a4a08e2017-05-23 22:34:18 +0200306
307 window.addEventListener("resize", function () {
Akron6f1302b2017-09-13 12:46:02 +0200308 // TODO:
309 // Only if text-size changed!
310 // TODO:
311 // This is currently untested
Akron3a4a08e2017-05-23 22:34:18 +0200312 this.show();
313 }.bind(this));
Akron6f1302b2017-09-13 12:46:02 +0200314
315 // Define marker arrows
Akron3a4a08e2017-05-23 22:34:18 +0200316 var defs = svg.appendChild(this._c("defs"));
317 var marker = defs.appendChild(this._c("marker"));
318 marker.setAttribute("refX", 9);
319 marker.setAttribute("id", "arr");
320 marker.setAttribute("orient", "auto-start-reverse");
321 marker.setAttribute("markerUnits","userSpaceOnUse");
Akron3a4a08e2017-05-23 22:34:18 +0200322 var arrow = this._c("path");
323 arrow.setAttribute("transform", "scale(0.8)");
324 arrow.setAttribute("d", "M 0,-5 0,5 10,0 Z");
325 marker.appendChild(arrow);
326
Akronf5dc5102017-05-16 20:32:57 +0200327 this._element = svg;
328 return this._element;
329 },
330
331 // Add a relation with a start, an end,
Akron6f1302b2017-09-13 12:46:02 +0200332 // a direction value and an optional label text
Akronc5b5f742017-05-23 16:04:35 +0200333 addRel : function (rel) {
334 this._arcs.push(rel);
335 return this;
Akronf5dc5102017-05-16 20:32:57 +0200336 },
337
Akronc5b5f742017-05-23 16:04:35 +0200338
Akron6f1302b2017-09-13 12:46:02 +0200339 // Add a token to the list (this will mostly be a word)
Akronc5b5f742017-05-23 16:04:35 +0200340 addToken : function(token) {
341 this._tokens.push(token);
342 return this;
343 },
344
Akronf5dc5102017-05-16 20:32:57 +0200345 /*
346 * All arcs need to be sorted before shown,
347 * to avoid nesting.
348 */
349 _sortArcs : function () {
350
Akrond67d45b2017-05-18 21:47:38 +0200351 // TODO:
352 // Keep in mind that the arcs may have long anchors!
353 // 1. Iterate over all arcs
354 // 2. Sort all multi
355 var anchors = [];
356
Akronf5dc5102017-05-16 20:32:57 +0200357 // 1. Sort by length
358 // 2. Tag all spans with the number of overlaps before
359 // a) Iterate over all spans
360 // b) check the latest preceeding overlapping span (lpos)
361 // -> not found: tag with 0
362 // -> found: Add +1 to the level of the (lpos)
363 // c) If the new tag is smaller than the previous element,
364 // reorder
Akron63ae00b2017-05-16 22:03:36 +0200365
366 // Normalize start and end
367 var sortedArcs = this._arcs.map(function (v) {
Akrond67d45b2017-05-18 21:47:38 +0200368
369 // Check for long anchors
370 if (v.start instanceof Array) {
371 var middle = Math.ceil(Math.abs(v.start[1] - v.start[0]) / 2) + v.start[0];
372
373 v.startAnchor = {
Akron3a4a08e2017-05-23 22:34:18 +0200374 "first": v.start[0],
375 "last" : v.start[1],
Akrond67d45b2017-05-18 21:47:38 +0200376 "length" : v.start[1] - v.start[0]
377 };
378
379 // Add to anchors list
380 anchors.push(v.startAnchor);
381 v.start = middle;
382 };
383
384 if (v.end instanceof Array) {
385 var middle = Math.abs(v.end[0] - v.end[1]) + v.end[0];
386 v.endAnchor = {
Akron3a4a08e2017-05-23 22:34:18 +0200387 "first": v.end[0],
388 "last" : v.end[1],
Akrond67d45b2017-05-18 21:47:38 +0200389 "length" : v.end[1] - v.end[0]
390 };
391
392 // Add to anchors list
393 anchors.push(v.endAnchor);
394 v.end = middle;
395 };
396
397 // calculate the arch length
Akron63ae00b2017-05-16 22:03:36 +0200398 if (v.start < v.end) {
399 v.first = v.start;
400 v.last = v.end;
401 v.length = v.end - v.start;
402 }
403 else {
404 v.first = v.end;
405 v.last = v.start;
406 v.length = v.start - v.end;
407 };
408 return v;
409 });
410
411 // Sort based on length
412 sortedArcs.sort(function (a, b) {
413 if (a.length < b.length)
414 return -1;
415 else
416 return 1;
417 });
418
Akron3a4a08e2017-05-23 22:34:18 +0200419 this._sortedArcs = lengthSort(sortedArcs, false);
Akrond67d45b2017-05-18 21:47:38 +0200420 this._sortedAnchors = lengthSort(anchors, true);
Akronf5dc5102017-05-16 20:32:57 +0200421 },
Akron6f1302b2017-09-13 12:46:02 +0200422
423
424 // Show the element
Akronf5dc5102017-05-16 20:32:57 +0200425 show : function () {
426 var svg = this._element;
Akron3a4a08e2017-05-23 22:34:18 +0200427 var height = this.maxArc;
428
Akron3a4a08e2017-05-23 22:34:18 +0200429 // Delete old group
430 if (svg.getElementsByTagName("g")[0] !== undefined) {
431 var group = svg.getElementsByTagName("g")[0];
432 svg.removeChild(group);
433 this._tokenElements = [];
434 };
435
436 var g = svg.appendChild(this._c("g"));
Akronf5dc5102017-05-16 20:32:57 +0200437
Akron6f1302b2017-09-13 12:46:02 +0200438 // Draw token list
Akron3a4a08e2017-05-23 22:34:18 +0200439 var text = g.appendChild(this._c("text"));
Akron3d204282017-09-07 18:24:18 +0200440 text.setAttribute('class', 'leaf');
Akron3a4a08e2017-05-23 22:34:18 +0200441 text.setAttribute("text-anchor", "start");
442 text.setAttribute("y", height);
Akronf5dc5102017-05-16 20:32:57 +0200443
Akron6f1302b2017-09-13 12:46:02 +0200444 // Calculate the start position
Akron3a4a08e2017-05-23 22:34:18 +0200445 this._y = height - (this.anchorStart);
446
Akron6f1302b2017-09-13 12:46:02 +0200447 // Introduce some prepending whitespace (yeah - I know ...)
Akron3a4a08e2017-05-23 22:34:18 +0200448 var ws = text.appendChild(this._c("tspan"));
449 ws.appendChild(document.createTextNode('\u00A0'));
450 ws.style.textAnchor = "start";
451
Akronf5dc5102017-05-16 20:32:57 +0200452 var lastRight = 0;
453 for (var node_i in this._tokens) {
454 // Append svg
455 var tspan = text.appendChild(this._c("tspan"));
456 tspan.appendChild(document.createTextNode(this._tokens[node_i]));
Akron3a4a08e2017-05-23 22:34:18 +0200457 tspan.setAttribute("text-anchor", "middle");
458
Akronf5dc5102017-05-16 20:32:57 +0200459 this._tokenElements.push(tspan);
460
461 // Add whitespace!
Akron3a4a08e2017-05-23 22:34:18 +0200462 tspan.setAttribute("dx", this.tokenSep);
Akronf5dc5102017-05-16 20:32:57 +0200463 };
464
Akron6f1302b2017-09-13 12:46:02 +0200465 // Get some global position data that may change on resize
Akron15175132017-09-07 18:12:55 +0200466 var globalBoundingBox = g.getBoundingClientRect();
467 this.offsetLeft = globalBoundingBox.left;
468
Akron6f1302b2017-09-13 12:46:02 +0200469 // The group of arcs
Akron3a4a08e2017-05-23 22:34:18 +0200470 var arcs = g.appendChild(this._c("g"));
Akron65d31082017-09-08 16:23:40 +0200471 this._arcsElement = arcs;
Akron3a4a08e2017-05-23 22:34:18 +0200472 arcs.classList.add("arcs");
Akron15175132017-09-07 18:12:55 +0200473
474 // Sort arcs if not sorted yet
Akron6f1302b2017-09-13 12:46:02 +0200475 if (this._sortedArcs === undefined)
Akron3a4a08e2017-05-23 22:34:18 +0200476 this._sortArcs();
Akrond67d45b2017-05-18 21:47:38 +0200477
Akron6f1302b2017-09-13 12:46:02 +0200478 // 1. Draw all anchors
Akrond67d45b2017-05-18 21:47:38 +0200479 var i;
480 for (i in this._sortedAnchors) {
Akron65d31082017-09-08 16:23:40 +0200481 this._drawAnchor(this._sortedAnchors[i]);
Akrond67d45b2017-05-18 21:47:38 +0200482 };
Akron15175132017-09-07 18:12:55 +0200483
Akron6f1302b2017-09-13 12:46:02 +0200484 // 2. Draw all arcs
Akrond67d45b2017-05-18 21:47:38 +0200485 for (i in this._sortedArcs) {
Akron65d31082017-09-08 16:23:40 +0200486 this._drawArc(this._sortedArcs[i]);
Akronf5dc5102017-05-16 20:32:57 +0200487 };
Akron3a4a08e2017-05-23 22:34:18 +0200488
Akron6f1302b2017-09-13 12:46:02 +0200489 // Resize the svg with some reasonable margins
Akron3a4a08e2017-05-23 22:34:18 +0200490 var width = text.getBoundingClientRect().width;
Akronf3d7d8e2017-05-23 22:52:54 +0200491 svg.setAttribute("width", width + 20);
492 svg.setAttribute("height", height + 20);
Akron3a4a08e2017-05-23 22:34:18 +0200493 svg.setAttribute("class", "relTree");
Akronf5dc5102017-05-16 20:32:57 +0200494 }
Akrond67d45b2017-05-18 21:47:38 +0200495 };
496
Akron6f1302b2017-09-13 12:46:02 +0200497 // Sort relations regarding their span
Akrond67d45b2017-05-18 21:47:38 +0200498 function lengthSort (list, inclusive) {
499
500 /*
501 * The "inclusive" flag allows to
502 * modify the behaviour for inclusivity check,
503 * e.g. if identical start or endpoints mean overlap or not.
504 */
505
506 var stack = [];
507
508 // Iterate over all definitions
509 for (var i = 0; i < list.length; i++) {
510 var current = list[i];
511
512 // Check the stack order
513 var overlaps = 0;
Akrond67d45b2017-05-18 21:47:38 +0200514 for (var j = (stack.length - 1); j >= 0; j--) {
515 var check = stack[j];
516
517 // (a..(b..b)..a)
518 if (current.first <= check.first && current.last >= check.last) {
519 overlaps = check.overlaps + 1;
520 break;
521 }
522
523 // (a..(b..a)..b)
524 else if (current.first <= check.first && current.last >= check.first) {
525
526 if (inclusive || (current.first != check.first && current.last != check.first)) {
527 overlaps = check.overlaps + (current.length == check.length ? 0 : 1);
528 };
529 }
530
531 // (b..(a..b)..a)
532 else if (current.first <= check.last && current.last >= check.last) {
533
534 if (inclusive || (current.first != check.last && current.last != check.last)) {
535 overlaps = check.overlaps + (current.length == check.length ? 0 : 1);
536 };
537 };
538 };
539
540 // Set overlaps
541 current.overlaps = overlaps;
542
543 stack.push(current);
544
545 // Although it is already sorted,
546 // the new item has to be put at the correct place
Akron6f1302b2017-09-13 12:46:02 +0200547 // TODO:
548 // Use something like splice() instead
Akrond67d45b2017-05-18 21:47:38 +0200549 stack.sort(function (a,b) {
550 b.overlaps - a.overlaps
551 });
552 };
553
554 return stack;
555 };
Akronf5dc5102017-05-16 20:32:57 +0200556});