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 | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 93 | console.log("Match: " + match); |
| 94 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 95 | var tree = d3.select(match).select("div > div.treeInfo"); |
| 96 | |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 97 | console.log("Tree: " + tree); |
| 98 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 99 | if (tree.classed("active")) { |
| 100 | tree.classed("active", false); |
| 101 | return; |
| 102 | } |
| 103 | else if (!tree.select("svg").empty()) { |
| 104 | tree.classed("active", true); |
| 105 | return; |
| 106 | }; |
| 107 | |
| 108 | var corpusID = match.getAttribute('data-corpus-id'); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 109 | console.log(corpusID); |
| 110 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 111 | var docID = match.getAttribute('data-doc-id'); |
| 112 | var matchID = match.getAttribute('data-match-id'); |
| 113 | var url = |
| 114 | '/corpus' + |
| 115 | '/' + corpusID + |
| 116 | '/' + docID + |
| 117 | '/' + matchID + |
| 118 | '?foundry=' + foundry + |
| 119 | '&layer=' + layer + |
| 120 | '&spans=true'; |
| 121 | |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 122 | var wrapper = new String("<span class=\"context-left\"></span><span class=\"match\"><span title=\"xip/c:MC\"><span title=\"xip/c:TOP\"><span title=\"xip/c:PP\"><span title=\"xip/c:PREP\">Mit</span> <span title=\"xip/c:NP\"><span title=\"xip/c:DET\">dieser</span> <span title=\"xip/c:NPA\"><span title=\"xip/c:NOUN\">Methode</span></span></span></span> <span title=\"xip/c:VERB\">ist</span> <span title=\"xip/c:NP\"><span title=\"xip/c:PRON\">es</span></span> <span title=\"xip/c:AP\"><span title=\"xip/c:ADV\">nun</span> <span title=\"xip/c:ADJ\">möglich</span></span> <span title=\"xip/c:ADV\">z. B.</span> <span title=\"xip/c:NPA\"><span title=\"xip/c:NP\"><span title=\"xip/c:NOUN\">Voice</span></span></span> (<span title=\"xip/c:INS\"><span title=\"xip/c:NPA\"><span title=\"xip/c:NP\"><span title=\"xip/c:NOUN\">Sprache</span></span></span></span>) <span title=\"xip/c:VERB\">bevorzugt</span> <span title=\"xip/c:PP\"><span title=\"xip/c:PREP\">in</span> <span title=\"xip/c:NP\"><span title=\"xip/c:PRON\">der</span></span> <span title=\"xip/c:NPA\"><span title=\"xip/c:NP\"><span title=\"xip/c:NOUN\">Bridge</span></span></span></span> <span title=\"xip/c:INFC\"><span title=\"xip/c:INS\"><span title=\"xip/c:VERB\">weiterzugeben</span></span></span></span></span></span><span class=\"context-right\"></span>"); |
| 123 | |
| 124 | var svg = tree.append("svg"); |
| 125 | var svgGroup = svg.append("svg:g"); |
| 126 | |
| 127 | var treething = translateTree(wrapper); |
| 128 | console.log(treething); |
| 129 | |
| 130 | var layout = renderer.run(treething, svgGroup); |
| 131 | // 10 pixel padding |
| 132 | var w = layout.graph().width; |
| 133 | var h = layout.graph().height; |
| 134 | svg.attr("width", w + 10); |
| 135 | svg.attr("height", h + 10); |
| 136 | svgGroup.attr("transform", "translate(5, 5)"); |
| 137 | tree.classed("active", true); |
| 138 | |
| 139 | /* |
| 140 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 141 | jQuery.getJSON(url, function (res) { |
| 142 | var svg = tree.append("svg"); |
| 143 | var svgGroup = svg.append("svg:g"); |
| 144 | var treething = translateTree(res['snippet']); |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 145 | var layout = renderer.run(treething, svgGroup); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 146 | // 10 pixel padding |
| 147 | var w = layout.graph().width; |
| 148 | var h = layout.graph().height; |
| 149 | svg.attr("width", w + 10); |
| 150 | svg.attr("height", h + 10); |
| 151 | svgGroup.attr("transform", "translate(5, 5)"); |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 152 | tree.classed("active", true); |
| 153 | }); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 154 | |
| 155 | */ |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 156 | }; |