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