blob: ba7214ea513a71b04b89d06083a3e71af76cd700 [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;
Akronedbf33a2018-02-05 19:18:03 +010041 this._visibleTable = false;
42 this._visibleMeta = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +000043 this.opened = false;
44 return this;
45 },
46
Akron151bc872018-02-02 14:04:15 +010047
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 /**
49 * Get match object
50 */
51 match : function () {
52 return this._match;
53 },
54
Nils Diewald7148c6f2015-05-04 15:07:53 +000055
56 /**
57 * Open the information view,
58 * if closed, otherwise close.
59 */
Akronbd342982018-01-25 18:01:46 +010060 /*
Nils Diewald0e6992a2015-04-14 20:13:52 +000061 toggle : function () {
Akron3bb91bc2016-12-02 16:43:17 +010062
Akron08b82d62016-12-05 15:06:05 +010063 var elem = this._match.element();
Akron3bb91bc2016-12-02 16:43:17 +010064
65 if (this.opened == true) {
Akrond67d45b2017-05-18 21:47:38 +020066 elem.removeChild(
67 this.element()
68 );
69 this.opened = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +000070 }
71 else {
Akrond67d45b2017-05-18 21:47:38 +020072 // Append element to match
Akron3bb91bc2016-12-02 16:43:17 +010073 elem.appendChild(
Akrond67d45b2017-05-18 21:47:38 +020074 this.element()
75 );
76 this.opened = true;
Nils Diewald0e6992a2015-04-14 20:13:52 +000077 };
78
79 return this.opened;
80 },
Akronbd342982018-01-25 18:01:46 +010081 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000082
83
84 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000085 * Retrieve and parse snippet for table
86 * representation
Nils Diewald0e6992a2015-04-14 20:13:52 +000087 */
Akron151bc872018-02-02 14:04:15 +010088 getTableData : function (tokens, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000089 var focus = [];
90
91 // Get all tokens
92 if (tokens === undefined) {
Akrond67d45b2017-05-18 21:47:38 +020093 focus = this._match.getTokens();
Nils Diewald0e6992a2015-04-14 20:13:52 +000094 }
95
96 // Get only some tokens
97 else {
Akrond67d45b2017-05-18 21:47:38 +020098
99 // Push newly to focus array
100 for (var i = 0; i < tokens.length; i++) {
101 var term = tokens[i];
102 try {
103 // Create info layer objects
104 var layer = infoLayerClass.create(term);
105 layer.type = "tokens";
106 focus.push(layer);
107 }
108 catch (e) {
109 continue;
110 };
111 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000112 };
113
114 // No tokens chosen
115 if (focus.length == 0)
Akrond67d45b2017-05-18 21:47:38 +0200116 cb(null);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000117
118 // Get info (may be cached)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000119 KorAP.API.getMatchInfo(
Akrond67d45b2017-05-18 21:47:38 +0200120 this._match,
121 { 'spans' : false, 'layer' : focus },
122
123 // Callback for retrieval
124 function (matchResponse) {
Akron3bb91bc2016-12-02 16:43:17 +0100125
Akron515851a2017-05-02 12:53:17 +0200126 if (matchResponse === undefined)
127 cb(null);
128
Akrond67d45b2017-05-18 21:47:38 +0200129 // Get snippet from match info
130 if (matchResponse["snippet"] !== undefined) {
131 this._table = matchTableClass.create(matchResponse["snippet"]);
132 cb(this._table);
133 };
134 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000135 );
136
137 /*
138 // Todo: Store the table as a hash of the focus
139 return null;
140 */
141 },
Akronbd342982018-01-25 18:01:46 +0100142
143
Akron0ad7cd22018-02-08 18:03:06 +0100144 getMetaData : function (cb) {
145 KorAP.API.getTextInfo(
146 this._match, {}, function (textResponse) {
147
148 if (textResponse === undefined) {
149 cb(null);
150 return;
151 };
152
153 var doc = textResponse["document"];
154
155
156 if (doc === undefined) {
157 cb(null);
158 return;
159 };
160
161 var fields = doc["fields"];
162 if (fields === undefined) {
163 cb(null);
164 return;
165 };
166
167 // Add metainfo to matchview
168 cb(matchMetaClass.create(
169 this._match, fields
170 ));
171 }
172 );
Akronbd342982018-01-25 18:01:46 +0100173 },
Nils Diewald0e6992a2015-04-14 20:13:52 +0000174
175
176 /**
177 * Retrieve and parse snippet for tree representation
178 */
Akron151bc872018-02-02 14:04:15 +0100179 getTreeData : function (foundry, layer, type, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000180 var focus = [];
181
182 // TODO: Support and cache multiple trees
183 KorAP.API.getMatchInfo(
Akrond67d45b2017-05-18 21:47:38 +0200184 this._match, {
185 'spans' : true,
186 'foundry' : foundry,
187 'layer' : layer
188 },
189 function (matchResponse) {
Akron0ad7cd22018-02-08 18:03:06 +0100190 if (matchResponse === undefined) {
191 cb(null);
192 return;
193 };
194
Akrond67d45b2017-05-18 21:47:38 +0200195 // Get snippet from match info
196 if (matchResponse["snippet"] !== undefined) {
197 // Todo: This should be cached somehow
Akronc56cf2d2016-11-09 22:02:38 +0100198
Akron0988d882017-11-10 16:13:12 +0100199 if (type === "spans") {
Akron41387d22018-02-02 18:10:06 +0100200 cb(matchTreeHierarchyClass.create(matchResponse["snippet"]));
Akron0988d882017-11-10 16:13:12 +0100201 }
202 else if (type === "rels") {
Akron41387d22018-02-02 18:10:06 +0100203 cb(matchTreeArcClass.create(matchResponse["snippet"]));
Akron0988d882017-11-10 16:13:12 +0100204 }
205
206 // Unknown tree type
207 else {
208 cb(null);
209 };
Akrond67d45b2017-05-18 21:47:38 +0200210 }
211 else {
212 cb(null);
213 };
214 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000215 );
216 },
217
Akron151bc872018-02-02 14:04:15 +0100218
Nils Diewald0e6992a2015-04-14 20:13:52 +0000219 /**
220 * Destroy this match information view.
221 */
222 destroy : function () {
223
224 // Remove circular reference
Akron8b592d42018-01-26 18:33:06 +0100225 /*
Nils Diewald0e6992a2015-04-14 20:13:52 +0000226 if (this._treeMenu !== undefined)
Akron99713ef2017-06-28 18:19:28 +0200227 delete this._treeMenu["info"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000228
229 this._treeMenu.destroy();
230 this._treeMenu = undefined;
Akron8b592d42018-01-26 18:33:06 +0100231 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000232 this._match = undefined;
Akron99713ef2017-06-28 18:19:28 +0200233 this._matchCreator = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000234 // Element destroy
235 },
236
Akron151bc872018-02-02 14:04:15 +0100237
Nils Diewald0e6992a2015-04-14 20:13:52 +0000238 /**
239 * Add a new tree view to the list
240 */
Akron151bc872018-02-02 14:04:15 +0100241 showTree : function (foundry, layer, type, cb) {
Akron0b489ad2018-02-02 16:49:32 +0100242 var matchtree = d.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100243 matchtree.classList.add('matchtree', 'loading');
244
245 this.element().appendChild(matchtree);
Akrond67d45b2017-05-18 21:47:38 +0200246
Akron151bc872018-02-02 14:04:15 +0100247 // Add title line
248 var h6 = matchtree.addE('h6');
249 h6.addE('span').addT(foundry);
250 h6.addE('span').addT(layer);
251
252 var tree = matchtree.addE('div');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000253
Akron151bc872018-02-02 14:04:15 +0100254 // Add close action button
Akronc8eb4a12018-02-03 00:39:58 +0100255 var actions = this._addButton('close', matchtree, function (e) {
256 this.parentNode.removeChild(this);
257 e.halt();
258 });
Nils Diewald0e6992a2015-04-14 20:13:52 +0000259
Akronc8eb4a12018-02-03 00:39:58 +0100260 // tree.classList.add('loading'); // alternatively
Nils Diewald0ec142f2015-05-05 00:29:23 +0000261
Nils Diewald0e6992a2015-04-14 20:13:52 +0000262 // Get tree data async
Akron151bc872018-02-02 14:04:15 +0100263 this.getTreeData(foundry, layer, type, function (treeObj) {
Akronc8eb4a12018-02-03 00:39:58 +0100264 matchtree.classList.remove('loading');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000265
Akrond67d45b2017-05-18 21:47:38 +0200266 // Something went wrong - probably log!!!
Nils Diewald0ec142f2015-05-05 00:29:23 +0000267
Akrond67d45b2017-05-18 21:47:38 +0200268 if (treeObj === null) {
Akron151bc872018-02-02 14:04:15 +0100269 tree.addT('No data available.');
Akrond67d45b2017-05-18 21:47:38 +0200270 }
271 else {
272 tree.appendChild(treeObj.element());
Akron0988d882017-11-10 16:13:12 +0100273 treeObj.show();
Akron151bc872018-02-02 14:04:15 +0100274
Akrond67d45b2017-05-18 21:47:38 +0200275 // Reposition the view to the center
276 // (This may in a future release be a reposition
Akron151bc872018-02-02 14:04:15 +0100277 // to move the root to the actual match)
Akronc56cf2d2016-11-09 22:02:38 +0100278
Akron0988d882017-11-10 16:13:12 +0100279 // This is currently not supported by relations
280 if (type === "spans") {
Akron0b489ad2018-02-02 16:49:32 +0100281 var dl = d.createElement('li');
Akron0988d882017-11-10 16:13:12 +0100282 dl.className = 'download';
283 dl.addEventListener(
284 'click', function (e) {
Akron151bc872018-02-02 14:04:15 +0100285 var a = treeObj.downloadLink();
Akron0b489ad2018-02-02 16:49:32 +0100286 d.body.appendChild(a);
Akron0988d882017-11-10 16:13:12 +0100287 a.click();
Akron0b489ad2018-02-02 16:49:32 +0100288 d.body.removeChild(a)
Akron0988d882017-11-10 16:13:12 +0100289 e.halt();
290 }
291 );
292
293 actions.appendChild(dl);
294 };
295
Akronc56cf2d2016-11-09 22:02:38 +0100296 treeObj.center();
Akrond67d45b2017-05-18 21:47:38 +0200297 };
298
299 if (cb !== undefined)
300 cb(treeObj);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000301 });
Akronc8eb4a12018-02-03 00:39:58 +0100302 matchtree.classList.remove('loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000303 },
Akronbd342982018-01-25 18:01:46 +0100304
305
Akrone7679692018-01-26 12:06:33 +0100306 // Add meta information to match
Akron151bc872018-02-02 14:04:15 +0100307 showMeta : function () {
Akronedbf33a2018-02-05 19:18:03 +0100308
309 // Already visible
310 if (this._visibleMeta)
311 return;
312
313 this._visibleMeta = true;
314
Akronaeceda72018-02-02 20:44:06 +0100315 var metaTable = document.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100316 metaTable.classList.add('metatable', 'loading');
317 this.element().appendChild(metaTable);
Akronbd342982018-01-25 18:01:46 +0100318
Akron0ad7cd22018-02-08 18:03:06 +0100319 /*
320 * This was temporary
Akronbd342982018-01-25 18:01:46 +0100321 var metaInfo = this._match.element().getAttribute('data-info');
Akronbd342982018-01-25 18:01:46 +0100322 if (metaInfo)
323 metaInfo = JSON.parse(metaInfo);
Akron0ad7cd22018-02-08 18:03:06 +0100324 */
Akronedbf33a2018-02-05 19:18:03 +0100325 var that = this;
326
Akron0ad7cd22018-02-08 18:03:06 +0100327 this.getMetaData(function (meta) {
Akronc8eb4a12018-02-03 00:39:58 +0100328 // Load data
329 metaTable.classList.remove('loading');
330
Akron0ad7cd22018-02-08 18:03:06 +0100331 metaTable.appendChild(meta.element());
Akronaeceda72018-02-02 20:44:06 +0100332
333 // Add button
Akron0ad7cd22018-02-08 18:03:06 +0100334 that._addButton('close', metaTable, function (e) {
Akronaeceda72018-02-02 20:44:06 +0100335 this.parentNode.removeChild(this);
Akronedbf33a2018-02-05 19:18:03 +0100336 that._visibleMeta = false;
Akronaeceda72018-02-02 20:44:06 +0100337 e.halt();
338 });
Akron0ad7cd22018-02-08 18:03:06 +0100339 });
Akron41387d22018-02-02 18:10:06 +0100340
Akron0ad7cd22018-02-08 18:03:06 +0100341 // Do not load any longer
Akronaeceda72018-02-02 20:44:06 +0100342 metaTable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100343 },
344
Akron151bc872018-02-02 14:04:15 +0100345
Akronbd342982018-01-25 18:01:46 +0100346 // Add table
Akron151bc872018-02-02 14:04:15 +0100347 showTable : function () {
Akronbd342982018-01-25 18:01:46 +0100348
Akronedbf33a2018-02-05 19:18:03 +0100349 // Already visible
350 if (this._visibleTable)
351 return;
352
353 this._visibleTable = true;
354
Nils Diewald0e6992a2015-04-14 20:13:52 +0000355 // Append default table
Akron0b489ad2018-02-02 16:49:32 +0100356 var matchtable = d.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100357 matchtable.classList.add('matchtable', 'loading');
Akronaeceda72018-02-02 20:44:06 +0100358 var info = this.element();
359 info.appendChild(matchtable);
360
Akronedbf33a2018-02-05 19:18:03 +0100361 var that = this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000362
Akronb6685bb2018-02-04 00:44:47 +0100363 // TODO:
364 // Create try-catch-exception-handling
365
Nils Diewald0e6992a2015-04-14 20:13:52 +0000366 // Create the table asynchronous
Akron151bc872018-02-02 14:04:15 +0100367 this.getTableData(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100368
Akronc8eb4a12018-02-03 00:39:58 +0100369 // Load data
370 matchtable.classList.remove('loading');
371
Akrond67d45b2017-05-18 21:47:38 +0200372 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100373 matchtable.appendChild(table.element());
374 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000375 });
Akronaeceda72018-02-02 20:44:06 +0100376
377 // Add button
378 this._addButton('close', matchtable, function (e) {
379 this.parentNode.removeChild(this);
Akronedbf33a2018-02-05 19:18:03 +0100380 that._visibleTable = false;
Akronaeceda72018-02-02 20:44:06 +0100381 e.halt();
382 });
383
384 // Load data
385 matchtable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100386 },
387
Akronc8eb4a12018-02-03 00:39:58 +0100388 // Add action button
Akronaeceda72018-02-02 20:44:06 +0100389 _addButton : function (buttonType, element, cb) {
390 // TODO: Unless existent
391 var actions = document.createElement('ul');
392 actions.classList.add('action', 'image');
393 var b = actions.addE('li');
394 b.className = buttonType;
395 b.addE('span').addT(buttonType);
396 b.addEventListener(
397 'click', cb.bind(element)
398 );
399
400 element.appendChild(actions);
401 return actions;
402 },
403
404
Akronbd342982018-01-25 18:01:46 +0100405 /**
406 * Create match information view.
407 */
408 element : function () {
409
410 if (this._element !== undefined)
411 return this._element;
412
413 // Create info table
Akron0b489ad2018-02-02 16:49:32 +0100414 var info = d.createElement('div');
Akronbd342982018-01-25 18:01:46 +0100415 info.classList.add('matchinfo');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000416
417 this._element = info;
418
Akronbd342982018-01-25 18:01:46 +0100419 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000420 }
421 };
422});