blob: bca7f09bff985e8b468655713589f16a964090ed [file] [log] [blame]
Nils Diewalde8518f82015-03-18 22:41:49 +00001/**
Nils Diewalda297f062015-04-02 00:23:46 +00002 * Get information on matches,
3 * generate annotation tables and trees.
Nils Diewalde8518f82015-03-18 22:41:49 +00004 *
5 * @author Nils Diewald
6 */
7/*
Nils Diewald6e43ffd2015-03-25 18:55:39 +00008 * - Highlight (at least mark as bold) the match
9 * - Scroll to match vertically per default
Akron02360e42016-06-07 13:41:12 +020010 * - A click on a table field and a tree node should at the field description to the fragments list.
Nils Diewalde8518f82015-03-18 22:41:49 +000011 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000012define([
Akron24866cf2018-01-23 20:22:01 +010013 'match/info', // rename to anno
Akron8b592d42018-01-26 18:33:06 +010014 'match/treemenu',
15 'util'
16], function (infoClass,matchTreeMenuClass) { //, refClass) {
Nils Diewald4f6521a2015-03-20 21:30:13 +000017
Nils Diewald6e43ffd2015-03-25 18:55:39 +000018 // Localization values
Akron0b489ad2018-02-02 16:49:32 +010019 const loc = KorAP.Locale;
Akron24866cf2018-01-23 20:22:01 +010020 loc.ADDTREE = loc.ADDTREE || 'Add tree view';
21 loc.SHOWINFO = loc.SHOWINFO || 'Show information';
22 loc.CLOSE = loc.CLOSE || 'Close';
23 loc.SHOW_META = loc.SHOW_META || 'Show metadata';
Nils Diewald0e6992a2015-04-14 20:13:52 +000024
Akron0a6768f2016-07-13 18:00:43 +020025 // 'corpusID', 'docID', 'textID'
Akron0b489ad2018-02-02 16:49:32 +010026 const _matchTerms = ['textSigle', 'matchID', 'available'];
27
28 const d = document;
Nils Diewalda297f062015-04-02 00:23:46 +000029
30 /**
31 * Match object
32 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 return {
Nils Diewalde8518f82015-03-18 22:41:49 +000034
35 /**
36 * Create a new annotation object.
37 * Expects an array of available foundry/layer=type terms.
38 * Supported types are 'spans', 'tokens' and 'rels'.
39 */
Nils Diewalda297f062015-04-02 00:23:46 +000040 create : function (match) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000041 return Object.create(this)._init(match);
Nils Diewalde8518f82015-03-18 22:41:49 +000042 },
43
Nils Diewald7c8ced22015-04-15 19:21:00 +000044
Nils Diewald6e43ffd2015-03-25 18:55:39 +000045 /**
Nils Diewalda297f062015-04-02 00:23:46 +000046 * Initialize match.
Nils Diewald6e43ffd2015-03-25 18:55:39 +000047 */
Nils Diewalda297f062015-04-02 00:23:46 +000048 _init : function (match) {
49 this._element = null;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000050
Nils Diewalda297f062015-04-02 00:23:46 +000051 // No match defined
52 if (arguments.length < 1 ||
Akron19d97fe2016-09-06 20:47:05 +020053 match === null ||
54 match === undefined) {
55 throw new Error('Missing parameters');
Nils Diewalda297f062015-04-02 00:23:46 +000056 }
Nils Diewald6e43ffd2015-03-25 18:55:39 +000057
Nils Diewalda297f062015-04-02 00:23:46 +000058 // Match defined as a node
59 else if (match instanceof Node) {
Akron19d97fe2016-09-06 20:47:05 +020060 this._element = match;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000061
Akron19d97fe2016-09-06 20:47:05 +020062 // Circular reference !!
63 match["_match"] = this;
Nils Diewalda297f062015-04-02 00:23:46 +000064
Akron19d97fe2016-09-06 20:47:05 +020065 /*
66 this.corpusID = match.getAttribute('data-corpus-id'),
67 this.docID = match.getAttribute('data-doc-id'),
68 this.textID = match.getAttribute('data-text-id'),
69 */
70 if (match.hasAttribute('data-text-sigle')) {
71 this.textSigle = match.getAttribute('data-text-sigle')
72 }
73 else {
74 this.textSigle = match.getAttribute('data-corpus-id') +
75 '/' +
76 match.getAttribute('data-doc-id') +
77 '/' +
78 match.getAttribute('data-text-id');
79 };
Akron0a6768f2016-07-13 18:00:43 +020080
Akron19d97fe2016-09-06 20:47:05 +020081 this.matchID = match.getAttribute('data-match-id');
Nils Diewalda297f062015-04-02 00:23:46 +000082
Akron19d97fe2016-09-06 20:47:05 +020083 // List of available annotations
84 this.available = match.getAttribute('data-available-info').split(' ');
Nils Diewalda297f062015-04-02 00:23:46 +000085 }
86
87 // Match as an object
88 else {
89
Akron19d97fe2016-09-06 20:47:05 +020090 // Iterate over allowed match terms
91 for (var i in _matchTerms) {
92 var term = _matchTerms[i];
93 this[term] = match[term] !== undefined ? match[term] : undefined;
94 };
Nils Diewalda297f062015-04-02 00:23:46 +000095 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000096
Nils Diewald7c8ced22015-04-15 19:21:00 +000097 this._avail = {
Akron19d97fe2016-09-06 20:47:05 +020098 tokens : [],
99 spans : [],
100 rels : []
Nils Diewalde8518f82015-03-18 22:41:49 +0000101 };
Nils Diewalda297f062015-04-02 00:23:46 +0000102
103 // Iterate over info layers
104 for (var i = 0; i < this.available.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +0200105 var term = this.available[i];
Nils Diewalda297f062015-04-02 00:23:46 +0000106
Akron19d97fe2016-09-06 20:47:05 +0200107 // Create info layer objects
108 try {
109 var layer = require('match/infolayer').create(term);
110 this._avail[layer.type].push(layer);
111 }
112 catch (e) {
113 continue;
114 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000115 };
Akron3a4a08e2017-05-23 22:34:18 +0200116
Nils Diewalde8518f82015-03-18 22:41:49 +0000117 return this;
118 },
119
Nils Diewalde8518f82015-03-18 22:41:49 +0000120 /**
121 * Return a list of parseable tree annotations.
122 */
123 getSpans : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000124 return this._avail.spans;
Nils Diewalde8518f82015-03-18 22:41:49 +0000125 },
126
127
128 /**
129 * Return a list of parseable token annotations.
130 */
131 getTokens : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000132 return this._avail.tokens;
Nils Diewalde8518f82015-03-18 22:41:49 +0000133 },
134
135
136 /**
137 * Return a list of parseable relation annotations.
138 */
139 getRels : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000140 return this._avail.rels;
Nils Diewalde8518f82015-03-18 22:41:49 +0000141 },
142
Nils Diewald7c8ced22015-04-15 19:21:00 +0000143
Nils Diewalda297f062015-04-02 00:23:46 +0000144 /**
145 * Open match
146 */
147 open : function () {
148
149 // Add actions unless it's already activated
150 var element = this._element;
151
152 // There is an element to open
153 if (this._element === undefined || this._element === null)
Akron19d97fe2016-09-06 20:47:05 +0200154 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000155
156 // The element is already opened
157 if (element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200158 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000159
160 // Add active class to element
161 element.classList.add('active');
162
Nils Diewald7c8ced22015-04-15 19:21:00 +0000163 // Already there
164 if (element.classList.contains('action'))
Akron19d97fe2016-09-06 20:47:05 +0200165 return true;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000166
Nils Diewalda297f062015-04-02 00:23:46 +0000167 // Create action buttons
Akron0b489ad2018-02-02 16:49:32 +0100168 var ul = d.createElement('ul');
Nils Diewalda297f062015-04-02 00:23:46 +0000169 ul.classList.add('action', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +0000170
Nils Diewald7c8ced22015-04-15 19:21:00 +0000171 element.appendChild(ul);
172 element.classList.add('action');
173
174 // Todo: Open in new frame
Nils Diewalda297f062015-04-02 00:23:46 +0000175
176 // Add close button
Akron0b489ad2018-02-02 16:49:32 +0100177 var close = d.createElement('li');
178 close.addE('span').addT(loc.CLOSE);
Nils Diewalda297f062015-04-02 00:23:46 +0000179 close.classList.add('close');
180 close.setAttribute('title', loc.CLOSE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000181
Nils Diewalda297f062015-04-02 00:23:46 +0000182 var that = this;
183
Akron24866cf2018-01-23 20:22:01 +0100184 // Add meta button
185 var refLine = element.querySelector("p.ref");
186
Akron0b489ad2018-02-02 16:49:32 +0100187 // No reference found
Akron151bc872018-02-02 14:04:15 +0100188 if (!refLine)
189 return;
190
Akron0b489ad2018-02-02 16:49:32 +0100191 var ops = d.createElement('div');
Akron151bc872018-02-02 14:04:15 +0100192 ops.classList.add('action', 'bottom', 'button-group');
Akron0ad7cd22018-02-08 18:03:06 +0100193
194 /*
195 Temporarily disabled
Akron0b489ad2018-02-02 16:49:32 +0100196 var meta = ops.addE('span');
197 meta.addT('Meta');
Akron151bc872018-02-02 14:04:15 +0100198 meta.setAttribute('title', loc.SHOW_META);
199 meta.classList.add('meta');
Akron0ad7cd22018-02-08 18:03:06 +0100200 */
Akron8b592d42018-01-26 18:33:06 +0100201
Akron0ad7cd22018-02-08 18:03:06 +0100202 // TODO: Rename anno
Akron0b489ad2018-02-02 16:49:32 +0100203 var info = ops.addE('span');
Akron0ad7cd22018-02-08 18:03:06 +0100204 info.addT(loc.SHOWINFO);
Akron151bc872018-02-02 14:04:15 +0100205 info.setAttribute('title', loc.SHOWINFO);
206 info.classList.add('info');
Akron8b592d42018-01-26 18:33:06 +0100207
Akron0b489ad2018-02-02 16:49:32 +0100208 var tree = ops.addE('span');
Akron0ad7cd22018-02-08 18:03:06 +0100209 tree.addT(loc.ADDTREE);
Akron151bc872018-02-02 14:04:15 +0100210 tree.setAttribute('title', loc.ADDTREE);
211 tree.classList.add('tree');
Akron8b592d42018-01-26 18:33:06 +0100212
Akron151bc872018-02-02 14:04:15 +0100213 // Insert before reference line
214 refLine.insertBefore(
215 ops,
216 refLine.firstChild
217 );
Akron8b592d42018-01-26 18:33:06 +0100218
Akron151bc872018-02-02 14:04:15 +0100219 // Click on meta - add meta (unless already there)
Akron0ad7cd22018-02-08 18:03:06 +0100220 /*
Akron151bc872018-02-02 14:04:15 +0100221 meta.addEventListener(
222 'click', function (e) {
223 e.halt();
224 that.info().showMeta();
225 }
226 );
Akron0ad7cd22018-02-08 18:03:06 +0100227 */
Akron8b592d42018-01-26 18:33:06 +0100228
Akron151bc872018-02-02 14:04:15 +0100229 // Click on token annotations - add token annotations (unless already there)
230 info.addEventListener(
231 'click', function (e) {
232 e.halt();
233 that.info().showTable();
234 }
235 );
Akron24866cf2018-01-23 20:22:01 +0100236
Akron151bc872018-02-02 14:04:15 +0100237 // Click to show tree menu
238 tree.addEventListener(
239 'click', function (e) {
240 e.halt();
Akron8b592d42018-01-26 18:33:06 +0100241
Akron151bc872018-02-02 14:04:15 +0100242 if (KorAP.TreeMenu === undefined) {
243 KorAP.TreeMenu = matchTreeMenuClass.create([]);
244 };
Akron8b592d42018-01-26 18:33:06 +0100245
Akron151bc872018-02-02 14:04:15 +0100246 var tm = KorAP.TreeMenu;
Akron8b592d42018-01-26 18:33:06 +0100247
Akron151bc872018-02-02 14:04:15 +0100248 // Reread list
249 tm.info(that.info());
250 tm.readItems(that.treeMenuList());
Akroneaba63e2018-01-26 19:49:30 +0100251
Akron151bc872018-02-02 14:04:15 +0100252 // Reposition and show menu
Akron151bc872018-02-02 14:04:15 +0100253 tm.show();
Akronc6967d72018-02-05 18:39:39 +0100254 tm.attachTo(this);
Akron151bc872018-02-02 14:04:15 +0100255 tm.focus();
256 }
257 );
Akron24866cf2018-01-23 20:22:01 +0100258
Nils Diewalda297f062015-04-02 00:23:46 +0000259 // Close match
260 close.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200261 e.halt();
262 that.close()
Nils Diewalda297f062015-04-02 00:23:46 +0000263 });
264
Nils Diewalda297f062015-04-02 00:23:46 +0000265 ul.appendChild(close);
Nils Diewalda297f062015-04-02 00:23:46 +0000266
267 return true;
268 },
269
Akron8c468a12016-11-13 23:57:41 +0100270
Akron6a535d42015-08-26 20:16:58 +0200271 // Todo: Test toggle
272 toggle : function () {
273 if (this._element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200274 this.close();
Akron6a535d42015-08-26 20:16:58 +0200275 else
Akron19d97fe2016-09-06 20:47:05 +0200276 this.open();
Akron6a535d42015-08-26 20:16:58 +0200277 },
278
Nils Diewald7c8ced22015-04-15 19:21:00 +0000279
Nils Diewald8bc7e412015-03-19 22:08:27 +0000280 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000281 * Close info view
282 */
283 close : function () {
284 this._element.classList.remove('active');
Akron151bc872018-02-02 14:04:15 +0100285 /*
286 if (this._info !== undefined) {
287 this._info.destroy();
288 };
289 */
Nils Diewalda297f062015-04-02 00:23:46 +0000290 },
291
292
Nils Diewalda297f062015-04-02 00:23:46 +0000293 /**
Akron151bc872018-02-02 14:04:15 +0100294 * Get and open associated match infos.
Nils Diewalda297f062015-04-02 00:23:46 +0000295 */
296 info : function () {
297
298 // Create match info
299 if (this._info === undefined)
Akron19d97fe2016-09-06 20:47:05 +0200300 this._info = infoClass.create(this);
Nils Diewalda297f062015-04-02 00:23:46 +0000301
302 // There is an element to append
303 if (this._element === undefined ||
Akron19d97fe2016-09-06 20:47:05 +0200304 this._element === null)
305 return this._info;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000306
Nils Diewalda297f062015-04-02 00:23:46 +0000307 // Info is already activated
Nils Diewald5c5a7472015-04-02 22:13:38 +0000308 if (this._info._element !== undefined)
Akron19d97fe2016-09-06 20:47:05 +0200309 return this._info;
Nils Diewalda297f062015-04-02 00:23:46 +0000310
Akronbd342982018-01-25 18:01:46 +0100311 var refLine = this._element.querySelector("p.ref");
312 this._element.insertBefore(
313 this._info.element(),
314 refLine
315 );
316
Nils Diewalda297f062015-04-02 00:23:46 +0000317 return this._info;
318 },
319
Akron8b592d42018-01-26 18:33:06 +0100320
Akron151bc872018-02-02 14:04:15 +0100321 // Return tree menu list
Akroneaba63e2018-01-26 19:49:30 +0100322 treeMenuList : function () {
323
324 if (this._menuList)
325 return this._menuList;
Akron8b592d42018-01-26 18:33:06 +0100326
327 // Join spans and relations
328 var treeLayers = []
329 var spans = this.getSpans();
330 var rels = this.getRels();
331 var i;
332 for (i in spans) {
333 treeLayers.push(spans[i]);
334 };
335 for (i in rels) {
336 treeLayers.push(rels[i]);
337 };
338
339 // Get spans
340 treeLayers = treeLayers.sort(
341 function (a, b) {
342 if (a.foundry < b.foundry) {
343 return -1;
344 }
345 else if (a.foundry > b.foundry) {
346 return 1;
347 }
348 else if (a.layer < b.layer) {
349 return -1;
350 }
351 else if (a.layer > b.layer) {
352 return 1;
353 };
354 return 0;
355 });
356
357 var menuList = [];
358
359 // Show tree views
360 for (var i = 0; i < treeLayers.length; i++) {
361 var span = treeLayers[i];
362
363 // Add foundry/layer to menu list
364 menuList.push([
365 span.foundry + '/' + span.layer,
366 span.foundry,
367 span.layer,
368 span.type
369 ]);
370 };
371
372 // Create tree menu
Akroneaba63e2018-01-26 19:49:30 +0100373 this._menuList = menuList;
374 return menuList;
Akron8b592d42018-01-26 18:33:06 +0100375 },
376
Nils Diewald7c8ced22015-04-15 19:21:00 +0000377
Nils Diewalda297f062015-04-02 00:23:46 +0000378 /**
379 * Get match element.
380 */
381 element : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000382 return this._element; // May be null
Nils Diewalda297f062015-04-02 00:23:46 +0000383 }
384 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000385});