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