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 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame^] | 219 | let e, anno; |
| 220 | value.forEach(function(v) { |
| 221 | e = c.addE('div'); |
| 222 | e.addT(v); |
| 223 | |
| 224 | anno = ah.getDesc(key, v); |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 225 | |
| 226 | if (anno) |
| 227 | e.setAttribute("title", anno); |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame^] | 228 | }); |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 229 | } |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 230 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 231 | else { |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 232 | |
| 233 | if (value instanceof Array) |
| 234 | value = value[0]; |
| 235 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 236 | c.addT(value); |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 237 | |
| 238 | // Add tooltip |
| 239 | var anno = ah.getDesc(key, value); |
| 240 | if (anno) |
| 241 | c.setAttribute("title", anno); |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 242 | }; |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 243 | |
| 244 | return c; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | tr.addCell = addCell; |
| 248 | |
| 249 | // Add header information |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 250 | tr.addCell('th', undefined, 'Foundry'); |
| 251 | tr.addCell('th', undefined, 'Layer'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 252 | |
| 253 | // Add tokens |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 254 | Object.keys(this._token).forEach(function(i) { |
Akron | 5dc3117 | 2019-05-15 18:43:48 +0200 | [diff] [blame] | 255 | let surface = this.getToken(i); |
| 256 | var c = tr.addCell('th', undefined, surface); |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 257 | if (this._mark[i]) { |
| 258 | c.classList.add('mark'); |
| 259 | if (this._markE === undefined) { |
| 260 | this._markE = c; |
| 261 | }; |
Akron | 083ec57 | 2019-05-16 18:30:40 +0200 | [diff] [blame] | 262 | } |
Akron | 1a780fe | 2019-05-21 15:59:00 +0200 | [diff] [blame] | 263 | else if (this._cutted[0] == i || this._cutted[1] == i) { |
Akron | 083ec57 | 2019-05-16 18:30:40 +0200 | [diff] [blame] | 264 | c.classList.add('cutted'); |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 265 | }; |
Akron | 5dc3117 | 2019-05-15 18:43:48 +0200 | [diff] [blame] | 266 | |
| 267 | // In case the title is very long - add a title attribute |
| 268 | if (surface.length > 20) { |
| 269 | c.setAttribute("title", surface) |
| 270 | } |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 271 | }, this); |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 272 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 273 | var tbody = table.addE('tbody'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 274 | |
| 275 | var foundryList = Object.keys(this._foundry).sort(); |
| 276 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame^] | 277 | let layerList, key, v, value, cell; |
| 278 | |
| 279 | foundryList.forEach(function(foundry) { |
| 280 | let layerList = |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 281 | Object.keys(this._foundry[foundry]).sort(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 282 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame^] | 283 | layerList.forEach(function(layer) { |
| 284 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 285 | tr = tbody.addE('tr'); |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 286 | tr.setAttribute('tabindex', 0); |
| 287 | tr.addCell = addCell; |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 288 | tr.addCell('th', undefined, foundry); |
| 289 | tr.addCell('th', undefined, layer); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 290 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame^] | 291 | key = foundry + '/' + layer + '='; |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 292 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame^] | 293 | for (v = 0; v < this.length(); v++) { |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 294 | |
| 295 | // Get the cell value |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame^] | 296 | value = this.getValue(v, foundry, layer); |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 297 | |
| 298 | // Add cell to row |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame^] | 299 | cell = tr.addCell( |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 300 | 'td', |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 301 | key, |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 302 | value |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 303 | ); |
Akron | ad1e46a | 2018-09-19 15:55:40 +0200 | [diff] [blame] | 304 | |
| 305 | if (this._mark[v]) { |
| 306 | cell.classList.add('mark'); |
| 307 | }; |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 308 | }; |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame^] | 309 | }, this); |
| 310 | }, this); |
Akron | b6685bb | 2018-02-04 00:44:47 +0100 | [diff] [blame] | 311 | |
| 312 | // Add query creator |
| 313 | this._matchCreator = matchQueryCreator.create(this._element); |
| 314 | |
Akron | aeceda7 | 2018-02-02 20:44:06 +0100 | [diff] [blame] | 315 | return this._element; |
| 316 | }, |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 317 | }; |
| 318 | }); |