Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 1 | /** |
| 2 | * Parse a relational tree and visualize using arcs. |
| 3 | * |
| 4 | * @author Nils Diewald |
| 5 | */ |
| 6 | |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 7 | define([], function () { |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 8 | "use strict"; |
| 9 | |
| 10 | var svgNS = "http://www.w3.org/2000/svg"; |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 11 | var _TermRE = new RegExp("^(?:([^\/]+?)\/)?([^:]+?):(.+?)$"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 12 | |
| 13 | return { |
| 14 | create : function (snippet) { |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 15 | return Object.create(this)._init(snippet); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 16 | }, |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 17 | |
| 18 | // Initialize the state of the object |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 19 | _init : function (snippet) { |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 20 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 21 | // Predefine some values |
| 22 | this._tokens = []; |
| 23 | this._arcs = [] |
| 24 | this._tokenElements = []; |
| 25 | this._y = 0; |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 26 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 27 | // 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; |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 36 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 37 | // No snippet to process |
| 38 | if (snippet == undefined || snippet == null) |
| 39 | return this; |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 40 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 41 | // Parse the snippet |
| 42 | var html = document.createElement("div"); |
| 43 | html.innerHTML = snippet; |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 44 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 45 | // 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 |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 52 | // 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 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 84 | return this; |
| 85 | }, |
| 86 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 87 | // Parse a node of the tree snippet |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 88 | _parse : function (parent, children, mark) { |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 89 | |
| 90 | // Iterate over all child nodes |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 91 | 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 |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 101 | // TODO: |
| 102 | // Target may be a span! |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 103 | this.temp['target'][c.getAttribute('xml:id')] = this.temp['pos']; |
| 104 | } |
| 105 | |
| 106 | // Node is a relation |
| 107 | else if (c.hasAttribute('xlink:href')) { |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 108 | var label; |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 109 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 110 | // Get target id |
| 111 | var target = c.getAttribute('xlink:href').replace(/^#/, ""); |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 112 | |
| 113 | if (c.hasAttribute('xlink:title')) { |
| 114 | label = this._clean(c.getAttribute('xlink:title')); |
| 115 | }; |
| 116 | |
| 117 | // Remember the defined edge |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 118 | // TODO: |
| 119 | // src may be a span! |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 120 | this.temp['edges'].push({ |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 121 | label : label, |
| 122 | src : this.temp['pos'], |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 123 | targetID : target |
| 124 | }); |
| 125 | }; |
| 126 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 127 | // Go on with child nodes |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 128 | 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 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 152 | |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 153 | // Remove foundry and layer for labels |
| 154 | _clean : function (title) { |
| 155 | return title.replace(_TermRE, "$3"); |
| 156 | }, |
| 157 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 158 | |
| 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. |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 163 | size : function () { |
| 164 | return this._tokens.length; |
| 165 | }, |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 166 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 167 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 168 | // This is a shorthand for SVG element creation |
| 169 | _c : function (tag) { |
| 170 | return document.createElementNS(svgNS, tag); |
| 171 | }, |
| 172 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 173 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 174 | // 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 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 180 | |
| 181 | // Draws an anchor |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 182 | _drawAnchor : function (anchor) { |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 183 | |
| 184 | // Calculate the span of the first and last token, the anchor spans |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 185 | var firstBox = this._tokenElements[anchor.first].getBoundingClientRect(); |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 186 | var lastBox = this._tokenElements[anchor.last].getBoundingClientRect(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 187 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 188 | var startPos = firstBox.left - this.offsetLeft; |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 189 | var endPos = lastBox.right - this.offsetLeft; |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 190 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 191 | var y = this._y + (anchor.overlaps * this.anchorDiff) - this.anchorStart; |
| 192 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 193 | var l = this._c('path'); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 194 | this._arcsElement.appendChild(l); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 195 | l.setAttribute("d", "M " + startPos + "," + y + " L " + endPos + "," + y); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 196 | l.setAttribute("class", "anchor"); |
| 197 | anchor.element = l; |
| 198 | anchor.y = y; |
| 199 | return l; |
| 200 | }, |
| 201 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 202 | |
| 203 | // Create an arc with an optional label |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 204 | // Potentially needs a height parameter for stacks |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 205 | _drawArc : function (arc) { |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 206 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 207 | var startPos, endPos; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 208 | var startY = this._y, endY = this._y; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 209 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 210 | if (arc.startAnchor !== undefined) { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 211 | startPos = this._tokenPoint(arc.startAnchor.element); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 212 | 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 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 226 | startPos -= this.offsetLeft; |
| 227 | endPos -= this.offsetLeft; |
| 228 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 229 | var g = this._c("g"); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 230 | g.setAttribute("class", "arc"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 231 | var p = g.appendChild(this._c("path")); |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 232 | p.setAttribute('class', 'edge'); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 233 | |
| 234 | // Attach the new arc before drawing, so computed values are available |
| 235 | this._arcsElement.appendChild(g); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 236 | |
| 237 | // Create arc |
| 238 | var middle = Math.abs(endPos - startPos) / 2; |
| 239 | |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 240 | // TODO: take the number of tokens into account! |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 241 | var cHeight = this.arcDiff + arc.overlaps * this.overlapDiff + (middle / 2); |
| 242 | |
| 243 | // Respect the maximum height |
| 244 | cHeight = cHeight < this.maxArc ? cHeight : this.maxArc; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 245 | |
| 246 | var x = Math.min(startPos, endPos); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 247 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 248 | //var controlY = (startY + endY - cHeight); |
| 249 | var controlY = (endY - cHeight); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 250 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 251 | var arcE = "M "+ startPos + "," + startY + |
| 252 | " C " + startPos + "," + controlY + |
| 253 | " " + endPos + "," + controlY + |
| 254 | " " + endPos + "," + endY; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 255 | |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 256 | p.setAttribute("d", arcE); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 257 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 258 | 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 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 265 | if (arc.label === undefined) |
| 266 | return g; |
| 267 | |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 268 | /* |
| 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 |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 275 | var middleY = (((startY + controlY) / 2) + controlY) / 2; |
| 276 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 277 | // 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); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 284 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 285 | var labelBox = labelE.getBBox(); |
| 286 | var textWidth = labelBox.width; // labelE.getComputedTextLength(); |
| 287 | var textHeight = labelBox.height; // labelE.getComputedTextLength(); |
Akron | 1dc8790 | 2017-05-29 16:04:56 +0200 | [diff] [blame] | 288 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 289 | // 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); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 297 | }, |
| 298 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 299 | // Get the svg element |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 300 | element : function () { |
| 301 | if (this._element !== undefined) |
| 302 | return this._element; |
| 303 | |
| 304 | // Create svg |
| 305 | var svg = this._c("svg"); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 306 | |
| 307 | window.addEventListener("resize", function () { |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 308 | // TODO: |
| 309 | // Only if text-size changed! |
| 310 | // TODO: |
| 311 | // This is currently untested |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 312 | this.show(); |
| 313 | }.bind(this)); |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 314 | |
| 315 | // Define marker arrows |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 316 | 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"); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 322 | 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 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 327 | this._element = svg; |
| 328 | return this._element; |
| 329 | }, |
| 330 | |
| 331 | // Add a relation with a start, an end, |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 332 | // a direction value and an optional label text |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 333 | addRel : function (rel) { |
| 334 | this._arcs.push(rel); |
| 335 | return this; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 336 | }, |
| 337 | |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 338 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 339 | // Add a token to the list (this will mostly be a word) |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 340 | addToken : function(token) { |
| 341 | this._tokens.push(token); |
| 342 | return this; |
| 343 | }, |
| 344 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 345 | /* |
| 346 | * All arcs need to be sorted before shown, |
| 347 | * to avoid nesting. |
| 348 | */ |
| 349 | _sortArcs : function () { |
| 350 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 351 | // 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 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 357 | // 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 |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 365 | |
| 366 | // Normalize start and end |
| 367 | var sortedArcs = this._arcs.map(function (v) { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 368 | |
| 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 = { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 374 | "first": v.start[0], |
| 375 | "last" : v.start[1], |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 376 | "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 = { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 387 | "first": v.end[0], |
| 388 | "last" : v.end[1], |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 389 | "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 |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 398 | 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 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 419 | this._sortedArcs = lengthSort(sortedArcs, false); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 420 | this._sortedAnchors = lengthSort(anchors, true); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 421 | }, |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 422 | |
| 423 | |
| 424 | // Show the element |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 425 | show : function () { |
| 426 | var svg = this._element; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 427 | var height = this.maxArc; |
| 428 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 429 | // 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")); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 437 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 438 | // Draw token list |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 439 | var text = g.appendChild(this._c("text")); |
Akron | 3d20428 | 2017-09-07 18:24:18 +0200 | [diff] [blame] | 440 | text.setAttribute('class', 'leaf'); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 441 | text.setAttribute("text-anchor", "start"); |
| 442 | text.setAttribute("y", height); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 443 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 444 | // Calculate the start position |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 445 | this._y = height - (this.anchorStart); |
| 446 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 447 | // Introduce some prepending whitespace (yeah - I know ...) |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 448 | var ws = text.appendChild(this._c("tspan")); |
| 449 | ws.appendChild(document.createTextNode('\u00A0')); |
| 450 | ws.style.textAnchor = "start"; |
| 451 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 452 | 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])); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 457 | tspan.setAttribute("text-anchor", "middle"); |
| 458 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 459 | this._tokenElements.push(tspan); |
| 460 | |
| 461 | // Add whitespace! |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 462 | tspan.setAttribute("dx", this.tokenSep); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 463 | }; |
| 464 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 465 | // Get some global position data that may change on resize |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 466 | var globalBoundingBox = g.getBoundingClientRect(); |
| 467 | this.offsetLeft = globalBoundingBox.left; |
| 468 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 469 | // The group of arcs |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 470 | var arcs = g.appendChild(this._c("g")); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 471 | this._arcsElement = arcs; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 472 | arcs.classList.add("arcs"); |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 473 | |
| 474 | // Sort arcs if not sorted yet |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 475 | if (this._sortedArcs === undefined) |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 476 | this._sortArcs(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 477 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 478 | // 1. Draw all anchors |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 479 | var i; |
| 480 | for (i in this._sortedAnchors) { |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 481 | this._drawAnchor(this._sortedAnchors[i]); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 482 | }; |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 483 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 484 | // 2. Draw all arcs |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 485 | for (i in this._sortedArcs) { |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 486 | this._drawArc(this._sortedArcs[i]); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 487 | }; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 488 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 489 | // Resize the svg with some reasonable margins |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 490 | var width = text.getBoundingClientRect().width; |
Akron | f3d7d8e | 2017-05-23 22:52:54 +0200 | [diff] [blame] | 491 | svg.setAttribute("width", width + 20); |
| 492 | svg.setAttribute("height", height + 20); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 493 | svg.setAttribute("class", "relTree"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 494 | } |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 495 | }; |
| 496 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 497 | // Sort relations regarding their span |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 498 | 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; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 514 | 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 |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame^] | 547 | // TODO: |
| 548 | // Use something like splice() instead |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 549 | stack.sort(function (a,b) { |
| 550 | b.overlaps - a.overlaps |
| 551 | }); |
| 552 | }; |
| 553 | |
| 554 | return stack; |
| 555 | }; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 556 | }); |