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