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 |
Akron | fee0b62 | 2017-09-13 14:46:43 +0200 | [diff] [blame] | 29 | this.overlapDiff = 40; // Difference on overlaps and minimum height for self-refernces |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 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 | fee0b62 | 2017-09-13 14:46:43 +0200 | [diff] [blame] | 208 | var startY = this._y; |
| 209 | var endY = this._y; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 210 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 211 | if (arc.startAnchor !== undefined) { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 212 | startPos = this._tokenPoint(arc.startAnchor.element); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 213 | startY = arc.startAnchor.y; |
| 214 | } |
| 215 | else { |
| 216 | startPos = this._tokenPoint(this._tokenElements[arc.first]); |
| 217 | }; |
| 218 | |
| 219 | if (arc.endAnchor !== undefined) { |
| 220 | endPos = this._tokenPoint(arc.endAnchor.element) |
| 221 | endY = arc.endAnchor.y; |
| 222 | } |
| 223 | else { |
| 224 | endPos = this._tokenPoint(this._tokenElements[arc.last]); |
| 225 | }; |
| 226 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 227 | startPos -= this.offsetLeft; |
| 228 | endPos -= this.offsetLeft; |
| 229 | |
Akron | fee0b62 | 2017-09-13 14:46:43 +0200 | [diff] [blame] | 230 | // Special treatment for self-references |
| 231 | var overlaps = arc.overlaps; |
| 232 | if (startPos == endPos) { |
| 233 | startPos -= this.overlapDiff / 3; |
Akron | c9f2302 | 2017-09-13 15:43:52 +0200 | [diff] [blame^] | 234 | endPos += this.overlapDiff / 3; |
Akron | fee0b62 | 2017-09-13 14:46:43 +0200 | [diff] [blame] | 235 | overlaps += .5; |
| 236 | }; |
| 237 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 238 | var g = this._c("g"); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 239 | g.setAttribute("class", "arc"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 240 | var p = g.appendChild(this._c("path")); |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 241 | p.setAttribute('class', 'edge'); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 242 | |
| 243 | // Attach the new arc before drawing, so computed values are available |
| 244 | this._arcsElement.appendChild(g); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 245 | |
| 246 | // Create arc |
| 247 | var middle = Math.abs(endPos - startPos) / 2; |
| 248 | |
Akron | fee0b62 | 2017-09-13 14:46:43 +0200 | [diff] [blame] | 249 | // TODO: |
| 250 | // take the number of tokens into account! |
| 251 | var cHeight = this.arcDiff + (overlaps * this.overlapDiff) + (middle / 2); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 252 | |
| 253 | // Respect the maximum height |
| 254 | cHeight = cHeight < this.maxArc ? cHeight : this.maxArc; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 255 | |
| 256 | var x = Math.min(startPos, endPos); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 257 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 258 | //var controlY = (startY + endY - cHeight); |
| 259 | var controlY = (endY - cHeight); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 260 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 261 | var arcE = "M "+ startPos + "," + startY + |
| 262 | " C " + startPos + "," + controlY + |
| 263 | " " + endPos + "," + controlY + |
| 264 | " " + endPos + "," + endY; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 265 | |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 266 | p.setAttribute("d", arcE); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 267 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 268 | if (arc.direction !== undefined) { |
| 269 | p.setAttribute("marker-end", "url(#arr)"); |
| 270 | if (arc.direction === 'bi') { |
| 271 | p.setAttribute("marker-start", "url(#arr)"); |
| 272 | }; |
| 273 | }; |
| 274 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 275 | if (arc.label === undefined) |
| 276 | return g; |
| 277 | |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 278 | /* |
| 279 | * Calculate the top point of the arc for labeling using |
| 280 | * de Casteljau's algorithm, see e.g. |
| 281 | * http://blog.sklambert.com/finding-the-control-points-of-a-bezier-curve/ |
| 282 | * of course simplified to symmetric arcs ... |
| 283 | */ |
| 284 | // Interpolate one side of the control polygon |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 285 | var middleY = (((startY + controlY) / 2) + controlY) / 2; |
| 286 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 287 | // Create a boxed label |
Akron | c9f2302 | 2017-09-13 15:43:52 +0200 | [diff] [blame^] | 288 | g = this._c("g"); |
| 289 | g.setAttribute("class", "label"); |
| 290 | this._labelsElement.appendChild(g); |
| 291 | |
| 292 | var that = this; |
| 293 | g.addEventListener('mouseenter', function () { |
| 294 | that._labelsElement.appendChild(this); |
| 295 | }); |
| 296 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 297 | var labelE = g.appendChild(this._c("text")); |
| 298 | labelE.setAttribute("x", x + middle); |
| 299 | labelE.setAttribute("y", middleY + 3); |
| 300 | labelE.setAttribute("text-anchor", "middle"); |
| 301 | var textNode = document.createTextNode(arc.label); |
| 302 | labelE.appendChild(textNode); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 303 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 304 | var labelBox = labelE.getBBox(); |
| 305 | var textWidth = labelBox.width; // labelE.getComputedTextLength(); |
| 306 | var textHeight = labelBox.height; // labelE.getComputedTextLength(); |
Akron | 1dc8790 | 2017-05-29 16:04:56 +0200 | [diff] [blame] | 307 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 308 | // Add box with padding to left and right |
| 309 | var labelR = g.insertBefore(this._c("rect"), labelE); |
| 310 | var boxWidth = textWidth + 2 * this.xPadding; |
| 311 | labelR.setAttribute("x", x + middle - (boxWidth / 2)); |
| 312 | labelR.setAttribute("ry", 5); |
| 313 | labelR.setAttribute("y", labelBox.y - this.yPadding); |
| 314 | labelR.setAttribute("width", boxWidth); |
| 315 | labelR.setAttribute("height", textHeight + 2 * this.yPadding); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 316 | }, |
| 317 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 318 | // Get the svg element |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 319 | element : function () { |
| 320 | if (this._element !== undefined) |
| 321 | return this._element; |
| 322 | |
| 323 | // Create svg |
| 324 | var svg = this._c("svg"); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 325 | |
| 326 | window.addEventListener("resize", function () { |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 327 | // TODO: |
| 328 | // Only if text-size changed! |
| 329 | // TODO: |
| 330 | // This is currently untested |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 331 | this.show(); |
| 332 | }.bind(this)); |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 333 | |
| 334 | // Define marker arrows |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 335 | var defs = svg.appendChild(this._c("defs")); |
| 336 | var marker = defs.appendChild(this._c("marker")); |
| 337 | marker.setAttribute("refX", 9); |
| 338 | marker.setAttribute("id", "arr"); |
| 339 | marker.setAttribute("orient", "auto-start-reverse"); |
| 340 | marker.setAttribute("markerUnits","userSpaceOnUse"); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 341 | var arrow = this._c("path"); |
| 342 | arrow.setAttribute("transform", "scale(0.8)"); |
| 343 | arrow.setAttribute("d", "M 0,-5 0,5 10,0 Z"); |
| 344 | marker.appendChild(arrow); |
| 345 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 346 | this._element = svg; |
| 347 | return this._element; |
| 348 | }, |
| 349 | |
| 350 | // Add a relation with a start, an end, |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 351 | // a direction value and an optional label text |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 352 | addRel : function (rel) { |
| 353 | this._arcs.push(rel); |
| 354 | return this; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 355 | }, |
| 356 | |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 357 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 358 | // Add a token to the list (this will mostly be a word) |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 359 | addToken : function(token) { |
| 360 | this._tokens.push(token); |
| 361 | return this; |
| 362 | }, |
| 363 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 364 | /* |
| 365 | * All arcs need to be sorted before shown, |
| 366 | * to avoid nesting. |
| 367 | */ |
| 368 | _sortArcs : function () { |
| 369 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 370 | // TODO: |
| 371 | // Keep in mind that the arcs may have long anchors! |
| 372 | // 1. Iterate over all arcs |
| 373 | // 2. Sort all multi |
| 374 | var anchors = []; |
| 375 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 376 | // 1. Sort by length |
| 377 | // 2. Tag all spans with the number of overlaps before |
| 378 | // a) Iterate over all spans |
| 379 | // b) check the latest preceeding overlapping span (lpos) |
| 380 | // -> not found: tag with 0 |
| 381 | // -> found: Add +1 to the level of the (lpos) |
| 382 | // c) If the new tag is smaller than the previous element, |
| 383 | // reorder |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 384 | |
| 385 | // Normalize start and end |
| 386 | var sortedArcs = this._arcs.map(function (v) { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 387 | |
| 388 | // Check for long anchors |
| 389 | if (v.start instanceof Array) { |
| 390 | var middle = Math.ceil(Math.abs(v.start[1] - v.start[0]) / 2) + v.start[0]; |
| 391 | |
| 392 | v.startAnchor = { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 393 | "first": v.start[0], |
| 394 | "last" : v.start[1], |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 395 | "length" : v.start[1] - v.start[0] |
| 396 | }; |
| 397 | |
| 398 | // Add to anchors list |
| 399 | anchors.push(v.startAnchor); |
| 400 | v.start = middle; |
| 401 | }; |
| 402 | |
| 403 | if (v.end instanceof Array) { |
| 404 | var middle = Math.abs(v.end[0] - v.end[1]) + v.end[0]; |
| 405 | v.endAnchor = { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 406 | "first": v.end[0], |
| 407 | "last" : v.end[1], |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 408 | "length" : v.end[1] - v.end[0] |
| 409 | }; |
| 410 | |
| 411 | // Add to anchors list |
| 412 | anchors.push(v.endAnchor); |
| 413 | v.end = middle; |
| 414 | }; |
| 415 | |
| 416 | // calculate the arch length |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 417 | if (v.start < v.end) { |
| 418 | v.first = v.start; |
| 419 | v.last = v.end; |
| 420 | v.length = v.end - v.start; |
| 421 | } |
| 422 | else { |
| 423 | v.first = v.end; |
| 424 | v.last = v.start; |
| 425 | v.length = v.start - v.end; |
| 426 | }; |
| 427 | return v; |
| 428 | }); |
| 429 | |
| 430 | // Sort based on length |
| 431 | sortedArcs.sort(function (a, b) { |
| 432 | if (a.length < b.length) |
| 433 | return -1; |
| 434 | else |
| 435 | return 1; |
| 436 | }); |
| 437 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 438 | this._sortedArcs = lengthSort(sortedArcs, false); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 439 | this._sortedAnchors = lengthSort(anchors, true); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 440 | }, |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 441 | |
| 442 | |
| 443 | // Show the element |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 444 | show : function () { |
| 445 | var svg = this._element; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 446 | var height = this.maxArc; |
| 447 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 448 | // Delete old group |
| 449 | if (svg.getElementsByTagName("g")[0] !== undefined) { |
| 450 | var group = svg.getElementsByTagName("g")[0]; |
| 451 | svg.removeChild(group); |
| 452 | this._tokenElements = []; |
| 453 | }; |
| 454 | |
| 455 | var g = svg.appendChild(this._c("g")); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 456 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 457 | // Draw token list |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 458 | var text = g.appendChild(this._c("text")); |
Akron | 3d20428 | 2017-09-07 18:24:18 +0200 | [diff] [blame] | 459 | text.setAttribute('class', 'leaf'); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 460 | text.setAttribute("text-anchor", "start"); |
| 461 | text.setAttribute("y", height); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 462 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 463 | // Calculate the start position |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 464 | this._y = height - (this.anchorStart); |
| 465 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 466 | // Introduce some prepending whitespace (yeah - I know ...) |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 467 | var ws = text.appendChild(this._c("tspan")); |
| 468 | ws.appendChild(document.createTextNode('\u00A0')); |
| 469 | ws.style.textAnchor = "start"; |
| 470 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 471 | var lastRight = 0; |
| 472 | for (var node_i in this._tokens) { |
| 473 | // Append svg |
| 474 | var tspan = text.appendChild(this._c("tspan")); |
| 475 | tspan.appendChild(document.createTextNode(this._tokens[node_i])); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 476 | tspan.setAttribute("text-anchor", "middle"); |
| 477 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 478 | this._tokenElements.push(tspan); |
| 479 | |
| 480 | // Add whitespace! |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 481 | tspan.setAttribute("dx", this.tokenSep); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 482 | }; |
| 483 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 484 | // Get some global position data that may change on resize |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 485 | var globalBoundingBox = g.getBoundingClientRect(); |
| 486 | this.offsetLeft = globalBoundingBox.left; |
| 487 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 488 | // The group of arcs |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 489 | var arcs = g.appendChild(this._c("g")); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 490 | this._arcsElement = arcs; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 491 | arcs.classList.add("arcs"); |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 492 | |
Akron | c9f2302 | 2017-09-13 15:43:52 +0200 | [diff] [blame^] | 493 | var labels = g.appendChild(this._c("g")); |
| 494 | this._labelsElement = labels; |
| 495 | labels.classList.add("labels"); |
| 496 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 497 | // Sort arcs if not sorted yet |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 498 | if (this._sortedArcs === undefined) |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 499 | this._sortArcs(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 500 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 501 | // 1. Draw all anchors |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 502 | var i; |
| 503 | for (i in this._sortedAnchors) { |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 504 | this._drawAnchor(this._sortedAnchors[i]); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 505 | }; |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 506 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 507 | // 2. Draw all arcs |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 508 | for (i in this._sortedArcs) { |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 509 | this._drawArc(this._sortedArcs[i]); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 510 | }; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 511 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 512 | // Resize the svg with some reasonable margins |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 513 | var width = text.getBoundingClientRect().width; |
Akron | f3d7d8e | 2017-05-23 22:52:54 +0200 | [diff] [blame] | 514 | svg.setAttribute("width", width + 20); |
| 515 | svg.setAttribute("height", height + 20); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 516 | svg.setAttribute("class", "relTree"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 517 | } |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 518 | }; |
| 519 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 520 | // Sort relations regarding their span |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 521 | function lengthSort (list, inclusive) { |
| 522 | |
| 523 | /* |
| 524 | * The "inclusive" flag allows to |
| 525 | * modify the behaviour for inclusivity check, |
| 526 | * e.g. if identical start or endpoints mean overlap or not. |
| 527 | */ |
| 528 | |
| 529 | var stack = []; |
| 530 | |
| 531 | // Iterate over all definitions |
| 532 | for (var i = 0; i < list.length; i++) { |
| 533 | var current = list[i]; |
| 534 | |
| 535 | // Check the stack order |
| 536 | var overlaps = 0; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 537 | for (var j = (stack.length - 1); j >= 0; j--) { |
| 538 | var check = stack[j]; |
| 539 | |
| 540 | // (a..(b..b)..a) |
| 541 | if (current.first <= check.first && current.last >= check.last) { |
| 542 | overlaps = check.overlaps + 1; |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | // (a..(b..a)..b) |
| 547 | else if (current.first <= check.first && current.last >= check.first) { |
| 548 | |
| 549 | if (inclusive || (current.first != check.first && current.last != check.first)) { |
| 550 | overlaps = check.overlaps + (current.length == check.length ? 0 : 1); |
| 551 | }; |
| 552 | } |
| 553 | |
| 554 | // (b..(a..b)..a) |
| 555 | else if (current.first <= check.last && current.last >= check.last) { |
| 556 | |
| 557 | if (inclusive || (current.first != check.last && current.last != check.last)) { |
| 558 | overlaps = check.overlaps + (current.length == check.length ? 0 : 1); |
| 559 | }; |
| 560 | }; |
| 561 | }; |
| 562 | |
| 563 | // Set overlaps |
| 564 | current.overlaps = overlaps; |
| 565 | |
| 566 | stack.push(current); |
| 567 | |
| 568 | // Although it is already sorted, |
| 569 | // the new item has to be put at the correct place |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 570 | // TODO: |
| 571 | // Use something like splice() instead |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 572 | stack.sort(function (a,b) { |
| 573 | b.overlaps - a.overlaps |
| 574 | }); |
| 575 | }; |
| 576 | |
| 577 | return stack; |
| 578 | }; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 579 | }); |