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