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 = []; |
| 36 | this._info = []; |
| 37 | this._foundry = {}; |
| 38 | this._layer = {}; |
| 39 | |
| 40 | // Parse the snippet |
| 41 | this._parse(html.childNodes); |
Akron | b6685bb | 2018-02-04 00:44:47 +0100 | [diff] [blame^] | 42 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 43 | html.innerHTML = ''; |
| 44 | return this; |
| 45 | }, |
Akron | b6685bb | 2018-02-04 00:44:47 +0100 | [diff] [blame^] | 46 | |
| 47 | // TODO: Destroy match! |
| 48 | destroy : function () { |
| 49 | this._matchCreator = undefined; |
| 50 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Length of the table (columns), |
| 54 | * aka the number of tokens |
| 55 | * in the snippet. |
| 56 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 57 | length : function () { |
| 58 | return this._pos; |
| 59 | }, |
| 60 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 61 | /** |
| 62 | * Get the token in the snippet |
| 63 | * At a given position. |
| 64 | * |
| 65 | * @param pos |
| 66 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 67 | getToken : function (pos) { |
| 68 | if (pos === undefined) |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 69 | return this._token; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 70 | return this._token[pos]; |
| 71 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Get the annotation of a token |
| 75 | * in the snippet based on the position, |
| 76 | * the foundry, and the layer. |
| 77 | * |
| 78 | * @param pos |
| 79 | * @param foundry |
| 80 | * @param layer |
| 81 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 82 | getValue : function (pos, foundry, layer) { |
| 83 | return this._info[pos][foundry + '/' + layer] |
| 84 | }, |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 85 | |
Akron | aeceda7 | 2018-02-02 20:44:06 +0100 | [diff] [blame] | 86 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 87 | // Parse the snippet |
| 88 | _parse : function (children) { |
| 89 | |
| 90 | // Get all children |
| 91 | for (var i in children) { |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 92 | var c = children[i]; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 93 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 94 | // Create object on position unless it exists |
| 95 | if (this._info[this._pos] === undefined) { |
| 96 | this._info[this._pos] = {}; |
| 97 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 98 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 99 | // Store at position in foundry/layer as array |
| 100 | var found = this._info[this._pos]; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 101 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 102 | // Element with title |
| 103 | if (c.nodeType === 1) { |
| 104 | if (c.getAttribute("title") && |
| 105 | _TermRE.exec(c.getAttribute("title"))) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 106 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 107 | // Fill position with info |
| 108 | var foundry, layer, value; |
| 109 | if (RegExp.$2) { |
| 110 | foundry = RegExp.$1; |
| 111 | layer = RegExp.$2; |
| 112 | } |
| 113 | else { |
| 114 | foundry = "base"; |
| 115 | layer = RegExp.$1 |
| 116 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 117 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 118 | value = RegExp.$3; |
| 119 | |
| 120 | if (found[foundry + "/" + layer] === undefined) { |
| 121 | found[foundry + "/" + layer] = [value]; |
| 122 | } |
| 123 | else { |
| 124 | if (found[foundry + "/" + layer].indexOf(value) === -1) { |
| 125 | // Push value to foundry/layer at correct position |
| 126 | found[foundry + "/" + layer].push(value); |
| 127 | }; |
| 128 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 129 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 130 | // Set foundry |
| 131 | if (this._foundry[foundry] === undefined) |
| 132 | this._foundry[foundry] = {}; |
| 133 | this._foundry[foundry][layer] = 1; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 134 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 135 | // Set layer |
| 136 | if (this._layer[layer] === undefined) |
| 137 | this._layer[layer] = {}; |
| 138 | this._layer[layer][foundry] = 1; |
| 139 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 140 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 141 | // depth search |
| 142 | if (c.hasChildNodes()) |
| 143 | this._parse(c.childNodes); |
| 144 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 145 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 146 | // Leaf node |
| 147 | // store string on position and go to next string |
| 148 | else if (c.nodeType === 3) { |
| 149 | if (c.nodeValue.match(/[a-z0-9]/i)) |
| 150 | this._token[this._pos++] = c.nodeValue; |
| 151 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | delete this._info[this._pos]; |
| 155 | }, |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * Get HTML table view of annotations. |
| 160 | */ |
| 161 | element : function () { |
| 162 | if (this._element !== undefined) |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 163 | return this._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 164 | |
| 165 | // First the legend table |
Akron | aeceda7 | 2018-02-02 20:44:06 +0100 | [diff] [blame] | 166 | var wrap = d.createElement('div'); |
| 167 | |
| 168 | var table = wrap.addE('table'); |
| 169 | |
| 170 | this._element = wrap; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 171 | |
| 172 | // Single row in head |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 173 | var tr = table.addE('thead').addE('tr'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 174 | |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 175 | var ah = KorAP.annotationHelper || { "getDesc" : function () {}}; |
| 176 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 177 | // Add cell to row |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 178 | var addCell = function (type, key, value) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 179 | var c = this.addE(type); |
| 180 | |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 181 | if (value === undefined) |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 182 | return c; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 183 | |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 184 | if (key && value instanceof Array && value[1] !== undefined) { |
Akron | 4e47d0b | 2017-07-03 17:58:37 +0200 | [diff] [blame] | 185 | |
| 186 | // There are multiple values to add |
Akron | 856af1e | 2017-07-03 19:57:46 +0200 | [diff] [blame] | 187 | c.classList.add('matchkeyvalues'); |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 188 | for (var n = 0; n < value.length; n++) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 189 | var e = c.addE('div').addT(value[n]); |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 190 | |
| 191 | var anno = ah.getDesc(key, value[n]); |
| 192 | |
| 193 | if (anno) |
| 194 | e.setAttribute("title", anno); |
Akron | 856af1e | 2017-07-03 19:57:46 +0200 | [diff] [blame] | 195 | }; |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 196 | } |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 197 | |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 198 | else { |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 199 | |
| 200 | if (value instanceof Array) |
| 201 | value = value[0]; |
| 202 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 203 | c.addT(value); |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 204 | |
| 205 | // Add tooltip |
| 206 | var anno = ah.getDesc(key, value); |
| 207 | if (anno) |
| 208 | c.setAttribute("title", anno); |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 209 | }; |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 210 | |
| 211 | return c; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | tr.addCell = addCell; |
| 215 | |
| 216 | // Add header information |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 217 | tr.addCell('th', undefined, 'Foundry'); |
| 218 | tr.addCell('th', undefined, 'Layer'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 219 | |
| 220 | // Add tokens |
| 221 | for (var i in this._token) { |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 222 | tr.addCell('th', undefined, this.getToken(i)); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 223 | }; |
Akron | 916ec25 | 2016-11-10 17:06:32 +0100 | [diff] [blame] | 224 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 225 | var tbody = table.addE('tbody'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 226 | |
| 227 | var foundryList = Object.keys(this._foundry).sort(); |
| 228 | |
| 229 | for (var f = 0; f < foundryList.length; f++) { |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 230 | var foundry = foundryList[f]; |
| 231 | var layerList = |
| 232 | Object.keys(this._foundry[foundry]).sort(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 233 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 234 | for (var l = 0; l < layerList.length; l++) { |
| 235 | var layer = layerList[l]; |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 236 | tr = tbody.addE('tr'); |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 237 | tr.setAttribute('tabindex', 0); |
| 238 | tr.addCell = addCell; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 239 | |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 240 | tr.addCell('th', undefined, foundry); |
| 241 | tr.addCell('th', undefined, layer); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 242 | |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 243 | var key = foundry + '/' + layer + '='; |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 244 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 245 | for (var v = 0; v < this.length(); v++) { |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 246 | |
| 247 | // Get the cell value |
| 248 | var value = this.getValue(v, foundry, layer); |
| 249 | |
| 250 | // Add cell to row |
| 251 | var cell = tr.addCell( |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 252 | 'td', |
Akron | f2279c4 | 2017-12-21 13:48:46 +0100 | [diff] [blame] | 253 | key, |
Akron | 8005599 | 2017-12-20 16:30:52 +0100 | [diff] [blame] | 254 | value |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 255 | ); |
| 256 | }; |
| 257 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 258 | }; |
Akron | b6685bb | 2018-02-04 00:44:47 +0100 | [diff] [blame^] | 259 | |
| 260 | // Add query creator |
| 261 | this._matchCreator = matchQueryCreator.create(this._element); |
| 262 | |
Akron | aeceda7 | 2018-02-02 20:44:06 +0100 | [diff] [blame] | 263 | return this._element; |
| 264 | }, |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 265 | }; |
| 266 | }); |