blob: a72b66fd9a99c975ec5472086a80938df47e3c45 [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
Akron151bc872018-02-02 14:04:15 +0100144 getMetaData : function (metaInfo, cb) {
145 // ...
Akronbd342982018-01-25 18:01:46 +0100146 },
Nils Diewald0e6992a2015-04-14 20:13:52 +0000147
148
149 /**
150 * Retrieve and parse snippet for tree representation
151 */
Akron151bc872018-02-02 14:04:15 +0100152 getTreeData : function (foundry, layer, type, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000153 var focus = [];
154
155 // TODO: Support and cache multiple trees
156 KorAP.API.getMatchInfo(
Akrond67d45b2017-05-18 21:47:38 +0200157 this._match, {
158 'spans' : true,
159 'foundry' : foundry,
160 'layer' : layer
161 },
162 function (matchResponse) {
163 // Get snippet from match info
164 if (matchResponse["snippet"] !== undefined) {
165 // Todo: This should be cached somehow
Akronc56cf2d2016-11-09 22:02:38 +0100166
Akron0988d882017-11-10 16:13:12 +0100167 if (type === "spans") {
Akron41387d22018-02-02 18:10:06 +0100168 cb(matchTreeHierarchyClass.create(matchResponse["snippet"]));
Akron0988d882017-11-10 16:13:12 +0100169 }
170 else if (type === "rels") {
Akron41387d22018-02-02 18:10:06 +0100171 cb(matchTreeArcClass.create(matchResponse["snippet"]));
Akron0988d882017-11-10 16:13:12 +0100172 }
173
174 // Unknown tree type
175 else {
176 cb(null);
177 };
Akrond67d45b2017-05-18 21:47:38 +0200178 }
179 else {
180 cb(null);
181 };
182 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000183 );
184 },
185
Akron151bc872018-02-02 14:04:15 +0100186
Nils Diewald0e6992a2015-04-14 20:13:52 +0000187 /**
188 * Destroy this match information view.
189 */
190 destroy : function () {
191
192 // Remove circular reference
Akron8b592d42018-01-26 18:33:06 +0100193 /*
Nils Diewald0e6992a2015-04-14 20:13:52 +0000194 if (this._treeMenu !== undefined)
Akron99713ef2017-06-28 18:19:28 +0200195 delete this._treeMenu["info"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000196
197 this._treeMenu.destroy();
198 this._treeMenu = undefined;
Akron8b592d42018-01-26 18:33:06 +0100199 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000200 this._match = undefined;
Akron99713ef2017-06-28 18:19:28 +0200201 this._matchCreator = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000202 // Element destroy
203 },
204
Akron151bc872018-02-02 14:04:15 +0100205
Nils Diewald0e6992a2015-04-14 20:13:52 +0000206 /**
207 * Add a new tree view to the list
208 */
Akron151bc872018-02-02 14:04:15 +0100209 showTree : function (foundry, layer, type, cb) {
Akron0b489ad2018-02-02 16:49:32 +0100210 var matchtree = d.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100211 matchtree.classList.add('matchtree', 'loading');
212
213 this.element().appendChild(matchtree);
Akrond67d45b2017-05-18 21:47:38 +0200214
Akron151bc872018-02-02 14:04:15 +0100215 // Add title line
216 var h6 = matchtree.addE('h6');
217 h6.addE('span').addT(foundry);
218 h6.addE('span').addT(layer);
219
220 var tree = matchtree.addE('div');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000221
Akron151bc872018-02-02 14:04:15 +0100222 // Add close action button
Akronc8eb4a12018-02-03 00:39:58 +0100223 var actions = this._addButton('close', matchtree, function (e) {
224 this.parentNode.removeChild(this);
225 e.halt();
226 });
Nils Diewald0e6992a2015-04-14 20:13:52 +0000227
Akronc8eb4a12018-02-03 00:39:58 +0100228 // tree.classList.add('loading'); // alternatively
Nils Diewald0ec142f2015-05-05 00:29:23 +0000229
Nils Diewald0e6992a2015-04-14 20:13:52 +0000230 // Get tree data async
Akron151bc872018-02-02 14:04:15 +0100231 this.getTreeData(foundry, layer, type, function (treeObj) {
Akronc8eb4a12018-02-03 00:39:58 +0100232 matchtree.classList.remove('loading');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000233
Akrond67d45b2017-05-18 21:47:38 +0200234 // Something went wrong - probably log!!!
Nils Diewald0ec142f2015-05-05 00:29:23 +0000235
Akrond67d45b2017-05-18 21:47:38 +0200236 if (treeObj === null) {
Akron151bc872018-02-02 14:04:15 +0100237 tree.addT('No data available.');
Akrond67d45b2017-05-18 21:47:38 +0200238 }
239 else {
240 tree.appendChild(treeObj.element());
Akron0988d882017-11-10 16:13:12 +0100241 treeObj.show();
Akron151bc872018-02-02 14:04:15 +0100242
Akrond67d45b2017-05-18 21:47:38 +0200243 // Reposition the view to the center
244 // (This may in a future release be a reposition
Akron151bc872018-02-02 14:04:15 +0100245 // to move the root to the actual match)
Akronc56cf2d2016-11-09 22:02:38 +0100246
Akron0988d882017-11-10 16:13:12 +0100247 // This is currently not supported by relations
248 if (type === "spans") {
Akron0b489ad2018-02-02 16:49:32 +0100249 var dl = d.createElement('li');
Akron0988d882017-11-10 16:13:12 +0100250 dl.className = 'download';
251 dl.addEventListener(
252 'click', function (e) {
Akron151bc872018-02-02 14:04:15 +0100253 var a = treeObj.downloadLink();
Akron0b489ad2018-02-02 16:49:32 +0100254 d.body.appendChild(a);
Akron0988d882017-11-10 16:13:12 +0100255 a.click();
Akron0b489ad2018-02-02 16:49:32 +0100256 d.body.removeChild(a)
Akron0988d882017-11-10 16:13:12 +0100257 e.halt();
258 }
259 );
260
261 actions.appendChild(dl);
262 };
263
Akronc56cf2d2016-11-09 22:02:38 +0100264 treeObj.center();
Akrond67d45b2017-05-18 21:47:38 +0200265 };
266
267 if (cb !== undefined)
268 cb(treeObj);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000269 });
Akronc8eb4a12018-02-03 00:39:58 +0100270 matchtree.classList.remove('loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000271 },
Akronbd342982018-01-25 18:01:46 +0100272
273
Akrone7679692018-01-26 12:06:33 +0100274 // Add meta information to match
Akron151bc872018-02-02 14:04:15 +0100275 showMeta : function () {
Akronedbf33a2018-02-05 19:18:03 +0100276
277 // Already visible
278 if (this._visibleMeta)
279 return;
280
281 this._visibleMeta = true;
282
Akronaeceda72018-02-02 20:44:06 +0100283 var metaTable = document.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100284 metaTable.classList.add('metatable', 'loading');
285 this.element().appendChild(metaTable);
Akronbd342982018-01-25 18:01:46 +0100286
287 // TODO: This is part of the getMeta!
288 var metaInfo = this._match.element().getAttribute('data-info');
289
290 if (metaInfo)
291 metaInfo = JSON.parse(metaInfo);
292
Akronedbf33a2018-02-05 19:18:03 +0100293 var that = this;
294
Akronbd342982018-01-25 18:01:46 +0100295 // There is metainfo
296 if (metaInfo) {
297
Akronc8eb4a12018-02-03 00:39:58 +0100298 // Load data
299 metaTable.classList.remove('loading');
300
Akronbd342982018-01-25 18:01:46 +0100301 // Add metainfo to matchview
Akron151bc872018-02-02 14:04:15 +0100302 var metaElem = matchMetaClass.create(this._match).element(metaInfo);
Akronaeceda72018-02-02 20:44:06 +0100303 metaTable.appendChild(metaElem);
Akronaeceda72018-02-02 20:44:06 +0100304
305 // Add button
306 this._addButton('close', metaTable, function (e) {
307 this.parentNode.removeChild(this);
Akronedbf33a2018-02-05 19:18:03 +0100308 that._visibleMeta = false;
Akronaeceda72018-02-02 20:44:06 +0100309 e.halt();
310 });
Akronbd342982018-01-25 18:01:46 +0100311 };
Akron41387d22018-02-02 18:10:06 +0100312
313 // Load data
Akronaeceda72018-02-02 20:44:06 +0100314 metaTable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100315 },
316
Akron151bc872018-02-02 14:04:15 +0100317
Akronbd342982018-01-25 18:01:46 +0100318 // Add table
Akron151bc872018-02-02 14:04:15 +0100319 showTable : function () {
Akronbd342982018-01-25 18:01:46 +0100320
Akronedbf33a2018-02-05 19:18:03 +0100321 // Already visible
322 if (this._visibleTable)
323 return;
324
325 this._visibleTable = true;
326
Nils Diewald0e6992a2015-04-14 20:13:52 +0000327 // Append default table
Akron0b489ad2018-02-02 16:49:32 +0100328 var matchtable = d.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100329 matchtable.classList.add('matchtable', 'loading');
Akronaeceda72018-02-02 20:44:06 +0100330 var info = this.element();
331 info.appendChild(matchtable);
332
Akronedbf33a2018-02-05 19:18:03 +0100333 var that = this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000334
Akronb6685bb2018-02-04 00:44:47 +0100335 // TODO:
336 // Create try-catch-exception-handling
337
Nils Diewald0e6992a2015-04-14 20:13:52 +0000338 // Create the table asynchronous
Akron151bc872018-02-02 14:04:15 +0100339 this.getTableData(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100340
Akronc8eb4a12018-02-03 00:39:58 +0100341 // Load data
342 matchtable.classList.remove('loading');
343
Akrond67d45b2017-05-18 21:47:38 +0200344 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100345 matchtable.appendChild(table.element());
346 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000347 });
Akronaeceda72018-02-02 20:44:06 +0100348
349 // Add button
350 this._addButton('close', matchtable, function (e) {
351 this.parentNode.removeChild(this);
Akronedbf33a2018-02-05 19:18:03 +0100352 that._visibleTable = false;
Akronaeceda72018-02-02 20:44:06 +0100353 e.halt();
354 });
355
356 // Load data
357 matchtable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100358 },
359
Akronc8eb4a12018-02-03 00:39:58 +0100360 // Add action button
Akronaeceda72018-02-02 20:44:06 +0100361 _addButton : function (buttonType, element, cb) {
362 // TODO: Unless existent
363 var actions = document.createElement('ul');
364 actions.classList.add('action', 'image');
365 var b = actions.addE('li');
366 b.className = buttonType;
367 b.addE('span').addT(buttonType);
368 b.addEventListener(
369 'click', cb.bind(element)
370 );
371
372 element.appendChild(actions);
373 return actions;
374 },
375
376
Akronbd342982018-01-25 18:01:46 +0100377 /**
378 * Create match information view.
379 */
380 element : function () {
381
382 if (this._element !== undefined)
383 return this._element;
384
385 // Create info table
Akron0b489ad2018-02-02 16:49:32 +0100386 var info = d.createElement('div');
Akronbd342982018-01-25 18:01:46 +0100387 info.classList.add('matchinfo');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000388
389 this._element = info;
390
Akronbd342982018-01-25 18:01:46 +0100391 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000392 }
393 };
394});