Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 2 | * Visualize span annotations as a tree |
| 3 | * using Dagre. |
Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 4 | * |
| 5 | * This should be lazy loaded! |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 6 | */ |
Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 7 | "use strict"; |
| 8 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 9 | define(['lib/dagre'], function (dagre) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 10 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 11 | const d = document; |
| 12 | const svgNS = "http://www.w3.org/2000/svg"; |
| 13 | const _TermRE = new RegExp("^(?:([^\/]+?)\/)?([^:]+?):(.+?)$"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 14 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 15 | // Node size |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 16 | var WIDTH = 55, HEIGHT = 20, LINEHEIGHT = 14; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 17 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 18 | // Create path for node connections |
| 19 | function _line (src, target) { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 20 | const x1 = src.x, |
| 21 | y1 = src.y, |
| 22 | x2 = target.x, |
| 23 | y2 = target.y - target.height / 2; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 24 | |
| 25 | // c 0,0 -10,0 |
| 26 | return 'M ' + x1 + ',' + y1 + ' ' + |
| 27 | 'C ' + x1 + ',' + y1 + ' ' + |
| 28 | x2 + ',' + (y2 - (y2 - y1) / 2) + ' ' + |
| 29 | x2 + ',' + y2; |
| 30 | }; |
| 31 | |
| 32 | return { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Create new tree visualization based |
| 36 | * on a match snippet. |
| 37 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 38 | create : function (snippet) { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 39 | return Object.create(this). |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 40 | _init(snippet); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 41 | }, |
| 42 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 43 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 44 | // Initialize the tree based on a snippet. |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 45 | _init : function (snippet) { |
| 46 | this._next = new Number(0); |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 47 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 48 | // Create html for traversal |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 49 | let html = d.createElement("div"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 50 | html.innerHTML = snippet; |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 51 | |
| 52 | const g = new dagre.graphlib.Graph({ |
| 53 | "directed" : true |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 54 | }); |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 55 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 56 | g.setGraph({ |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 57 | "nodesep" : 35, |
| 58 | "ranksep" : 15, |
| 59 | "marginx" : 40, |
| 60 | "marginy" : 10 |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 61 | }); |
| 62 | g.setDefaultEdgeLabel({}); |
| 63 | |
| 64 | this._graph = g; |
| 65 | |
| 66 | // This is a new root |
| 67 | this._addNode( |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 68 | this._next++, |
| 69 | { "class" : "root" } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 70 | ); |
| 71 | |
| 72 | // Parse nodes from root |
Akron | 98a933f | 2016-08-11 00:19:17 +0200 | [diff] [blame] | 73 | this._parse(0, html.childNodes, undefined); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 74 | |
| 75 | // Root node has only one child - remove |
| 76 | if (g.outEdges(0).length === 1) |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 77 | g.removeNode(0); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 78 | |
| 79 | html = undefined; |
| 80 | return this; |
| 81 | }, |
| 82 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 83 | |
| 84 | _c : function (tag) { |
| 85 | return d.createElementNS(svgNS, tag); |
| 86 | }, |
| 87 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 88 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 89 | /** |
| 90 | * The number of nodes in the tree. |
| 91 | */ |
| 92 | nodes : function () { |
| 93 | return this._next; |
| 94 | }, |
| 95 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 96 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 97 | // Add new node to graph |
| 98 | _addNode : function (id, obj) { |
| 99 | obj["width"] = WIDTH; |
| 100 | obj["height"] = HEIGHT; |
| 101 | this._graph.setNode(id, obj) |
Akron | 98a933f | 2016-08-11 00:19:17 +0200 | [diff] [blame] | 102 | return obj; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 103 | }, |
| 104 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 105 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 106 | // Add new edge to graph |
| 107 | _addEdge : function (src, target) { |
| 108 | this._graph.setEdge(src, target); |
| 109 | }, |
| 110 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 111 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 112 | // Remove foundry and layer for labels |
| 113 | _clean : function (title) { |
| 114 | return title.replace(_TermRE, "$3"); |
| 115 | }, |
| 116 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 117 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 118 | // Parse the snippet |
Akron | 98a933f | 2016-08-11 00:19:17 +0200 | [diff] [blame] | 119 | _parse : function (parent, children, mark) { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 120 | children.forEach(function(c) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 121 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 122 | // Element node |
| 123 | if (c.nodeType == 1) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 124 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 125 | // Get title from html |
| 126 | if (c.getAttribute("title")) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 127 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 128 | // Add child node |
| 129 | const id = this._next++; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 130 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 131 | const obj = this._addNode(id, { |
| 132 | "class" : "middle", |
| 133 | "label" : this._clean(c.getAttribute("title")) |
| 134 | }); |
Akron | 98a933f | 2016-08-11 00:19:17 +0200 | [diff] [blame] | 135 | |
| 136 | if (mark !== undefined) { |
| 137 | obj.class += ' mark'; |
| 138 | }; |
| 139 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 140 | this._addEdge(parent, id); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 141 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 142 | // Check for next level |
| 143 | if (c.hasChildNodes()) |
| 144 | this._parse(id, c.childNodes, mark); |
| 145 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 146 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 147 | // Step further |
| 148 | else if (c.hasChildNodes()) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 149 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 150 | this._parse( |
| 151 | parent, |
| 152 | c.childNodes, |
| 153 | c.tagName === 'MARK' ? true : mark |
| 154 | ); |
Akron | 98a933f | 2016-08-11 00:19:17 +0200 | [diff] [blame] | 155 | }; |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 156 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 157 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 158 | // Text node |
| 159 | else if (c.nodeType == 3) |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 160 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 161 | if (c.nodeValue.match(/[-a-z0-9]/i)) { |
| 162 | |
| 163 | // Add child node |
| 164 | const id = this._next++; |
| 165 | this._addNode(id, { |
| 166 | "class" : "leaf", |
| 167 | "label" : c.nodeValue |
| 168 | }); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 169 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 170 | this._addEdge(parent, id); |
| 171 | }; |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 172 | }, this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 173 | return this; |
| 174 | }, |
| 175 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 176 | |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame] | 177 | // Dummy method to be compatible with relTree |
| 178 | show : function () { |
| 179 | return; |
| 180 | }, |
| 181 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 182 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 183 | /** |
| 184 | * Center the viewport of the canvas |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame] | 185 | * TODO: |
| 186 | * This is identical to relations |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 187 | */ |
| 188 | center : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 189 | if (this._el === undefined) |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 190 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 191 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 192 | const treeDiv = this._el.parentNode; |
| 193 | const cWidth = parseFloat(window.getComputedStyle(this._el).width); |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 194 | const treeWidth = parseFloat(window.getComputedStyle(treeDiv).width); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 195 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 196 | // Reposition: |
| 197 | if (cWidth > treeWidth) { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 198 | treeDiv.scrollLeft = (cWidth - treeWidth) / 2; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 199 | }; |
| 200 | }, |
| 201 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 202 | |
Akron | 151bc87 | 2018-02-02 14:04:15 +0100 | [diff] [blame] | 203 | /** |
| 204 | * Create svg and serialize as base64 |
| 205 | */ |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 206 | toBase64 : function () { |
Akron | 6f32f82 | 2016-11-10 00:23:40 +0100 | [diff] [blame] | 207 | |
| 208 | // First clone element |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 209 | const svgWrapper = d.createElement('div') |
Akron | 6f32f82 | 2016-11-10 00:23:40 +0100 | [diff] [blame] | 210 | svgWrapper.innerHTML = this.element().outerHTML; |
Akron | 6f32f82 | 2016-11-10 00:23:40 +0100 | [diff] [blame] | 211 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 212 | const svg = svgWrapper.firstChild; |
| 213 | const style = this._c('style'); |
| 214 | |
Akron | 6f32f82 | 2016-11-10 00:23:40 +0100 | [diff] [blame] | 215 | svg.getElementsByTagName('defs')[0].appendChild(style); |
| 216 | |
| 217 | style.innerHTML = |
| 218 | 'path.edge ' + '{ stroke: black; stroke-width: 2pt; fill: none; }' + |
| 219 | 'g.root rect.empty,' + |
| 220 | 'g.middle rect' + '{ stroke: black; stroke-width: 2pt; fill: #bbb; }' + |
| 221 | 'g.leaf > rect ' + '{ display: none }' + |
| 222 | 'g > text > tspan ' + '{ text-anchor: middle; font-size: 9pt }' + |
| 223 | 'g.leaf > text > tspan ' + '{ font-size: 10pt; overflow: visible; }'; |
Akron | 3bdac53 | 2019-03-04 13:24:43 +0100 | [diff] [blame] | 224 | |
| 225 | return btoa(unescape(encodeURIComponent(svg.outerHTML)).replace(/ /g, ' ')); |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 226 | }, |
| 227 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 228 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 229 | /** |
| 230 | * Get the dom element of the tree view. |
| 231 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 232 | element : function () { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 233 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 234 | if (this._el !== undefined) |
| 235 | return this._el; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 236 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 237 | const g = this._graph; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 238 | dagre.layout(g); |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 239 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 240 | const canvas = this._c('svg'); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 241 | this._el = canvas; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 242 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 243 | canvas.appendChild(this._c('defs')); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 244 | |
| 245 | // Create edges |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 246 | const that = this; |
| 247 | |
| 248 | let src, target, p; |
| 249 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 250 | g.edges().forEach( |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 251 | function (e) { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 252 | src = g.node(e.v); |
| 253 | target = g.node(e.w); |
| 254 | p = that._c('path'); |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 255 | p.setAttributeNS(null, "d", _line(src, target)); |
| 256 | p.classList.add('edge'); |
| 257 | canvas.appendChild(p); |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 258 | } |
| 259 | ); |
| 260 | |
| 261 | let height = g.graph().height; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 262 | |
| 263 | // Create nodes |
| 264 | g.nodes().forEach( |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 265 | function (v) { |
| 266 | v = g.node(v); |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 267 | const group = that._c('g'); |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 268 | group.setAttribute('class', v.class); |
| 269 | |
| 270 | // Add node box |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 271 | const rect = group.appendChild(that._c('rect')); |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 272 | rect.setAttribute('x', v.x - v.width / 2); |
| 273 | rect.setAttribute('y', v.y - v.height / 2); |
| 274 | rect.setAttribute('rx', 5); |
| 275 | rect.setAttribute('ry', 5); |
| 276 | rect.setAttribute('width', v.width); |
| 277 | rect.setAttribute('height', v.height); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 278 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 279 | if (v.class === 'root' && v.label === undefined) { |
| 280 | rect.setAttribute('width', v.height); |
| 281 | rect.setAttribute('x', v.x - v.height / 2); |
| 282 | rect.setAttribute('class', 'empty'); |
| 283 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 284 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 285 | // Add label |
| 286 | if (v.label !== undefined) { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 287 | const text = group.appendChild(that._c('text')); |
| 288 | let y = v.y - v.height / 2; |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 289 | text.setAttribute('y', y); |
| 290 | text.setAttribute( |
| 291 | 'transform', |
| 292 | 'translate(' + v.width/2 + ',' + ((v.height / 2) + 5) + ')' |
| 293 | ); |
Akron | 3bdac53 | 2019-03-04 13:24:43 +0100 | [diff] [blame] | 294 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 295 | const vLabel = v.label.replace(/ /g, " ") |
| 296 | .replace(/&/g, '&') |
| 297 | .replace(/</g, '<') |
| 298 | .replace(/>/g, '>'); |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 299 | |
| 300 | if (v.class === "leaf") { |
Akron | 3bdac53 | 2019-03-04 13:24:43 +0100 | [diff] [blame] | 301 | text.setAttribute('title', vLabel); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 302 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 303 | let n = 0; |
| 304 | let tspan; |
| 305 | vLabel.split(" ").forEach(function(p) { |
| 306 | if (p.length === 0) |
| 307 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 308 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 309 | tspan = that._c('tspan'); |
| 310 | tspan.appendChild(d.createTextNode(p)); |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 311 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 312 | if (n !== 0) |
| 313 | tspan.setAttribute('dy', LINEHEIGHT + 'pt'); |
| 314 | else |
| 315 | n = 1; |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 316 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 317 | tspan.setAttribute('x', v.x - v.width / 2); |
| 318 | y += LINEHEIGHT; |
| 319 | text.appendChild(tspan); |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 320 | }); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 321 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 322 | y += LINEHEIGHT; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 323 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 324 | // The text is below the canvas - readjust the height! |
| 325 | if (y > height) |
| 326 | height = y; |
| 327 | } |
| 328 | else { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 329 | const tspan = that._c('tspan'); |
Akron | 3bdac53 | 2019-03-04 13:24:43 +0100 | [diff] [blame] | 330 | tspan.appendChild(d.createTextNode(vLabel)); |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 331 | tspan.setAttribute('x', v.x - v.width / 2); |
| 332 | text.appendChild(tspan); |
| 333 | }; |
| 334 | }; |
| 335 | canvas.appendChild(group); |
| 336 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 337 | ); |
| 338 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 339 | canvas.setAttribute('width', g.graph().width); |
| 340 | canvas.setAttribute('height', height); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 341 | return this._el; |
Akron | 151bc87 | 2018-02-02 14:04:15 +0100 | [diff] [blame] | 342 | }, |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 343 | |
Akron | 151bc87 | 2018-02-02 14:04:15 +0100 | [diff] [blame] | 344 | downloadLink : function () { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 345 | const a = d.createElement('a'); |
Akron | 151bc87 | 2018-02-02 14:04:15 +0100 | [diff] [blame] | 346 | a.setAttribute('href-lang', 'image/svg+xml'); |
| 347 | a.setAttribute('href', 'data:image/svg+xml;base64,' + this.toBase64()); |
| 348 | a.setAttribute('download', 'tree.svg'); |
| 349 | a.target = '_blank'; |
| 350 | a.setAttribute('rel', 'noopener noreferrer'); |
| 351 | return a; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 352 | } |
| 353 | }; |
| 354 | }); |