blob: 43319e2eb6414be2daf86c409bd923ecd7c70ba3 [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',
7 'match/tree',
Akron151bc872018-02-02 14:04:15 +01008 'match/meta',
Akron0988d882017-11-10 16:13:12 +01009 'match/relations',
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,
14 matchTreeClass,
Akron151bc872018-02-02 14:04:15 +010015 matchMetaClass,
Akron0988d882017-11-10 16:13:12 +010016 matchRelClass,
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") {
168 cb(matchTreeClass.create(matchResponse["snippet"]));
169 }
170 else if (type === "rels") {
171 cb(matchRelClass.create(matchResponse["snippet"]));
172 }
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 () {
Akron0b489ad2018-02-02 16:49:32 +0100283 var matchmeta = d.createElement('div');
Akronbd342982018-01-25 18:01:46 +0100284
285 // TODO: This is part of the getMeta!
286 var metaInfo = this._match.element().getAttribute('data-info');
287
288 if (metaInfo)
289 metaInfo = JSON.parse(metaInfo);
290
291 // There is metainfo
292 if (metaInfo) {
293
294 // Add metainfo to matchview
Akron151bc872018-02-02 14:04:15 +0100295 var metaElem = matchMetaClass.create(this._match).element(metaInfo);
Akrone7679692018-01-26 12:06:33 +0100296 var elem = this.element();
Akronbd342982018-01-25 18:01:46 +0100297
Akrone7679692018-01-26 12:06:33 +0100298 elem.insertBefore(
299 metaElem,
300 elem.firstChild
301 );
Akronbd342982018-01-25 18:01:46 +0100302 };
303 },
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
309 var info = this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000310
311 // Append default table
Akron0b489ad2018-02-02 16:49:32 +0100312 var matchtable = d.createElement('div');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000313 matchtable.classList.add('matchtable', 'loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000314 info.appendChild(matchtable);
315
316 // Create the table asynchronous
Akron151bc872018-02-02 14:04:15 +0100317 this.getTableData(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100318
Akrond67d45b2017-05-18 21:47:38 +0200319 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100320 matchtable.appendChild(table.element());
321 };
Akron151bc872018-02-02 14:04:15 +0100322
323 // Load data
324 matchtable.classList.remove('loading');
Akron99713ef2017-06-28 18:19:28 +0200325
326 // Add query creator
Akrone8ea0002017-06-28 18:51:52 +0200327 this._matchCreator = matchQueryCreator.create(info);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000328 });
Akronbd342982018-01-25 18:01:46 +0100329 },
330
Akron151bc872018-02-02 14:04:15 +0100331
Akronbd342982018-01-25 18:01:46 +0100332 /**
333 * Create match information view.
334 */
335 element : function () {
336
337 if (this._element !== undefined)
338 return this._element;
339
340 // Create info table
Akron0b489ad2018-02-02 16:49:32 +0100341 var info = d.createElement('div');
Akronbd342982018-01-25 18:01:46 +0100342 info.classList.add('matchinfo');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000343
344 this._element = info;
345
Akronbd342982018-01-25 18:01:46 +0100346 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000347 }
348 };
349});