Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 1 | /** |
Akron | b6685bb | 2018-02-04 00:44:47 +0100 | [diff] [blame] | 2 | * Table representation of token-based |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 3 | * annotations of a match. |
| 4 | */ |
Akron | b6685bb | 2018-02-04 00:44:47 +0100 | [diff] [blame] | 5 | define([ |
| 6 | 'match/querycreator', |
| 7 | "util" |
| 8 | ], function (matchQueryCreator) { |
| 9 | /* |
| 10 | * TODO: |
| 11 | * Create base object for all matchinfo classes! |
| 12 | * TODO: |
| 13 | * Rename to match/annotationtable |
| 14 | */ |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 15 | const _TermRE = new RegExp("^(?:([^\/]+?)\/)?([^:]+?):(.+?)$"); |
| 16 | const d = document; |
| 17 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 18 | return { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Create new table view for a match |
| 22 | * based on a snippet string. |
| 23 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 24 | create : function (snippet) { |
| 25 | return Object.create(this)._init(snippet); |
| 26 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 27 | |
| 28 | // Initialize table based on snippet |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 29 | _init : function (snippet) { |
| 30 | // Create html for traversal |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 31 | var html = d.createElement("div"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 32 | html.innerHTML = snippet; |
| 33 | |
| 34 | this._pos = 0; |
| 35 | this._token = []; |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 36 | this._mark = []; |
| 37 | this._markE = undefined; |
Akron | 1a780fe | 2019-05-21 15:59:00 +0200 | [diff] [blame] | 38 | this._cutted = []; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 39 | this._info = []; |
| 40 | this._foundry = {}; |
| 41 | this._layer = {}; |
| 42 | |
| 43 | // Parse the snippet |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 44 | this._parse(html.childNodes, false); |
Akron | b6685bb | 2018-02-04 00:44:47 +0100 | [diff] [blame] | 45 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 46 | html.innerHTML = ''; |
| 47 | return this; |
| 48 | }, |
Akron | b6685bb | 2018-02-04 00:44:47 +0100 | [diff] [blame] | 49 | |
| 50 | // TODO: Destroy match! |
| 51 | destroy : function () { |
| 52 | this._matchCreator = undefined; |
| 53 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Length of the table (columns), |
| 57 | * aka the number of tokens |
| 58 | * in the snippet. |
| 59 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 60 | length : function () { |
| 61 | return this._pos; |
| 62 | }, |
| 63 | |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Move the viewport to the match |
| 67 | */ |
| 68 | toMark : function () { |
| 69 | if (this._markE === undefined) |
| 70 | return; |
| 71 | this._markE.scrollIntoView({ |
| 72 | inline: "start", |
| 73 | block: "nearest" |
| 74 | }); |
| 75 | }, |
| 76 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 77 | /** |
| 78 | * Get the token in the snippet |
| 79 | * At a given position. |
| 80 | * |
| 81 | * @param pos |
| 82 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 83 | getToken : function (pos) { |
| 84 | if (pos === undefined) |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 85 | return this._token; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 86 | return this._token[pos]; |
| 87 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Get the annotation of a token |
| 91 | * in the snippet based on the position, |
| 92 | * the foundry, and the layer. |
| 93 | * |
| 94 | * @param pos |
| 95 | * @param foundry |
| 96 | * @param layer |
| 97 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 98 | getValue : function (pos, foundry, layer) { |
| 99 | return this._info[pos][foundry + '/' + layer] |
| 100 | }, |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 101 | |
Akron | aeceda7 | 2018-02-02 20:44:06 +0100 | [diff] [blame] | 102 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 103 | // Parse the snippet |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 104 | _parse : function (children, mark) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 105 | |
| 106 | // Get all children |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame^] | 107 | children.forEach(function(c) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 108 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 109 | // Create object on position unless it exists |
| 110 | if (this._info[this._pos] === undefined) { |
| 111 | this._info[this._pos] = {}; |
| 112 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 113 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 114 | // Store at position in foundry/layer as array |
| 115 | var found = this._info[this._pos]; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 116 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 117 | // Element with title |
| 118 | if (c.nodeType === 1) { |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 119 | var newMark = mark; |
| 120 | |
| 121 | if (c.tagName === 'MARK') { |
| 122 | newMark = true; |
| 123 | } |
| 124 | |
Akron | 083ec57 | 2019-05-16 18:30:40 +0200 | [diff] [blame] | 125 | else if (c.hasAttribute("title") && |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 126 | _TermRE.exec(c.getAttribute("title"))) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 127 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 128 | // Fill position with info |
| 129 | var foundry, layer, value; |
| 130 | if (RegExp.$2) { |
| 131 | foundry = RegExp.$1; |
| 132 | layer = RegExp.$2; |
| 133 | } |
| 134 | else { |
| 135 | foundry = "base"; |
| 136 | layer = RegExp.$1 |
| 137 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 138 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 139 | value = RegExp.$3; |
| 140 | |
| 141 | if (found[foundry + "/" + layer] === undefined) { |
| 142 | found[foundry + "/" + layer] = [value]; |
| 143 | } |
| 144 | else { |
Akron | 3b253d3 | 2018-07-15 10:16:06 +0200 | [diff] [blame] | 145 | // if (found[foundry + "/" + layer].indexOf(value) === -1) { |
| 146 | if (!found[foundry + "/" + layer].includes(value)) { |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 147 | // Push value to foundry/layer at correct position |
| 148 | found[foundry + "/" + layer].push(value); |
| 149 | }; |
| 150 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 151 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 152 | // Set foundry |
| 153 | if (this._foundry[foundry] === undefined) |
| 154 | this._foundry[foundry] = {}; |
| 155 | this._foundry[foundry][layer] = 1; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 156 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 157 | // Set layer |
| 158 | if (this._layer[layer] === undefined) |
| 159 | this._layer[layer] = {}; |
| 160 | this._layer[layer][foundry] = 1; |
Akron | 083ec57 | 2019-05-16 18:30:40 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // The current position marks a cut |
| 164 | else if (c.hasAttribute("class") && c.getAttribute("class") == "cutted") { |
Akron | 1a780fe | 2019-05-21 15:59:00 +0200 | [diff] [blame] | 165 | this._cutted.push(this._pos); |
Akron | 083ec57 | 2019-05-16 18:30:40 +0200 | [diff] [blame] | 166 | this._token[this._pos++] = ""; |
| 167 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 168 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 169 | // depth search |
| 170 | if (c.hasChildNodes()) |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 171 | this._parse(c.childNodes, newMark); |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 172 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 173 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 174 | // Leaf node |
| 175 | // store string on position and go to next string |
| 176 | else if (c.nodeType === 3) { |
Akron | 158fce1 | 2019-12-17 14:43:29 +0100 | [diff] [blame] | 177 | if (c.nodeValue.match(/[a-z0-9\u25ae]/iu)) { |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 178 | this._mark[this._pos] = mark ? true : false; |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 179 | this._token[this._pos++] = c.nodeValue; |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 180 | }; |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 181 | }; |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame^] | 182 | }, this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 183 | |
| 184 | delete this._info[this._pos]; |
| 185 | }, |
| 186 | |
| 187 | |
| 188 | /** |
| 189 | * Get HTML table view of annotations. |
| 190 | */ |
| 191 | element : function () { |
| 192 | if (this._element !== undefined) |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 193 | return this._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 194 | |
| 195 | // First the legend table |
Akron | aeceda7 | 2018-02-02 20:44:06 +0100 | [diff] [blame] | 196 | var wrap = d.createElement('div'); |
| 197 | |
| 198 | var table = wrap.addE('table'); |
| 199 | |
| 200 | this._element = wrap; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 201 | |
| 202 | // Single row in head |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 203 | var tr = table.addE('thead').addE('tr'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 204 | |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 205 | var ah = KorAP.annotationHelper || { "getDesc" : function () {}}; |
| 206 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 207 | // Add cell to row |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 208 | var addCell = function (type, key, value) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 209 | var c = this.addE(type); |
| 210 | |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 211 | if (value === undefined) |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 212 | return c; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 213 | |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 214 | if (key && value instanceof Array && value[1] !== undefined) { |
Akron | 4e47d0b | 2017-07-03 17:58:37 +0200 | [diff] [blame] | 215 | |
| 216 | // There are multiple values to add |
Akron | 856af1e | 2017-07-03 19:57:46 +0200 | [diff] [blame] | 217 | c.classList.add('matchkeyvalues'); |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 218 | for (var n = 0; n < value.length; n++) { |
Akron | 5b1a6af | 2018-02-05 15:41:16 +0100 | [diff] [blame] | 219 | var e = c.addE('div'); |
| 220 | e.addT(value[n]); |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 221 | |
| 222 | var anno = ah.getDesc(key, value[n]); |
| 223 | |
| 224 | if (anno) |
| 225 | e.setAttribute("title", anno); |
Akron | 856af1e | 2017-07-03 19:57:46 +0200 | [diff] [blame] | 226 | }; |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 227 | } |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 228 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 229 | else { |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 230 | |
| 231 | if (value instanceof Array) |
| 232 | value = value[0]; |
| 233 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 234 | c.addT(value); |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 235 | |
| 236 | // Add tooltip |
| 237 | var anno = ah.getDesc(key, value); |
| 238 | if (anno) |
| 239 | c.setAttribute("title", anno); |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 240 | }; |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 241 | |
| 242 | return c; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | tr.addCell = addCell; |
| 246 | |
| 247 | // Add header information |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 248 | tr.addCell('th', undefined, 'Foundry'); |
| 249 | tr.addCell('th', undefined, 'Layer'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 250 | |
| 251 | // Add tokens |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame^] | 252 | Object.keys(this._token).forEach(function(i) { |
Akron | 5dc3117 | 2019-05-15 18:43:48 +0200 | [diff] [blame] | 253 | let surface = this.getToken(i); |
| 254 | var c = tr.addCell('th', undefined, surface); |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 255 | if (this._mark[i]) { |
| 256 | c.classList.add('mark'); |
| 257 | if (this._markE === undefined) { |
| 258 | this._markE = c; |
| 259 | }; |
Akron | 083ec57 | 2019-05-16 18:30:40 +0200 | [diff] [blame] | 260 | } |
Akron | 1a780fe | 2019-05-21 15:59:00 +0200 | [diff] [blame] | 261 | else if (this._cutted[0] == i || this._cutted[1] == i) { |
Akron | 083ec57 | 2019-05-16 18:30:40 +0200 | [diff] [blame] | 262 | c.classList.add('cutted'); |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 263 | }; |
Akron | 5dc3117 | 2019-05-15 18:43:48 +0200 | [diff] [blame] | 264 | |
| 265 | // In case the title is very long - add a title attribute |
| 266 | if (surface.length > 20) { |
| 267 | c.setAttribute("title", surface) |
| 268 | } |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame^] | 269 | }, this); |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 270 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 271 | var tbody = table.addE('tbody'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 272 | |
| 273 | var foundryList = Object.keys(this._foundry).sort(); |
| 274 | |
| 275 | for (var f = 0; f < foundryList.length; f++) { |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 276 | var foundry = foundryList[f]; |
| 277 | var layerList = |
| 278 | Object.keys(this._foundry[foundry]).sort(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 279 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 280 | for (var l = 0; l < layerList.length; l++) { |
| 281 | var layer = layerList[l]; |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 282 | tr = tbody.addE('tr'); |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 283 | tr.setAttribute('tabindex', 0); |
| 284 | tr.addCell = addCell; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 285 | |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 286 | tr.addCell('th', undefined, foundry); |
| 287 | tr.addCell('th', undefined, layer); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 288 | |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 289 | var key = foundry + '/' + layer + '='; |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 290 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 291 | for (var v = 0; v < this.length(); v++) { |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 292 | |
| 293 | // Get the cell value |
| 294 | var value = this.getValue(v, foundry, layer); |
| 295 | |
| 296 | // Add cell to row |
| 297 | var cell = tr.addCell( |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 298 | 'td', |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 299 | key, |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 300 | value |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 301 | ); |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 302 | |
| 303 | if (this._mark[v]) { |
| 304 | cell.classList.add('mark'); |
| 305 | }; |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 306 | }; |
| 307 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 308 | }; |
Akron | b6685bb | 2018-02-04 00:44:47 +0100 | [diff] [blame] | 309 | |
| 310 | // Add query creator |
| 311 | this._matchCreator = matchQueryCreator.create(this._element); |
| 312 | |
Akron | aeceda7 | 2018-02-02 20:44:06 +0100 | [diff] [blame] | 313 | return this._element; |
| 314 | }, |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 315 | }; |
| 316 | }); |