Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 1 | // Store Table object in global object |
| 2 | |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 3 | var splitRegex = /^([^\/]+?)(?:\/([^:]+?))?:(.+?)$/; |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 4 | |
| 5 | var textFoundry = "Foundry"; |
| 6 | var textLayer = "Layer"; |
| 7 | |
| 8 | |
| 9 | // SnippetTable constructor |
| 10 | function 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 Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 20 | // 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 Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 26 | // 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 Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 42 | if (found[foundry + "/" + layer] === undefined) |
| 43 | found[foundry + "/" + layer] = []; |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 44 | |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 45 | found[foundry + "/" + layer].push(RegExp.$3); |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 46 | |
| 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 Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 65 | else if (c.nodeType === 3) { |
| 66 | if (c.nodeValue.match(/[a-z0-9]/i)) |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 67 | this.info[this.pos++]["-s"] = c.nodeValue; |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 68 | }; |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 69 | }; |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 70 | delete this.info[this.pos]; |
Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 71 | 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 Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 167 | 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 Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 176 | }; |
| 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 Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 194 | function showTable (o) { |
| 195 | var match = o.parentNode.parentNode; |
Nils Diewald | e99d904 | 2014-11-20 23:36:54 +0000 | [diff] [blame] | 196 | var table = match.getElementsByClassName("tokenInfo")[0]; |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 197 | |
Nils Diewald | e99d904 | 2014-11-20 23:36:54 +0000 | [diff] [blame] | 198 | if (table.classList.contains("active")) { |
| 199 | table.classList.remove("active"); |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 200 | return; |
| 201 | } |
Nils Diewald | e99d904 | 2014-11-20 23:36:54 +0000 | [diff] [blame] | 202 | else if (table.getElementsByTagName("table").length > 0) { |
| 203 | table.classList.add("active"); |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 204 | return; |
| 205 | }; |
| 206 | |
Nils Diewald | e99d904 | 2014-11-20 23:36:54 +0000 | [diff] [blame] | 207 | |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 208 | var corpusID = match.getAttribute('data-corpus-id'); |
| 209 | var docID = match.getAttribute('data-doc-id'); |
| 210 | var matchID = match.getAttribute('data-match-id'); |
Nils Diewald | 3d87184 | 2015-03-10 23:14:34 +0000 | [diff] [blame] | 211 | var url = '';; |
| 212 | if (document.location.pathname.indexOf('kalamar') >= 0) { |
| 213 | url = '/kalamar'; |
| 214 | }; |
| 215 | url += '/corpus/' + corpusID + '/' + docID + '/' + matchID; |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 216 | var snippet; |
| 217 | |
Nils Diewald | e99d904 | 2014-11-20 23:36:54 +0000 | [diff] [blame] | 218 | Ajax.getJSON(url, function (res) { |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 219 | var snippet = new SnippetTable(res['snippet']); |
Nils Diewald | e99d904 | 2014-11-20 23:36:54 +0000 | [diff] [blame] | 220 | table.classList.add("active"); |
| 221 | table.appendChild(snippet.toTable()); |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 222 | }); |
| 223 | }; |
| 224 | |
| 225 | |