Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Information about a match. |
| 3 | */ |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 4 | define([ |
| 5 | 'match/infolayer', |
| 6 | 'match/table', |
| 7 | 'match/tree', |
| 8 | 'match/treemenu', |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 9 | 'match/querycreator', |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 10 | 'util' |
| 11 | ], function (infoLayerClass, |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 12 | matchTableClass, |
| 13 | matchTreeClass, |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 14 | matchTreeMenuClass, |
| 15 | matchQueryCreator) { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 16 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 17 | // Override |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 18 | KorAP.API.getMatchInfo = KorAP.API.getMatchInfo || function () { |
| 19 | KorAP.log(0, 'KorAP.API.getMatchInfo() not implemented') |
| 20 | return {}; |
| 21 | }; |
| 22 | |
| 23 | var loc = KorAP.Locale; |
| 24 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 25 | return { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Create new match object |
| 29 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 30 | create : function (match) { |
| 31 | return Object.create(this)._init(match); |
| 32 | }, |
| 33 | |
| 34 | /** |
| 35 | * Initialize object |
| 36 | */ |
| 37 | _init : function (match) { |
| 38 | this._match = match; |
| 39 | this.opened = false; |
| 40 | return this; |
| 41 | }, |
| 42 | |
| 43 | /** |
| 44 | * Get match object |
| 45 | */ |
| 46 | match : function () { |
| 47 | return this._match; |
| 48 | }, |
| 49 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Open the information view, |
| 53 | * if closed, otherwise close. |
| 54 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 55 | toggle : function () { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 56 | |
Akron | 08b82d6 | 2016-12-05 15:06:05 +0100 | [diff] [blame] | 57 | var elem = this._match.element(); |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 58 | |
| 59 | if (this.opened == true) { |
| 60 | elem.removeChild( |
| 61 | this.element() |
| 62 | ); |
| 63 | this.opened = false; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 64 | } |
| 65 | else { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 66 | // Append element to match |
| 67 | elem.appendChild( |
| 68 | this.element() |
| 69 | ); |
| 70 | this.opened = true; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | return this.opened; |
| 74 | }, |
| 75 | |
| 76 | |
| 77 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 78 | * Retrieve and parse snippet for table |
| 79 | * representation |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 80 | */ |
| 81 | getTable : function (tokens, cb) { |
| 82 | var focus = []; |
| 83 | |
| 84 | // Get all tokens |
| 85 | if (tokens === undefined) { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 86 | focus = this._match.getTokens(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Get only some tokens |
| 90 | else { |
| 91 | |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 92 | // Push newly to focus array |
| 93 | for (var i = 0; i < tokens.length; i++) { |
| 94 | var term = tokens[i]; |
| 95 | try { |
| 96 | // Create info layer objects |
| 97 | var layer = infoLayerClass.create(term); |
| 98 | layer.type = "tokens"; |
| 99 | focus.push(layer); |
| 100 | } |
| 101 | catch (e) { |
| 102 | continue; |
| 103 | }; |
| 104 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | // No tokens chosen |
| 108 | if (focus.length == 0) |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 109 | cb(null); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 110 | |
| 111 | // Get info (may be cached) |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 112 | KorAP.API.getMatchInfo( |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 113 | this._match, |
| 114 | { 'spans' : false, 'layer' : focus }, |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 115 | |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 116 | // Callback for retrieval |
| 117 | function (matchResponse) { |
| 118 | |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 119 | if (matchResponse === undefined) |
| 120 | cb(null); |
| 121 | |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 122 | // Get snippet from match info |
| 123 | if (matchResponse["snippet"] !== undefined) { |
| 124 | this._table = matchTableClass.create(matchResponse["snippet"]); |
| 125 | cb(this._table); |
| 126 | }; |
| 127 | }.bind(this) |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 128 | ); |
| 129 | |
| 130 | /* |
| 131 | // Todo: Store the table as a hash of the focus |
| 132 | return null; |
| 133 | */ |
| 134 | }, |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Retrieve and parse snippet for tree representation |
| 139 | */ |
| 140 | getTree : function (foundry, layer, cb) { |
| 141 | var focus = []; |
| 142 | |
| 143 | // TODO: Support and cache multiple trees |
| 144 | KorAP.API.getMatchInfo( |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 145 | this._match, { |
| 146 | 'spans' : true, |
| 147 | 'foundry' : foundry, |
| 148 | 'layer' : layer |
| 149 | }, |
| 150 | function (matchResponse) { |
| 151 | // Get snippet from match info |
| 152 | if (matchResponse["snippet"] !== undefined) { |
| 153 | // Todo: This should be cached somehow |
| 154 | |
| 155 | cb(matchTreeClass.create(matchResponse["snippet"])); |
| 156 | } |
| 157 | else { |
| 158 | cb(null); |
| 159 | }; |
| 160 | }.bind(this) |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 161 | ); |
| 162 | }, |
| 163 | |
| 164 | /** |
| 165 | * Destroy this match information view. |
| 166 | */ |
| 167 | destroy : function () { |
| 168 | |
| 169 | // Remove circular reference |
| 170 | if (this._treeMenu !== undefined) |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 171 | delete this._treeMenu["info"]; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 172 | |
| 173 | this._treeMenu.destroy(); |
| 174 | this._treeMenu = undefined; |
| 175 | this._match = undefined; |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 176 | this._matchCreator = undefined; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 177 | // Element destroy |
| 178 | }, |
| 179 | |
| 180 | /** |
| 181 | * Add a new tree view to the list |
| 182 | */ |
| 183 | addTree : function (foundry, layer, cb) { |
| 184 | var matchtree = document.createElement('div'); |
| 185 | matchtree.classList.add('matchtree'); |
| 186 | |
| 187 | var h6 = matchtree.appendChild(document.createElement('h6')); |
| 188 | h6.appendChild(document.createElement('span')) |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 189 | .appendChild(document.createTextNode(foundry)); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 190 | h6.appendChild(document.createElement('span')) |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 191 | .appendChild(document.createTextNode(layer)); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 192 | |
| 193 | var tree = matchtree.appendChild( |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 194 | document.createElement('div') |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 195 | ); |
| 196 | |
| 197 | this._element.insertBefore(matchtree, this._element.lastChild); |
| 198 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 199 | var actions = tree.appendChild(document.createElement('ul')); |
| 200 | actions.classList.add('action', 'image'); |
| 201 | var close = actions.appendChild(document.createElement('li')); |
| 202 | close.className = 'close'; |
| 203 | close.appendChild(document.createElement('span')); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 204 | close.addEventListener( |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 205 | 'click', function (e) { |
| 206 | matchtree.parentNode.removeChild(matchtree); |
| 207 | e.halt(); |
| 208 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 209 | ); |
| 210 | |
Nils Diewald | 0ec142f | 2015-05-05 00:29:23 +0000 | [diff] [blame] | 211 | tree.classList.add('loading'); |
| 212 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 213 | // Get tree data async |
| 214 | this.getTree(foundry, layer, function (treeObj) { |
Nils Diewald | 0ec142f | 2015-05-05 00:29:23 +0000 | [diff] [blame] | 215 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 216 | tree.classList.remove('loading'); |
Nils Diewald | 0ec142f | 2015-05-05 00:29:23 +0000 | [diff] [blame] | 217 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 218 | // Something went wrong - probably log!!! |
Nils Diewald | 0ec142f | 2015-05-05 00:29:23 +0000 | [diff] [blame] | 219 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 220 | if (treeObj === null) { |
| 221 | tree.appendChild(document.createTextNode('No data available.')); |
| 222 | } |
| 223 | else { |
| 224 | tree.appendChild(treeObj.element()); |
| 225 | // Reposition the view to the center |
| 226 | // (This may in a future release be a reposition |
| 227 | // to move the root into the center or the actual |
| 228 | // match) |
| 229 | |
| 230 | var dl = document.createElement('li'); |
| 231 | dl.className = 'download'; |
| 232 | dl.addEventListener( |
| 233 | 'click', function (e) { |
| 234 | |
| 235 | var a = document.createElement('a'); |
| 236 | a.setAttribute('href-lang', 'image/svg+xml'); |
| 237 | a.setAttribute('href', 'data:image/svg+xml;base64,'+treeObj.toBase64()); |
| 238 | a.setAttribute('download', 'tree.svg'); |
| 239 | a.target = '_blank'; |
| 240 | |
| 241 | document.body.appendChild(a); |
| 242 | a.click(); |
| 243 | document.body.removeChild(a) |
| 244 | |
| 245 | e.halt(); |
| 246 | } |
| 247 | ); |
| 248 | actions.appendChild(dl); |
| 249 | treeObj.center(); |
| 250 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 251 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 252 | if (cb !== undefined) |
| 253 | cb(treeObj); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 254 | }); |
| 255 | }, |
| 256 | |
| 257 | /** |
| 258 | * Create match information view. |
| 259 | */ |
| 260 | element : function () { |
| 261 | |
| 262 | if (this._element !== undefined) |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 263 | return this._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 264 | |
| 265 | // Create info table |
| 266 | var info = document.createElement('div'); |
| 267 | info.classList.add('matchinfo'); |
| 268 | |
| 269 | // Append default table |
| 270 | var matchtable = document.createElement('div'); |
Nils Diewald | 0ec142f | 2015-05-05 00:29:23 +0000 | [diff] [blame] | 271 | matchtable.classList.add('matchtable', 'loading'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 272 | info.appendChild(matchtable); |
| 273 | |
| 274 | // Create the table asynchronous |
| 275 | this.getTable(undefined, function (table) { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 276 | |
| 277 | if (table !== null) { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 278 | matchtable.appendChild(table.element()); |
| 279 | }; |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 280 | matchtable.classList.remove('loading'); |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 281 | |
| 282 | // Add query creator |
Akron | e8ea000 | 2017-06-28 18:51:52 +0200 | [diff] [blame] | 283 | this._matchCreator = matchQueryCreator.create(info); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 284 | }); |
| 285 | |
| 286 | // Get spans |
| 287 | var spanLayers = this._match.getSpans().sort( |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 288 | function (a, b) { |
| 289 | if (a.foundry < b.foundry) { |
| 290 | return -1; |
| 291 | } |
| 292 | else if (a.foundry > b.foundry) { |
| 293 | return 1; |
| 294 | } |
| 295 | else if (a.layer < b.layer) { |
| 296 | return -1; |
| 297 | } |
| 298 | else if (a.layer > b.layer) { |
| 299 | return 1; |
| 300 | }; |
| 301 | return 0; |
| 302 | }); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 303 | |
| 304 | var menuList = []; |
| 305 | |
| 306 | // Show tree views |
| 307 | for (var i = 0; i < spanLayers.length; i++) { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 308 | var span = spanLayers[i]; |
| 309 | |
| 310 | // Add foundry/layer to menu list |
| 311 | menuList.push([ |
| 312 | span.foundry + '/' + span.layer, |
| 313 | span.foundry, |
| 314 | span.layer |
| 315 | ]); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 316 | }; |
| 317 | |
| 318 | // Create tree menu |
| 319 | var treemenu = this.treeMenu(menuList); |
| 320 | var span = info.appendChild(document.createElement('p')); |
| 321 | span.classList.add('addtree'); |
| 322 | span.appendChild(document.createTextNode(loc.ADDTREE)); |
| 323 | |
| 324 | var treeElement = treemenu.element(); |
| 325 | span.appendChild(treeElement); |
| 326 | |
| 327 | span.addEventListener('click', function (e) { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 328 | treemenu.show(); |
| 329 | treemenu.focus(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 330 | }); |
| 331 | |
| 332 | this._element = info; |
| 333 | |
| 334 | return info; |
| 335 | }, |
| 336 | |
| 337 | |
| 338 | /** |
| 339 | * Get tree menu. |
| 340 | * There is only one menu rendered |
| 341 | * - no matter how many trees exist |
| 342 | */ |
| 343 | treeMenu : function (list) { |
| 344 | if (this._treeMenu !== undefined) |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 345 | return this._treeMenu; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 346 | |
| 347 | return this._treeMenu = matchTreeMenuClass.create(this, list); |
| 348 | } |
| 349 | }; |
| 350 | }); |