blob: 7880a63eb8cff99c0e1de22fdce7e0785a3020d0 [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',
Akron0988d882017-11-10 16:13:12 +01008 'match/relations',
Nils Diewald7c8ced22015-04-15 19:21:00 +00009 'match/treemenu',
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,
Akron0988d882017-11-10 16:13:12 +010015 matchRelClass,
Akron99713ef2017-06-28 18:19:28 +020016 matchTreeMenuClass,
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
25 var loc = KorAP.Locale;
26
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +000028
29 /**
30 * Create new match object
31 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 create : function (match) {
33 return Object.create(this)._init(match);
34 },
35
36 /**
37 * Initialize object
38 */
39 _init : function (match) {
40 this._match = match;
41 this.opened = false;
42 return this;
43 },
44
45 /**
46 * Get match object
47 */
48 match : function () {
49 return this._match;
50 },
51
Nils Diewald7148c6f2015-05-04 15:07:53 +000052
53 /**
54 * Open the information view,
55 * if closed, otherwise close.
56 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000057 toggle : function () {
Akron3bb91bc2016-12-02 16:43:17 +010058
Akron08b82d62016-12-05 15:06:05 +010059 var elem = this._match.element();
Akron3bb91bc2016-12-02 16:43:17 +010060
61 if (this.opened == true) {
Akrond67d45b2017-05-18 21:47:38 +020062 elem.removeChild(
63 this.element()
64 );
65 this.opened = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +000066 }
67 else {
Akrond67d45b2017-05-18 21:47:38 +020068 // Append element to match
Akron3bb91bc2016-12-02 16:43:17 +010069 elem.appendChild(
Akrond67d45b2017-05-18 21:47:38 +020070 this.element()
71 );
72 this.opened = true;
Nils Diewald0e6992a2015-04-14 20:13:52 +000073 };
74
75 return this.opened;
76 },
77
78
79 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000080 * Retrieve and parse snippet for table
81 * representation
Nils Diewald0e6992a2015-04-14 20:13:52 +000082 */
83 getTable : function (tokens, cb) {
84 var focus = [];
85
86 // Get all tokens
87 if (tokens === undefined) {
Akrond67d45b2017-05-18 21:47:38 +020088 focus = this._match.getTokens();
Nils Diewald0e6992a2015-04-14 20:13:52 +000089 }
90
91 // Get only some tokens
92 else {
Akrond67d45b2017-05-18 21:47:38 +020093
94 // Push newly to focus array
95 for (var i = 0; i < tokens.length; i++) {
96 var term = tokens[i];
97 try {
98 // Create info layer objects
99 var layer = infoLayerClass.create(term);
100 layer.type = "tokens";
101 focus.push(layer);
102 }
103 catch (e) {
104 continue;
105 };
106 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000107 };
108
109 // No tokens chosen
110 if (focus.length == 0)
Akrond67d45b2017-05-18 21:47:38 +0200111 cb(null);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000112
113 // Get info (may be cached)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000114 KorAP.API.getMatchInfo(
Akrond67d45b2017-05-18 21:47:38 +0200115 this._match,
116 { 'spans' : false, 'layer' : focus },
117
118 // Callback for retrieval
119 function (matchResponse) {
Akron3bb91bc2016-12-02 16:43:17 +0100120
Akron515851a2017-05-02 12:53:17 +0200121 if (matchResponse === undefined)
122 cb(null);
123
Akrond67d45b2017-05-18 21:47:38 +0200124 // Get snippet from match info
125 if (matchResponse["snippet"] !== undefined) {
126 this._table = matchTableClass.create(matchResponse["snippet"]);
127 cb(this._table);
128 };
129 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000130 );
131
132 /*
133 // Todo: Store the table as a hash of the focus
134 return null;
135 */
136 },
137
138
139 /**
140 * Retrieve and parse snippet for tree representation
141 */
Akron0988d882017-11-10 16:13:12 +0100142 getTree : function (foundry, layer, type, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000143 var focus = [];
144
145 // TODO: Support and cache multiple trees
146 KorAP.API.getMatchInfo(
Akrond67d45b2017-05-18 21:47:38 +0200147 this._match, {
148 'spans' : true,
149 'foundry' : foundry,
150 'layer' : layer
151 },
152 function (matchResponse) {
153 // Get snippet from match info
154 if (matchResponse["snippet"] !== undefined) {
155 // Todo: This should be cached somehow
Akronc56cf2d2016-11-09 22:02:38 +0100156
Akron0988d882017-11-10 16:13:12 +0100157 if (type === "spans") {
158 cb(matchTreeClass.create(matchResponse["snippet"]));
159 }
160 else if (type === "rels") {
161 cb(matchRelClass.create(matchResponse["snippet"]));
162 }
163
164 // Unknown tree type
165 else {
166 cb(null);
167 };
Akrond67d45b2017-05-18 21:47:38 +0200168 }
169 else {
170 cb(null);
171 };
172 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000173 );
174 },
175
176 /**
177 * Destroy this match information view.
178 */
179 destroy : function () {
180
181 // Remove circular reference
182 if (this._treeMenu !== undefined)
Akron99713ef2017-06-28 18:19:28 +0200183 delete this._treeMenu["info"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000184
185 this._treeMenu.destroy();
186 this._treeMenu = undefined;
187 this._match = undefined;
Akron99713ef2017-06-28 18:19:28 +0200188 this._matchCreator = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000189 // Element destroy
190 },
191
192 /**
193 * Add a new tree view to the list
194 */
Akron0988d882017-11-10 16:13:12 +0100195 addTree : function (foundry, layer, type, cb) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000196 var matchtree = document.createElement('div');
197 matchtree.classList.add('matchtree');
198
199 var h6 = matchtree.appendChild(document.createElement('h6'));
200 h6.appendChild(document.createElement('span'))
Akron99713ef2017-06-28 18:19:28 +0200201 .appendChild(document.createTextNode(foundry));
Nils Diewald0e6992a2015-04-14 20:13:52 +0000202 h6.appendChild(document.createElement('span'))
Akrond67d45b2017-05-18 21:47:38 +0200203 .appendChild(document.createTextNode(layer));
204
Nils Diewald0e6992a2015-04-14 20:13:52 +0000205 var tree = matchtree.appendChild(
Akrond67d45b2017-05-18 21:47:38 +0200206 document.createElement('div')
Nils Diewald0e6992a2015-04-14 20:13:52 +0000207 );
208
209 this._element.insertBefore(matchtree, this._element.lastChild);
210
Akronc56cf2d2016-11-09 22:02:38 +0100211 var actions = tree.appendChild(document.createElement('ul'));
212 actions.classList.add('action', 'image');
213 var close = actions.appendChild(document.createElement('li'));
214 close.className = 'close';
215 close.appendChild(document.createElement('span'));
Nils Diewald0e6992a2015-04-14 20:13:52 +0000216 close.addEventListener(
Akrond67d45b2017-05-18 21:47:38 +0200217 'click', function (e) {
218 matchtree.parentNode.removeChild(matchtree);
219 e.halt();
220 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000221 );
222
Nils Diewald0ec142f2015-05-05 00:29:23 +0000223 tree.classList.add('loading');
224
Nils Diewald0e6992a2015-04-14 20:13:52 +0000225 // Get tree data async
Akron0988d882017-11-10 16:13:12 +0100226 this.getTree(foundry, layer, type, function (treeObj) {
Nils Diewald0ec142f2015-05-05 00:29:23 +0000227
Akrond67d45b2017-05-18 21:47:38 +0200228 tree.classList.remove('loading');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000229
Akrond67d45b2017-05-18 21:47:38 +0200230 // Something went wrong - probably log!!!
Nils Diewald0ec142f2015-05-05 00:29:23 +0000231
Akrond67d45b2017-05-18 21:47:38 +0200232 if (treeObj === null) {
233 tree.appendChild(document.createTextNode('No data available.'));
234 }
235 else {
236 tree.appendChild(treeObj.element());
Akron0988d882017-11-10 16:13:12 +0100237 treeObj.show();
Akrond67d45b2017-05-18 21:47:38 +0200238 // Reposition the view to the center
239 // (This may in a future release be a reposition
240 // to move the root into the center or the actual
241 // match)
Akronc56cf2d2016-11-09 22:02:38 +0100242
Akron0988d882017-11-10 16:13:12 +0100243 // This is currently not supported by relations
244 if (type === "spans") {
245 var dl = document.createElement('li');
246 dl.className = 'download';
247 dl.addEventListener(
248 'click', function (e) {
Akronc56cf2d2016-11-09 22:02:38 +0100249
Akron0988d882017-11-10 16:13:12 +0100250 var a = document.createElement('a');
251 a.setAttribute('href-lang', 'image/svg+xml');
252 a.setAttribute('href', 'data:image/svg+xml;base64,'+treeObj.toBase64());
253 a.setAttribute('download', 'tree.svg');
254 a.target = '_blank';
255
256 document.body.appendChild(a);
257 a.click();
258 document.body.removeChild(a)
259 e.halt();
260 }
261 );
262
263 actions.appendChild(dl);
264 };
265
Akronc56cf2d2016-11-09 22:02:38 +0100266 treeObj.center();
Akrond67d45b2017-05-18 21:47:38 +0200267 };
268
269 if (cb !== undefined)
270 cb(treeObj);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000271 });
272 },
273
274 /**
275 * Create match information view.
276 */
277 element : function () {
278
279 if (this._element !== undefined)
Akrond67d45b2017-05-18 21:47:38 +0200280 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000281
282 // Create info table
283 var info = document.createElement('div');
284 info.classList.add('matchinfo');
285
286 // Append default table
287 var matchtable = document.createElement('div');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000288 matchtable.classList.add('matchtable', 'loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000289 info.appendChild(matchtable);
290
291 // Create the table asynchronous
292 this.getTable(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100293
Akrond67d45b2017-05-18 21:47:38 +0200294 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100295 matchtable.appendChild(table.element());
296 };
Akron515851a2017-05-02 12:53:17 +0200297 matchtable.classList.remove('loading');
Akron99713ef2017-06-28 18:19:28 +0200298
299 // Add query creator
Akrone8ea0002017-06-28 18:51:52 +0200300 this._matchCreator = matchQueryCreator.create(info);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000301 });
302
Akrond67d45b2017-05-18 21:47:38 +0200303 // Join spans and relations
304 var treeLayers = []
305 var spans = this._match.getSpans();
306 var rels = this._match.getRels();
307 var i;
308 for (i in spans) {
309 treeLayers.push(spans[i]);
310 };
311 for (i in rels) {
312 treeLayers.push(rels[i]);
313 };
314
Nils Diewald0e6992a2015-04-14 20:13:52 +0000315 // Get spans
Akrond67d45b2017-05-18 21:47:38 +0200316 treeLayers = treeLayers.sort(
317 function (a, b) {
318 if (a.foundry < b.foundry) {
319 return -1;
320 }
321 else if (a.foundry > b.foundry) {
322 return 1;
323 }
324 else if (a.layer < b.layer) {
325 return -1;
326 }
327 else if (a.layer > b.layer) {
328 return 1;
329 };
330 return 0;
331 });
Nils Diewald0e6992a2015-04-14 20:13:52 +0000332
333 var menuList = [];
334
335 // Show tree views
Akrond67d45b2017-05-18 21:47:38 +0200336 for (var i = 0; i < treeLayers.length; i++) {
337 var span = treeLayers[i];
338
339 // Add foundry/layer to menu list
340 menuList.push([
341 span.foundry + '/' + span.layer,
342 span.foundry,
Akron0988d882017-11-10 16:13:12 +0100343 span.layer,
344 span.type
Akrond67d45b2017-05-18 21:47:38 +0200345 ]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000346 };
347
348 // Create tree menu
349 var treemenu = this.treeMenu(menuList);
350 var span = info.appendChild(document.createElement('p'));
351 span.classList.add('addtree');
352 span.appendChild(document.createTextNode(loc.ADDTREE));
353
354 var treeElement = treemenu.element();
355 span.appendChild(treeElement);
356
357 span.addEventListener('click', function (e) {
Akrond67d45b2017-05-18 21:47:38 +0200358 treemenu.show();
359 treemenu.focus();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000360 });
361
362 this._element = info;
363
364 return info;
365 },
366
367
368 /**
369 * Get tree menu.
370 * There is only one menu rendered
371 * - no matter how many trees exist
372 */
373 treeMenu : function (list) {
374 if (this._treeMenu !== undefined)
Akrond67d45b2017-05-18 21:47:38 +0200375 return this._treeMenu;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000376
377 return this._treeMenu = matchTreeMenuClass.create(this, list);
378 }
379 };
380});