blob: 6ac43783c40635345ce0e91b9ebb0ddc78deb2ce [file] [log] [blame]
Nils Diewald1eba6572014-06-17 19:49:53 +00001var cleanRegex = /^([^\/]+?\/)?[^\:]+?\:/;
2
Nils Diewaldf2e02a92014-11-12 18:31:05 +00003var renderer = new dagreD3.Renderer();
4var oldDrawNodes = renderer.drawNodes();
5renderer.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 Diewald1eba6572014-06-17 19:49:53 +000013
Nils Diewaldf2e02a92014-11-12 18:31:05 +000014// Disable pan and zoom
15renderer.zoom(false);
16
17function SnippetTree (graph) {
18 this.next = new Number(0);
19 this.graph = graph;
20
21 // Fix title
Nils Diewald1eba6572014-06-17 19:49:53 +000022 this.cleanTitle = function (title) {
23 return title.replace(cleanRegex, "");
24 };
25
Nils Diewaldf2e02a92014-11-12 18:31:05 +000026 // This is a new root
27 this.graph.addNode(this.next++,{
28 nodeclass: "root"
29 })
Nils Diewald1eba6572014-06-17 19:49:53 +000030
Nils Diewaldf2e02a92014-11-12 18:31:05 +000031 // Add the children to the node
32 this.parseChildren = function (parent, children) {
Nils Diewald1eba6572014-06-17 19:49:53 +000033 for (var i in children) {
34 var c = children[i];
Nils Diewaldf2e02a92014-11-12 18:31:05 +000035
36 // Element node
37 if (c.nodeType == 1) {
38
39 // Get title from html
Nils Diewald1eba6572014-06-17 19:49:53 +000040 if (c.getAttribute("title")) {
41 var title = this.cleanTitle(c.getAttribute("title"));
Nils Diewaldf2e02a92014-11-12 18:31:05 +000042
43 // Add child node
44 var id = this.next++;
45 this.graph.addNode(id, {
46 nodeclass : "middle",
47 label : title
Nils Diewald1eba6572014-06-17 19:49:53 +000048 });
Nils Diewaldf2e02a92014-11-12 18:31:05 +000049 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 Diewald1eba6572014-06-17 19:49:53 +000071 };
72 };
73 return this;
74 };
Nils Diewald1eba6572014-06-17 19:49:53 +000075};
76
Nils Diewald1eba6572014-06-17 19:49:53 +000077function translateTree (snippet) {
78 var html = document.createElement("tree");
79 html.innerHTML = snippet;
Nils Diewaldf2e02a92014-11-12 18:31:05 +000080 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 Diewald1eba6572014-06-17 19:49:53 +000088};
89
Nils Diewaldf2e02a92014-11-12 18:31:05 +000090function showTree (o, foundry, layer) {
91 var match = o.parentNode.parentNode;
Nils Diewald1eba6572014-06-17 19:49:53 +000092
Nils Diewald996aa552014-12-02 03:26:44 +000093console.log("Match: " + match);
94
Nils Diewaldf2e02a92014-11-12 18:31:05 +000095 var tree = d3.select(match).select("div > div.treeInfo");
96
Nils Diewald996aa552014-12-02 03:26:44 +000097console.log("Tree: " + tree);
98
Nils Diewaldf2e02a92014-11-12 18:31:05 +000099 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 Diewald996aa552014-12-02 03:26:44 +0000109console.log(corpusID);
110
Nils Diewaldf2e02a92014-11-12 18:31:05 +0000111 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 Diewald996aa552014-12-02 03:26:44 +0000122 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);
128console.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 Diewaldf2e02a92014-11-12 18:31:05 +0000141 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 Diewaldf2e02a92014-11-12 18:31:05 +0000145 var layout = renderer.run(treething, svgGroup);
Nils Diewald996aa552014-12-02 03:26:44 +0000146 // 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 Diewaldf2e02a92014-11-12 18:31:05 +0000152 tree.classed("active", true);
153 });
Nils Diewald996aa552014-12-02 03:26:44 +0000154
155*/
Nils Diewaldf2e02a92014-11-12 18:31:05 +0000156};