blob: 628659f38061ea15da7581f8b6472028f372c8fe [file] [log] [blame]
Akron99713ef2017-06-28 18:19:28 +02001/**
2 * Information about a match.
3 */
Akronbec4a6a2018-07-10 14:45:15 +02004/*
5 * TODO:
6 * Create a "views" object, that is the parent of this
7 * class and supports a simple .add() method to add views
8 * to an element.
9 */
Nils Diewald7c8ced22015-04-15 19:21:00 +000010define([
11 'match/infolayer',
12 'match/table',
Akron41387d22018-02-02 18:10:06 +010013 'match/treehierarchy',
14 'match/treearc',
Akron151bc872018-02-02 14:04:15 +010015 'match/meta',
Nils Diewald7c8ced22015-04-15 19:21:00 +000016 'util'
17], function (infoLayerClass,
Akron3bb91bc2016-12-02 16:43:17 +010018 matchTableClass,
Akron41387d22018-02-02 18:10:06 +010019 matchTreeHierarchyClass,
20 matchTreeArcClass,
Akronb6685bb2018-02-04 00:44:47 +010021 matchMetaClass) {
Akron3bb91bc2016-12-02 16:43:17 +010022
Nils Diewald7148c6f2015-05-04 15:07:53 +000023 // Override
Nils Diewald0e6992a2015-04-14 20:13:52 +000024 KorAP.API.getMatchInfo = KorAP.API.getMatchInfo || function () {
25 KorAP.log(0, 'KorAP.API.getMatchInfo() not implemented')
26 return {};
27 };
28
Akron0b489ad2018-02-02 16:49:32 +010029 const loc = KorAP.Locale;
30 const d = document;
Nils Diewald0e6992a2015-04-14 20:13:52 +000031
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +000033
34 /**
35 * Create new match object
36 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000037 create : function (match) {
38 return Object.create(this)._init(match);
39 },
40
Akron151bc872018-02-02 14:04:15 +010041
Nils Diewald0e6992a2015-04-14 20:13:52 +000042 /**
43 * Initialize object
44 */
45 _init : function (match) {
46 this._match = match;
Akronedbf33a2018-02-05 19:18:03 +010047 this._visibleTable = false;
48 this._visibleMeta = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +000049 this.opened = false;
50 return this;
51 },
52
Akron151bc872018-02-02 14:04:15 +010053
Nils Diewald0e6992a2015-04-14 20:13:52 +000054 /**
55 * Get match object
56 */
57 match : function () {
58 return this._match;
59 },
60
Nils Diewald7148c6f2015-05-04 15:07:53 +000061
62 /**
63 * Open the information view,
64 * if closed, otherwise close.
65 */
Akronbd342982018-01-25 18:01:46 +010066 /*
Nils Diewald0e6992a2015-04-14 20:13:52 +000067 toggle : function () {
Akron3bb91bc2016-12-02 16:43:17 +010068
Akron08b82d62016-12-05 15:06:05 +010069 var elem = this._match.element();
Akron3bb91bc2016-12-02 16:43:17 +010070
71 if (this.opened == true) {
Akrond67d45b2017-05-18 21:47:38 +020072 elem.removeChild(
73 this.element()
74 );
75 this.opened = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +000076 }
77 else {
Akrond67d45b2017-05-18 21:47:38 +020078 // Append element to match
Akron3bb91bc2016-12-02 16:43:17 +010079 elem.appendChild(
Akrond67d45b2017-05-18 21:47:38 +020080 this.element()
81 );
82 this.opened = true;
Nils Diewald0e6992a2015-04-14 20:13:52 +000083 };
84
85 return this.opened;
86 },
Akronbd342982018-01-25 18:01:46 +010087 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000088
89
90 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000091 * Retrieve and parse snippet for table
92 * representation
Nils Diewald0e6992a2015-04-14 20:13:52 +000093 */
Akron151bc872018-02-02 14:04:15 +010094 getTableData : function (tokens, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000095 var focus = [];
96
97 // Get all tokens
98 if (tokens === undefined) {
Akrond67d45b2017-05-18 21:47:38 +020099 focus = this._match.getTokens();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000100 }
101
102 // Get only some tokens
103 else {
Akrond67d45b2017-05-18 21:47:38 +0200104
105 // Push newly to focus array
106 for (var i = 0; i < tokens.length; i++) {
107 var term = tokens[i];
108 try {
109 // Create info layer objects
110 var layer = infoLayerClass.create(term);
111 layer.type = "tokens";
112 focus.push(layer);
113 }
114 catch (e) {
115 continue;
116 };
117 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000118 };
119
120 // No tokens chosen
121 if (focus.length == 0)
Akrond67d45b2017-05-18 21:47:38 +0200122 cb(null);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000123
Akron4f791442018-02-12 13:25:47 +0100124 try {
125 // Get info (may be cached)
126 KorAP.API.getMatchInfo(
127 this._match,
128 { 'spans' : false, 'layer' : focus },
Akrond67d45b2017-05-18 21:47:38 +0200129
Akron4f791442018-02-12 13:25:47 +0100130 // Callback for retrieval
131 function (matchResponse) {
Akron3bb91bc2016-12-02 16:43:17 +0100132
Akron4f791442018-02-12 13:25:47 +0100133 if (matchResponse === undefined)
134 cb(null);
Akron515851a2017-05-02 12:53:17 +0200135
Akron4f791442018-02-12 13:25:47 +0100136 // Get snippet from match info
137 if (matchResponse["snippet"] !== undefined) {
138 this._table = matchTableClass.create(matchResponse["snippet"]);
139 cb(this._table);
140 };
141 }.bind(this)
142 );
143 }
144 catch (e) {
145 KorAP.log(0, e);
146 cb(null);
147 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000148
149 /*
150 // Todo: Store the table as a hash of the focus
151 return null;
152 */
153 },
Akronbd342982018-01-25 18:01:46 +0100154
155
Akron4f791442018-02-12 13:25:47 +0100156 /**
157 * Receive meta data from server.
158 */
Akron0ad7cd22018-02-08 18:03:06 +0100159 getMetaData : function (cb) {
Akronb5d05d72018-02-12 15:09:12 +0100160
161 var match = this._match;
162
Akron4f791442018-02-12 13:25:47 +0100163 try {
164 KorAP.API.getTextInfo(
Akronb5d05d72018-02-12 15:09:12 +0100165 match, {}, function (textResponse) {
166
Akron4f791442018-02-12 13:25:47 +0100167 if (textResponse === undefined) {
168 cb(null);
169 return;
170 };
Akron0ad7cd22018-02-08 18:03:06 +0100171
Akron4f791442018-02-12 13:25:47 +0100172 var doc = textResponse["document"];
Akron0ad7cd22018-02-08 18:03:06 +0100173
Akron4f791442018-02-12 13:25:47 +0100174 if (doc === undefined) {
175 cb(null);
176 return;
177 };
Akron0ad7cd22018-02-08 18:03:06 +0100178
Akron4f791442018-02-12 13:25:47 +0100179 var fields = doc["fields"];
180 if (fields === undefined) {
181 cb(null);
182 return;
183 };
Akron0ad7cd22018-02-08 18:03:06 +0100184
Akron4f791442018-02-12 13:25:47 +0100185 // Add metainfo to matchview
186 cb(matchMetaClass.create(
Akronb5d05d72018-02-12 15:09:12 +0100187 match, fields
Akron4f791442018-02-12 13:25:47 +0100188 ));
189 }
190 );
191 }
192 catch (e) {
193 KorAP.log(0, e);
194 cb(null);
195 };
Akronbd342982018-01-25 18:01:46 +0100196 },
Nils Diewald0e6992a2015-04-14 20:13:52 +0000197
198
199 /**
200 * Retrieve and parse snippet for tree representation
201 */
Akron151bc872018-02-02 14:04:15 +0100202 getTreeData : function (foundry, layer, type, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000203 var focus = [];
Akron0ad7cd22018-02-08 18:03:06 +0100204
Akron4f791442018-02-12 13:25:47 +0100205 try {
206 // TODO: Support and cache multiple trees
207 KorAP.API.getMatchInfo(
208 this._match, {
209 'spans' : true,
210 'foundry' : foundry,
211 'layer' : layer
212 },
213 function (matchResponse) {
214 if (matchResponse === undefined) {
215 cb(null);
216 return;
217 };
Akronc56cf2d2016-11-09 22:02:38 +0100218
Akron4f791442018-02-12 13:25:47 +0100219 // Get snippet from match info
220 if (matchResponse["snippet"] !== undefined) {
221 // Todo: This should be cached somehow
222
223 if (type === "spans") {
224 cb(matchTreeHierarchyClass.create(matchResponse["snippet"]));
225 }
226 else if (type === "rels") {
227 cb(matchTreeArcClass.create(matchResponse["snippet"]));
228 }
229
230 // Unknown tree type
231 else {
232 cb(null);
233 };
Akron0988d882017-11-10 16:13:12 +0100234 }
Akron0988d882017-11-10 16:13:12 +0100235 else {
236 cb(null);
237 };
Akron4f791442018-02-12 13:25:47 +0100238 }.bind(this)
239 );
240 }
241 catch (e) {
242 KorAP.log(0, e);
243 cb(null);
244 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000245 },
246
Akron151bc872018-02-02 14:04:15 +0100247
Nils Diewald0e6992a2015-04-14 20:13:52 +0000248 /**
249 * Destroy this match information view.
250 */
251 destroy : function () {
252
253 // Remove circular reference
Akron8b592d42018-01-26 18:33:06 +0100254 /*
Nils Diewald0e6992a2015-04-14 20:13:52 +0000255 if (this._treeMenu !== undefined)
Akron99713ef2017-06-28 18:19:28 +0200256 delete this._treeMenu["info"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000257
258 this._treeMenu.destroy();
259 this._treeMenu = undefined;
Akron8b592d42018-01-26 18:33:06 +0100260 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000261 this._match = undefined;
Akron99713ef2017-06-28 18:19:28 +0200262 this._matchCreator = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000263 // Element destroy
264 },
265
Akron151bc872018-02-02 14:04:15 +0100266
Nils Diewald0e6992a2015-04-14 20:13:52 +0000267 /**
268 * Add a new tree view to the list
269 */
Akron151bc872018-02-02 14:04:15 +0100270 showTree : function (foundry, layer, type, cb) {
Akron0b489ad2018-02-02 16:49:32 +0100271 var matchtree = d.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100272 matchtree.classList.add('matchtree', 'loading');
273
274 this.element().appendChild(matchtree);
Akrond67d45b2017-05-18 21:47:38 +0200275
Akron151bc872018-02-02 14:04:15 +0100276 // Add title line
277 var h6 = matchtree.addE('h6');
278 h6.addE('span').addT(foundry);
279 h6.addE('span').addT(layer);
280
281 var tree = matchtree.addE('div');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000282
Akron151bc872018-02-02 14:04:15 +0100283 // Add close action button
Akronc8eb4a12018-02-03 00:39:58 +0100284 var actions = this._addButton('close', matchtree, function (e) {
285 this.parentNode.removeChild(this);
286 e.halt();
287 });
Nils Diewald0e6992a2015-04-14 20:13:52 +0000288
Akronc8eb4a12018-02-03 00:39:58 +0100289 // tree.classList.add('loading'); // alternatively
Nils Diewald0ec142f2015-05-05 00:29:23 +0000290
Nils Diewald0e6992a2015-04-14 20:13:52 +0000291 // Get tree data async
Akron151bc872018-02-02 14:04:15 +0100292 this.getTreeData(foundry, layer, type, function (treeObj) {
Akronc8eb4a12018-02-03 00:39:58 +0100293 matchtree.classList.remove('loading');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000294
Akrond67d45b2017-05-18 21:47:38 +0200295 // Something went wrong - probably log!!!
Nils Diewald0ec142f2015-05-05 00:29:23 +0000296
Akrond67d45b2017-05-18 21:47:38 +0200297 if (treeObj === null) {
Akron151bc872018-02-02 14:04:15 +0100298 tree.addT('No data available.');
Akrond67d45b2017-05-18 21:47:38 +0200299 }
300 else {
301 tree.appendChild(treeObj.element());
Akron0988d882017-11-10 16:13:12 +0100302 treeObj.show();
Akron151bc872018-02-02 14:04:15 +0100303
Akrond67d45b2017-05-18 21:47:38 +0200304 // Reposition the view to the center
305 // (This may in a future release be a reposition
Akron151bc872018-02-02 14:04:15 +0100306 // to move the root to the actual match)
Akronc56cf2d2016-11-09 22:02:38 +0100307
Akron0988d882017-11-10 16:13:12 +0100308 // This is currently not supported by relations
309 if (type === "spans") {
Akron0b489ad2018-02-02 16:49:32 +0100310 var dl = d.createElement('li');
Akron0988d882017-11-10 16:13:12 +0100311 dl.className = 'download';
312 dl.addEventListener(
313 'click', function (e) {
Akron151bc872018-02-02 14:04:15 +0100314 var a = treeObj.downloadLink();
Akron0b489ad2018-02-02 16:49:32 +0100315 d.body.appendChild(a);
Akron0988d882017-11-10 16:13:12 +0100316 a.click();
Akron0b489ad2018-02-02 16:49:32 +0100317 d.body.removeChild(a)
Akron0988d882017-11-10 16:13:12 +0100318 e.halt();
319 }
320 );
321
322 actions.appendChild(dl);
323 };
324
Akronc56cf2d2016-11-09 22:02:38 +0100325 treeObj.center();
Akrond67d45b2017-05-18 21:47:38 +0200326 };
327
328 if (cb !== undefined)
329 cb(treeObj);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000330 });
Akronc8eb4a12018-02-03 00:39:58 +0100331 matchtree.classList.remove('loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000332 },
Akronbd342982018-01-25 18:01:46 +0100333
334
Akrone7679692018-01-26 12:06:33 +0100335 // Add meta information to match
Akron151bc872018-02-02 14:04:15 +0100336 showMeta : function () {
Akronedbf33a2018-02-05 19:18:03 +0100337
338 // Already visible
339 if (this._visibleMeta)
340 return;
341
342 this._visibleMeta = true;
343
Akronaeceda72018-02-02 20:44:06 +0100344 var metaTable = document.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100345 metaTable.classList.add('metatable', 'loading');
346 this.element().appendChild(metaTable);
Akronbd342982018-01-25 18:01:46 +0100347
Akron0ad7cd22018-02-08 18:03:06 +0100348 /*
349 * This was temporary
Akronbd342982018-01-25 18:01:46 +0100350 var metaInfo = this._match.element().getAttribute('data-info');
Akronbd342982018-01-25 18:01:46 +0100351 if (metaInfo)
352 metaInfo = JSON.parse(metaInfo);
Akron0ad7cd22018-02-08 18:03:06 +0100353 */
Akronedbf33a2018-02-05 19:18:03 +0100354 var that = this;
355
Akron0ad7cd22018-02-08 18:03:06 +0100356 this.getMetaData(function (meta) {
Akronb5d05d72018-02-12 15:09:12 +0100357
358 if (meta === null)
359 return;
360
Akronc8eb4a12018-02-03 00:39:58 +0100361 // Load data
362 metaTable.classList.remove('loading');
363
Akron0ad7cd22018-02-08 18:03:06 +0100364 metaTable.appendChild(meta.element());
Akronaeceda72018-02-02 20:44:06 +0100365
366 // Add button
Akron0ad7cd22018-02-08 18:03:06 +0100367 that._addButton('close', metaTable, function (e) {
Akronaeceda72018-02-02 20:44:06 +0100368 this.parentNode.removeChild(this);
Akronedbf33a2018-02-05 19:18:03 +0100369 that._visibleMeta = false;
Akronaeceda72018-02-02 20:44:06 +0100370 e.halt();
371 });
Akron0ad7cd22018-02-08 18:03:06 +0100372 });
Akron41387d22018-02-02 18:10:06 +0100373
Akron0ad7cd22018-02-08 18:03:06 +0100374 // Do not load any longer
Akronaeceda72018-02-02 20:44:06 +0100375 metaTable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100376 },
377
Akron151bc872018-02-02 14:04:15 +0100378
Akronbd342982018-01-25 18:01:46 +0100379 // Add table
Akron151bc872018-02-02 14:04:15 +0100380 showTable : function () {
Akronbd342982018-01-25 18:01:46 +0100381
Akronedbf33a2018-02-05 19:18:03 +0100382 // Already visible
383 if (this._visibleTable)
384 return;
385
386 this._visibleTable = true;
387
Nils Diewald0e6992a2015-04-14 20:13:52 +0000388 // Append default table
Akron0b489ad2018-02-02 16:49:32 +0100389 var matchtable = d.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100390 matchtable.classList.add('matchtable', 'loading');
Akronaeceda72018-02-02 20:44:06 +0100391 var info = this.element();
392 info.appendChild(matchtable);
393
Akronedbf33a2018-02-05 19:18:03 +0100394 var that = this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000395
Akronb6685bb2018-02-04 00:44:47 +0100396 // TODO:
397 // Create try-catch-exception-handling
398
Nils Diewald0e6992a2015-04-14 20:13:52 +0000399 // Create the table asynchronous
Akron151bc872018-02-02 14:04:15 +0100400 this.getTableData(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100401
Akronc8eb4a12018-02-03 00:39:58 +0100402 // Load data
403 matchtable.classList.remove('loading');
404
Akrond67d45b2017-05-18 21:47:38 +0200405 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100406 matchtable.appendChild(table.element());
407 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000408 });
Akronaeceda72018-02-02 20:44:06 +0100409
410 // Add button
411 this._addButton('close', matchtable, function (e) {
412 this.parentNode.removeChild(this);
Akronedbf33a2018-02-05 19:18:03 +0100413 that._visibleTable = false;
Akronaeceda72018-02-02 20:44:06 +0100414 e.halt();
415 });
416
417 // Load data
418 matchtable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100419 },
420
Akronc8eb4a12018-02-03 00:39:58 +0100421 // Add action button
Akronaeceda72018-02-02 20:44:06 +0100422 _addButton : function (buttonType, element, cb) {
423 // TODO: Unless existent
424 var actions = document.createElement('ul');
425 actions.classList.add('action', 'image');
426 var b = actions.addE('li');
427 b.className = buttonType;
428 b.addE('span').addT(buttonType);
429 b.addEventListener(
430 'click', cb.bind(element)
431 );
432
433 element.appendChild(actions);
434 return actions;
435 },
436
437
Akronbd342982018-01-25 18:01:46 +0100438 /**
439 * Create match information view.
440 */
441 element : function () {
442
443 if (this._element !== undefined)
444 return this._element;
445
446 // Create info table
Akron0b489ad2018-02-02 16:49:32 +0100447 var info = d.createElement('div');
Akronbd342982018-01-25 18:01:46 +0100448 info.classList.add('matchinfo');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000449
450 this._element = info;
451
Akronbd342982018-01-25 18:01:46 +0100452 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000453 }
454 };
455});