blob: 09906229ef09688468270a1d2ef8a6aac24a9185 [file] [log] [blame]
Nils Diewald1eba6572014-06-17 19:49:53 +00001// Store Table object in global object
2
Nils Diewald4af3f0b2014-06-25 01:43:17 +00003var splitRegex = /^([^\/]+?)(?:\/([^:]+?))?:(.+?)$/;
Nils Diewald1eba6572014-06-17 19:49:53 +00004
5var textFoundry = "Foundry";
6var textLayer = "Layer";
7
8
9// SnippetTable constructor
10function SnippetTable (snippet) {
11 this.info = [];
12 this.foundry = {};
13 this.layer = {};
14 this.pos = 0;
15
16 this.load = function (children) {
17 for (var i in children) {
18 var c = children[i];
19
Nils Diewald4af3f0b2014-06-25 01:43:17 +000020 // Create object on position unless it exists
21 if (this.info[this.pos] === undefined)
22 this.info[this.pos] = {};
23
24 var found = this.info[this.pos];
25
Nils Diewald1eba6572014-06-17 19:49:53 +000026 // element with title
27 if (c.nodeType === 1) {
28 if (c.getAttribute("title")) {
29 if (splitRegex.exec(c.getAttribute("title"))) {
30
31 // Fill position with info
32 var foundry, layer;
33 if (RegExp.$2) {
34 foundry = RegExp.$1;
35 layer = RegExp.$2;
36 }
37 else {
38 foundry = "base";
39 layer = RegExp.$1
40 };
41
Nils Diewald4af3f0b2014-06-25 01:43:17 +000042 if (found[foundry + "/" + layer] === undefined)
43 found[foundry + "/" + layer] = [];
Nils Diewald1eba6572014-06-17 19:49:53 +000044
Nils Diewald4af3f0b2014-06-25 01:43:17 +000045 found[foundry + "/" + layer].push(RegExp.$3);
Nils Diewald1eba6572014-06-17 19:49:53 +000046
47 // Set foundry
48 if (!this.foundry[foundry])
49 this.foundry[foundry] = {};
50 this.foundry[foundry][layer] = 1;
51
52 // Set layer
53 if (!this.layer[layer])
54 this.layer[layer] = {};
55 this.layer[layer][foundry] = 1;
56 };
57 };
58
59 // depth search
60 if (c.hasChildNodes())
61 this.load(c.childNodes);
62 }
63
64 // Leaf node - store string on position and go to next string
Nils Diewald4af3f0b2014-06-25 01:43:17 +000065 else if (c.nodeType === 3) {
66 if (c.nodeValue.match(/[a-z0-9]/i))
Nils Diewald1eba6572014-06-17 19:49:53 +000067 this.info[this.pos++]["-s"] = c.nodeValue;
Nils Diewald4af3f0b2014-06-25 01:43:17 +000068 };
Nils Diewald1eba6572014-06-17 19:49:53 +000069 };
Nils Diewald4af3f0b2014-06-25 01:43:17 +000070 delete this.info[this.pos];
Nils Diewald1eba6572014-06-17 19:49:53 +000071 return this;
72 };
73
74 this.toTable = function (base) {
75 var i, f, l;
76
77 // Create HTML based on info
78 var d = document;
79 var table = d.createElement('table');
80 var tr = d.createElement('tr');
81 table.appendChild(tr);
82 var th = d.createElement('th');
83 th.appendChild(document.createTextNode(base === "layer" ? textLayer : textFoundry));
84
85 // Add icon to switch sorting
86 var span = document.createElement("span");
87
88 // Add switch event
89 var that = this;
90 span.addEventListener("click", function (obj) {
91 var x = that.toTable(base === "layer" ? "foundry" : "layer");
92 table.parentNode.replaceChild(x, table);
93 }, false);
94
95 span.setAttribute("class", "switchSort");
96 var icon = document.createElement("i");
97 icon.setAttribute("class", "fa fa-arrows-h");
98 span.appendChild(icon);
99 th.appendChild(span);
100
101 tr.appendChild(th);
102 th = d.createElement('th');
103 th.appendChild(document.createTextNode(base === "layer" ? textFoundry : textLayer));
104 tr.appendChild(th);
105
106 // Header line with surface strings
107 for (i in this.info) {
108 th = d.createElement('th');
109 tr.appendChild(th);
110 th.appendChild(d.createTextNode(this.info[i]["-s"]));
111 };
112
113 // Sort keys
114 var baseArray = [];
115 if (base === "layer") {
116 for (i in this.layer) {
117 baseArray.push(i);
118 };
119 }
120 else {
121 for (i in this.foundry) {
122 baseArray.push(i);
123 };
124 };
125 baseArray.sort();
126
127 // Annotations
128 for (f in baseArray) {
129 f = baseArray[f];
130 var thBase = d.createElement('th');
131 thBase.appendChild(d.createTextNode(f));
132
133 var rowSpan = 0;
134
135 // Sort keys
136 var subArray = [];
137 if (base === "layer") {
138 for (i in this.layer[f]) {
139 subArray.push(i);
140 };
141 }
142 else {
143 for (i in this.foundry[f]) {
144 subArray.push(i);
145 };
146 };
147 subArray.sort();
148
149 for (l in subArray) {
150 l = subArray[l];
151 tr = d.createElement('tr');
152 table.appendChild(tr);
153
154 if (rowSpan === 0)
155 tr.appendChild(thBase);
156
157 th = d.createElement('th');
158 tr.appendChild(th);
159 th.appendChild(d.createTextNode(l));
160
161 var infoString = base === "layer" ? l + '/' + f : f + '/' + l;
162
163 for (t in this.info) {
164 var td = d.createElement('td');
165 tr.appendChild(td);
166
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000167 var surfaces = this.info[t][infoString];
168 if (surfaces !== undefined) {
169 for (i in surfaces) {
170 td.appendChild(d.createTextNode(surfaces[i]));
171 if (i !== surfaces.length -1) {
172 td.appendChild(d.createElement("br"));
173 };
174 };
175 };
Nils Diewald1eba6572014-06-17 19:49:53 +0000176 };
177 rowSpan++;
178 };
179 thBase.setAttribute("rowspan", rowSpan);
180 };
181 // return HTML object
182 return table;
183 };
184
185 // Create wrapper element
186 var html = document.createElement("table");
187 html.innerHTML = snippet;
188
189 // Create table object and load data from HTML
190 this.load(html.childNodes);
191};
192
193
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000194function showTable (o) {
195 var match = o.parentNode.parentNode;
Nils Diewalde99d9042014-11-20 23:36:54 +0000196 var table = match.getElementsByClassName("tokenInfo")[0];
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000197
Nils Diewalde99d9042014-11-20 23:36:54 +0000198 if (table.classList.contains("active")) {
199 table.classList.remove("active");
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000200 return;
201 }
Nils Diewalde99d9042014-11-20 23:36:54 +0000202 else if (table.getElementsByTagName("table").length > 0) {
203 table.classList.add("active");
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000204 return;
205 };
206
Nils Diewalde99d9042014-11-20 23:36:54 +0000207
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000208 var corpusID = match.getAttribute('data-corpus-id');
209 var docID = match.getAttribute('data-doc-id');
210 var matchID = match.getAttribute('data-match-id');
Nils Diewald3d871842015-03-10 23:14:34 +0000211 var url = '';;
212 if (document.location.pathname.indexOf('kalamar') >= 0) {
213 url = '/kalamar';
214 };
215 url += '/corpus/' + corpusID + '/' + docID + '/' + matchID;
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000216 var snippet;
217
Nils Diewalde99d9042014-11-20 23:36:54 +0000218 Ajax.getJSON(url, function (res) {
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000219 var snippet = new SnippetTable(res['snippet']);
Nils Diewalde99d9042014-11-20 23:36:54 +0000220 table.classList.add("active");
221 table.appendChild(snippet.toTable());
Nils Diewald4af3f0b2014-06-25 01:43:17 +0000222 });
223};
224
225