Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 1 | define(function () { |
| 2 | "use strict"; |
| 3 | |
| 4 | var svgNS = "http://www.w3.org/2000/svg"; |
| 5 | |
| 6 | return { |
| 7 | create : function (snippet) { |
| 8 | var obj = Object.create(this)._init(snippet); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 9 | obj._tokens = []; |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 10 | obj._arcs = [] |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 11 | obj._tokenElements = []; |
| 12 | obj._y = 0; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 13 | |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 14 | // Some configurations |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 15 | obj.maxArc = 200; // maximum height of the bezier control point |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 16 | obj.overlapDiff = 40; |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 17 | obj.arcDiff = 15; |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 18 | obj.anchorDiff = 8; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 19 | obj.anchorStart = 15; |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 20 | obj.tokenSep = 30; |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 21 | obj.xPadding = 10; |
| 22 | obj.yPadding = 5; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 23 | return obj; |
| 24 | }, |
| 25 | |
| 26 | _init : function (snippet) { |
| 27 | /* |
| 28 | var html = document.createElement("div"); |
| 29 | html.innerHTML = snippet; |
| 30 | */ |
| 31 | return this; |
| 32 | }, |
| 33 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 34 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 35 | // This is a shorthand for SVG element creation |
| 36 | _c : function (tag) { |
| 37 | return document.createElementNS(svgNS, tag); |
| 38 | }, |
| 39 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 40 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 41 | // Returns the center point of the requesting token |
| 42 | _tokenPoint : function (node) { |
| 43 | var box = node.getBoundingClientRect(); |
| 44 | return box.x + (box.width / 2); |
| 45 | }, |
| 46 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 47 | |
| 48 | // Draws an anchor |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 49 | _drawAnchor : function (anchor) { |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 50 | |
| 51 | // Calculate the span of the first and last token, the anchor spans |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 52 | var firstBox = this._tokenElements[anchor.first].getBoundingClientRect(); |
| 53 | var lastBox = this._tokenElements[anchor.last].getBoundingClientRect(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 54 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 55 | var startPos = firstBox.left - this.offsetLeft; |
| 56 | var endPos = lastBox.right - this.offsetLeft; |
| 57 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 58 | var y = this._y + (anchor.overlaps * this.anchorDiff) - this.anchorStart; |
| 59 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 60 | var l = this._c('path'); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 61 | this._arcsElement.appendChild(l); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 62 | l.setAttribute("d", "M " + startPos + "," + y + " L " + endPos + "," + y); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 63 | l.setAttribute("class", "anchor"); |
| 64 | anchor.element = l; |
| 65 | anchor.y = y; |
| 66 | return l; |
| 67 | }, |
| 68 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 69 | // Create an arc with a label |
| 70 | // Potentially needs a height parameter for stacks |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 71 | _drawArc : function (arc) { |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 72 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 73 | var startPos, endPos; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 74 | var startY = this._y, endY = this._y; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 75 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 76 | if (arc.startAnchor !== undefined) { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 77 | startPos = this._tokenPoint(arc.startAnchor.element); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 78 | startY = arc.startAnchor.y; |
| 79 | } |
| 80 | else { |
| 81 | startPos = this._tokenPoint(this._tokenElements[arc.first]); |
| 82 | }; |
| 83 | |
| 84 | if (arc.endAnchor !== undefined) { |
| 85 | endPos = this._tokenPoint(arc.endAnchor.element) |
| 86 | endY = arc.endAnchor.y; |
| 87 | } |
| 88 | else { |
| 89 | endPos = this._tokenPoint(this._tokenElements[arc.last]); |
| 90 | }; |
| 91 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 92 | startPos -= this.offsetLeft; |
| 93 | endPos -= this.offsetLeft; |
| 94 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 95 | var g = this._c("g"); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 96 | g.setAttribute("class", "arc"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 97 | var p = g.appendChild(this._c("path")); |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 98 | p.setAttribute('class', 'edge'); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 99 | |
| 100 | // Attach the new arc before drawing, so computed values are available |
| 101 | this._arcsElement.appendChild(g); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 102 | |
| 103 | // Create arc |
| 104 | var middle = Math.abs(endPos - startPos) / 2; |
| 105 | |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 106 | // TODO: take the number of tokens into account! |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 107 | var cHeight = this.arcDiff + arc.overlaps * this.overlapDiff + (middle / 2); |
| 108 | |
| 109 | // Respect the maximum height |
| 110 | cHeight = cHeight < this.maxArc ? cHeight : this.maxArc; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 111 | |
| 112 | var x = Math.min(startPos, endPos); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 113 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 114 | //var controlY = (startY + endY - cHeight); |
| 115 | var controlY = (endY - cHeight); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 116 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 117 | var arcE = "M "+ startPos + "," + startY + |
| 118 | " C " + startPos + "," + controlY + |
| 119 | " " + endPos + "," + controlY + |
| 120 | " " + endPos + "," + endY; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 121 | |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 122 | p.setAttribute("d", arcE); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 123 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 124 | if (arc.direction !== undefined) { |
| 125 | p.setAttribute("marker-end", "url(#arr)"); |
| 126 | if (arc.direction === 'bi') { |
| 127 | p.setAttribute("marker-start", "url(#arr)"); |
| 128 | }; |
| 129 | }; |
| 130 | |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 131 | /* |
| 132 | * Calculate the top point of the arc for labeling using |
| 133 | * de Casteljau's algorithm, see e.g. |
| 134 | * http://blog.sklambert.com/finding-the-control-points-of-a-bezier-curve/ |
| 135 | * of course simplified to symmetric arcs ... |
| 136 | */ |
| 137 | // Interpolate one side of the control polygon |
| 138 | // var controlInterpY1 = (startY + controlY) / 2; |
| 139 | // var controlInterpY2 = (controlInterpY1 + controlY) / 2; |
| 140 | var middleY = (((startY + controlY) / 2) + controlY) / 2; |
| 141 | |
| 142 | // WARNING! |
| 143 | // This won't respect span anchors, adjusting startY and endY! |
| 144 | |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 145 | if (arc.label !== undefined) { |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 146 | var labelE = g.appendChild(this._c("text")); |
| 147 | labelE.setAttribute("x", x + middle); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 148 | labelE.setAttribute("y", middleY + 3); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 149 | labelE.setAttribute("text-anchor", "middle"); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 150 | var textNode = document.createTextNode(arc.label); |
| 151 | labelE.appendChild(textNode); |
Akron | 1dc8790 | 2017-05-29 16:04:56 +0200 | [diff] [blame] | 152 | |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 153 | var labelBox = labelE.getBBox(); |
| 154 | var textWidth = labelBox.width; // labelE.getComputedTextLength(); |
| 155 | var textHeight = labelBox.height; // labelE.getComputedTextLength(); |
Akron | 1dc8790 | 2017-05-29 16:04:56 +0200 | [diff] [blame] | 156 | |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 157 | // Add padding to left and right |
| 158 | |
| 159 | // var labelR = g.appendChild(this._c("rect")); |
| 160 | var labelR = g.insertBefore(this._c("rect"), labelE); |
| 161 | var boxWidth = textWidth + 2 * this.xPadding; |
| 162 | labelR.setAttribute("x", x + middle - (boxWidth / 2)); |
| 163 | labelR.setAttribute("ry", 5); |
| 164 | labelR.setAttribute("y", labelBox.y - this.yPadding); |
| 165 | labelR.setAttribute("width", boxWidth); |
| 166 | labelR.setAttribute("height", textHeight + 2*this.yPadding); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 167 | }; |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 168 | |
| 169 | // return g; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 170 | }, |
| 171 | |
| 172 | element : function () { |
| 173 | if (this._element !== undefined) |
| 174 | return this._element; |
| 175 | |
| 176 | // Create svg |
| 177 | var svg = this._c("svg"); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 178 | |
| 179 | window.addEventListener("resize", function () { |
| 180 | // TODO: Only if text-size changed! |
| 181 | this.show(); |
| 182 | }.bind(this)); |
| 183 | |
| 184 | var defs = svg.appendChild(this._c("defs")); |
| 185 | var marker = defs.appendChild(this._c("marker")); |
| 186 | marker.setAttribute("refX", 9); |
| 187 | marker.setAttribute("id", "arr"); |
| 188 | marker.setAttribute("orient", "auto-start-reverse"); |
| 189 | marker.setAttribute("markerUnits","userSpaceOnUse"); |
| 190 | |
| 191 | var arrow = this._c("path"); |
| 192 | arrow.setAttribute("transform", "scale(0.8)"); |
| 193 | arrow.setAttribute("d", "M 0,-5 0,5 10,0 Z"); |
| 194 | marker.appendChild(arrow); |
| 195 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 196 | this._element = svg; |
| 197 | return this._element; |
| 198 | }, |
| 199 | |
| 200 | // Add a relation with a start, an end, |
| 201 | // a direction value and a label text |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 202 | addRel : function (rel) { |
| 203 | this._arcs.push(rel); |
| 204 | return this; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 205 | }, |
| 206 | |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 207 | |
| 208 | addToken : function(token) { |
| 209 | this._tokens.push(token); |
| 210 | return this; |
| 211 | }, |
| 212 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 213 | /* |
| 214 | * All arcs need to be sorted before shown, |
| 215 | * to avoid nesting. |
| 216 | */ |
| 217 | _sortArcs : function () { |
| 218 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 219 | |
| 220 | // TODO: |
| 221 | // Keep in mind that the arcs may have long anchors! |
| 222 | // 1. Iterate over all arcs |
| 223 | // 2. Sort all multi |
| 224 | var anchors = []; |
| 225 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 226 | // 1. Sort by length |
| 227 | // 2. Tag all spans with the number of overlaps before |
| 228 | // a) Iterate over all spans |
| 229 | // b) check the latest preceeding overlapping span (lpos) |
| 230 | // -> not found: tag with 0 |
| 231 | // -> found: Add +1 to the level of the (lpos) |
| 232 | // c) If the new tag is smaller than the previous element, |
| 233 | // reorder |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 234 | |
| 235 | // Normalize start and end |
| 236 | var sortedArcs = this._arcs.map(function (v) { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 237 | |
| 238 | // Check for long anchors |
| 239 | if (v.start instanceof Array) { |
| 240 | var middle = Math.ceil(Math.abs(v.start[1] - v.start[0]) / 2) + v.start[0]; |
| 241 | |
| 242 | v.startAnchor = { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 243 | "first": v.start[0], |
| 244 | "last" : v.start[1], |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 245 | "length" : v.start[1] - v.start[0] |
| 246 | }; |
| 247 | |
| 248 | // Add to anchors list |
| 249 | anchors.push(v.startAnchor); |
| 250 | v.start = middle; |
| 251 | }; |
| 252 | |
| 253 | if (v.end instanceof Array) { |
| 254 | var middle = Math.abs(v.end[0] - v.end[1]) + v.end[0]; |
| 255 | v.endAnchor = { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 256 | "first": v.end[0], |
| 257 | "last" : v.end[1], |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 258 | "length" : v.end[1] - v.end[0] |
| 259 | }; |
| 260 | |
| 261 | // Add to anchors list |
| 262 | anchors.push(v.endAnchor); |
| 263 | v.end = middle; |
| 264 | }; |
| 265 | |
| 266 | // calculate the arch length |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 267 | if (v.start < v.end) { |
| 268 | v.first = v.start; |
| 269 | v.last = v.end; |
| 270 | v.length = v.end - v.start; |
| 271 | } |
| 272 | else { |
| 273 | v.first = v.end; |
| 274 | v.last = v.start; |
| 275 | v.length = v.start - v.end; |
| 276 | }; |
| 277 | return v; |
| 278 | }); |
| 279 | |
| 280 | // Sort based on length |
| 281 | sortedArcs.sort(function (a, b) { |
| 282 | if (a.length < b.length) |
| 283 | return -1; |
| 284 | else |
| 285 | return 1; |
| 286 | }); |
| 287 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 288 | this._sortedArcs = lengthSort(sortedArcs, false); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 289 | this._sortedAnchors = lengthSort(anchors, true); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 290 | }, |
| 291 | |
| 292 | show : function () { |
| 293 | var svg = this._element; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 294 | var height = this.maxArc; |
| 295 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 296 | // Delete old group |
| 297 | if (svg.getElementsByTagName("g")[0] !== undefined) { |
| 298 | var group = svg.getElementsByTagName("g")[0]; |
| 299 | svg.removeChild(group); |
| 300 | this._tokenElements = []; |
| 301 | }; |
| 302 | |
| 303 | var g = svg.appendChild(this._c("g")); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 304 | |
| 305 | /* |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 306 | * Create token list |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 307 | */ |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 308 | var text = g.appendChild(this._c("text")); |
Akron | 3d20428 | 2017-09-07 18:24:18 +0200 | [diff] [blame] | 309 | text.setAttribute('class', 'leaf'); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 310 | text.setAttribute("text-anchor", "start"); |
| 311 | text.setAttribute("y", height); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 312 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 313 | this._y = height - (this.anchorStart); |
| 314 | |
| 315 | var ws = text.appendChild(this._c("tspan")); |
| 316 | ws.appendChild(document.createTextNode('\u00A0')); |
| 317 | ws.style.textAnchor = "start"; |
| 318 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 319 | var lastRight = 0; |
| 320 | for (var node_i in this._tokens) { |
| 321 | // Append svg |
| 322 | var tspan = text.appendChild(this._c("tspan")); |
| 323 | tspan.appendChild(document.createTextNode(this._tokens[node_i])); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 324 | tspan.setAttribute("text-anchor", "middle"); |
| 325 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 326 | this._tokenElements.push(tspan); |
| 327 | |
| 328 | // Add whitespace! |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 329 | //var ws = text.appendChild(this._c("tspan")); |
| 330 | //ws.appendChild(document.createTextNode(" ")); |
| 331 | // ws.setAttribute("class", "rel-ws"); |
| 332 | tspan.setAttribute("dx", this.tokenSep); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 333 | }; |
| 334 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 335 | var globalBoundingBox = g.getBoundingClientRect(); |
| 336 | this.offsetLeft = globalBoundingBox.left; |
| 337 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 338 | var arcs = g.appendChild(this._c("g")); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 339 | this._arcsElement = arcs; |
| 340 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 341 | arcs.classList.add("arcs"); |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 342 | |
| 343 | // Sort arcs if not sorted yet |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 344 | if (this._sortedArcs === undefined) { |
| 345 | this._sortArcs(); |
| 346 | }; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 347 | |
| 348 | var i; |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 349 | |
| 350 | // Draw all anchors |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 351 | for (i in this._sortedAnchors) { |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 352 | this._drawAnchor(this._sortedAnchors[i]); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 353 | }; |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 354 | |
| 355 | |
| 356 | // draw all arcs |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 357 | for (i in this._sortedArcs) { |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 358 | this._drawArc(this._sortedArcs[i]); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 359 | }; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 360 | |
| 361 | var width = text.getBoundingClientRect().width; |
Akron | f3d7d8e | 2017-05-23 22:52:54 +0200 | [diff] [blame] | 362 | svg.setAttribute("width", width + 20); |
| 363 | svg.setAttribute("height", height + 20); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 364 | svg.setAttribute("class", "relTree"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 365 | } |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | function lengthSort (list, inclusive) { |
| 369 | |
| 370 | /* |
| 371 | * The "inclusive" flag allows to |
| 372 | * modify the behaviour for inclusivity check, |
| 373 | * e.g. if identical start or endpoints mean overlap or not. |
| 374 | */ |
| 375 | |
| 376 | var stack = []; |
| 377 | |
| 378 | // Iterate over all definitions |
| 379 | for (var i = 0; i < list.length; i++) { |
| 380 | var current = list[i]; |
| 381 | |
| 382 | // Check the stack order |
| 383 | var overlaps = 0; |
| 384 | |
| 385 | for (var j = (stack.length - 1); j >= 0; j--) { |
| 386 | var check = stack[j]; |
| 387 | |
| 388 | // (a..(b..b)..a) |
| 389 | if (current.first <= check.first && current.last >= check.last) { |
| 390 | overlaps = check.overlaps + 1; |
| 391 | break; |
| 392 | } |
| 393 | |
| 394 | // (a..(b..a)..b) |
| 395 | else if (current.first <= check.first && current.last >= check.first) { |
| 396 | |
| 397 | if (inclusive || (current.first != check.first && current.last != check.first)) { |
| 398 | overlaps = check.overlaps + (current.length == check.length ? 0 : 1); |
| 399 | }; |
| 400 | } |
| 401 | |
| 402 | // (b..(a..b)..a) |
| 403 | else if (current.first <= check.last && current.last >= check.last) { |
| 404 | |
| 405 | if (inclusive || (current.first != check.last && current.last != check.last)) { |
| 406 | overlaps = check.overlaps + (current.length == check.length ? 0 : 1); |
| 407 | }; |
| 408 | }; |
| 409 | }; |
| 410 | |
| 411 | // Set overlaps |
| 412 | current.overlaps = overlaps; |
| 413 | |
| 414 | stack.push(current); |
| 415 | |
| 416 | // Although it is already sorted, |
| 417 | // the new item has to be put at the correct place |
| 418 | // TODO: Use something like splice() instead |
| 419 | stack.sort(function (a,b) { |
| 420 | b.overlaps - a.overlaps |
| 421 | }); |
| 422 | }; |
| 423 | |
| 424 | return stack; |
| 425 | }; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 426 | }); |