blob: 249cad5e04eafbe7be17ac2c3f419a4d39d9853d [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',
Akron671fdb92017-09-12 18:09:46 +020010 'match/relations',
Nils Diewald7c8ced22015-04-15 19:21:00 +000011 'util'
12], function (infoLayerClass,
Akron3bb91bc2016-12-02 16:43:17 +010013 matchTableClass,
14 matchTreeClass,
Akron99713ef2017-06-28 18:19:28 +020015 matchTreeMenuClass,
Akron671fdb92017-09-12 18:09:46 +020016 matchQueryCreator,
17 matchRelClass) {
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 */
142 getTree : function (foundry, layer, cb) {
143 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
Akrond67d45b2017-05-18 21:47:38 +0200157 cb(matchTreeClass.create(matchResponse["snippet"]));
158 }
159 else {
160 cb(null);
161 };
162 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000163 );
164 },
165
166 /**
167 * Destroy this match information view.
168 */
169 destroy : function () {
170
171 // Remove circular reference
172 if (this._treeMenu !== undefined)
Akron99713ef2017-06-28 18:19:28 +0200173 delete this._treeMenu["info"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000174
175 this._treeMenu.destroy();
176 this._treeMenu = undefined;
177 this._match = undefined;
Akron99713ef2017-06-28 18:19:28 +0200178 this._matchCreator = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000179 // Element destroy
180 },
181
182 /**
183 * Add a new tree view to the list
184 */
185 addTree : function (foundry, layer, cb) {
186 var matchtree = document.createElement('div');
187 matchtree.classList.add('matchtree');
188
189 var h6 = matchtree.appendChild(document.createElement('h6'));
190 h6.appendChild(document.createElement('span'))
Akron99713ef2017-06-28 18:19:28 +0200191 .appendChild(document.createTextNode(foundry));
Nils Diewald0e6992a2015-04-14 20:13:52 +0000192 h6.appendChild(document.createElement('span'))
Akrond67d45b2017-05-18 21:47:38 +0200193 .appendChild(document.createTextNode(layer));
194
Nils Diewald0e6992a2015-04-14 20:13:52 +0000195 var tree = matchtree.appendChild(
Akrond67d45b2017-05-18 21:47:38 +0200196 document.createElement('div')
Nils Diewald0e6992a2015-04-14 20:13:52 +0000197 );
198
199 this._element.insertBefore(matchtree, this._element.lastChild);
200
Akronc56cf2d2016-11-09 22:02:38 +0100201 var actions = tree.appendChild(document.createElement('ul'));
202 actions.classList.add('action', 'image');
203 var close = actions.appendChild(document.createElement('li'));
204 close.className = 'close';
205 close.appendChild(document.createElement('span'));
Nils Diewald0e6992a2015-04-14 20:13:52 +0000206 close.addEventListener(
Akrond67d45b2017-05-18 21:47:38 +0200207 'click', function (e) {
208 matchtree.parentNode.removeChild(matchtree);
209 e.halt();
210 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000211 );
212
Nils Diewald0ec142f2015-05-05 00:29:23 +0000213 tree.classList.add('loading');
214
Nils Diewald0e6992a2015-04-14 20:13:52 +0000215 // Get tree data async
216 this.getTree(foundry, layer, function (treeObj) {
Nils Diewald0ec142f2015-05-05 00:29:23 +0000217
Akrond67d45b2017-05-18 21:47:38 +0200218 tree.classList.remove('loading');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000219
Akrond67d45b2017-05-18 21:47:38 +0200220 // Something went wrong - probably log!!!
Nils Diewald0ec142f2015-05-05 00:29:23 +0000221
Akrond67d45b2017-05-18 21:47:38 +0200222 if (treeObj === null) {
223 tree.appendChild(document.createTextNode('No data available.'));
224 }
225 else {
226 tree.appendChild(treeObj.element());
227 // Reposition the view to the center
228 // (This may in a future release be a reposition
229 // to move the root into the center or the actual
230 // match)
Akronc56cf2d2016-11-09 22:02:38 +0100231
232 var dl = document.createElement('li');
233 dl.className = 'download';
234 dl.addEventListener(
Akrond67d45b2017-05-18 21:47:38 +0200235 'click', function (e) {
Akronc56cf2d2016-11-09 22:02:38 +0100236
237 var a = document.createElement('a');
238 a.setAttribute('href-lang', 'image/svg+xml');
239 a.setAttribute('href', 'data:image/svg+xml;base64,'+treeObj.toBase64());
240 a.setAttribute('download', 'tree.svg');
241 a.target = '_blank';
242
243 document.body.appendChild(a);
244 a.click();
245 document.body.removeChild(a)
246
247 e.halt();
Akrond67d45b2017-05-18 21:47:38 +0200248 }
Akronc56cf2d2016-11-09 22:02:38 +0100249 );
250 actions.appendChild(dl);
251 treeObj.center();
Akrond67d45b2017-05-18 21:47:38 +0200252 };
253
254 if (cb !== undefined)
255 cb(treeObj);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000256 });
257 },
258
259 /**
260 * Create match information view.
261 */
262 element : function () {
263
264 if (this._element !== undefined)
Akrond67d45b2017-05-18 21:47:38 +0200265 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000266
267 // Create info table
268 var info = document.createElement('div');
269 info.classList.add('matchinfo');
270
271 // Append default table
272 var matchtable = document.createElement('div');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000273 matchtable.classList.add('matchtable', 'loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000274 info.appendChild(matchtable);
275
276 // Create the table asynchronous
277 this.getTable(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100278
Akrond67d45b2017-05-18 21:47:38 +0200279 if (table !== null) {
Akron3bb91bc2016-12-02 16:43:17 +0100280 matchtable.appendChild(table.element());
281 };
Akron515851a2017-05-02 12:53:17 +0200282 matchtable.classList.remove('loading');
Akron99713ef2017-06-28 18:19:28 +0200283
284 // Add query creator
Akrone8ea0002017-06-28 18:51:52 +0200285 this._matchCreator = matchQueryCreator.create(info);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000286 });
287
Akrond67d45b2017-05-18 21:47:38 +0200288 // Join spans and relations
289 var treeLayers = []
290 var spans = this._match.getSpans();
291 var rels = this._match.getRels();
292 var i;
293 for (i in spans) {
294 treeLayers.push(spans[i]);
295 };
296 for (i in rels) {
297 treeLayers.push(rels[i]);
298 };
299
Nils Diewald0e6992a2015-04-14 20:13:52 +0000300 // Get spans
Akrond67d45b2017-05-18 21:47:38 +0200301 treeLayers = treeLayers.sort(
302 function (a, b) {
303 if (a.foundry < b.foundry) {
304 return -1;
305 }
306 else if (a.foundry > b.foundry) {
307 return 1;
308 }
309 else if (a.layer < b.layer) {
310 return -1;
311 }
312 else if (a.layer > b.layer) {
313 return 1;
314 };
315 return 0;
316 });
Nils Diewald0e6992a2015-04-14 20:13:52 +0000317
318 var menuList = [];
319
320 // Show tree views
Akrond67d45b2017-05-18 21:47:38 +0200321 for (var i = 0; i < treeLayers.length; i++) {
322 var span = treeLayers[i];
323
324 // Add foundry/layer to menu list
325 menuList.push([
326 span.foundry + '/' + span.layer,
327 span.foundry,
328 span.layer
329 ]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000330 };
331
332 // Create tree menu
333 var treemenu = this.treeMenu(menuList);
334 var span = info.appendChild(document.createElement('p'));
335 span.classList.add('addtree');
336 span.appendChild(document.createTextNode(loc.ADDTREE));
337
338 var treeElement = treemenu.element();
339 span.appendChild(treeElement);
340
341 span.addEventListener('click', function (e) {
Akrond67d45b2017-05-18 21:47:38 +0200342 treemenu.show();
343 treemenu.focus();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000344 });
345
346 this._element = info;
347
348 return info;
349 },
350
351
352 /**
353 * Get tree menu.
354 * There is only one menu rendered
355 * - no matter how many trees exist
356 */
357 treeMenu : function (list) {
358 if (this._treeMenu !== undefined)
Akrond67d45b2017-05-18 21:47:38 +0200359 return this._treeMenu;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000360
361 return this._treeMenu = matchTreeMenuClass.create(this, list);
362 }
363 };
364});