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 |
Akron | 71c87de | 2017-09-15 14:24:21 +0200 | [diff] [blame] | 22 | this._tokens = []; |
| 23 | this._arcs = []; |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 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 | |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 71 | // Check if the source is a span anchor |
Akron | 0d04a18 | 2017-09-15 14:34:50 +0200 | [diff] [blame] | 72 | /* |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 73 | var start = edge.srcStart; |
| 74 | if (start !== edge.srcEnd) { |
| 75 | start = [start, edge.srcEnd]; |
| 76 | }; |
Akron | 0d04a18 | 2017-09-15 14:34:50 +0200 | [diff] [blame] | 77 | */ |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 78 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 79 | // Add relation |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 80 | var relation = { |
Akron | 0d04a18 | 2017-09-15 14:34:50 +0200 | [diff] [blame] | 81 | start : [edge.srcStart, edge.srcEnd], |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 82 | end : target, |
| 83 | direction : 'uni', |
| 84 | label : edge.label |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 85 | }; |
| 86 | // console.log(relation); |
| 87 | this.addRel(relation); |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 88 | }; |
| 89 | }; |
| 90 | |
| 91 | // Reset parsing memory |
| 92 | this.temp = {}; |
| 93 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 94 | return this; |
| 95 | }, |
| 96 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 97 | // Parse a node of the tree snippet |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 98 | _parse : function (parent, children, mark) { |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 99 | |
| 100 | // Iterate over all child nodes |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 101 | for (var i in children) { |
| 102 | var c = children[i]; |
| 103 | |
| 104 | // Element node |
| 105 | if (c.nodeType == 1) { |
| 106 | |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 107 | var xmlid, target; |
| 108 | |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 109 | // Node is an identifier |
| 110 | if (c.hasAttribute('xml:id')) { |
| 111 | |
| 112 | // Remember that pos has this identifier |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 113 | xmlid = c.getAttribute('xml:id'); |
| 114 | this.temp['target'][xmlid] = [this.temp['pos'], this.temp['pos']]; |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Node is a relation |
| 118 | else if (c.hasAttribute('xlink:href')) { |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 119 | var label; |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 120 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 121 | // Get target id |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 122 | target = c.getAttribute('xlink:href').replace(/^#/, ""); |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 123 | |
| 124 | if (c.hasAttribute('xlink:title')) { |
| 125 | label = this._clean(c.getAttribute('xlink:title')); |
| 126 | }; |
| 127 | |
| 128 | // Remember the defined edge |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 129 | var edge = { |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 130 | label : label, |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 131 | srcStart : this.temp['pos'], |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 132 | targetID : target |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 133 | }; |
| 134 | this.temp['edges'].push(edge); |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 137 | // Go on with child nodes |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 138 | if (c.hasChildNodes()) { |
| 139 | this._parse(0, c.childNodes, mark); |
| 140 | }; |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 141 | |
| 142 | if (xmlid !== undefined) { |
| 143 | this.temp['target'][xmlid][1] = this.temp['pos'] -1; |
| 144 | |
| 145 | /* |
| 146 | console.log('Target ' + xmlid + ' spans from ' + |
| 147 | this.temp['target'][xmlid][0] + |
| 148 | ' to ' + |
| 149 | this.temp['target'][xmlid][1] |
| 150 | ); |
| 151 | */ |
| 152 | xmlid = undefined; |
| 153 | } |
| 154 | else if (target !== undefined) { |
| 155 | edge["srcEnd"] = this.temp['pos'] -1; |
| 156 | |
| 157 | /* |
| 158 | console.log('Source spans from ' + |
| 159 | edge["srcStart"] + |
| 160 | ' to ' + |
| 161 | edge["srcEnd"] |
| 162 | ); |
| 163 | */ |
| 164 | target = undefined; |
| 165 | }; |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Text node |
| 169 | else if (c.nodeType == 3) { |
| 170 | |
| 171 | // Check, if there is a non-whitespace token |
| 172 | if (c.nodeValue !== undefined) { |
| 173 | var str = c.nodeValue.trim(); |
| 174 | if (str !== undefined && str.length > 0) { |
| 175 | |
| 176 | // Add token to token list |
| 177 | this.addToken(str); |
| 178 | |
| 179 | // Move token position |
| 180 | this.temp['pos']++; |
| 181 | }; |
| 182 | }; |
| 183 | } |
| 184 | }; |
| 185 | }, |
| 186 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 187 | |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 188 | // Remove foundry and layer for labels |
| 189 | _clean : function (title) { |
| 190 | return title.replace(_TermRE, "$3"); |
| 191 | }, |
| 192 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 193 | |
| 194 | // Return the number of leaf nodes |
| 195 | // (not necessarily part of a relation). |
| 196 | // Consecutive nodes that are not part of any |
| 197 | // relation are summarized in one node. |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 198 | size : function () { |
| 199 | return this._tokens.length; |
| 200 | }, |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 201 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 202 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 203 | // This is a shorthand for SVG element creation |
| 204 | _c : function (tag) { |
| 205 | return document.createElementNS(svgNS, tag); |
| 206 | }, |
| 207 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 208 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 209 | // Returns the center point of the requesting token |
| 210 | _tokenPoint : function (node) { |
| 211 | var box = node.getBoundingClientRect(); |
| 212 | return box.x + (box.width / 2); |
| 213 | }, |
| 214 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 215 | |
| 216 | // Draws an anchor |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 217 | _drawAnchor : function (anchor) { |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 218 | |
| 219 | // Calculate the span of the first and last token, the anchor spans |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 220 | var firstBox = this._tokenElements[anchor.first].getBoundingClientRect(); |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 221 | var lastBox = this._tokenElements[anchor.last].getBoundingClientRect(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 222 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 223 | var startPos = firstBox.left - this.offsetLeft; |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 224 | var endPos = lastBox.right - this.offsetLeft; |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 225 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 226 | var y = this._y + (anchor.overlaps * this.anchorDiff) - this.anchorStart; |
| 227 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 228 | var l = this._c('path'); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 229 | this._arcsElement.appendChild(l); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 230 | l.setAttribute("d", "M " + startPos + "," + y + " L " + endPos + "," + y); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 231 | l.setAttribute("class", "anchor"); |
| 232 | anchor.element = l; |
| 233 | anchor.y = y; |
| 234 | return l; |
| 235 | }, |
| 236 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 237 | |
| 238 | // Create an arc with an optional label |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 239 | // Potentially needs a height parameter for stacks |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 240 | _drawArc : function (arc) { |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 241 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 242 | var startPos, endPos; |
Akron | fee0b62 | 2017-09-13 14:46:43 +0200 | [diff] [blame] | 243 | var startY = this._y; |
| 244 | var endY = this._y; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 245 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 246 | if (arc.startAnchor !== undefined) { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 247 | startPos = this._tokenPoint(arc.startAnchor.element); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 248 | startY = arc.startAnchor.y; |
| 249 | } |
| 250 | else { |
| 251 | startPos = this._tokenPoint(this._tokenElements[arc.first]); |
| 252 | }; |
| 253 | |
| 254 | if (arc.endAnchor !== undefined) { |
| 255 | endPos = this._tokenPoint(arc.endAnchor.element) |
| 256 | endY = arc.endAnchor.y; |
| 257 | } |
| 258 | else { |
| 259 | endPos = this._tokenPoint(this._tokenElements[arc.last]); |
| 260 | }; |
| 261 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 262 | startPos -= this.offsetLeft; |
| 263 | endPos -= this.offsetLeft; |
| 264 | |
Akron | fee0b62 | 2017-09-13 14:46:43 +0200 | [diff] [blame] | 265 | // Special treatment for self-references |
| 266 | var overlaps = arc.overlaps; |
| 267 | if (startPos == endPos) { |
| 268 | startPos -= this.overlapDiff / 3; |
Akron | c9f2302 | 2017-09-13 15:43:52 +0200 | [diff] [blame] | 269 | endPos += this.overlapDiff / 3; |
Akron | fee0b62 | 2017-09-13 14:46:43 +0200 | [diff] [blame] | 270 | overlaps += .5; |
| 271 | }; |
| 272 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 273 | var g = this._c("g"); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 274 | g.setAttribute("class", "arc"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 275 | var p = g.appendChild(this._c("path")); |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 276 | p.setAttribute('class', 'edge'); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 277 | |
| 278 | // Attach the new arc before drawing, so computed values are available |
| 279 | this._arcsElement.appendChild(g); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 280 | |
| 281 | // Create arc |
| 282 | var middle = Math.abs(endPos - startPos) / 2; |
| 283 | |
Akron | fee0b62 | 2017-09-13 14:46:43 +0200 | [diff] [blame] | 284 | // TODO: |
| 285 | // take the number of tokens into account! |
| 286 | var cHeight = this.arcDiff + (overlaps * this.overlapDiff) + (middle / 2); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 287 | |
| 288 | // Respect the maximum height |
| 289 | cHeight = cHeight < this.maxArc ? cHeight : this.maxArc; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 290 | |
| 291 | var x = Math.min(startPos, endPos); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 292 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 293 | //var controlY = (startY + endY - cHeight); |
| 294 | var controlY = (endY - cHeight); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 295 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 296 | var arcE = "M "+ startPos + "," + startY + |
| 297 | " C " + startPos + "," + controlY + |
| 298 | " " + endPos + "," + controlY + |
| 299 | " " + endPos + "," + endY; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 300 | |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 301 | p.setAttribute("d", arcE); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 302 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 303 | if (arc.direction !== undefined) { |
| 304 | p.setAttribute("marker-end", "url(#arr)"); |
| 305 | if (arc.direction === 'bi') { |
| 306 | p.setAttribute("marker-start", "url(#arr)"); |
| 307 | }; |
| 308 | }; |
| 309 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 310 | if (arc.label === undefined) |
| 311 | return g; |
| 312 | |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 313 | /* |
| 314 | * Calculate the top point of the arc for labeling using |
| 315 | * de Casteljau's algorithm, see e.g. |
| 316 | * http://blog.sklambert.com/finding-the-control-points-of-a-bezier-curve/ |
| 317 | * of course simplified to symmetric arcs ... |
| 318 | */ |
| 319 | // Interpolate one side of the control polygon |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 320 | var middleY = (((startY + controlY) / 2) + controlY) / 2; |
| 321 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 322 | // Create a boxed label |
Akron | c9f2302 | 2017-09-13 15:43:52 +0200 | [diff] [blame] | 323 | g = this._c("g"); |
| 324 | g.setAttribute("class", "label"); |
| 325 | this._labelsElement.appendChild(g); |
| 326 | |
| 327 | var that = this; |
| 328 | g.addEventListener('mouseenter', function () { |
| 329 | that._labelsElement.appendChild(this); |
| 330 | }); |
| 331 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 332 | var labelE = g.appendChild(this._c("text")); |
| 333 | labelE.setAttribute("x", x + middle); |
| 334 | labelE.setAttribute("y", middleY + 3); |
| 335 | labelE.setAttribute("text-anchor", "middle"); |
| 336 | var textNode = document.createTextNode(arc.label); |
| 337 | labelE.appendChild(textNode); |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 338 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 339 | var labelBox = labelE.getBBox(); |
| 340 | var textWidth = labelBox.width; // labelE.getComputedTextLength(); |
| 341 | var textHeight = labelBox.height; // labelE.getComputedTextLength(); |
Akron | 1dc8790 | 2017-05-29 16:04:56 +0200 | [diff] [blame] | 342 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 343 | // Add box with padding to left and right |
| 344 | var labelR = g.insertBefore(this._c("rect"), labelE); |
| 345 | var boxWidth = textWidth + 2 * this.xPadding; |
| 346 | labelR.setAttribute("x", x + middle - (boxWidth / 2)); |
| 347 | labelR.setAttribute("ry", 5); |
| 348 | labelR.setAttribute("y", labelBox.y - this.yPadding); |
| 349 | labelR.setAttribute("width", boxWidth); |
| 350 | labelR.setAttribute("height", textHeight + 2 * this.yPadding); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 351 | }, |
| 352 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 353 | // Get the svg element |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 354 | element : function () { |
| 355 | if (this._element !== undefined) |
| 356 | return this._element; |
| 357 | |
| 358 | // Create svg |
| 359 | var svg = this._c("svg"); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 360 | |
| 361 | window.addEventListener("resize", function () { |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 362 | // TODO: |
| 363 | // Only if text-size changed! |
| 364 | // TODO: |
| 365 | // This is currently untested |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 366 | this.show(); |
| 367 | }.bind(this)); |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 368 | |
| 369 | // Define marker arrows |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 370 | var defs = svg.appendChild(this._c("defs")); |
| 371 | var marker = defs.appendChild(this._c("marker")); |
| 372 | marker.setAttribute("refX", 9); |
| 373 | marker.setAttribute("id", "arr"); |
| 374 | marker.setAttribute("orient", "auto-start-reverse"); |
| 375 | marker.setAttribute("markerUnits","userSpaceOnUse"); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 376 | var arrow = this._c("path"); |
| 377 | arrow.setAttribute("transform", "scale(0.8)"); |
| 378 | arrow.setAttribute("d", "M 0,-5 0,5 10,0 Z"); |
| 379 | marker.appendChild(arrow); |
| 380 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 381 | this._element = svg; |
| 382 | return this._element; |
| 383 | }, |
| 384 | |
| 385 | // Add a relation with a start, an end, |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 386 | // a direction value and an optional label text |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 387 | addRel : function (rel) { |
| 388 | this._arcs.push(rel); |
| 389 | return this; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 390 | }, |
| 391 | |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 392 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 393 | // Add a token to the list (this will mostly be a word) |
Akron | c5b5f74 | 2017-05-23 16:04:35 +0200 | [diff] [blame] | 394 | addToken : function(token) { |
| 395 | this._tokens.push(token); |
| 396 | return this; |
| 397 | }, |
| 398 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 399 | /* |
| 400 | * All arcs need to be sorted before shown, |
| 401 | * to avoid nesting. |
| 402 | */ |
| 403 | _sortArcs : function () { |
| 404 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 405 | // TODO: |
| 406 | // Keep in mind that the arcs may have long anchors! |
| 407 | // 1. Iterate over all arcs |
| 408 | // 2. Sort all multi |
Akron | 71c87de | 2017-09-15 14:24:21 +0200 | [diff] [blame] | 409 | var anchors = {}; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 410 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 411 | // 1. Sort by length |
| 412 | // 2. Tag all spans with the number of overlaps before |
| 413 | // a) Iterate over all spans |
| 414 | // b) check the latest preceeding overlapping span (lpos) |
| 415 | // -> not found: tag with 0 |
| 416 | // -> found: Add +1 to the level of the (lpos) |
| 417 | // c) If the new tag is smaller than the previous element, |
| 418 | // reorder |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 419 | |
| 420 | // Normalize start and end |
| 421 | var sortedArcs = this._arcs.map(function (v) { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 422 | |
| 423 | // Check for long anchors |
| 424 | if (v.start instanceof Array) { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 425 | |
Akron | 0d04a18 | 2017-09-15 14:34:50 +0200 | [diff] [blame] | 426 | if (v.start[0] == v.start[1]) { |
| 427 | v.start = v.start[0]; |
| 428 | } |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 429 | |
Akron | 0d04a18 | 2017-09-15 14:34:50 +0200 | [diff] [blame] | 430 | else { |
| 431 | |
| 432 | var middle = Math.ceil(Math.abs(v.start[1] - v.start[0]) / 2) + v.start[0]; |
| 433 | |
| 434 | // Calculate signature to avoid multiple anchors |
| 435 | var anchorSig = "#" + v.start[0] + "_" + v.start[1]; |
| 436 | if (v.start[0] > v.start[1]) { |
| 437 | anchorSig = "#" + v.start[1] + "_" + v.start[0]; |
Akron | 71c87de | 2017-09-15 14:24:21 +0200 | [diff] [blame] | 438 | }; |
Akron | 0d04a18 | 2017-09-15 14:34:50 +0200 | [diff] [blame] | 439 | |
| 440 | // Check if the anchor already exist |
| 441 | var anchor = anchors[anchorSig]; |
| 442 | if (anchor === undefined) { |
| 443 | anchor = { |
| 444 | "first": v.start[0], |
| 445 | "last" : v.start[1], |
| 446 | "length" : v.start[1] - v.start[0] |
| 447 | }; |
| 448 | anchors[anchorSig] = anchor; |
| 449 | // anchors.push(v.startAnchor); |
| 450 | }; |
| 451 | |
| 452 | v.startAnchor = anchor; |
| 453 | |
| 454 | // Add to anchors list |
| 455 | v.start = middle; |
Akron | 71c87de | 2017-09-15 14:24:21 +0200 | [diff] [blame] | 456 | }; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 457 | }; |
| 458 | |
| 459 | if (v.end instanceof Array) { |
Akron | 71c87de | 2017-09-15 14:24:21 +0200 | [diff] [blame] | 460 | |
Akron | 0d04a18 | 2017-09-15 14:34:50 +0200 | [diff] [blame] | 461 | if (v.end[0] == v.end[1]) { |
| 462 | v.end = v.end[0]; |
| 463 | } |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 464 | |
Akron | 0d04a18 | 2017-09-15 14:34:50 +0200 | [diff] [blame] | 465 | else { |
| 466 | |
| 467 | var middle = Math.abs(v.end[0] - v.end[1]) + v.end[0]; |
| 468 | |
| 469 | // Calculate signature to avoid multiple anchors |
| 470 | var anchorSig = "#" + v.end[0] + "_" + v.end[1]; |
| 471 | if (v.end[0] > v.end[1]) { |
| 472 | anchorSig = "#" + v.end[1] + "_" + v.end[0]; |
Akron | 71c87de | 2017-09-15 14:24:21 +0200 | [diff] [blame] | 473 | }; |
Akron | 0d04a18 | 2017-09-15 14:34:50 +0200 | [diff] [blame] | 474 | |
| 475 | // Check if the anchor already exist |
| 476 | var anchor = anchors[anchorSig]; |
| 477 | if (anchor === undefined) { |
| 478 | anchor = { |
| 479 | "first": v.end[0], |
| 480 | "last" : v.end[1], |
| 481 | "length" : v.end[1] - v.end[0] |
| 482 | }; |
| 483 | anchors[anchorSig] = anchor; |
| 484 | // anchors.push(v.startAnchor); |
| 485 | }; |
| 486 | |
| 487 | v.endAnchor = anchor; |
| 488 | |
| 489 | // Add to anchors list |
| 490 | // anchors.push(v.endAnchor); |
| 491 | v.end = middle; |
Akron | 71c87de | 2017-09-15 14:24:21 +0200 | [diff] [blame] | 492 | }; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 493 | }; |
| 494 | |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 495 | v.first = v.start; |
| 496 | v.last = v.end; |
| 497 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 498 | // calculate the arch length |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 499 | if (v.start < v.end) { |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 500 | v.length = v.end - v.start; |
| 501 | } |
| 502 | else { |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 503 | // v.first = v.end; |
| 504 | // v.last = v.start; |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 505 | v.length = v.start - v.end; |
| 506 | }; |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 507 | |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 508 | return v; |
| 509 | }); |
| 510 | |
| 511 | // Sort based on length |
| 512 | sortedArcs.sort(function (a, b) { |
| 513 | if (a.length < b.length) |
| 514 | return -1; |
| 515 | else |
| 516 | return 1; |
| 517 | }); |
| 518 | |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame] | 519 | // Add sorted arcs and anchors |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 520 | this._sortedArcs = lengthSort(sortedArcs, false); |
Akron | 71c87de | 2017-09-15 14:24:21 +0200 | [diff] [blame] | 521 | |
| 522 | // Translate map to array (there is probably a better JS method) |
| 523 | var sortedAnchors = []; |
| 524 | for (var i in anchors) { |
| 525 | sortedAnchors.push(anchors[i]); |
| 526 | }; |
| 527 | this._sortedAnchors = lengthSort(sortedAnchors, true); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 528 | }, |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 529 | |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame] | 530 | /** |
| 531 | * Center the viewport of the canvas |
| 532 | * TODO: |
| 533 | * This is identical to tree |
| 534 | */ |
| 535 | center : function () { |
| 536 | if (this._element === undefined) |
| 537 | return; |
| 538 | |
| 539 | var treeDiv = this._element.parentNode; |
| 540 | |
| 541 | var cWidth = parseFloat(window.getComputedStyle(this._element).width); |
| 542 | var treeWidth = parseFloat(window.getComputedStyle(treeDiv).width); |
| 543 | // Reposition: |
| 544 | if (cWidth > treeWidth) { |
| 545 | var scrollValue = (cWidth - treeWidth) / 2; |
| 546 | treeDiv.scrollLeft = scrollValue; |
| 547 | }; |
| 548 | }, |
| 549 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 550 | |
| 551 | // Show the element |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 552 | show : function () { |
| 553 | var svg = this._element; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 554 | var height = this.maxArc; |
| 555 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 556 | // Delete old group |
| 557 | if (svg.getElementsByTagName("g")[0] !== undefined) { |
| 558 | var group = svg.getElementsByTagName("g")[0]; |
| 559 | svg.removeChild(group); |
| 560 | this._tokenElements = []; |
| 561 | }; |
| 562 | |
| 563 | var g = svg.appendChild(this._c("g")); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 564 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 565 | // Draw token list |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 566 | var text = g.appendChild(this._c("text")); |
Akron | 3d20428 | 2017-09-07 18:24:18 +0200 | [diff] [blame] | 567 | text.setAttribute('class', 'leaf'); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 568 | text.setAttribute("text-anchor", "start"); |
| 569 | text.setAttribute("y", height); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 570 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 571 | // Calculate the start position |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 572 | this._y = height - (this.anchorStart); |
| 573 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 574 | // Introduce some prepending whitespace (yeah - I know ...) |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 575 | var ws = text.appendChild(this._c("tspan")); |
| 576 | ws.appendChild(document.createTextNode('\u00A0')); |
| 577 | ws.style.textAnchor = "start"; |
| 578 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 579 | var lastRight = 0; |
| 580 | for (var node_i in this._tokens) { |
| 581 | // Append svg |
| 582 | var tspan = text.appendChild(this._c("tspan")); |
| 583 | tspan.appendChild(document.createTextNode(this._tokens[node_i])); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 584 | tspan.setAttribute("text-anchor", "middle"); |
| 585 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 586 | this._tokenElements.push(tspan); |
| 587 | |
| 588 | // Add whitespace! |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 589 | tspan.setAttribute("dx", this.tokenSep); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 590 | }; |
| 591 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 592 | // Get some global position data that may change on resize |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 593 | var globalBoundingBox = g.getBoundingClientRect(); |
| 594 | this.offsetLeft = globalBoundingBox.left; |
| 595 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 596 | // The group of arcs |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 597 | var arcs = g.appendChild(this._c("g")); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 598 | this._arcsElement = arcs; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 599 | arcs.classList.add("arcs"); |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 600 | |
Akron | c9f2302 | 2017-09-13 15:43:52 +0200 | [diff] [blame] | 601 | var labels = g.appendChild(this._c("g")); |
| 602 | this._labelsElement = labels; |
| 603 | labels.classList.add("labels"); |
| 604 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 605 | // Sort arcs if not sorted yet |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 606 | if (this._sortedArcs === undefined) |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 607 | this._sortArcs(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 608 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 609 | // 1. Draw all anchors |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 610 | var i; |
| 611 | for (i in this._sortedAnchors) { |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 612 | this._drawAnchor(this._sortedAnchors[i]); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 613 | }; |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 614 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 615 | // 2. Draw all arcs |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 616 | for (i in this._sortedArcs) { |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 617 | this._drawArc(this._sortedArcs[i]); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 618 | }; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 619 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 620 | // Resize the svg with some reasonable margins |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 621 | var width = text.getBoundingClientRect().width; |
Akron | f3d7d8e | 2017-05-23 22:52:54 +0200 | [diff] [blame] | 622 | svg.setAttribute("width", width + 20); |
| 623 | svg.setAttribute("height", height + 20); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 624 | svg.setAttribute("class", "relTree"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 625 | } |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 626 | }; |
| 627 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 628 | // Sort relations regarding their span |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 629 | function lengthSort (list, inclusive) { |
| 630 | |
| 631 | /* |
| 632 | * The "inclusive" flag allows to |
| 633 | * modify the behaviour for inclusivity check, |
| 634 | * e.g. if identical start or endpoints mean overlap or not. |
| 635 | */ |
| 636 | |
| 637 | var stack = []; |
| 638 | |
| 639 | // Iterate over all definitions |
| 640 | for (var i = 0; i < list.length; i++) { |
| 641 | var current = list[i]; |
| 642 | |
| 643 | // Check the stack order |
| 644 | var overlaps = 0; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 645 | for (var j = (stack.length - 1); j >= 0; j--) { |
| 646 | var check = stack[j]; |
| 647 | |
| 648 | // (a..(b..b)..a) |
| 649 | if (current.first <= check.first && current.last >= check.last) { |
| 650 | overlaps = check.overlaps + 1; |
| 651 | break; |
| 652 | } |
| 653 | |
| 654 | // (a..(b..a)..b) |
| 655 | else if (current.first <= check.first && current.last >= check.first) { |
| 656 | |
| 657 | if (inclusive || (current.first != check.first && current.last != check.first)) { |
| 658 | overlaps = check.overlaps + (current.length == check.length ? 0 : 1); |
| 659 | }; |
| 660 | } |
| 661 | |
| 662 | // (b..(a..b)..a) |
| 663 | else if (current.first <= check.last && current.last >= check.last) { |
| 664 | |
| 665 | if (inclusive || (current.first != check.last && current.last != check.last)) { |
| 666 | overlaps = check.overlaps + (current.length == check.length ? 0 : 1); |
| 667 | }; |
| 668 | }; |
| 669 | }; |
| 670 | |
| 671 | // Set overlaps |
| 672 | current.overlaps = overlaps; |
| 673 | |
| 674 | stack.push(current); |
| 675 | |
| 676 | // Although it is already sorted, |
| 677 | // the new item has to be put at the correct place |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 678 | // TODO: |
| 679 | // Use something like splice() instead |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 680 | stack.sort(function (a,b) { |
| 681 | b.overlaps - a.overlaps |
| 682 | }); |
| 683 | }; |
| 684 | |
| 685 | return stack; |
| 686 | }; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 687 | }); |