blob: 4d8ea9dfa1321cbb6595f5e8d1a297f133394977 [file] [log] [blame]
Akron99713ef2017-06-28 18:19:28 +02001/**
2 * Information about a match.
3 */
Nils Diewald7c8ced22015-04-15 19:21:00 +00004define([
5 'match/infolayer',
6 'match/table',
Akron41387d22018-02-02 18:10:06 +01007 'match/treehierarchy',
8 'match/treearc',
Akron151bc872018-02-02 14:04:15 +01009 'match/meta',
Nils Diewald7c8ced22015-04-15 19:21:00 +000010 'util'
11], function (infoLayerClass,
Akron3bb91bc2016-12-02 16:43:17 +010012 matchTableClass,
Akron41387d22018-02-02 18:10:06 +010013 matchTreeHierarchyClass,
14 matchTreeArcClass,
Akronb6685bb2018-02-04 00:44:47 +010015 matchMetaClass) {
Akron3bb91bc2016-12-02 16:43:17 +010016
Nils Diewald7148c6f2015-05-04 15:07:53 +000017 // Override
Nils Diewald0e6992a2015-04-14 20:13:52 +000018 KorAP.API.getMatchInfo = KorAP.API.getMatchInfo || function () {
19 KorAP.log(0, 'KorAP.API.getMatchInfo() not implemented')
20 return {};
21 };
22
Akron0b489ad2018-02-02 16:49:32 +010023 const loc = KorAP.Locale;
24 const d = document;
Nils Diewald0e6992a2015-04-14 20:13:52 +000025
Nils Diewald0e6992a2015-04-14 20:13:52 +000026 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +000027
28 /**
29 * Create new match object
30 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 create : function (match) {
32 return Object.create(this)._init(match);
33 },
34
Akron151bc872018-02-02 14:04:15 +010035
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 /**
37 * Initialize object
38 */
39 _init : function (match) {
40 this._match = match;
41 this.opened = false;
42 return this;
43 },
44
Akron151bc872018-02-02 14:04:15 +010045
Nils Diewald0e6992a2015-04-14 20:13:52 +000046 /**
47 * Get match object
48 */
49 match : function () {
50 return this._match;
51 },
52
Nils Diewald7148c6f2015-05-04 15:07:53 +000053
54 /**
55 * Open the information view,
56 * if closed, otherwise close.
57 */
Akronbd342982018-01-25 18:01:46 +010058 /*
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 toggle : function () {
Akron3bb91bc2016-12-02 16:43:17 +010060
Akron08b82d62016-12-05 15:06:05 +010061 var elem = this._match.element();
Akron3bb91bc2016-12-02 16:43:17 +010062
63 if (this.opened == true) {
Akrond67d45b2017-05-18 21:47:38 +020064 elem.removeChild(
65 this.element()
66 );
67 this.opened = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +000068 }
69 else {
Akrond67d45b2017-05-18 21:47:38 +020070 // Append element to match
Akron3bb91bc2016-12-02 16:43:17 +010071 elem.appendChild(
Akrond67d45b2017-05-18 21:47:38 +020072 this.element()
73 );
74 this.opened = true;
Nils Diewald0e6992a2015-04-14 20:13:52 +000075 };
76
77 return this.opened;
78 },
Akronbd342982018-01-25 18:01:46 +010079 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000080
81
82 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000083 * Retrieve and parse snippet for table
84 * representation
Nils Diewald0e6992a2015-04-14 20:13:52 +000085 */
Akron151bc872018-02-02 14:04:15 +010086 getTableData : function (tokens, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000087 var focus = [];
88
89 // Get all tokens
90 if (tokens === undefined) {
Akrond67d45b2017-05-18 21:47:38 +020091 focus = this._match.getTokens();
Nils Diewald0e6992a2015-04-14 20:13:52 +000092 }
93
94 // Get only some tokens
95 else {
Akrond67d45b2017-05-18 21:47:38 +020096
97 // Push newly to focus array
98 for (var i = 0; i < tokens.length; i++) {
99 var term = tokens[i];
100 try {
101 // Create info layer objects
102 var layer = infoLayerClass.create(term);
103 layer.type = "tokens";
104 focus.push(layer);
105 }
106 catch (e) {
107 continue;
108 };
109 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000110 };
111
112 // No tokens chosen
113 if (focus.length == 0)
Akrond67d45b2017-05-18 21:47:38 +0200114 cb(null);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000115
116 // Get info (may be cached)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000117 KorAP.API.getMatchInfo(
Akrond67d45b2017-05-18 21:47:38 +0200118 this._match,
119 { 'spans' : false, 'layer' : focus },
120
121 // Callback for retrieval
122 function (matchResponse) {
Akron3bb91bc2016-12-02 16:43:17 +0100123
Akron515851a2017-05-02 12:53:17 +0200124 if (matchResponse === undefined)
125 cb(null);
126
Akrond67d45b2017-05-18 21:47:38 +0200127 // Get snippet from match info
128 if (matchResponse["snippet"] !== undefined) {
129 this._table = matchTableClass.create(matchResponse["snippet"]);
130 cb(this._table);
131 };
132 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000133 );
134
135 /*
136 // Todo: Store the table as a hash of the focus
137 return null;
138 */
139 },
Akronbd342982018-01-25 18:01:46 +0100140
141
Akron151bc872018-02-02 14:04:15 +0100142 getMetaData : function (metaInfo, cb) {
143 // ...
Akronbd342982018-01-25 18:01:46 +0100144 },
Nils Diewald0e6992a2015-04-14 20:13:52 +0000145
146
147 /**
148 * Retrieve and parse snippet for tree representation
149 */
Akron151bc872018-02-02 14:04:15 +0100150 getTreeData : function (foundry, layer, type, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000151 var focus = [];
152
153 // TODO: Support and cache multiple trees
154 KorAP.API.getMatchInfo(
Akrond67d45b2017-05-18 21:47:38 +0200155 this._match, {
156 'spans' : true,
157 'foundry' : foundry,
158 'layer' : layer
159 },
160 function (matchResponse) {
161 // Get snippet from match info
162 if (matchResponse["snippet"] !== undefined) {
163 // Todo: This should be cached somehow
Akronc56cf2d2016-11-09 22:02:38 +0100164
Akron0988d882017-11-10 16:13:12 +0100165 if (type === "spans") {
Akron41387d22018-02-02 18:10:06 +0100166 cb(matchTreeHierarchyClass.create(matchResponse["snippet"]));
Akron0988d882017-11-10 16:13:12 +0100167 }
168 else if (type === "rels") {
Akron41387d22018-02-02 18:10:06 +0100169 cb(matchTreeArcClass.create(matchResponse["snippet"]));
Akron0988d882017-11-10 16:13:12 +0100170 }
171
172 // Unknown tree type
173 else {
174 cb(null);
175 };
Akrond67d45b2017-05-18 21:47:38 +0200176 }
177 else {
178 cb(null);
179 };
180 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000181 );
182 },
183
Akron151bc872018-02-02 14:04:15 +0100184
Nils Diewald0e6992a2015-04-14 20:13:52 +0000185 /**
186 * Destroy this match information view.
187 */
188 destroy : function () {
189
190 // Remove circular reference
Akron8b592d42018-01-26 18:33:06 +0100191 /*
Nils Diewald0e6992a2015-04-14 20:13:52 +0000192 if (this._treeMenu !== undefined)
Akron99713ef2017-06-28 18:19:28 +0200193 delete this._treeMenu["info"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000194
195 this._treeMenu.destroy();
196 this._treeMenu = undefined;
Akron8b592d42018-01-26 18:33:06 +0100197 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000198 this._match = undefined;
Akron99713ef2017-06-28 18:19:28 +0200199 this._matchCreator = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000200 // Element destroy
201 },
202
Akron151bc872018-02-02 14:04:15 +0100203
Nils Diewald0e6992a2015-04-14 20:13:52 +0000204 /**
205 * Add a new tree view to the list
206 */
Akron151bc872018-02-02 14:04:15 +0100207 showTree : function (foundry, layer, type, cb) {
Akron0b489ad2018-02-02 16:49:32 +0100208 var matchtree = d.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100209 matchtree.classList.add('matchtree', 'loading');
210
211 this.element().appendChild(matchtree);
Akrond67d45b2017-05-18 21:47:38 +0200212
Akron151bc872018-02-02 14:04:15 +0100213 // Add title line
214 var h6 = matchtree.addE('h6');
215 h6.addE('span').addT(foundry);
216 h6.addE('span').addT(layer);
217
218 var tree = matchtree.addE('div');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000219
Akron151bc872018-02-02 14:04:15 +0100220 // Add close action button
Akronc8eb4a12018-02-03 00:39:58 +0100221 var actions = this._addButton('close', matchtree, function (e) {
222 this.parentNode.removeChild(this);
223 e.halt();
224 });
Nils Diewald0e6992a2015-04-14 20:13:52 +0000225
Akronc8eb4a12018-02-03 00:39:58 +0100226 // tree.classList.add('loading'); // alternatively
Nils Diewald0ec142f2015-05-05 00:29:23 +0000227
Nils Diewald0e6992a2015-04-14 20:13:52 +0000228 // Get tree data async
Akron151bc872018-02-02 14:04:15 +0100229 this.getTreeData(foundry, layer, type, function (treeObj) {
Akronc8eb4a12018-02-03 00:39:58 +0100230 matchtree.classList.remove('loading');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000231
Akrond67d45b2017-05-18 21:47:38 +0200232 // Something went wrong - probably log!!!
Nils Diewald0ec142f2015-05-05 00:29:23 +0000233
Akrond67d45b2017-05-18 21:47:38 +0200234 if (treeObj === null) {
Akron151bc872018-02-02 14:04:15 +0100235 tree.addT('No data available.');
Akrond67d45b2017-05-18 21:47:38 +0200236 }
237 else {
238 tree.appendChild(treeObj.element());
Akron0988d882017-11-10 16:13:12 +0100239 treeObj.show();
Akron151bc872018-02-02 14:04:15 +0100240
Akrond67d45b2017-05-18 21:47:38 +0200241 // Reposition the view to the center
242 // (This may in a future release be a reposition
Akron151bc872018-02-02 14:04:15 +0100243 // to move the root to the actual match)
Akronc56cf2d2016-11-09 22:02:38 +0100244
Akron0988d882017-11-10 16:13:12 +0100245 // This is currently not supported by relations
246 if (type === "spans") {
Akron0b489ad2018-02-02 16:49:32 +0100247 var dl = d.createElement('li');
Akron0988d882017-11-10 16:13:12 +0100248 dl.className = 'download';
249 dl.addEventListener(
250 'click', function (e) {
Akron151bc872018-02-02 14:04:15 +0100251 var a = treeObj.downloadLink();
Akron0b489ad2018-02-02 16:49:32 +0100252 d.body.appendChild(a);
Akron0988d882017-11-10 16:13:12 +0100253 a.click();
Akron0b489ad2018-02-02 16:49:32 +0100254 d.body.removeChild(a)
Akron0988d882017-11-10 16:13:12 +0100255 e.halt();
256 }
257 );
258
259 actions.appendChild(dl);
260 };
261
Akronc56cf2d2016-11-09 22:02:38 +0100262 treeObj.center();
Akrond67d45b2017-05-18 21:47:38 +0200263 };
264
265 if (cb !== undefined)
266 cb(treeObj);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000267 });
Akronc8eb4a12018-02-03 00:39:58 +0100268 matchtree.classList.remove('loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000269 },
Akronbd342982018-01-25 18:01:46 +0100270
271
Akrone7679692018-01-26 12:06:33 +0100272 // Add meta information to match
Akron151bc872018-02-02 14:04:15 +0100273 showMeta : function () {
Akronaeceda72018-02-02 20:44:06 +0100274 var metaTable = document.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100275 metaTable.classList.add('metatable', 'loading');
276 this.element().appendChild(metaTable);
Akronbd342982018-01-25 18:01:46 +0100277
278 // TODO: This is part of the getMeta!
279 var metaInfo = this._match.element().getAttribute('data-info');
280
281 if (metaInfo)
282 metaInfo = JSON.parse(metaInfo);
283
284 // There is metainfo
285 if (metaInfo) {
286
Akronc8eb4a12018-02-03 00:39:58 +0100287 // Load data
288 metaTable.classList.remove('loading');
289
Akronbd342982018-01-25 18:01:46 +0100290 // Add metainfo to matchview
Akron151bc872018-02-02 14:04:15 +0100291 var metaElem = matchMetaClass.create(this._match).element(metaInfo);
Akronaeceda72018-02-02 20:44:06 +0100292 metaTable.appendChild(metaElem);
Akronaeceda72018-02-02 20:44:06 +0100293
294 // Add button
295 this._addButton('close', metaTable, function (e) {
296 this.parentNode.removeChild(this);
297 e.halt();
298 });
Akronbd342982018-01-25 18:01:46 +0100299 };
Akron41387d22018-02-02 18:10:06 +0100300
301 // Load data
Akronaeceda72018-02-02 20:44:06 +0100302 metaTable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100303 },
304
Akron151bc872018-02-02 14:04:15 +0100305
Akronbd342982018-01-25 18:01:46 +0100306 // Add table
Akron151bc872018-02-02 14:04:15 +0100307 showTable : function () {
Akronbd342982018-01-25 18:01:46 +0100308
Nils Diewald0e6992a2015-04-14 20:13:52 +0000309 // Append default table
Akron0b489ad2018-02-02 16:49:32 +0100310 var matchtable = d.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100311 matchtable.classList.add('matchtable', 'loading');
Akronaeceda72018-02-02 20:44:06 +0100312 var info = this.element();
313 info.appendChild(matchtable);
314
Nils Diewald0e6992a2015-04-14 20:13:52 +0000315
Akronb6685bb2018-02-04 00:44:47 +0100316 // TODO:
317 // Create try-catch-exception-handling
318
Nils Diewald0e6992a2015-04-14 20:13:52 +0000319 // Create the table asynchronous
Akron151bc872018-02-02 14:04:15 +0100320 this.getTableData(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100321
Akronc8eb4a12018-02-03 00:39:58 +0100322 // Load data
323 matchtable.classList.remove('loading');
324
Akrond67d45b2017-05-18 21:47:38 +0200325 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100326 matchtable.appendChild(table.element());
327 };
Akron151bc872018-02-02 14:04:15 +0100328
329 // Load data
330 matchtable.classList.remove('loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000331 });
Akronaeceda72018-02-02 20:44:06 +0100332
333 // Add button
334 this._addButton('close', matchtable, function (e) {
335 this.parentNode.removeChild(this);
336 e.halt();
337 });
338
339 // Load data
340 matchtable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100341 },
342
Akronc8eb4a12018-02-03 00:39:58 +0100343 // Add action button
Akronaeceda72018-02-02 20:44:06 +0100344 _addButton : function (buttonType, element, cb) {
345 // TODO: Unless existent
346 var actions = document.createElement('ul');
347 actions.classList.add('action', 'image');
348 var b = actions.addE('li');
349 b.className = buttonType;
350 b.addE('span').addT(buttonType);
351 b.addEventListener(
352 'click', cb.bind(element)
353 );
354
355 element.appendChild(actions);
356 return actions;
357 },
358
359
Akronbd342982018-01-25 18:01:46 +0100360 /**
361 * Create match information view.
362 */
363 element : function () {
364
365 if (this._element !== undefined)
366 return this._element;
367
368 // Create info table
Akron0b489ad2018-02-02 16:49:32 +0100369 var info = d.createElement('div');
Akronbd342982018-01-25 18:01:46 +0100370 info.classList.add('matchinfo');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000371
372 this._element = info;
373
Akronbd342982018-01-25 18:01:46 +0100374 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000375 }
376 };
377});