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; |
| 196 | var table = $(match).children("div").children("div.tokenInfo").first(); |
| 197 | |
Nils Diewald | f2e02a9 | 2014-11-12 18:31:05 +0000 | [diff] [blame] | 198 | console.log(table); |
| 199 | |
Nils Diewald | 4af3f0b | 2014-06-25 01:43:17 +0000 | [diff] [blame] | 200 | if (table.hasClass("active")) { |
| 201 | table.removeClass("active"); |
| 202 | return; |
| 203 | } |
| 204 | else if (table.children("table").length > 0) { |
| 205 | table.addClass("active"); |
| 206 | return; |
| 207 | }; |
| 208 | |
| 209 | var corpusID = match.getAttribute('data-corpus-id'); |
| 210 | var docID = match.getAttribute('data-doc-id'); |
| 211 | var matchID = match.getAttribute('data-match-id'); |
| 212 | var url = '/corpus/' + corpusID + '/' + docID + '/' + matchID; |
| 213 | var snippet; |
| 214 | |
| 215 | jQuery.getJSON(url, function (res) { |
| 216 | var snippet = new SnippetTable(res['snippet']); |
| 217 | table.addClass("active"); |
| 218 | table.append(snippet.toTable()); |
| 219 | }); |
| 220 | }; |
| 221 | |
| 222 | |