blob: e70d97552e529cf3f792ff1e974e0a7d7b59202c [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
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 var 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'
26 var _matchTerms = ['textSigle', 'matchID', 'available'];
Nils Diewalda297f062015-04-02 00:23:46 +000027
28 /**
29 * Match object
30 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 return {
Nils Diewalde8518f82015-03-18 22:41:49 +000032
33 /**
34 * Create a new annotation object.
35 * Expects an array of available foundry/layer=type terms.
36 * Supported types are 'spans', 'tokens' and 'rels'.
37 */
Nils Diewalda297f062015-04-02 00:23:46 +000038 create : function (match) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000039 return Object.create(this)._init(match);
Nils Diewalde8518f82015-03-18 22:41:49 +000040 },
41
Nils Diewald7c8ced22015-04-15 19:21:00 +000042
Nils Diewald6e43ffd2015-03-25 18:55:39 +000043 /**
Nils Diewalda297f062015-04-02 00:23:46 +000044 * Initialize match.
Nils Diewald6e43ffd2015-03-25 18:55:39 +000045 */
Nils Diewalda297f062015-04-02 00:23:46 +000046 _init : function (match) {
47 this._element = null;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000048
Nils Diewalda297f062015-04-02 00:23:46 +000049 // No match defined
50 if (arguments.length < 1 ||
Akron19d97fe2016-09-06 20:47:05 +020051 match === null ||
52 match === undefined) {
53 throw new Error('Missing parameters');
Nils Diewalda297f062015-04-02 00:23:46 +000054 }
Nils Diewald6e43ffd2015-03-25 18:55:39 +000055
Nils Diewalda297f062015-04-02 00:23:46 +000056 // Match defined as a node
57 else if (match instanceof Node) {
Akron19d97fe2016-09-06 20:47:05 +020058 this._element = match;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000059
Akron19d97fe2016-09-06 20:47:05 +020060 // Circular reference !!
61 match["_match"] = this;
Nils Diewalda297f062015-04-02 00:23:46 +000062
Akron19d97fe2016-09-06 20:47:05 +020063 /*
64 this.corpusID = match.getAttribute('data-corpus-id'),
65 this.docID = match.getAttribute('data-doc-id'),
66 this.textID = match.getAttribute('data-text-id'),
67 */
68 if (match.hasAttribute('data-text-sigle')) {
69 this.textSigle = match.getAttribute('data-text-sigle')
70 }
71 else {
72 this.textSigle = match.getAttribute('data-corpus-id') +
73 '/' +
74 match.getAttribute('data-doc-id') +
75 '/' +
76 match.getAttribute('data-text-id');
77 };
Akron0a6768f2016-07-13 18:00:43 +020078
Akron19d97fe2016-09-06 20:47:05 +020079 this.matchID = match.getAttribute('data-match-id');
Nils Diewalda297f062015-04-02 00:23:46 +000080
Akron19d97fe2016-09-06 20:47:05 +020081 // List of available annotations
82 this.available = match.getAttribute('data-available-info').split(' ');
Nils Diewalda297f062015-04-02 00:23:46 +000083 }
84
85 // Match as an object
86 else {
87
Akron19d97fe2016-09-06 20:47:05 +020088 // Iterate over allowed match terms
89 for (var i in _matchTerms) {
90 var term = _matchTerms[i];
91 this[term] = match[term] !== undefined ? match[term] : undefined;
92 };
Nils Diewalda297f062015-04-02 00:23:46 +000093 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000094
Nils Diewald7c8ced22015-04-15 19:21:00 +000095 this._avail = {
Akron19d97fe2016-09-06 20:47:05 +020096 tokens : [],
97 spans : [],
98 rels : []
Nils Diewalde8518f82015-03-18 22:41:49 +000099 };
Nils Diewalda297f062015-04-02 00:23:46 +0000100
101 // Iterate over info layers
102 for (var i = 0; i < this.available.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +0200103 var term = this.available[i];
Nils Diewalda297f062015-04-02 00:23:46 +0000104
Akron19d97fe2016-09-06 20:47:05 +0200105 // Create info layer objects
106 try {
107 var layer = require('match/infolayer').create(term);
108 this._avail[layer.type].push(layer);
109 }
110 catch (e) {
111 continue;
112 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000113 };
Akron3a4a08e2017-05-23 22:34:18 +0200114
Nils Diewalde8518f82015-03-18 22:41:49 +0000115 return this;
116 },
117
Nils Diewalde8518f82015-03-18 22:41:49 +0000118 /**
119 * Return a list of parseable tree annotations.
120 */
121 getSpans : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000122 return this._avail.spans;
Nils Diewalde8518f82015-03-18 22:41:49 +0000123 },
124
125
126 /**
127 * Return a list of parseable token annotations.
128 */
129 getTokens : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000130 return this._avail.tokens;
Nils Diewalde8518f82015-03-18 22:41:49 +0000131 },
132
133
134 /**
135 * Return a list of parseable relation annotations.
136 */
137 getRels : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000138 return this._avail.rels;
Nils Diewalde8518f82015-03-18 22:41:49 +0000139 },
140
Nils Diewald7c8ced22015-04-15 19:21:00 +0000141
Nils Diewalda297f062015-04-02 00:23:46 +0000142 /**
143 * Open match
144 */
145 open : function () {
146
147 // Add actions unless it's already activated
148 var element = this._element;
149
150 // There is an element to open
151 if (this._element === undefined || this._element === null)
Akron19d97fe2016-09-06 20:47:05 +0200152 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000153
154 // The element is already opened
155 if (element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200156 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000157
158 // Add active class to element
159 element.classList.add('active');
160
Nils Diewald7c8ced22015-04-15 19:21:00 +0000161 // Already there
162 if (element.classList.contains('action'))
Akron19d97fe2016-09-06 20:47:05 +0200163 return true;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000164
Nils Diewalda297f062015-04-02 00:23:46 +0000165 // Create action buttons
166 var ul = document.createElement('ul');
167 ul.classList.add('action', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +0000168
Nils Diewald7c8ced22015-04-15 19:21:00 +0000169 element.appendChild(ul);
170 element.classList.add('action');
171
172 // Todo: Open in new frame
Nils Diewalda297f062015-04-02 00:23:46 +0000173
174 // Add close button
175 var close = document.createElement('li');
176 close.appendChild(document.createElement('span'))
Akron19d97fe2016-09-06 20:47:05 +0200177 .appendChild(document.createTextNode(loc.CLOSE));
Nils Diewalda297f062015-04-02 00:23:46 +0000178 close.classList.add('close');
179 close.setAttribute('title', loc.CLOSE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000180
Nils Diewalda297f062015-04-02 00:23:46 +0000181 var that = this;
182
Akron24866cf2018-01-23 20:22:01 +0100183 // Add meta button
184 var refLine = element.querySelector("p.ref");
185
Akron151bc872018-02-02 14:04:15 +0100186 if (!refLine)
187 return;
188
189 // TODO: Simplify
190 var ops = document.createElement('div');
191 ops.classList.add('action', 'bottom', 'button-group');
192
193 var meta = document.createElement('span');
194 ops.appendChild(meta);
195 meta.appendChild(document.createTextNode('Meta'));
196 meta.setAttribute('title', loc.SHOW_META);
197 meta.classList.add('meta');
Akron8b592d42018-01-26 18:33:06 +0100198
Akron151bc872018-02-02 14:04:15 +0100199 var info = document.createElement('span');
200 ops.appendChild(info);
201 info.appendChild(document.createTextNode('Anno'));
202 info.setAttribute('title', loc.SHOWINFO);
203 info.classList.add('info');
Akron8b592d42018-01-26 18:33:06 +0100204
Akron151bc872018-02-02 14:04:15 +0100205 var tree = document.createElement('span');
206 ops.appendChild(tree);
207 tree.appendChild(document.createTextNode('+ Tree'));
208 tree.setAttribute('title', loc.ADDTREE);
209 tree.classList.add('tree');
Akron8b592d42018-01-26 18:33:06 +0100210
Akron151bc872018-02-02 14:04:15 +0100211 // Insert before reference line
212 refLine.insertBefore(
213 ops,
214 refLine.firstChild
215 );
Akron8b592d42018-01-26 18:33:06 +0100216
Akron151bc872018-02-02 14:04:15 +0100217 // Click on meta - add meta (unless already there)
218 meta.addEventListener(
219 'click', function (e) {
220 e.halt();
221 that.info().showMeta();
222 }
223 );
Akron8b592d42018-01-26 18:33:06 +0100224
Akron151bc872018-02-02 14:04:15 +0100225 // Click on token annotations - add token annotations (unless already there)
226 info.addEventListener(
227 'click', function (e) {
228 e.halt();
229 that.info().showTable();
230 }
231 );
Akron24866cf2018-01-23 20:22:01 +0100232
Akron151bc872018-02-02 14:04:15 +0100233 // Click to show tree menu
234 tree.addEventListener(
235 'click', function (e) {
236 e.halt();
Akron8b592d42018-01-26 18:33:06 +0100237
Akron151bc872018-02-02 14:04:15 +0100238 if (KorAP.TreeMenu === undefined) {
239 KorAP.TreeMenu = matchTreeMenuClass.create([]);
240 };
Akron8b592d42018-01-26 18:33:06 +0100241
Akron151bc872018-02-02 14:04:15 +0100242 var tm = KorAP.TreeMenu;
Akron8b592d42018-01-26 18:33:06 +0100243
Akron151bc872018-02-02 14:04:15 +0100244 // Reread list
245 tm.info(that.info());
246 tm.readItems(that.treeMenuList());
Akroneaba63e2018-01-26 19:49:30 +0100247
Akron151bc872018-02-02 14:04:15 +0100248 // Reposition and show menu
249 tm.attachTo(this);
250 tm.show();
251 tm.focus();
252 }
253 );
Akron24866cf2018-01-23 20:22:01 +0100254
Nils Diewalda297f062015-04-02 00:23:46 +0000255 // Close match
256 close.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200257 e.halt();
258 that.close()
Nils Diewalda297f062015-04-02 00:23:46 +0000259 });
260
Nils Diewalda297f062015-04-02 00:23:46 +0000261 ul.appendChild(close);
Nils Diewalda297f062015-04-02 00:23:46 +0000262
263 return true;
264 },
265
Akron8c468a12016-11-13 23:57:41 +0100266
Akron6a535d42015-08-26 20:16:58 +0200267 // Todo: Test toggle
268 toggle : function () {
269 if (this._element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200270 this.close();
Akron6a535d42015-08-26 20:16:58 +0200271 else
Akron19d97fe2016-09-06 20:47:05 +0200272 this.open();
Akron6a535d42015-08-26 20:16:58 +0200273 },
274
Nils Diewald7c8ced22015-04-15 19:21:00 +0000275
Nils Diewald8bc7e412015-03-19 22:08:27 +0000276 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000277 * Close info view
278 */
279 close : function () {
280 this._element.classList.remove('active');
Akron151bc872018-02-02 14:04:15 +0100281 /*
282 if (this._info !== undefined) {
283 this._info.destroy();
284 };
285 */
Nils Diewalda297f062015-04-02 00:23:46 +0000286 },
287
288
Nils Diewalda297f062015-04-02 00:23:46 +0000289 /**
Akron151bc872018-02-02 14:04:15 +0100290 * Get and open associated match infos.
Nils Diewalda297f062015-04-02 00:23:46 +0000291 */
292 info : function () {
293
294 // Create match info
295 if (this._info === undefined)
Akron19d97fe2016-09-06 20:47:05 +0200296 this._info = infoClass.create(this);
Nils Diewalda297f062015-04-02 00:23:46 +0000297
298 // There is an element to append
299 if (this._element === undefined ||
Akron19d97fe2016-09-06 20:47:05 +0200300 this._element === null)
301 return this._info;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000302
Nils Diewalda297f062015-04-02 00:23:46 +0000303 // Info is already activated
Nils Diewald5c5a7472015-04-02 22:13:38 +0000304 if (this._info._element !== undefined)
Akron19d97fe2016-09-06 20:47:05 +0200305 return this._info;
Nils Diewalda297f062015-04-02 00:23:46 +0000306
Akronbd342982018-01-25 18:01:46 +0100307 var refLine = this._element.querySelector("p.ref");
308 this._element.insertBefore(
309 this._info.element(),
310 refLine
311 );
312
Nils Diewalda297f062015-04-02 00:23:46 +0000313 return this._info;
314 },
315
Akron8b592d42018-01-26 18:33:06 +0100316
Akron151bc872018-02-02 14:04:15 +0100317 // Return tree menu list
Akroneaba63e2018-01-26 19:49:30 +0100318 treeMenuList : function () {
319
320 if (this._menuList)
321 return this._menuList;
Akron8b592d42018-01-26 18:33:06 +0100322
323 // Join spans and relations
324 var treeLayers = []
325 var spans = this.getSpans();
326 var rels = this.getRels();
327 var i;
328 for (i in spans) {
329 treeLayers.push(spans[i]);
330 };
331 for (i in rels) {
332 treeLayers.push(rels[i]);
333 };
334
335 // Get spans
336 treeLayers = treeLayers.sort(
337 function (a, b) {
338 if (a.foundry < b.foundry) {
339 return -1;
340 }
341 else if (a.foundry > b.foundry) {
342 return 1;
343 }
344 else if (a.layer < b.layer) {
345 return -1;
346 }
347 else if (a.layer > b.layer) {
348 return 1;
349 };
350 return 0;
351 });
352
353 var menuList = [];
354
355 // Show tree views
356 for (var i = 0; i < treeLayers.length; i++) {
357 var span = treeLayers[i];
358
359 // Add foundry/layer to menu list
360 menuList.push([
361 span.foundry + '/' + span.layer,
362 span.foundry,
363 span.layer,
364 span.type
365 ]);
366 };
367
368 // Create tree menu
Akroneaba63e2018-01-26 19:49:30 +0100369 this._menuList = menuList;
370 return menuList;
Akron8b592d42018-01-26 18:33:06 +0100371 },
372
Nils Diewald7c8ced22015-04-15 19:21:00 +0000373
Nils Diewalda297f062015-04-02 00:23:46 +0000374 /**
375 * Get match element.
376 */
377 element : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000378 return this._element; // May be null
Nils Diewalda297f062015-04-02 00:23:46 +0000379 }
380 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000381});