blob: 7a659ba80b20e3bc2600eba8caa0e03c0f7b2353 [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',
8 'match/treemenu',
Akron99713ef2017-06-28 18:19:28 +02009 'match/querycreator',
Nils Diewald7c8ced22015-04-15 19:21:00 +000010 'util'
11], function (infoLayerClass,
Akron3bb91bc2016-12-02 16:43:17 +010012 matchTableClass,
13 matchTreeClass,
Akron99713ef2017-06-28 18:19:28 +020014 matchTreeMenuClass,
15 matchQueryCreator) {
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
23 var loc = KorAP.Locale;
24
Nils Diewald0e6992a2015-04-14 20:13:52 +000025 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +000026
27 /**
28 * Create new match object
29 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000030 create : function (match) {
31 return Object.create(this)._init(match);
32 },
33
34 /**
35 * Initialize object
36 */
37 _init : function (match) {
38 this._match = match;
39 this.opened = false;
40 return this;
41 },
42
43 /**
44 * Get match object
45 */
46 match : function () {
47 return this._match;
48 },
49
Nils Diewald7148c6f2015-05-04 15:07:53 +000050
51 /**
52 * Open the information view,
53 * if closed, otherwise close.
54 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000055 toggle : function () {
Akron3bb91bc2016-12-02 16:43:17 +010056
Akron08b82d62016-12-05 15:06:05 +010057 var elem = this._match.element();
Akron3bb91bc2016-12-02 16:43:17 +010058
59 if (this.opened == true) {
60 elem.removeChild(
61 this.element()
62 );
63 this.opened = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +000064 }
65 else {
Akron3bb91bc2016-12-02 16:43:17 +010066 // Append element to match
67 elem.appendChild(
68 this.element()
69 );
70 this.opened = true;
Nils Diewald0e6992a2015-04-14 20:13:52 +000071 };
72
73 return this.opened;
74 },
75
76
77 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000078 * Retrieve and parse snippet for table
79 * representation
Nils Diewald0e6992a2015-04-14 20:13:52 +000080 */
81 getTable : function (tokens, cb) {
82 var focus = [];
83
84 // Get all tokens
85 if (tokens === undefined) {
Akron3bb91bc2016-12-02 16:43:17 +010086 focus = this._match.getTokens();
Nils Diewald0e6992a2015-04-14 20:13:52 +000087 }
88
89 // Get only some tokens
90 else {
91
Akron3bb91bc2016-12-02 16:43:17 +010092 // Push newly to focus array
93 for (var i = 0; i < tokens.length; i++) {
94 var term = tokens[i];
95 try {
96 // Create info layer objects
97 var layer = infoLayerClass.create(term);
98 layer.type = "tokens";
99 focus.push(layer);
100 }
101 catch (e) {
102 continue;
103 };
104 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000105 };
106
107 // No tokens chosen
108 if (focus.length == 0)
Akron3bb91bc2016-12-02 16:43:17 +0100109 cb(null);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000110
111 // Get info (may be cached)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000112 KorAP.API.getMatchInfo(
Akron3bb91bc2016-12-02 16:43:17 +0100113 this._match,
114 { 'spans' : false, 'layer' : focus },
Nils Diewald0e6992a2015-04-14 20:13:52 +0000115
Akron3bb91bc2016-12-02 16:43:17 +0100116 // Callback for retrieval
117 function (matchResponse) {
118
Akron515851a2017-05-02 12:53:17 +0200119 if (matchResponse === undefined)
120 cb(null);
121
Akron3bb91bc2016-12-02 16:43:17 +0100122 // Get snippet from match info
123 if (matchResponse["snippet"] !== undefined) {
124 this._table = matchTableClass.create(matchResponse["snippet"]);
125 cb(this._table);
126 };
127 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000128 );
129
130 /*
131 // Todo: Store the table as a hash of the focus
132 return null;
133 */
134 },
135
136
137 /**
138 * Retrieve and parse snippet for tree representation
139 */
140 getTree : function (foundry, layer, cb) {
141 var focus = [];
142
143 // TODO: Support and cache multiple trees
144 KorAP.API.getMatchInfo(
Akronc56cf2d2016-11-09 22:02:38 +0100145 this._match, {
146 'spans' : true,
147 'foundry' : foundry,
148 'layer' : layer
149 },
150 function (matchResponse) {
151 // Get snippet from match info
152 if (matchResponse["snippet"] !== undefined) {
153 // Todo: This should be cached somehow
154
155 cb(matchTreeClass.create(matchResponse["snippet"]));
156 }
157 else {
158 cb(null);
159 };
160 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000161 );
162 },
163
164 /**
165 * Destroy this match information view.
166 */
167 destroy : function () {
168
169 // Remove circular reference
170 if (this._treeMenu !== undefined)
Akron99713ef2017-06-28 18:19:28 +0200171 delete this._treeMenu["info"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000172
173 this._treeMenu.destroy();
174 this._treeMenu = undefined;
175 this._match = undefined;
Akron99713ef2017-06-28 18:19:28 +0200176 this._matchCreator = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000177 // Element destroy
178 },
179
180 /**
181 * Add a new tree view to the list
182 */
183 addTree : function (foundry, layer, cb) {
184 var matchtree = document.createElement('div');
185 matchtree.classList.add('matchtree');
186
187 var h6 = matchtree.appendChild(document.createElement('h6'));
188 h6.appendChild(document.createElement('span'))
Akron99713ef2017-06-28 18:19:28 +0200189 .appendChild(document.createTextNode(foundry));
Nils Diewald0e6992a2015-04-14 20:13:52 +0000190 h6.appendChild(document.createElement('span'))
Akron99713ef2017-06-28 18:19:28 +0200191 .appendChild(document.createTextNode(layer));
Nils Diewald0e6992a2015-04-14 20:13:52 +0000192
193 var tree = matchtree.appendChild(
Akronc56cf2d2016-11-09 22:02:38 +0100194 document.createElement('div')
Nils Diewald0e6992a2015-04-14 20:13:52 +0000195 );
196
197 this._element.insertBefore(matchtree, this._element.lastChild);
198
Akronc56cf2d2016-11-09 22:02:38 +0100199 var actions = tree.appendChild(document.createElement('ul'));
200 actions.classList.add('action', 'image');
201 var close = actions.appendChild(document.createElement('li'));
202 close.className = 'close';
203 close.appendChild(document.createElement('span'));
Nils Diewald0e6992a2015-04-14 20:13:52 +0000204 close.addEventListener(
Akronc56cf2d2016-11-09 22:02:38 +0100205 'click', function (e) {
206 matchtree.parentNode.removeChild(matchtree);
207 e.halt();
208 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000209 );
210
Nils Diewald0ec142f2015-05-05 00:29:23 +0000211 tree.classList.add('loading');
212
Nils Diewald0e6992a2015-04-14 20:13:52 +0000213 // Get tree data async
214 this.getTree(foundry, layer, function (treeObj) {
Nils Diewald0ec142f2015-05-05 00:29:23 +0000215
Akronc56cf2d2016-11-09 22:02:38 +0100216 tree.classList.remove('loading');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000217
Akronc56cf2d2016-11-09 22:02:38 +0100218 // Something went wrong - probably log!!!
Nils Diewald0ec142f2015-05-05 00:29:23 +0000219
Akronc56cf2d2016-11-09 22:02:38 +0100220 if (treeObj === null) {
221 tree.appendChild(document.createTextNode('No data available.'));
222 }
223 else {
224 tree.appendChild(treeObj.element());
225 // Reposition the view to the center
226 // (This may in a future release be a reposition
227 // to move the root into the center or the actual
228 // match)
229
230 var dl = document.createElement('li');
231 dl.className = 'download';
232 dl.addEventListener(
233 'click', function (e) {
234
235 var a = document.createElement('a');
236 a.setAttribute('href-lang', 'image/svg+xml');
237 a.setAttribute('href', 'data:image/svg+xml;base64,'+treeObj.toBase64());
238 a.setAttribute('download', 'tree.svg');
239 a.target = '_blank';
240
241 document.body.appendChild(a);
242 a.click();
243 document.body.removeChild(a)
244
245 e.halt();
246 }
247 );
248 actions.appendChild(dl);
249 treeObj.center();
250 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000251
Akronc56cf2d2016-11-09 22:02:38 +0100252 if (cb !== undefined)
253 cb(treeObj);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000254 });
255 },
256
257 /**
258 * Create match information view.
259 */
260 element : function () {
261
262 if (this._element !== undefined)
Akronc56cf2d2016-11-09 22:02:38 +0100263 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000264
265 // Create info table
266 var info = document.createElement('div');
267 info.classList.add('matchinfo');
268
269 // Append default table
270 var matchtable = document.createElement('div');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000271 matchtable.classList.add('matchtable', 'loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000272 info.appendChild(matchtable);
273
274 // Create the table asynchronous
275 this.getTable(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100276
277 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100278 matchtable.appendChild(table.element());
279 };
Akron515851a2017-05-02 12:53:17 +0200280 matchtable.classList.remove('loading');
Akron99713ef2017-06-28 18:19:28 +0200281
282 // Add query creator
Akrone8ea0002017-06-28 18:51:52 +0200283 this._matchCreator = matchQueryCreator.create(info);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000284 });
285
286 // Get spans
287 var spanLayers = this._match.getSpans().sort(
Akron3bb91bc2016-12-02 16:43:17 +0100288 function (a, b) {
289 if (a.foundry < b.foundry) {
290 return -1;
291 }
292 else if (a.foundry > b.foundry) {
293 return 1;
294 }
295 else if (a.layer < b.layer) {
296 return -1;
297 }
298 else if (a.layer > b.layer) {
299 return 1;
300 };
301 return 0;
302 });
Nils Diewald0e6992a2015-04-14 20:13:52 +0000303
304 var menuList = [];
305
306 // Show tree views
307 for (var i = 0; i < spanLayers.length; i++) {
Akron3bb91bc2016-12-02 16:43:17 +0100308 var span = spanLayers[i];
309
310 // Add foundry/layer to menu list
311 menuList.push([
312 span.foundry + '/' + span.layer,
313 span.foundry,
314 span.layer
315 ]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000316 };
317
318 // Create tree menu
319 var treemenu = this.treeMenu(menuList);
320 var span = info.appendChild(document.createElement('p'));
321 span.classList.add('addtree');
322 span.appendChild(document.createTextNode(loc.ADDTREE));
323
324 var treeElement = treemenu.element();
325 span.appendChild(treeElement);
326
327 span.addEventListener('click', function (e) {
Akron3bb91bc2016-12-02 16:43:17 +0100328 treemenu.show();
329 treemenu.focus();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000330 });
331
332 this._element = info;
333
334 return info;
335 },
336
337
338 /**
339 * Get tree menu.
340 * There is only one menu rendered
341 * - no matter how many trees exist
342 */
343 treeMenu : function (list) {
344 if (this._treeMenu !== undefined)
Akron3bb91bc2016-12-02 16:43:17 +0100345 return this._treeMenu;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000346
347 return this._treeMenu = matchTreeMenuClass.create(this, list);
348 }
349 };
350});