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