blob: ac741ae8d0c816714dff4fe542ded8a6f736f14d [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');
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 () {
Akronaeceda72018-02-02 20:44:06 +0100276 var metaTable = document.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100277 metaTable.classList.add('metatable', 'loading');
278 this.element().appendChild(metaTable);
Akronbd342982018-01-25 18:01:46 +0100279
280 // TODO: This is part of the getMeta!
281 var metaInfo = this._match.element().getAttribute('data-info');
282
283 if (metaInfo)
284 metaInfo = JSON.parse(metaInfo);
285
286 // There is metainfo
287 if (metaInfo) {
288
Akronc8eb4a12018-02-03 00:39:58 +0100289 // Load data
290 metaTable.classList.remove('loading');
291
Akronbd342982018-01-25 18:01:46 +0100292 // Add metainfo to matchview
Akron151bc872018-02-02 14:04:15 +0100293 var metaElem = matchMetaClass.create(this._match).element(metaInfo);
Akronaeceda72018-02-02 20:44:06 +0100294 metaTable.appendChild(metaElem);
Akronaeceda72018-02-02 20:44:06 +0100295
296 // Add button
297 this._addButton('close', metaTable, function (e) {
298 this.parentNode.removeChild(this);
299 e.halt();
300 });
Akronbd342982018-01-25 18:01:46 +0100301 };
Akron41387d22018-02-02 18:10:06 +0100302
303 // Load data
Akronaeceda72018-02-02 20:44:06 +0100304 metaTable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100305 },
306
Akron151bc872018-02-02 14:04:15 +0100307
Akronbd342982018-01-25 18:01:46 +0100308 // Add table
Akron151bc872018-02-02 14:04:15 +0100309 showTable : function () {
Akronbd342982018-01-25 18:01:46 +0100310
Nils Diewald0e6992a2015-04-14 20:13:52 +0000311 // Append default table
Akron0b489ad2018-02-02 16:49:32 +0100312 var matchtable = d.createElement('div');
Akronc8eb4a12018-02-03 00:39:58 +0100313 matchtable.classList.add('matchtable', 'loading');
Akronaeceda72018-02-02 20:44:06 +0100314 var info = this.element();
315 info.appendChild(matchtable);
316
Nils Diewald0e6992a2015-04-14 20:13:52 +0000317
318 // Create the table asynchronous
Akron151bc872018-02-02 14:04:15 +0100319 this.getTableData(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100320
Akronc8eb4a12018-02-03 00:39:58 +0100321 // Load data
322 matchtable.classList.remove('loading');
323
Akrond67d45b2017-05-18 21:47:38 +0200324 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100325 matchtable.appendChild(table.element());
326 };
Akron151bc872018-02-02 14:04:15 +0100327
328 // Load data
329 matchtable.classList.remove('loading');
Akron99713ef2017-06-28 18:19:28 +0200330
331 // Add query creator
Akrone8ea0002017-06-28 18:51:52 +0200332 this._matchCreator = matchQueryCreator.create(info);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000333 });
Akronaeceda72018-02-02 20:44:06 +0100334
335 // Add button
336 this._addButton('close', matchtable, function (e) {
337 this.parentNode.removeChild(this);
338 e.halt();
339 });
340
341 // Load data
342 matchtable.classList.remove('loading');
Akronbd342982018-01-25 18:01:46 +0100343 },
344
Akronc8eb4a12018-02-03 00:39:58 +0100345 // Add action button
Akronaeceda72018-02-02 20:44:06 +0100346 _addButton : function (buttonType, element, cb) {
347 // TODO: Unless existent
348 var actions = document.createElement('ul');
349 actions.classList.add('action', 'image');
350 var b = actions.addE('li');
351 b.className = buttonType;
352 b.addE('span').addT(buttonType);
353 b.addEventListener(
354 'click', cb.bind(element)
355 );
356
357 element.appendChild(actions);
358 return actions;
359 },
360
361
Akronbd342982018-01-25 18:01:46 +0100362 /**
363 * Create match information view.
364 */
365 element : function () {
366
367 if (this._element !== undefined)
368 return this._element;
369
370 // Create info table
Akron0b489ad2018-02-02 16:49:32 +0100371 var info = d.createElement('div');
Akronbd342982018-01-25 18:01:46 +0100372 info.classList.add('matchinfo');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000373
374 this._element = info;
375
Akronbd342982018-01-25 18:01:46 +0100376 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000377 }
378 };
379});