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