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) { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 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 | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 66 | // Append element to match |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 67 | elem.appendChild( |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 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 | d67d45b | 2017-05-18 21:47:38 +0200 | [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 { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 91 | |
| 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 | d67d45b | 2017-05-18 21:47:38 +0200 | [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 | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 113 | this._match, |
| 114 | { 'spans' : false, 'layer' : focus }, |
| 115 | |
| 116 | // Callback for retrieval |
| 117 | function (matchResponse) { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 118 | |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 119 | if (matchResponse === undefined) |
| 120 | cb(null); |
| 121 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [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 | d67d45b | 2017-05-18 21:47:38 +0200 | [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 |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 154 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 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 | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 191 | .appendChild(document.createTextNode(layer)); |
| 192 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 193 | var tree = matchtree.appendChild( |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [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 | d67d45b | 2017-05-18 21:47:38 +0200 | [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 | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 216 | tree.classList.remove('loading'); |
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 | // Something went wrong - probably log!!! |
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 | 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) |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 229 | |
| 230 | var dl = document.createElement('li'); |
| 231 | dl.className = 'download'; |
| 232 | dl.addEventListener( |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 233 | 'click', function (e) { |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 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(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 246 | } |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 247 | ); |
| 248 | actions.appendChild(dl); |
| 249 | treeObj.center(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 250 | }; |
| 251 | |
| 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 | d67d45b | 2017-05-18 21:47:38 +0200 | [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 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 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 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 286 | // Join spans and relations |
| 287 | var treeLayers = [] |
| 288 | var spans = this._match.getSpans(); |
| 289 | var rels = this._match.getRels(); |
| 290 | var i; |
| 291 | for (i in spans) { |
| 292 | treeLayers.push(spans[i]); |
| 293 | }; |
| 294 | for (i in rels) { |
| 295 | treeLayers.push(rels[i]); |
| 296 | }; |
| 297 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 298 | // Get spans |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 299 | treeLayers = treeLayers.sort( |
| 300 | function (a, b) { |
| 301 | if (a.foundry < b.foundry) { |
| 302 | return -1; |
| 303 | } |
| 304 | else if (a.foundry > b.foundry) { |
| 305 | return 1; |
| 306 | } |
| 307 | else if (a.layer < b.layer) { |
| 308 | return -1; |
| 309 | } |
| 310 | else if (a.layer > b.layer) { |
| 311 | return 1; |
| 312 | }; |
| 313 | return 0; |
| 314 | }); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 315 | |
| 316 | var menuList = []; |
| 317 | |
| 318 | // Show tree views |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 319 | for (var i = 0; i < treeLayers.length; i++) { |
| 320 | var span = treeLayers[i]; |
| 321 | |
| 322 | // Add foundry/layer to menu list |
| 323 | menuList.push([ |
| 324 | span.foundry + '/' + span.layer, |
| 325 | span.foundry, |
| 326 | span.layer |
| 327 | ]); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | // Create tree menu |
| 331 | var treemenu = this.treeMenu(menuList); |
| 332 | var span = info.appendChild(document.createElement('p')); |
| 333 | span.classList.add('addtree'); |
| 334 | span.appendChild(document.createTextNode(loc.ADDTREE)); |
| 335 | |
| 336 | var treeElement = treemenu.element(); |
| 337 | span.appendChild(treeElement); |
| 338 | |
| 339 | span.addEventListener('click', function (e) { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 340 | treemenu.show(); |
| 341 | treemenu.focus(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 342 | }); |
| 343 | |
| 344 | this._element = info; |
| 345 | |
| 346 | return info; |
| 347 | }, |
| 348 | |
| 349 | |
| 350 | /** |
| 351 | * Get tree menu. |
| 352 | * There is only one menu rendered |
| 353 | * - no matter how many trees exist |
| 354 | */ |
| 355 | treeMenu : function (list) { |
| 356 | if (this._treeMenu !== undefined) |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 357 | return this._treeMenu; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 358 | |
| 359 | return this._treeMenu = matchTreeMenuClass.create(this, list); |
| 360 | } |
| 361 | }; |
| 362 | }); |