blob: e7ce83dec90b221bdcfd65001ecbee6670a58fd7 [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 Diewaldf2e02a92014-11-12 18:31:05 +000093 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};