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', |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 8 | 'match/relations', |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 9 | 'match/treemenu', |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 10 | 'match/querycreator', |
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 | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 15 | matchRelClass, |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 16 | matchTreeMenuClass, |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 17 | matchQueryCreator) { |
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 | */ |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 142 | getTree : function (foundry, layer, type, cb) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 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 | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 157 | if (type === "spans") { |
| 158 | cb(matchTreeClass.create(matchResponse["snippet"])); |
| 159 | } |
| 160 | else if (type === "rels") { |
| 161 | cb(matchRelClass.create(matchResponse["snippet"])); |
| 162 | } |
| 163 | |
| 164 | // Unknown tree type |
| 165 | else { |
| 166 | cb(null); |
| 167 | }; |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 168 | } |
| 169 | else { |
| 170 | cb(null); |
| 171 | }; |
| 172 | }.bind(this) |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 173 | ); |
| 174 | }, |
| 175 | |
| 176 | /** |
| 177 | * Destroy this match information view. |
| 178 | */ |
| 179 | destroy : function () { |
| 180 | |
| 181 | // Remove circular reference |
| 182 | if (this._treeMenu !== undefined) |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 183 | delete this._treeMenu["info"]; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 184 | |
| 185 | this._treeMenu.destroy(); |
| 186 | this._treeMenu = undefined; |
| 187 | this._match = undefined; |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 188 | this._matchCreator = undefined; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 189 | // Element destroy |
| 190 | }, |
| 191 | |
| 192 | /** |
| 193 | * Add a new tree view to the list |
| 194 | */ |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 195 | addTree : function (foundry, layer, type, cb) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 196 | var matchtree = document.createElement('div'); |
| 197 | matchtree.classList.add('matchtree'); |
| 198 | |
| 199 | var h6 = matchtree.appendChild(document.createElement('h6')); |
| 200 | h6.appendChild(document.createElement('span')) |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 201 | .appendChild(document.createTextNode(foundry)); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 202 | h6.appendChild(document.createElement('span')) |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 203 | .appendChild(document.createTextNode(layer)); |
| 204 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 205 | var tree = matchtree.appendChild( |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 206 | document.createElement('div') |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 207 | ); |
| 208 | |
| 209 | this._element.insertBefore(matchtree, this._element.lastChild); |
| 210 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 211 | var actions = tree.appendChild(document.createElement('ul')); |
| 212 | actions.classList.add('action', 'image'); |
| 213 | var close = actions.appendChild(document.createElement('li')); |
| 214 | close.className = 'close'; |
| 215 | close.appendChild(document.createElement('span')); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 216 | close.addEventListener( |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 217 | 'click', function (e) { |
| 218 | matchtree.parentNode.removeChild(matchtree); |
| 219 | e.halt(); |
| 220 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 221 | ); |
| 222 | |
Nils Diewald | 0ec142f | 2015-05-05 00:29:23 +0000 | [diff] [blame] | 223 | tree.classList.add('loading'); |
| 224 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 225 | // Get tree data async |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 226 | this.getTree(foundry, layer, type, function (treeObj) { |
Nils Diewald | 0ec142f | 2015-05-05 00:29:23 +0000 | [diff] [blame] | 227 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 228 | tree.classList.remove('loading'); |
Nils Diewald | 0ec142f | 2015-05-05 00:29:23 +0000 | [diff] [blame] | 229 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 230 | // Something went wrong - probably log!!! |
Nils Diewald | 0ec142f | 2015-05-05 00:29:23 +0000 | [diff] [blame] | 231 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 232 | if (treeObj === null) { |
| 233 | tree.appendChild(document.createTextNode('No data available.')); |
| 234 | } |
| 235 | else { |
| 236 | tree.appendChild(treeObj.element()); |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 237 | treeObj.show(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 238 | // Reposition the view to the center |
| 239 | // (This may in a future release be a reposition |
| 240 | // to move the root into the center or the actual |
| 241 | // match) |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 242 | |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 243 | // This is currently not supported by relations |
| 244 | if (type === "spans") { |
| 245 | var dl = document.createElement('li'); |
| 246 | dl.className = 'download'; |
| 247 | dl.addEventListener( |
| 248 | 'click', function (e) { |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 249 | |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 250 | var a = document.createElement('a'); |
| 251 | a.setAttribute('href-lang', 'image/svg+xml'); |
| 252 | a.setAttribute('href', 'data:image/svg+xml;base64,'+treeObj.toBase64()); |
| 253 | a.setAttribute('download', 'tree.svg'); |
| 254 | a.target = '_blank'; |
| 255 | |
| 256 | document.body.appendChild(a); |
| 257 | a.click(); |
| 258 | document.body.removeChild(a) |
| 259 | e.halt(); |
| 260 | } |
| 261 | ); |
| 262 | |
| 263 | actions.appendChild(dl); |
| 264 | }; |
| 265 | |
Akron | c56cf2d | 2016-11-09 22:02:38 +0100 | [diff] [blame] | 266 | treeObj.center(); |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | if (cb !== undefined) |
| 270 | cb(treeObj); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 271 | }); |
| 272 | }, |
| 273 | |
| 274 | /** |
| 275 | * Create match information view. |
| 276 | */ |
| 277 | element : function () { |
| 278 | |
| 279 | if (this._element !== undefined) |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 280 | return this._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 281 | |
| 282 | // Create info table |
| 283 | var info = document.createElement('div'); |
| 284 | info.classList.add('matchinfo'); |
| 285 | |
| 286 | // Append default table |
| 287 | var matchtable = document.createElement('div'); |
Nils Diewald | 0ec142f | 2015-05-05 00:29:23 +0000 | [diff] [blame] | 288 | matchtable.classList.add('matchtable', 'loading'); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 289 | info.appendChild(matchtable); |
| 290 | |
| 291 | // Create the table asynchronous |
| 292 | this.getTable(undefined, function (table) { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 293 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 294 | if (table !== null) { |
Akron | 3bb91bc | 2016-12-02 16:43:17 +0100 | [diff] [blame] | 295 | matchtable.appendChild(table.element()); |
| 296 | }; |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 297 | matchtable.classList.remove('loading'); |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 298 | |
| 299 | // Add query creator |
Akron | e8ea000 | 2017-06-28 18:51:52 +0200 | [diff] [blame] | 300 | this._matchCreator = matchQueryCreator.create(info); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 301 | }); |
| 302 | |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 303 | // Join spans and relations |
| 304 | var treeLayers = [] |
| 305 | var spans = this._match.getSpans(); |
| 306 | var rels = this._match.getRels(); |
| 307 | var i; |
| 308 | for (i in spans) { |
| 309 | treeLayers.push(spans[i]); |
| 310 | }; |
| 311 | for (i in rels) { |
| 312 | treeLayers.push(rels[i]); |
| 313 | }; |
| 314 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 315 | // Get spans |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 316 | treeLayers = treeLayers.sort( |
| 317 | function (a, b) { |
| 318 | if (a.foundry < b.foundry) { |
| 319 | return -1; |
| 320 | } |
| 321 | else if (a.foundry > b.foundry) { |
| 322 | return 1; |
| 323 | } |
| 324 | else if (a.layer < b.layer) { |
| 325 | return -1; |
| 326 | } |
| 327 | else if (a.layer > b.layer) { |
| 328 | return 1; |
| 329 | }; |
| 330 | return 0; |
| 331 | }); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 332 | |
| 333 | var menuList = []; |
| 334 | |
| 335 | // Show tree views |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 336 | for (var i = 0; i < treeLayers.length; i++) { |
| 337 | var span = treeLayers[i]; |
| 338 | |
| 339 | // Add foundry/layer to menu list |
| 340 | menuList.push([ |
| 341 | span.foundry + '/' + span.layer, |
| 342 | span.foundry, |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame^] | 343 | span.layer, |
| 344 | span.type |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 345 | ]); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | // Create tree menu |
| 349 | var treemenu = this.treeMenu(menuList); |
| 350 | var span = info.appendChild(document.createElement('p')); |
| 351 | span.classList.add('addtree'); |
| 352 | span.appendChild(document.createTextNode(loc.ADDTREE)); |
| 353 | |
| 354 | var treeElement = treemenu.element(); |
| 355 | span.appendChild(treeElement); |
| 356 | |
| 357 | span.addEventListener('click', function (e) { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 358 | treemenu.show(); |
| 359 | treemenu.focus(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 360 | }); |
| 361 | |
| 362 | this._element = info; |
| 363 | |
| 364 | return info; |
| 365 | }, |
| 366 | |
| 367 | |
| 368 | /** |
| 369 | * Get tree menu. |
| 370 | * There is only one menu rendered |
| 371 | * - no matter how many trees exist |
| 372 | */ |
| 373 | treeMenu : function (list) { |
| 374 | if (this._treeMenu !== undefined) |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 375 | return this._treeMenu; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 376 | |
| 377 | return this._treeMenu = matchTreeMenuClass.create(this, list); |
| 378 | } |
| 379 | }; |
| 380 | }); |