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