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