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