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