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