blob: 8779ee9f6d636adc53083e7c9934f1264d4d5bda [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',
Akron99713ef2017-06-28 18:19:28 +020010 'match/querycreator',
Nils Diewald7c8ced22015-04-15 19:21:00 +000011 'util'
12], function (infoLayerClass,
Akron3bb91bc2016-12-02 16:43:17 +010013 matchTableClass,
Akron41387d22018-02-02 18:10:06 +010014 matchTreeHierarchyClass,
15 matchTreeArcClass,
Akron151bc872018-02-02 14:04:15 +010016 matchMetaClass,
Akron0988d882017-11-10 16:13:12 +010017 matchQueryCreator) {
Akron3bb91bc2016-12-02 16:43:17 +010018
Nils Diewald7148c6f2015-05-04 15:07:53 +000019 // Override
Nils Diewald0e6992a2015-04-14 20:13:52 +000020 KorAP.API.getMatchInfo = KorAP.API.getMatchInfo || function () {
21 KorAP.log(0, 'KorAP.API.getMatchInfo() not implemented')
22 return {};
23 };
24
Akron0b489ad2018-02-02 16:49:32 +010025 const loc = KorAP.Locale;
26 const d = document;
Nils Diewald0e6992a2015-04-14 20:13:52 +000027
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +000029
30 /**
31 * Create new match object
32 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 create : function (match) {
34 return Object.create(this)._init(match);
35 },
36
Akron151bc872018-02-02 14:04:15 +010037
Nils Diewald0e6992a2015-04-14 20:13:52 +000038 /**
39 * Initialize object
40 */
41 _init : function (match) {
42 this._match = match;
43 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');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000211 matchtree.classList.add('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
220 this._element.insertBefore(matchtree, this._element.lastChild);
221
Akron151bc872018-02-02 14:04:15 +0100222 // Add close action button
223 var actions = tree.addE('ul');
Akronc56cf2d2016-11-09 22:02:38 +0100224 actions.classList.add('action', 'image');
Akron151bc872018-02-02 14:04:15 +0100225 var close = actions.addE('li');
Akronc56cf2d2016-11-09 22:02:38 +0100226 close.className = 'close';
Akron151bc872018-02-02 14:04:15 +0100227 close.addE('span');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000228 close.addEventListener(
Akrond67d45b2017-05-18 21:47:38 +0200229 'click', function (e) {
230 matchtree.parentNode.removeChild(matchtree);
231 e.halt();
232 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000233 );
234
Nils Diewald0ec142f2015-05-05 00:29:23 +0000235 tree.classList.add('loading');
236
Nils Diewald0e6992a2015-04-14 20:13:52 +0000237 // Get tree data async
Akron151bc872018-02-02 14:04:15 +0100238 this.getTreeData(foundry, layer, type, function (treeObj) {
Nils Diewald0ec142f2015-05-05 00:29:23 +0000239
Akrond67d45b2017-05-18 21:47:38 +0200240 tree.classList.remove('loading');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000241
Akrond67d45b2017-05-18 21:47:38 +0200242 // Something went wrong - probably log!!!
Nils Diewald0ec142f2015-05-05 00:29:23 +0000243
Akrond67d45b2017-05-18 21:47:38 +0200244 if (treeObj === null) {
Akron151bc872018-02-02 14:04:15 +0100245 tree.addT('No data available.');
Akrond67d45b2017-05-18 21:47:38 +0200246 }
247 else {
248 tree.appendChild(treeObj.element());
Akron0988d882017-11-10 16:13:12 +0100249 treeObj.show();
Akron151bc872018-02-02 14:04:15 +0100250
Akrond67d45b2017-05-18 21:47:38 +0200251 // Reposition the view to the center
252 // (This may in a future release be a reposition
Akron151bc872018-02-02 14:04:15 +0100253 // to move the root to the actual match)
Akronc56cf2d2016-11-09 22:02:38 +0100254
Akron0988d882017-11-10 16:13:12 +0100255 // This is currently not supported by relations
256 if (type === "spans") {
Akron0b489ad2018-02-02 16:49:32 +0100257 var dl = d.createElement('li');
Akron0988d882017-11-10 16:13:12 +0100258 dl.className = 'download';
259 dl.addEventListener(
260 'click', function (e) {
Akron151bc872018-02-02 14:04:15 +0100261 var a = treeObj.downloadLink();
Akron0b489ad2018-02-02 16:49:32 +0100262 d.body.appendChild(a);
Akron0988d882017-11-10 16:13:12 +0100263 a.click();
Akron0b489ad2018-02-02 16:49:32 +0100264 d.body.removeChild(a)
Akron0988d882017-11-10 16:13:12 +0100265 e.halt();
266 }
267 );
268
269 actions.appendChild(dl);
270 };
271
Akronc56cf2d2016-11-09 22:02:38 +0100272 treeObj.center();
Akrond67d45b2017-05-18 21:47:38 +0200273 };
274
275 if (cb !== undefined)
276 cb(treeObj);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000277 });
278 },
Akronbd342982018-01-25 18:01:46 +0100279
280
Akrone7679692018-01-26 12:06:33 +0100281 // Add meta information to match
Akron151bc872018-02-02 14:04:15 +0100282 showMeta : function () {
Akronaeceda72018-02-02 20:44:06 +0100283 var metaTable = document.createElement('div');
284 metaTable.classList.add('metatable'); // , 'loading');
Akronbd342982018-01-25 18:01:46 +0100285
286 // TODO: This is part of the getMeta!
287 var metaInfo = this._match.element().getAttribute('data-info');
288
289 if (metaInfo)
290 metaInfo = JSON.parse(metaInfo);
291
292 // There is metainfo
293 if (metaInfo) {
294
295 // Add metainfo to matchview
Akron151bc872018-02-02 14:04:15 +0100296 var metaElem = matchMetaClass.create(this._match).element(metaInfo);
Akronaeceda72018-02-02 20:44:06 +0100297 metaTable.appendChild(metaElem);
298 this.element().appendChild(metaTable);
299
300 // Add button
301 this._addButton('close', metaTable, function (e) {
302 this.parentNode.removeChild(this);
303 e.halt();
304 });
Akronbd342982018-01-25 18:01:46 +0100305 };
Akron41387d22018-02-02 18:10:06 +0100306
307 // Load data
Akronaeceda72018-02-02 20:44:06 +0100308 metaTable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100309 },
310
Akron151bc872018-02-02 14:04:15 +0100311
Akronbd342982018-01-25 18:01:46 +0100312 // Add table
Akron151bc872018-02-02 14:04:15 +0100313 showTable : function () {
Akronbd342982018-01-25 18:01:46 +0100314
Nils Diewald0e6992a2015-04-14 20:13:52 +0000315 // Append default table
Akron0b489ad2018-02-02 16:49:32 +0100316 var matchtable = d.createElement('div');
Akronaeceda72018-02-02 20:44:06 +0100317 matchtable.classList.add('matchtable'); // , 'loading');
318
319 var info = this.element();
320 info.appendChild(matchtable);
321
Nils Diewald0e6992a2015-04-14 20:13:52 +0000322
323 // Create the table asynchronous
Akron151bc872018-02-02 14:04:15 +0100324 this.getTableData(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100325
Akrond67d45b2017-05-18 21:47:38 +0200326 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100327 matchtable.appendChild(table.element());
328 };
Akron151bc872018-02-02 14:04:15 +0100329
330 // Load data
331 matchtable.classList.remove('loading');
Akron99713ef2017-06-28 18:19:28 +0200332
333 // Add query creator
Akrone8ea0002017-06-28 18:51:52 +0200334 this._matchCreator = matchQueryCreator.create(info);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000335 });
Akronaeceda72018-02-02 20:44:06 +0100336
337 // Add button
338 this._addButton('close', matchtable, function (e) {
339 this.parentNode.removeChild(this);
340 e.halt();
341 });
342
343 // Load data
344 matchtable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100345 },
346
Akronaeceda72018-02-02 20:44:06 +0100347
348 _addButton : function (buttonType, element, cb) {
349 // TODO: Unless existent
350 var actions = document.createElement('ul');
351 actions.classList.add('action', 'image');
352 var b = actions.addE('li');
353 b.className = buttonType;
354 b.addE('span').addT(buttonType);
355 b.addEventListener(
356 'click', cb.bind(element)
357 );
358
359 element.appendChild(actions);
360 return actions;
361 },
362
363
Akronbd342982018-01-25 18:01:46 +0100364 /**
365 * Create match information view.
366 */
367 element : function () {
368
369 if (this._element !== undefined)
370 return this._element;
371
372 // Create info table
Akron0b489ad2018-02-02 16:49:32 +0100373 var info = d.createElement('div');
Akronbd342982018-01-25 18:01:46 +0100374 info.classList.add('matchinfo');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000375
376 this._element = info;
377
Akronbd342982018-01-25 18:01:46 +0100378 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000379 }
380 };
381});