Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 1 | /**
|
| 2 | * Parse a relational tree and visualize using arcs.
|
| 3 | *
|
| 4 | * @author Nils Diewald
|
| 5 | */
|
Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 6 | "use strict";
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 7 |
|
| 8 | define([], function () {
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 9 |
|
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 10 | const svgNS = "http://www.w3.org/2000/svg";
|
| 11 | const _TermRE = new RegExp("^(?:([^\/]+?)\/)?([^:]+?):(.+?)$");
|
| 12 | const d = document;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 13 |
|
| 14 | return {
|
| 15 | create : function (snippet) {
|
| 16 | return Object.create(this)._init(snippet);
|
| 17 | },
|
| 18 |
|
| 19 | // Initialize the state of the object
|
| 20 | _init : function (snippet) {
|
| 21 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 22 | const t = this;
|
| 23 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 24 | // Predefine some values
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 25 | t._tokens = [];
|
| 26 | t._arcs = [];
|
| 27 | t._tokenElements = [];
|
| 28 | t._y = 0;
|
| 29 | t._currentInFocus = undefined;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 30 |
|
| 31 | // Some configurations
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 32 | t.maxArc = 200; // maximum height of the bezier control point
|
| 33 | t.overlapDiff = 40; // Difference on overlaps and minimum height for self-refernces
|
| 34 | t.arcDiff = 15;
|
| 35 | t.anchorDiff = 8;
|
| 36 | t.anchorStart = 15;
|
| 37 | t.tokenSep = 30;
|
| 38 | t.xPadding = 10;
|
| 39 | t.yPadding = 5;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 40 |
|
| 41 | // No snippet to process
|
| 42 | if (snippet == undefined || snippet == null)
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 43 | return t;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 44 |
|
| 45 | // Parse the snippet
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 46 | const html = d.createElement("div");
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 47 | html.innerHTML = snippet;
|
| 48 |
|
| 49 | // Establish temporary parsing memory
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 50 | t.temp = {
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 51 | target : {}, // Remember the map id => pos
|
| 52 | edges : [], // Remember edge definitions
|
| 53 | pos : 0 // Keep track of the current token position
|
| 54 | };
|
| 55 |
|
| 56 | // Start parsing from root
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 57 | t._parse(0, html.childNodes, undefined);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 58 |
|
| 59 | // Establish edge list
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 60 | const targetMap = t.temp['target'];
|
| 61 | const edges = t.temp['edges'];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 62 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 63 | let targetID, target, relation;
|
| 64 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 65 | // Iterate over edge lists
|
| 66 | // TODO:
|
| 67 | // Support spans for anchors!
|
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 68 | edges.forEach(function(edge) {
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 69 |
|
| 70 | // Check the target identifier
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 71 | targetID = edge.targetID;
|
| 72 | target = targetMap[targetID];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 73 |
|
| 74 | if (target != undefined) {
|
| 75 |
|
| 76 | // Check if the source is a span anchor
|
| 77 | /*
|
| 78 | var start = edge.srcStart;
|
| 79 | if (start !== edge.srcEnd) {
|
| 80 | start = [start, edge.srcEnd];
|
| 81 | };
|
| 82 | */
|
| 83 |
|
| 84 | // Add relation
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 85 | relation = {
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 86 | start : [edge.srcStart, edge.srcEnd],
|
| 87 | end : target,
|
| 88 | direction : 'uni',
|
| 89 | label : edge.label
|
| 90 | };
|
| 91 | // console.log(relation);
|
| 92 | this.addRel(relation);
|
| 93 | };
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 94 | }, t);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 95 |
|
| 96 | // Reset parsing memory
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 97 | delete t["temp"];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 98 |
|
| 99 | return this;
|
| 100 | },
|
| 101 |
|
| 102 | // Parse a node of the tree snippet
|
| 103 | _parse : function (parent, children, mark) {
|
| 104 |
|
| 105 | // Iterate over all child nodes
|
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 106 | Array.from(children).forEach(function(c) {
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 107 |
|
| 108 | // Element node
|
| 109 | if (c.nodeType == 1) {
|
| 110 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 111 | let xmlid, target, start, end;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 112 |
|
| 113 | // Node is an identifier
|
| 114 | if (c.hasAttribute('xml:id')) {
|
| 115 |
|
| 116 | // Remember that pos has this identifier
|
| 117 | xmlid = c.getAttribute('xml:id');
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 118 | // this.temp['target'][xmlid] =
|
| 119 | start = this.temp['pos'];
|
| 120 | end = this.temp['pos'];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 121 | }
|
| 122 |
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 123 | // Node is a link
|
| 124 | // Stricter: && c.hasAttribute('xlink:show')
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 125 | else if (c.hasAttribute('xlink:href')) {
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 126 |
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 127 | // Node is a continuation
|
| 128 | if (c.getAttribute('xlink:show') == "other" &&
|
| 129 | c.hasAttribute('data-action') &&
|
| 130 | c.getAttribute('data-action') == "join"
|
| 131 | ) {
|
| 132 | xmlid = c.getAttribute('xlink:href').replace(/^#/, "");
|
| 133 | start = this.temp['pos'];
|
| 134 | end = this.temp['pos'];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 135 |
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 136 | // this.temp['target'][xmlid][1] = this.temp['pos'] -1;
|
| 137 | // console.log("Here");
|
| 138 | }
|
| 139 |
|
| 140 | // Node is a relation
|
| 141 | // Stricter: c.getAttribute('xlink:show') == "none"
|
| 142 | else {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 143 | let label;
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 144 |
|
| 145 | // Get target id
|
| 146 | target = c.getAttribute('xlink:href').replace(/^#/, "");
|
| 147 |
|
| 148 | if (c.hasAttribute('xlink:title')) {
|
| 149 | label = this._clean(c.getAttribute('xlink:title'));
|
| 150 | };
|
| 151 |
|
| 152 | // Remember the defined edge
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 153 | // WRONG!
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 154 | var edge = {
|
| 155 | label : label,
|
| 156 | srcStart : this.temp['pos'],
|
| 157 | targetID : target
|
| 158 | };
|
| 159 |
|
| 160 | // TEMP: Hot-fix for root relations
|
| 161 | if (!label.match(/--$/) && !label.match(/ROOT$/)) {
|
| 162 | this.temp['edges'].push(edge);
|
| 163 | };
|
| 164 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 165 | };
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 166 | };
|
| 167 |
|
| 168 | // Go on with child nodes
|
| 169 | if (c.hasChildNodes()) {
|
| 170 | this._parse(0, c.childNodes, mark);
|
| 171 | };
|
| 172 |
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 173 |
|
| 174 | // The element is a defined anchor
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 175 | if (xmlid !== undefined) {
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 176 |
|
| 177 | // this.temp['target'][xmlid][1] = this.temp['pos'] -1;
|
| 178 |
|
| 179 | // Element already defined
|
| 180 | if (this.temp['target'][xmlid] !== undefined) {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 181 | const newtarget = this.temp['target'][xmlid];
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 182 | end = this.temp['pos'] - 1;
|
| 183 | newtarget[0] = start < newtarget[0] ? start : newtarget[0];
|
| 184 | newtarget[1] = end > newtarget[1] ? end : newtarget[1];
|
| 185 | }
|
| 186 |
|
| 187 | // Element not yet defined
|
| 188 | else {
|
| 189 | end = this.temp['pos'] - 1;
|
| 190 | this.temp['target'][xmlid] = [start, end];
|
| 191 | };
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 192 |
|
| 193 | /*
|
| 194 | console.log('Target ' + xmlid + ' spans from ' +
|
| 195 | this.temp['target'][xmlid][0] +
|
| 196 | ' to ' +
|
| 197 | this.temp['target'][xmlid][1]
|
| 198 | );
|
| 199 | */
|
| 200 | xmlid = undefined;
|
| 201 | }
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 202 |
|
| 203 | // Current element describes an arc
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 204 | else if (target !== undefined) {
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 205 |
|
| 206 | // TODO: This does not work yet!
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 207 | edge["srcEnd"] = this.temp['pos'] -1;
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 208 | // console.log('Here');
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 209 |
|
| 210 | /*
|
| 211 | console.log('Source spans from ' +
|
| 212 | edge["srcStart"] +
|
| 213 | ' to ' +
|
| 214 | edge["srcEnd"]
|
| 215 | );
|
| 216 | */
|
| 217 | target = undefined;
|
| 218 | };
|
| 219 | }
|
| 220 |
|
| 221 | // Text node
|
| 222 | else if (c.nodeType == 3) {
|
| 223 |
|
| 224 | // Check, if there is a non-whitespace token
|
| 225 | if (c.nodeValue !== undefined) {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 226 |
|
| 227 | const str = c.nodeValue.trim();
|
| 228 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 229 | if (str !== undefined && str.length > 0) {
|
| 230 |
|
| 231 | // Add token to token list
|
| 232 | this.addToken(str);
|
| 233 |
|
| 234 | // Move token position
|
| 235 | this.temp['pos']++;
|
| 236 | };
|
| 237 | };
|
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 238 | };
|
| 239 | }, this);
|
Akron | 6358105 | 2018-01-31 17:50:59 +0100 | [diff] [blame] | 240 |
|
| 241 | // Todo: define edges here!
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 242 | },
|
| 243 |
|
| 244 |
|
| 245 | // Remove foundry and layer for labels
|
| 246 | _clean : function (title) {
|
| 247 | return title.replace(_TermRE, "$3");
|
| 248 | },
|
| 249 |
|
| 250 |
|
| 251 | // Return the number of leaf nodes
|
| 252 | // (not necessarily part of a relation).
|
| 253 | // Consecutive nodes that are not part of any
|
| 254 | // relation are summarized in one node.
|
| 255 | size : function () {
|
| 256 | return this._tokens.length;
|
| 257 | },
|
| 258 |
|
| 259 |
|
| 260 | // This is a shorthand for SVG element creation
|
| 261 | _c : function (tag) {
|
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 262 | return d.createElementNS(svgNS, tag);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 263 | },
|
| 264 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 265 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 266 | // Get bounding box - with workaround for text nodes
|
| 267 | _rect : function (node) {
|
| 268 | if (node.tagName == "tspan" && !navigator.userAgent.match(/Edge/)) {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 269 | const range = d.createRange();
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 270 | range.selectNode(node);
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 271 | const rect = range.getBoundingClientRect();
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 272 | range.detach();
|
| 273 | return rect;
|
| 274 | };
|
| 275 | return node.getBoundingClientRect();
|
| 276 | },
|
| 277 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 278 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 279 | // Returns the center point of the requesting token
|
| 280 | _tokenPoint : function (node) {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 281 | const box = this._rect(node);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 282 | return box.left + (box.width / 2);
|
| 283 | },
|
| 284 |
|
| 285 |
|
| 286 | // Draws an anchor
|
| 287 | _drawAnchor : function (anchor) {
|
| 288 |
|
| 289 | // Calculate the span of the first and last token, the anchor spans
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 290 | const firstBox = this._rect(this._tokenElements[anchor.first]);
|
| 291 | const lastBox = this._rect(this._tokenElements[anchor.last]);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 292 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 293 | const y = this._y + (anchor.overlaps * this.anchorDiff) - this.anchorStart;
|
| 294 | const l = this._c('path');
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 295 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 296 | this._arcsElement.appendChild(l);
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 297 |
|
| 298 | const pathStr = "M " +
|
| 299 | (firstBox.left - this.offsetLeft) +
|
| 300 | "," +
|
| 301 | y +
|
| 302 | " L " +
|
| 303 | (lastBox.right - this.offsetLeft) +
|
| 304 | "," + y;
|
| 305 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 306 | l.setAttribute("d", pathStr);
|
| 307 | l.setAttribute("class", "anchor");
|
| 308 | anchor.element = l;
|
| 309 | anchor.y = y;
|
| 310 | return l;
|
| 311 | },
|
| 312 |
|
| 313 |
|
| 314 | // Create an arc with an optional label
|
| 315 | // Potentially needs a height parameter for stacks
|
| 316 | _drawArc : function (arc) {
|
| 317 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 318 | const t = this;
|
| 319 | let startPos, endPos;
|
| 320 | let startY = this._y;
|
| 321 | let endY = this._y;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 322 |
|
| 323 | if (arc.startAnchor !== undefined) {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 324 | startPos = t._tokenPoint(arc.startAnchor.element);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 325 | startY = arc.startAnchor.y;
|
| 326 | }
|
| 327 | else {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 328 | startPos = t._tokenPoint(t._tokenElements[arc.first]);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 329 | };
|
| 330 |
|
| 331 | if (arc.endAnchor !== undefined) {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 332 | endPos = t._tokenPoint(arc.endAnchor.element)
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 333 | endY = arc.endAnchor.y;
|
| 334 | }
|
| 335 | else {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 336 | endPos = t._tokenPoint(t._tokenElements[arc.last]);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 337 | };
|
| 338 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 339 | startPos -= t.offsetLeft;
|
| 340 | endPos -= t.offsetLeft;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 341 |
|
| 342 | // Special treatment for self-references
|
| 343 | var overlaps = arc.overlaps;
|
| 344 | if (startPos == endPos) {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 345 | startPos -= t.overlapDiff / 3;
|
| 346 | endPos += t.overlapDiff / 3;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 347 | overlaps += .5;
|
| 348 | };
|
| 349 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 350 | const g = t._c("g");
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 351 | g.setAttribute("class", "arc");
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 352 | const p = g.appendChild(t._c("path"));
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 353 | p.setAttribute('class', 'edge');
|
| 354 |
|
| 355 | // Attach the new arc before drawing, so computed values are available
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 356 | t._arcsElement.appendChild(g);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 357 |
|
| 358 | // Create arc
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 359 | let middle = Math.abs(endPos - startPos) / 2;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 360 |
|
| 361 | // TODO:
|
| 362 | // take the number of tokens into account!
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 363 | let cHeight = t.arcDiff + (overlaps * t.overlapDiff) + (middle / 2);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 364 |
|
| 365 | // Respect the maximum height
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 366 | cHeight = cHeight < t.maxArc ? cHeight : t.maxArc;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 367 |
|
| 368 | var x = Math.min(startPos, endPos);
|
| 369 |
|
| 370 | //var controlY = (startY + endY - cHeight);
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 371 | let controlY = (endY - cHeight);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 372 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 373 | const arcE = "M "+ startPos + "," + startY +
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 374 | " C " + startPos + "," + controlY +
|
| 375 | " " + endPos + "," + controlY +
|
| 376 | " " + endPos + "," + endY;
|
| 377 |
|
| 378 | p.setAttribute("d", arcE);
|
| 379 |
|
| 380 | if (arc.direction !== undefined) {
|
| 381 | p.setAttribute("marker-end", "url(#arr)");
|
| 382 | if (arc.direction === 'bi') {
|
| 383 | p.setAttribute("marker-start", "url(#arr)");
|
| 384 | };
|
| 385 | };
|
| 386 |
|
| 387 | if (arc.label === undefined)
|
| 388 | return g;
|
| 389 |
|
| 390 | /*
|
| 391 | * Calculate the top point of the arc for labeling using
|
| 392 | * de Casteljau's algorithm, see e.g.
|
| 393 | * http://blog.sklambert.com/finding-the-control-points-of-a-bezier-curve/
|
| 394 | * of course simplified to symmetric arcs ...
|
| 395 | */
|
| 396 | // Interpolate one side of the control polygon
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 397 | let middleY = (((startY + controlY) / 2) + controlY) / 2;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 398 |
|
| 399 | // Create a boxed label
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 400 | const label = this._c("g");
|
Akron | 430332b | 2017-11-20 15:36:51 +0100 | [diff] [blame] | 401 | label.setAttribute("class", "label");
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 402 | t._labelsElement.appendChild(label);
|
Akron | 430332b | 2017-11-20 15:36:51 +0100 | [diff] [blame] | 403 |
|
| 404 | // Set arc reference
|
| 405 | label.arcRef = g;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 406 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 407 | const that = t;
|
Akron | 430332b | 2017-11-20 15:36:51 +0100 | [diff] [blame] | 408 | label.addEventListener('mouseenter', function () {
|
| 409 | that.inFocus(this);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 410 | });
|
| 411 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 412 | const labelE = label.appendChild(t._c("text"));
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 413 | labelE.setAttribute("x", x + middle);
|
| 414 | labelE.setAttribute("y", middleY + 3);
|
| 415 | labelE.setAttribute("text-anchor", "middle");
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 416 | const textNode = d.createTextNode(arc.label);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 417 | labelE.appendChild(textNode);
|
| 418 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 419 | const labelBox = labelE.getBBox();
|
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 420 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 421 | /*
|
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 422 | if (!labelBox)
|
| 423 | console.log("----");
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 424 | */
|
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 425 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 426 | const textWidth = labelBox.width; // labelE.getComputedTextLength();
|
| 427 | const textHeight = labelBox.height; // labelE.getComputedTextLength();
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 428 |
|
| 429 | // Add box with padding to left and right
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 430 | const labelR = label.insertBefore(t._c("rect"), labelE);
|
| 431 | const boxWidth = textWidth + 2 * t.xPadding;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 432 | labelR.setAttribute("x", x + middle - (boxWidth / 2));
|
| 433 | labelR.setAttribute("ry", 5);
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 434 | labelR.setAttribute("y", labelBox.y - t.yPadding);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 435 | labelR.setAttribute("width", boxWidth);
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 436 | labelR.setAttribute("height", textHeight + 2 * t.yPadding);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 437 | },
|
| 438 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 439 |
|
| 440 | /**
|
| 441 | * Get the svg element
|
| 442 | */
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 443 | element : function () {
|
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame^] | 444 | if (this._el !== undefined)
|
| 445 | return this._el;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 446 |
|
| 447 | // Create svg
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 448 | const svg = this._c("svg");
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 449 |
|
| 450 | window.addEventListener("resize", function () {
|
| 451 | // TODO:
|
| 452 | // Only if text-size changed!
|
| 453 | // TODO:
|
| 454 | // This is currently untested
|
| 455 | this.show();
|
| 456 | }.bind(this));
|
| 457 |
|
| 458 | // Define marker arrows
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 459 | const defs = svg.appendChild(this._c("defs"));
|
| 460 | const marker = defs.appendChild(this._c("marker"));
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 461 | marker.setAttribute("refX", 9);
|
| 462 | marker.setAttribute("id", "arr");
|
| 463 | marker.setAttribute("orient", "auto-start-reverse");
|
| 464 | marker.setAttribute("markerUnits","userSpaceOnUse");
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 465 |
|
| 466 | const arrow = this._c("path");
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 467 | arrow.setAttribute("transform", "scale(0.8)");
|
| 468 | arrow.setAttribute("d", "M 0,-5 0,5 10,0 Z");
|
| 469 | marker.appendChild(arrow);
|
| 470 |
|
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame^] | 471 | this._el = svg;
|
| 472 | return this._el;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 473 | },
|
| 474 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 475 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 476 | // Add a relation with a start, an end,
|
| 477 | // a direction value and an optional label text
|
| 478 | addRel : function (rel) {
|
| 479 | this._arcs.push(rel);
|
| 480 | return this;
|
| 481 | },
|
| 482 |
|
| 483 |
|
| 484 | // Add a token to the list (this will mostly be a word)
|
| 485 | addToken : function(token) {
|
| 486 | this._tokens.push(token);
|
| 487 | return this;
|
| 488 | },
|
Akron | 430332b | 2017-11-20 15:36:51 +0100 | [diff] [blame] | 489 |
|
| 490 |
|
| 491 | // Move label and arc in focus
|
| 492 | inFocus : function (element) {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 493 | let cif;
|
Akron | 430332b | 2017-11-20 15:36:51 +0100 | [diff] [blame] | 494 |
|
| 495 | if (this._currentInFocus) {
|
| 496 |
|
| 497 | // Already in focus
|
| 498 | if (this._currentInFocus === element)
|
| 499 | return;
|
| 500 |
|
| 501 | cif = this._currentInFocus;
|
| 502 | cif.classList.remove('infocus');
|
| 503 | cif.arcRef.classList.remove('infocus');
|
| 504 | };
|
| 505 |
|
| 506 | cif = this._currentInFocus = element;
|
| 507 | this._labelsElement.appendChild(cif);
|
| 508 | this._arcsElement.appendChild(cif.arcRef);
|
| 509 | cif.classList.add('infocus');
|
| 510 | cif.arcRef.classList.add('infocus');
|
| 511 | },
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 512 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 513 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 514 | /*
|
| 515 | * All arcs need to be sorted before shown,
|
| 516 | * to avoid nesting.
|
| 517 | */
|
| 518 | _sortArcs : function () {
|
| 519 |
|
| 520 | // TODO:
|
| 521 | // Keep in mind that the arcs may have long anchors!
|
| 522 | // 1. Iterate over all arcs
|
| 523 | // 2. Sort all multi
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 524 | let anchors = {};
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 525 |
|
| 526 | // 1. Sort by length
|
| 527 | // 2. Tag all spans with the number of overlaps before
|
| 528 | // a) Iterate over all spans
|
| 529 | // b) check the latest preceeding overlapping span (lpos)
|
| 530 | // -> not found: tag with 0
|
| 531 | // -> found: Add +1 to the level of the (lpos)
|
| 532 | // c) If the new tag is smaller than the previous element,
|
| 533 | // reorder
|
| 534 |
|
| 535 | // Normalize start and end
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 536 | const sortedArcs = this._arcs.map(function (v) {
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 537 |
|
| 538 | // Check for long anchors
|
| 539 | if (v.start instanceof Array) {
|
| 540 |
|
| 541 | if (v.start[0] == v.start[1]) {
|
| 542 | v.start = v.start[0];
|
| 543 | }
|
| 544 |
|
| 545 | else {
|
| 546 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 547 | const middle = Math.ceil(Math.abs(v.start[1] - v.start[0]) / 2) + v.start[0];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 548 |
|
| 549 | // Calculate signature to avoid multiple anchors
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 550 | let anchorSig = "#" + v.start[0] + "_" + v.start[1];
|
| 551 |
|
| 552 | // Reverse signature
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 553 | if (v.start[0] > v.start[1]) {
|
| 554 | anchorSig = "#" + v.start[1] + "_" + v.start[0];
|
| 555 | };
|
| 556 |
|
| 557 | // Check if the anchor already exist
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 558 | let anchor = anchors[anchorSig];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 559 | if (anchor === undefined) {
|
| 560 | anchor = {
|
| 561 | "first": v.start[0],
|
| 562 | "last" : v.start[1],
|
| 563 | "length" : v.start[1] - v.start[0]
|
| 564 | };
|
| 565 | anchors[anchorSig] = anchor;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 566 | };
|
| 567 |
|
| 568 | v.startAnchor = anchor;
|
| 569 |
|
| 570 | // Add to anchors list
|
| 571 | v.start = middle;
|
| 572 | };
|
| 573 | };
|
| 574 |
|
| 575 | if (v.end instanceof Array) {
|
| 576 |
|
| 577 | if (v.end[0] == v.end[1]) {
|
| 578 | v.end = v.end[0];
|
| 579 | }
|
| 580 |
|
| 581 | else {
|
| 582 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 583 | const middle = Math.abs(v.end[0] - v.end[1]) + v.end[0];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 584 |
|
| 585 | // Calculate signature to avoid multiple anchors
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 586 | let anchorSig = "#" + v.end[0] + "_" + v.end[1];
|
| 587 |
|
| 588 | // Reverse signature
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 589 | if (v.end[0] > v.end[1]) {
|
| 590 | anchorSig = "#" + v.end[1] + "_" + v.end[0];
|
| 591 | };
|
| 592 |
|
| 593 | // Check if the anchor already exist
|
| 594 | var anchor = anchors[anchorSig];
|
| 595 | if (anchor === undefined) {
|
| 596 | anchor = {
|
| 597 | "first": v.end[0],
|
| 598 | "last" : v.end[1],
|
| 599 | "length" : v.end[1] - v.end[0]
|
| 600 | };
|
| 601 | anchors[anchorSig] = anchor;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 602 | };
|
| 603 |
|
| 604 | v.endAnchor = anchor;
|
| 605 |
|
| 606 | // Add to anchors list
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 607 | v.end = middle;
|
| 608 | };
|
| 609 | };
|
| 610 |
|
| 611 | v.first = v.start;
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 612 | v.last = v.end;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 613 |
|
| 614 | // calculate the arch length
|
| 615 | if (v.start < v.end) {
|
| 616 | v.length = v.end - v.start;
|
| 617 | }
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 618 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 619 | else {
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 620 | v.length = v.start - v.end;
|
| 621 | };
|
| 622 |
|
| 623 | return v;
|
| 624 | });
|
| 625 |
|
| 626 | // Sort based on length
|
| 627 | sortedArcs.sort(function (a, b) {
|
| 628 | if (a.length < b.length)
|
| 629 | return -1;
|
| 630 | else
|
| 631 | return 1;
|
| 632 | });
|
| 633 |
|
| 634 | // Add sorted arcs and anchors
|
| 635 | this._sortedArcs = lengthSort(sortedArcs, false);
|
Akron | 0a785d4 | 2020-10-12 17:21:46 +0200 | [diff] [blame] | 636 | this._sortedAnchors = lengthSort(Object.values(anchors), true);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 637 | },
|
| 638 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 639 |
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 640 | /**
|
| 641 | * Center the viewport of the canvas
|
| 642 | * TODO:
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 643 | * This is identical to treehierarchy
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 644 | */
|
| 645 | center : function () {
|
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame^] | 646 | if (this._el === undefined)
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 647 | return;
|
| 648 |
|
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame^] | 649 | const treeDiv = this._el.parentNode;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 650 |
|
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame^] | 651 | const cWidth = parseFloat(window.getComputedStyle(this._el).width);
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 652 | const treeWidth = parseFloat(window.getComputedStyle(treeDiv).width);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 653 | // Reposition:
|
| 654 | if (cWidth > treeWidth) {
|
| 655 | var scrollValue = (cWidth - treeWidth) / 2;
|
| 656 | treeDiv.scrollLeft = scrollValue;
|
| 657 | };
|
| 658 | },
|
| 659 |
|
| 660 |
|
| 661 | // Show the element
|
| 662 | show : function () {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 663 | const t = this;
|
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame^] | 664 | const svg = this._el;
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 665 | const height = this.maxArc;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 666 |
|
| 667 | // Delete old group
|
| 668 | if (svg.getElementsByTagName("g")[0] !== undefined) {
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 669 | svg.removeChild(
|
| 670 | svg.getElementsByTagName("g")[0]
|
| 671 | );
|
| 672 | t._tokenElements = [];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 673 | };
|
| 674 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 675 | const g = svg.appendChild(t._c("g"));
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 676 |
|
| 677 | // Draw token list
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 678 | const text = g.appendChild(t._c("text"));
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 679 | text.setAttribute('class', 'leaf');
|
| 680 | text.setAttribute("text-anchor", "start");
|
| 681 | text.setAttribute("y", height);
|
| 682 |
|
| 683 | // Calculate the start position
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 684 | t._y = height - (t.anchorStart);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 685 |
|
| 686 | // Introduce some prepending whitespace (yeah - I know ...)
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 687 | const ws = text.appendChild(t._c("tspan"));
|
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 688 | ws.appendChild(d.createTextNode('\u00A0'));
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 689 | ws.style.textAnchor = "start";
|
| 690 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 691 | t._tokens.forEach(function(node_i) {
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 692 | // Append svg
|
| 693 | // var x = text.appendChild(this._c("text"));
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 694 | const tspan = text.appendChild(this._c("tspan"));
|
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 695 | tspan.appendChild(d.createTextNode(node_i));
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 696 | tspan.setAttribute("text-anchor", "middle");
|
| 697 |
|
| 698 | this._tokenElements.push(tspan);
|
| 699 |
|
| 700 | // Add whitespace!
|
| 701 | tspan.setAttribute("dx", this.tokenSep);
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 702 | }, t);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 703 |
|
| 704 | // Get some global position data that may change on resize
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 705 | t.offsetLeft = t._rect(g).left;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 706 |
|
| 707 | // The group of arcs
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 708 | const arcs = g.appendChild(t._c("g"));
|
| 709 | t._arcsElement = arcs;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 710 | arcs.classList.add("arcs");
|
| 711 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 712 | const labels = g.appendChild(t._c("g"));
|
| 713 | t._labelsElement = labels;
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 714 | labels.classList.add("labels");
|
| 715 |
|
| 716 | // Sort arcs if not sorted yet
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 717 | if (t._sortedArcs === undefined)
|
| 718 | t._sortArcs();
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 719 |
|
| 720 | // 1. Draw all anchors
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 721 | t._sortedAnchors.forEach(
|
| 722 | i => t._drawAnchor(i)
|
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 723 | );
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 724 |
|
| 725 | // 2. Draw all arcs
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 726 | t._sortedArcs.forEach(
|
| 727 | i => t._drawArc(i)
|
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 728 | );
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 729 |
|
| 730 | // Resize the svg with some reasonable margins
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 731 | svg.setAttribute("width", t._rect(text).width + 20);
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 732 | svg.setAttribute("height", height + 20);
|
| 733 | svg.setAttribute("class", "relTree");
|
| 734 | }
|
| 735 | };
|
| 736 |
|
| 737 | // Sort relations regarding their span
|
| 738 | function lengthSort (list, inclusive) {
|
| 739 |
|
| 740 | /*
|
| 741 | * The "inclusive" flag allows to
|
| 742 | * modify the behaviour for inclusivity check,
|
| 743 | * e.g. if identical start or endpoints mean overlap or not.
|
| 744 | */
|
| 745 |
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 746 | let stack = [];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 747 |
|
| 748 | // Iterate over all definitions
|
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 749 | list.forEach(function(current) {
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 750 |
|
| 751 | // Check the stack order
|
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 752 | let overlaps = 0;
|
| 753 | let check;
|
| 754 | for (let j = (stack.length - 1); j >= 0; j--) {
|
| 755 | check = stack[j];
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 756 |
|
| 757 | // (a..(b..b)..a)
|
| 758 | if (current.first <= check.first && current.last >= check.last) {
|
| 759 | overlaps = check.overlaps + 1;
|
| 760 | break;
|
| 761 | }
|
| 762 |
|
| 763 | // (a..(b..a)..b)
|
| 764 | else if (current.first <= check.first && current.last >= check.first) {
|
| 765 |
|
| 766 | if (inclusive || (current.first != check.first && current.last != check.first)) {
|
| 767 | overlaps = check.overlaps + (current.length == check.length ? 0 : 1);
|
| 768 | };
|
| 769 | }
|
| 770 |
|
| 771 | // (b..(a..b)..a)
|
| 772 | else if (current.first <= check.last && current.last >= check.last) {
|
| 773 |
|
| 774 | if (inclusive || (current.first != check.last && current.last != check.last)) {
|
| 775 | overlaps = check.overlaps + (current.length == check.length ? 0 : 1);
|
| 776 | };
|
| 777 | };
|
| 778 | };
|
| 779 |
|
| 780 | // Set overlaps
|
| 781 | current.overlaps = overlaps;
|
| 782 |
|
| 783 | stack.push(current);
|
| 784 |
|
| 785 | // Although it is already sorted,
|
| 786 | // the new item has to be put at the correct place
|
| 787 | // TODO:
|
| 788 | // Use something like splice() instead
|
| 789 | stack.sort(function (a,b) {
|
| 790 | b.overlaps - a.overlaps
|
| 791 | });
|
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 792 | });
|
Akron | 3ebfd4e | 2017-11-13 17:56:49 +0100 | [diff] [blame] | 793 |
|
| 794 | return stack;
|
| 795 | };
|
| 796 | });
|