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