Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Parse a relational tree and visualize using arcs. |
| 3 | * |
| 4 | * @author Nils Diewald |
| 5 | */ |
| 6 | |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 7 | define([], function () { |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 8 | "use strict"; |
| 9 | |
| 10 | var svgNS = "http://www.w3.org/2000/svg"; |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 11 | var _TermRE = new RegExp("^(?:([^\/]+?)\/)?([^:]+?):(.+?)$"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 12 | |
| 13 | return { |
| 14 | create : function (snippet) { |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 15 | return Object.create(this)._init(snippet); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 16 | }, |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 17 | |
| 18 | // Initialize the state of the object |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 19 | _init : function (snippet) { |
Akron | 671fdb9 | 2017-09-12 18:09:46 +0200 | [diff] [blame] | 20 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 21 | // Predefine some values |
| 22 | this._tokens = []; |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame^] | 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 |
| 408 | var anchors = []; |
| 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 | |
| 426 | v.startAnchor = { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 427 | "first": v.start[0], |
| 428 | "last" : v.start[1], |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 429 | "length" : v.start[1] - v.start[0] |
| 430 | }; |
| 431 | |
| 432 | // Add to anchors list |
| 433 | anchors.push(v.startAnchor); |
| 434 | v.start = middle; |
| 435 | }; |
| 436 | |
| 437 | if (v.end instanceof Array) { |
| 438 | var middle = Math.abs(v.end[0] - v.end[1]) + v.end[0]; |
| 439 | v.endAnchor = { |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 440 | "first": v.end[0], |
| 441 | "last" : v.end[1], |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 442 | "length" : v.end[1] - v.end[0] |
| 443 | }; |
| 444 | |
| 445 | // Add to anchors list |
| 446 | anchors.push(v.endAnchor); |
| 447 | v.end = middle; |
| 448 | }; |
| 449 | |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame^] | 450 | v.first = v.start; |
| 451 | v.last = v.end; |
| 452 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 453 | // calculate the arch length |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 454 | if (v.start < v.end) { |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 455 | v.length = v.end - v.start; |
| 456 | } |
| 457 | else { |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame^] | 458 | // v.first = v.end; |
| 459 | // v.last = v.start; |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 460 | v.length = v.start - v.end; |
| 461 | }; |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame^] | 462 | |
| 463 | if (v.label === "OBJA") { |
| 464 | console.log(v); |
| 465 | }; |
| 466 | |
| 467 | // console.log(v); |
Akron | 63ae00b | 2017-05-16 22:03:36 +0200 | [diff] [blame] | 468 | return v; |
| 469 | }); |
| 470 | |
| 471 | // Sort based on length |
| 472 | sortedArcs.sort(function (a, b) { |
| 473 | if (a.length < b.length) |
| 474 | return -1; |
| 475 | else |
| 476 | return 1; |
| 477 | }); |
| 478 | |
Akron | 2c1d9ff | 2017-09-15 13:57:33 +0200 | [diff] [blame^] | 479 | // Add sorted arcs and anchors |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 480 | this._sortedArcs = lengthSort(sortedArcs, false); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 481 | this._sortedAnchors = lengthSort(anchors, true); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 482 | }, |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 483 | |
| 484 | |
| 485 | // Show the element |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 486 | show : function () { |
| 487 | var svg = this._element; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 488 | var height = this.maxArc; |
| 489 | |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 490 | // Delete old group |
| 491 | if (svg.getElementsByTagName("g")[0] !== undefined) { |
| 492 | var group = svg.getElementsByTagName("g")[0]; |
| 493 | svg.removeChild(group); |
| 494 | this._tokenElements = []; |
| 495 | }; |
| 496 | |
| 497 | var g = svg.appendChild(this._c("g")); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 498 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 499 | // Draw token list |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 500 | var text = g.appendChild(this._c("text")); |
Akron | 3d20428 | 2017-09-07 18:24:18 +0200 | [diff] [blame] | 501 | text.setAttribute('class', 'leaf'); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 502 | text.setAttribute("text-anchor", "start"); |
| 503 | text.setAttribute("y", height); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 504 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 505 | // Calculate the start position |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 506 | this._y = height - (this.anchorStart); |
| 507 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 508 | // Introduce some prepending whitespace (yeah - I know ...) |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 509 | var ws = text.appendChild(this._c("tspan")); |
| 510 | ws.appendChild(document.createTextNode('\u00A0')); |
| 511 | ws.style.textAnchor = "start"; |
| 512 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 513 | var lastRight = 0; |
| 514 | for (var node_i in this._tokens) { |
| 515 | // Append svg |
| 516 | var tspan = text.appendChild(this._c("tspan")); |
| 517 | tspan.appendChild(document.createTextNode(this._tokens[node_i])); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 518 | tspan.setAttribute("text-anchor", "middle"); |
| 519 | |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 520 | this._tokenElements.push(tspan); |
| 521 | |
| 522 | // Add whitespace! |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 523 | tspan.setAttribute("dx", this.tokenSep); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 524 | }; |
| 525 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 526 | // Get some global position data that may change on resize |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 527 | var globalBoundingBox = g.getBoundingClientRect(); |
| 528 | this.offsetLeft = globalBoundingBox.left; |
| 529 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 530 | // The group of arcs |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 531 | var arcs = g.appendChild(this._c("g")); |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 532 | this._arcsElement = arcs; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 533 | arcs.classList.add("arcs"); |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 534 | |
Akron | c9f2302 | 2017-09-13 15:43:52 +0200 | [diff] [blame] | 535 | var labels = g.appendChild(this._c("g")); |
| 536 | this._labelsElement = labels; |
| 537 | labels.classList.add("labels"); |
| 538 | |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 539 | // Sort arcs if not sorted yet |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 540 | if (this._sortedArcs === undefined) |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 541 | this._sortArcs(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 542 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 543 | // 1. Draw all anchors |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 544 | var i; |
| 545 | for (i in this._sortedAnchors) { |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 546 | this._drawAnchor(this._sortedAnchors[i]); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 547 | }; |
Akron | 1517513 | 2017-09-07 18:12:55 +0200 | [diff] [blame] | 548 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 549 | // 2. Draw all arcs |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 550 | for (i in this._sortedArcs) { |
Akron | 65d3108 | 2017-09-08 16:23:40 +0200 | [diff] [blame] | 551 | this._drawArc(this._sortedArcs[i]); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 552 | }; |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 553 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 554 | // Resize the svg with some reasonable margins |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 555 | var width = text.getBoundingClientRect().width; |
Akron | f3d7d8e | 2017-05-23 22:52:54 +0200 | [diff] [blame] | 556 | svg.setAttribute("width", width + 20); |
| 557 | svg.setAttribute("height", height + 20); |
Akron | 3a4a08e | 2017-05-23 22:34:18 +0200 | [diff] [blame] | 558 | svg.setAttribute("class", "relTree"); |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 559 | } |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 560 | }; |
| 561 | |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 562 | // Sort relations regarding their span |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 563 | function lengthSort (list, inclusive) { |
| 564 | |
| 565 | /* |
| 566 | * The "inclusive" flag allows to |
| 567 | * modify the behaviour for inclusivity check, |
| 568 | * e.g. if identical start or endpoints mean overlap or not. |
| 569 | */ |
| 570 | |
| 571 | var stack = []; |
| 572 | |
| 573 | // Iterate over all definitions |
| 574 | for (var i = 0; i < list.length; i++) { |
| 575 | var current = list[i]; |
| 576 | |
| 577 | // Check the stack order |
| 578 | var overlaps = 0; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 579 | for (var j = (stack.length - 1); j >= 0; j--) { |
| 580 | var check = stack[j]; |
| 581 | |
| 582 | // (a..(b..b)..a) |
| 583 | if (current.first <= check.first && current.last >= check.last) { |
| 584 | overlaps = check.overlaps + 1; |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | // (a..(b..a)..b) |
| 589 | else if (current.first <= check.first && current.last >= check.first) { |
| 590 | |
| 591 | if (inclusive || (current.first != check.first && current.last != check.first)) { |
| 592 | overlaps = check.overlaps + (current.length == check.length ? 0 : 1); |
| 593 | }; |
| 594 | } |
| 595 | |
| 596 | // (b..(a..b)..a) |
| 597 | else if (current.first <= check.last && current.last >= check.last) { |
| 598 | |
| 599 | if (inclusive || (current.first != check.last && current.last != check.last)) { |
| 600 | overlaps = check.overlaps + (current.length == check.length ? 0 : 1); |
| 601 | }; |
| 602 | }; |
| 603 | }; |
| 604 | |
| 605 | // Set overlaps |
| 606 | current.overlaps = overlaps; |
| 607 | |
| 608 | stack.push(current); |
| 609 | |
| 610 | // Although it is already sorted, |
| 611 | // the new item has to be put at the correct place |
Akron | 6f1302b | 2017-09-13 12:46:02 +0200 | [diff] [blame] | 612 | // TODO: |
| 613 | // Use something like splice() instead |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 614 | stack.sort(function (a,b) { |
| 615 | b.overlaps - a.overlaps |
| 616 | }); |
| 617 | }; |
| 618 | |
| 619 | return stack; |
| 620 | }; |
Akron | f5dc510 | 2017-05-16 20:32:57 +0200 | [diff] [blame] | 621 | }); |