blob: 35768678a6d92a24313cd723dfaa15e80b0afad3 [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
Akron52ed22d2018-07-11 17:05:19 +020014 'match/treeitem',
Akron13448c22018-07-10 13:05:46 +020015 'buttongroup',
Akron52ed22d2018-07-11 17:05:19 +020016 'buttongroup/menu',
Akron8b592d42018-01-26 18:33:06 +010017 'util'
Akron52ed22d2018-07-11 17:05:19 +020018], function (infoClass,treeItemClass,buttonGroupClass,buttonGroupMenuClass) { //, refClass) {
Nils Diewald4f6521a2015-03-20 21:30:13 +000019
Nils Diewald6e43ffd2015-03-25 18:55:39 +000020 // Localization values
Akron0b489ad2018-02-02 16:49:32 +010021 const loc = KorAP.Locale;
Akron24866cf2018-01-23 20:22:01 +010022 loc.SHOWINFO = loc.SHOWINFO || 'Show information';
Akrond5436a42018-02-09 11:09:16 +010023 loc.ADDTREE = loc.ADDTREE || 'Relations';
24 loc.SHOWANNO = loc.SHOWANNO || 'Tokens';
Akron24866cf2018-01-23 20:22:01 +010025 loc.CLOSE = loc.CLOSE || 'Close';
Akrond5436a42018-02-09 11:09:16 +010026 loc.SHOW_META = loc.SHOW_META || 'Metadata';
Nils Diewald0e6992a2015-04-14 20:13:52 +000027
Akron0a6768f2016-07-13 18:00:43 +020028 // 'corpusID', 'docID', 'textID'
Akron0b489ad2018-02-02 16:49:32 +010029 const _matchTerms = ['textSigle', 'matchID', 'available'];
30
31 const d = document;
Nils Diewalda297f062015-04-02 00:23:46 +000032
33 /**
34 * Match object
35 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 return {
Nils Diewalde8518f82015-03-18 22:41:49 +000037
38 /**
39 * Create a new annotation object.
40 * Expects an array of available foundry/layer=type terms.
41 * Supported types are 'spans', 'tokens' and 'rels'.
42 */
Nils Diewalda297f062015-04-02 00:23:46 +000043 create : function (match) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000044 return Object.create(this)._init(match);
Nils Diewalde8518f82015-03-18 22:41:49 +000045 },
46
Nils Diewald7c8ced22015-04-15 19:21:00 +000047
Nils Diewald6e43ffd2015-03-25 18:55:39 +000048 /**
Nils Diewalda297f062015-04-02 00:23:46 +000049 * Initialize match.
Nils Diewald6e43ffd2015-03-25 18:55:39 +000050 */
Nils Diewalda297f062015-04-02 00:23:46 +000051 _init : function (match) {
52 this._element = null;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000053
Nils Diewalda297f062015-04-02 00:23:46 +000054 // No match defined
55 if (arguments.length < 1 ||
Akron19d97fe2016-09-06 20:47:05 +020056 match === null ||
57 match === undefined) {
58 throw new Error('Missing parameters');
Nils Diewalda297f062015-04-02 00:23:46 +000059 }
Nils Diewald6e43ffd2015-03-25 18:55:39 +000060
Nils Diewalda297f062015-04-02 00:23:46 +000061 // Match defined as a node
62 else if (match instanceof Node) {
Akron19d97fe2016-09-06 20:47:05 +020063 this._element = match;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000064
Akron19d97fe2016-09-06 20:47:05 +020065 // Circular reference !!
66 match["_match"] = this;
Nils Diewalda297f062015-04-02 00:23:46 +000067
Akron19d97fe2016-09-06 20:47:05 +020068 /*
69 this.corpusID = match.getAttribute('data-corpus-id'),
70 this.docID = match.getAttribute('data-doc-id'),
71 this.textID = match.getAttribute('data-text-id'),
72 */
73 if (match.hasAttribute('data-text-sigle')) {
74 this.textSigle = match.getAttribute('data-text-sigle')
75 }
76 else {
77 this.textSigle = match.getAttribute('data-corpus-id') +
78 '/' +
79 match.getAttribute('data-doc-id') +
80 '/' +
81 match.getAttribute('data-text-id');
82 };
Akron0a6768f2016-07-13 18:00:43 +020083
Akron19d97fe2016-09-06 20:47:05 +020084 this.matchID = match.getAttribute('data-match-id');
Nils Diewalda297f062015-04-02 00:23:46 +000085
Akron19d97fe2016-09-06 20:47:05 +020086 // List of available annotations
87 this.available = match.getAttribute('data-available-info').split(' ');
Nils Diewalda297f062015-04-02 00:23:46 +000088 }
89
90 // Match as an object
91 else {
92
Akron19d97fe2016-09-06 20:47:05 +020093 // Iterate over allowed match terms
94 for (var i in _matchTerms) {
95 var term = _matchTerms[i];
96 this[term] = match[term] !== undefined ? match[term] : undefined;
97 };
Nils Diewalda297f062015-04-02 00:23:46 +000098 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000099
Nils Diewald7c8ced22015-04-15 19:21:00 +0000100 this._avail = {
Akron19d97fe2016-09-06 20:47:05 +0200101 tokens : [],
102 spans : [],
103 rels : []
Nils Diewalde8518f82015-03-18 22:41:49 +0000104 };
Nils Diewalda297f062015-04-02 00:23:46 +0000105
106 // Iterate over info layers
107 for (var i = 0; i < this.available.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +0200108 var term = this.available[i];
Nils Diewalda297f062015-04-02 00:23:46 +0000109
Akron19d97fe2016-09-06 20:47:05 +0200110 // Create info layer objects
111 try {
112 var layer = require('match/infolayer').create(term);
113 this._avail[layer.type].push(layer);
114 }
115 catch (e) {
116 continue;
117 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000118 };
Akron3a4a08e2017-05-23 22:34:18 +0200119
Nils Diewalde8518f82015-03-18 22:41:49 +0000120 return this;
121 },
122
Nils Diewalde8518f82015-03-18 22:41:49 +0000123 /**
124 * Return a list of parseable tree annotations.
125 */
126 getSpans : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000127 return this._avail.spans;
Nils Diewalde8518f82015-03-18 22:41:49 +0000128 },
129
130
131 /**
132 * Return a list of parseable token annotations.
133 */
134 getTokens : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000135 return this._avail.tokens;
Nils Diewalde8518f82015-03-18 22:41:49 +0000136 },
137
138
139 /**
140 * Return a list of parseable relation annotations.
141 */
142 getRels : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000143 return this._avail.rels;
Nils Diewalde8518f82015-03-18 22:41:49 +0000144 },
145
Nils Diewald7c8ced22015-04-15 19:21:00 +0000146
Nils Diewalda297f062015-04-02 00:23:46 +0000147 /**
148 * Open match
149 */
150 open : function () {
151
152 // Add actions unless it's already activated
153 var element = this._element;
154
155 // There is an element to open
156 if (this._element === undefined || this._element === null)
Akron19d97fe2016-09-06 20:47:05 +0200157 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000158
159 // The element is already opened
160 if (element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200161 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000162
163 // Add active class to element
164 element.classList.add('active');
165
Nils Diewald7c8ced22015-04-15 19:21:00 +0000166 // Already there
167 if (element.classList.contains('action'))
Akron19d97fe2016-09-06 20:47:05 +0200168 return true;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000169
Nils Diewalda297f062015-04-02 00:23:46 +0000170 // Create action buttons
Akron0b489ad2018-02-02 16:49:32 +0100171 var ul = d.createElement('ul');
Nils Diewalda297f062015-04-02 00:23:46 +0000172 ul.classList.add('action', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +0000173
Nils Diewald7c8ced22015-04-15 19:21:00 +0000174 element.appendChild(ul);
175 element.classList.add('action');
176
177 // Todo: Open in new frame
Nils Diewalda297f062015-04-02 00:23:46 +0000178
179 // Add close button
Akron0b489ad2018-02-02 16:49:32 +0100180 var close = d.createElement('li');
181 close.addE('span').addT(loc.CLOSE);
Nils Diewalda297f062015-04-02 00:23:46 +0000182 close.classList.add('close');
183 close.setAttribute('title', loc.CLOSE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000184
Nils Diewalda297f062015-04-02 00:23:46 +0000185 var that = this;
186
Akron537bc522018-07-13 19:06:27 +0200187 // TODO:
188 // Introduce panel object here!
189
Akron24866cf2018-01-23 20:22:01 +0100190 // Add meta button
191 var refLine = element.querySelector("p.ref");
192
Akron0b489ad2018-02-02 16:49:32 +0100193 // No reference found
Akron151bc872018-02-02 14:04:15 +0100194 if (!refLine)
195 return;
Akron0ad7cd22018-02-08 18:03:06 +0100196
Akron13448c22018-07-10 13:05:46 +0200197 var btns = buttonGroupClass.create(['action', 'bottom']);
Akron8b592d42018-01-26 18:33:06 +0100198
Akron13448c22018-07-10 13:05:46 +0200199 // Add meta button
200 btns.add(
201 loc.SHOW_META, ['meta'], function (e) {
Akron151bc872018-02-02 14:04:15 +0100202 that.info().showMeta();
203 }
204 );
Akron8b592d42018-01-26 18:33:06 +0100205
Akron13448c22018-07-10 13:05:46 +0200206 // Add token annotation button
207 btns.add(
208 loc.SHOWANNO, ['info'], function (e) {
Akron151bc872018-02-02 14:04:15 +0100209 that.info().showTable();
210 }
211 );
Akron24866cf2018-01-23 20:22:01 +0100212
Akron13448c22018-07-10 13:05:46 +0200213 // Add tree view button
214 btns.add(
215 loc.ADDTREE, ['tree'], function (e) {
Akron151bc872018-02-02 14:04:15 +0100216 if (KorAP.TreeMenu === undefined) {
Akron52ed22d2018-07-11 17:05:19 +0200217 KorAP.TreeMenu = buttonGroupMenuClass.create([], treeItemClass);
Akron151bc872018-02-02 14:04:15 +0100218 };
Akron8b592d42018-01-26 18:33:06 +0100219
Akron151bc872018-02-02 14:04:15 +0100220 var tm = KorAP.TreeMenu;
Akron8b592d42018-01-26 18:33:06 +0100221
Akron151bc872018-02-02 14:04:15 +0100222 // Reread list
223 tm.info(that.info());
224 tm.readItems(that.treeMenuList());
Akroneaba63e2018-01-26 19:49:30 +0100225
Akron151bc872018-02-02 14:04:15 +0100226 // Reposition and show menu
Akron151bc872018-02-02 14:04:15 +0100227 tm.show();
Akron52ed22d2018-07-11 17:05:19 +0200228 tm.button(this);
Akron151bc872018-02-02 14:04:15 +0100229 tm.focus();
230 }
231 );
Akron24866cf2018-01-23 20:22:01 +0100232
Akron13448c22018-07-10 13:05:46 +0200233
234 // Insert before reference line
235 refLine.insertBefore(
236 btns.element(),
237 refLine.firstChild
238 );
239
240
Nils Diewalda297f062015-04-02 00:23:46 +0000241 // Close match
242 close.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200243 e.halt();
244 that.close()
Nils Diewalda297f062015-04-02 00:23:46 +0000245 });
246
Nils Diewalda297f062015-04-02 00:23:46 +0000247 ul.appendChild(close);
Nils Diewalda297f062015-04-02 00:23:46 +0000248
249 return true;
250 },
251
Akron8c468a12016-11-13 23:57:41 +0100252
Akron6a535d42015-08-26 20:16:58 +0200253 // Todo: Test toggle
254 toggle : function () {
255 if (this._element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200256 this.close();
Akron6a535d42015-08-26 20:16:58 +0200257 else
Akron19d97fe2016-09-06 20:47:05 +0200258 this.open();
Akron6a535d42015-08-26 20:16:58 +0200259 },
260
Nils Diewald7c8ced22015-04-15 19:21:00 +0000261
Nils Diewald8bc7e412015-03-19 22:08:27 +0000262 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000263 * Close info view
264 */
265 close : function () {
266 this._element.classList.remove('active');
Akron151bc872018-02-02 14:04:15 +0100267 /*
268 if (this._info !== undefined) {
269 this._info.destroy();
270 };
271 */
Nils Diewalda297f062015-04-02 00:23:46 +0000272 },
273
274
Nils Diewalda297f062015-04-02 00:23:46 +0000275 /**
Akron151bc872018-02-02 14:04:15 +0100276 * Get and open associated match infos.
Nils Diewalda297f062015-04-02 00:23:46 +0000277 */
278 info : function () {
279
Akron537bc522018-07-13 19:06:27 +0200280 // TODO:
281 // Rename info() to panel()
282
Nils Diewalda297f062015-04-02 00:23:46 +0000283 // Create match info
284 if (this._info === undefined)
Akron19d97fe2016-09-06 20:47:05 +0200285 this._info = infoClass.create(this);
Nils Diewalda297f062015-04-02 00:23:46 +0000286
287 // There is an element to append
288 if (this._element === undefined ||
Akron19d97fe2016-09-06 20:47:05 +0200289 this._element === null)
290 return this._info;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000291
Nils Diewalda297f062015-04-02 00:23:46 +0000292 // Info is already activated
Nils Diewald5c5a7472015-04-02 22:13:38 +0000293 if (this._info._element !== undefined)
Akron19d97fe2016-09-06 20:47:05 +0200294 return this._info;
Nils Diewalda297f062015-04-02 00:23:46 +0000295
Akronbd342982018-01-25 18:01:46 +0100296 var refLine = this._element.querySelector("p.ref");
297 this._element.insertBefore(
298 this._info.element(),
299 refLine
300 );
301
Nils Diewalda297f062015-04-02 00:23:46 +0000302 return this._info;
303 },
304
Akron8b592d42018-01-26 18:33:06 +0100305
Akron151bc872018-02-02 14:04:15 +0100306 // Return tree menu list
Akroneaba63e2018-01-26 19:49:30 +0100307 treeMenuList : function () {
308
309 if (this._menuList)
310 return this._menuList;
Akron8b592d42018-01-26 18:33:06 +0100311
312 // Join spans and relations
313 var treeLayers = []
314 var spans = this.getSpans();
315 var rels = this.getRels();
316 var i;
317 for (i in spans) {
318 treeLayers.push(spans[i]);
319 };
320 for (i in rels) {
321 treeLayers.push(rels[i]);
322 };
323
324 // Get spans
325 treeLayers = treeLayers.sort(
326 function (a, b) {
327 if (a.foundry < b.foundry) {
328 return -1;
329 }
330 else if (a.foundry > b.foundry) {
331 return 1;
332 }
333 else if (a.layer < b.layer) {
334 return -1;
335 }
336 else if (a.layer > b.layer) {
337 return 1;
338 };
339 return 0;
340 });
341
342 var menuList = [];
343
344 // Show tree views
345 for (var i = 0; i < treeLayers.length; i++) {
346 var span = treeLayers[i];
347
348 // Add foundry/layer to menu list
349 menuList.push([
350 span.foundry + '/' + span.layer,
351 span.foundry,
352 span.layer,
353 span.type
354 ]);
355 };
356
357 // Create tree menu
Akroneaba63e2018-01-26 19:49:30 +0100358 this._menuList = menuList;
359 return menuList;
Akron8b592d42018-01-26 18:33:06 +0100360 },
361
Nils Diewald7c8ced22015-04-15 19:21:00 +0000362
Nils Diewalda297f062015-04-02 00:23:46 +0000363 /**
364 * Get match element.
365 */
366 element : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000367 return this._element; // May be null
Nils Diewalda297f062015-04-02 00:23:46 +0000368 }
369 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000370});