Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 1 | var cleanRegex = /^([^\/]+?\/)?[^\:]+?\:/; |
| 2 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame^] | 3 | var renderer = new dagreD3.Renderer(); |
| 4 | var oldDrawNodes = renderer.drawNodes(); |
| 5 | renderer.drawNodes( |
| 6 | function (graph, root) { |
| 7 | var svgNodes = oldDrawNodes(graph, root); |
| 8 | svgNodes.each(function(u) { |
| 9 | d3.select(this).classed(graph.node(u).nodeclass, true); |
| 10 | }); |
| 11 | return svgNodes; |
| 12 | }); |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 13 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame^] | 14 | // Disable pan and zoom |
| 15 | renderer.zoom(false); |
| 16 | |
| 17 | function SnippetTree (graph) { |
| 18 | this.next = new Number(0); |
| 19 | this.graph = graph; |
| 20 | |
| 21 | // Fix title |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 22 | this.cleanTitle = function (title) { |
| 23 | return title.replace(cleanRegex, ""); |
| 24 | }; |
| 25 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame^] | 26 | // This is a new root |
| 27 | this.graph.addNode(this.next++,{ |
| 28 | nodeclass: "root" |
| 29 | }) |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 30 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame^] | 31 | // Add the children to the node |
| 32 | this.parseChildren = function (parent, children) { |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 33 | for (var i in children) { |
| 34 | var c = children[i]; |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame^] | 35 | |
| 36 | // Element node |
| 37 | if (c.nodeType == 1) { |
| 38 | |
| 39 | // Get title from html |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 40 | if (c.getAttribute("title")) { |
| 41 | var title = this.cleanTitle(c.getAttribute("title")); |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame^] | 42 | |
| 43 | // Add child node |
| 44 | var id = this.next++; |
| 45 | this.graph.addNode(id, { |
| 46 | nodeclass : "middle", |
| 47 | label : title |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 48 | }); |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame^] | 49 | this.graph.addEdge(null, parent, id); |
| 50 | |
| 51 | // Check for next level |
| 52 | if (c.hasChildNodes()) |
| 53 | this.parseChildren(id, c.childNodes); |
| 54 | } |
| 55 | |
| 56 | // Step further |
| 57 | else if (c.hasChildNodes()) |
| 58 | this.parseChildren(parent, c.childNodes); |
| 59 | } |
| 60 | |
| 61 | // Text node |
| 62 | else if (c.nodeType == 3) |
| 63 | if (c.nodeValue.match(/[-a-z0-9]/i)) { |
| 64 | // Add child node |
| 65 | var id = this.next++; |
| 66 | this.graph.addNode(id, { |
| 67 | nodeclass : "leaf", |
| 68 | label : c.nodeValue |
| 69 | }); |
| 70 | this.graph.addEdge(null, parent, id); |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 71 | }; |
| 72 | }; |
| 73 | return this; |
| 74 | }; |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 77 | function translateTree (snippet) { |
| 78 | var html = document.createElement("tree"); |
| 79 | html.innerHTML = snippet; |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame^] | 80 | var st = new SnippetTree(new dagreD3.Digraph()); |
| 81 | st.parseChildren(0, html.childNodes); |
| 82 | var g = st.graph; |
| 83 | |
| 84 | // Root node has only one child |
| 85 | if (Object.keys(g._outEdges[0]).length === 1) |
| 86 | g.delNode(0); |
| 87 | return g; |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame^] | 90 | function showTree (o, foundry, layer) { |
| 91 | var match = o.parentNode.parentNode; |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 92 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame^] | 93 | var tree = d3.select(match).select("div > div.treeInfo"); |
| 94 | |
| 95 | if (tree.classed("active")) { |
| 96 | tree.classed("active", false); |
| 97 | return; |
| 98 | } |
| 99 | else if (!tree.select("svg").empty()) { |
| 100 | tree.classed("active", true); |
| 101 | return; |
| 102 | }; |
| 103 | |
| 104 | var corpusID = match.getAttribute('data-corpus-id'); |
| 105 | var docID = match.getAttribute('data-doc-id'); |
| 106 | var matchID = match.getAttribute('data-match-id'); |
| 107 | var url = |
| 108 | '/corpus' + |
| 109 | '/' + corpusID + |
| 110 | '/' + docID + |
| 111 | '/' + matchID + |
| 112 | '?foundry=' + foundry + |
| 113 | '&layer=' + layer + |
| 114 | '&spans=true'; |
| 115 | |
| 116 | jQuery.getJSON(url, function (res) { |
| 117 | var svg = tree.append("svg"); |
| 118 | var svgGroup = svg.append("svg:g"); |
| 119 | var treething = translateTree(res['snippet']); |
| 120 | |
| 121 | var layout = renderer.run(treething, svgGroup); |
| 122 | |
| 123 | svg.attr("width", layout.graph().width + 40) |
| 124 | .attr("height", layout.graph().height + 40); |
| 125 | |
| 126 | tree.classed("active", true); |
| 127 | }); |
| 128 | }; |