blob: df7db0325b9bd34d59c8c0f8fc84a165f8696925 [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 */
Akrone51eaa32020-11-10 09:35:53 +010012
13"use strict";
Nils Diewald0e6992a2015-04-14 20:13:52 +000014define([
Akron13448c22018-07-10 13:05:46 +020015 'buttongroup',
Akronbfe912c2018-07-17 19:30:52 +020016 'panel/match',
Akron8b592d42018-01-26 18:33:06 +010017 'util'
Akrone57e8802020-10-17 08:23:45 +020018], function (buttonGroupClass,matchPanelClass) {
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;
Akronec6bb8e2018-08-29 13:07:56 +020022 loc.MINIMIZE = loc.MINIMIZE || 'Minimize';
Nils Diewald0e6992a2015-04-14 20:13:52 +000023
Akron0a6768f2016-07-13 18:00:43 +020024 // 'corpusID', 'docID', 'textID'
Akron0b489ad2018-02-02 16:49:32 +010025 const _matchTerms = ['textSigle', 'matchID', 'available'];
26
27 const d = document;
Nils Diewalda297f062015-04-02 00:23:46 +000028
29 /**
30 * Match object
31 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 return {
Nils Diewalde8518f82015-03-18 22:41:49 +000033
34 /**
Akron7f9a6a32018-07-18 15:05:23 +020035 * Create a new match object.
36 * Expects an element with match descriptions.
Nils Diewalde8518f82015-03-18 22:41:49 +000037 */
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) {
Akrone57e8802020-10-17 08:23:45 +020047 const t= this;
Akron24aa0052020-11-10 11:00:34 +010048 t._el = null;
Akrone57e8802020-10-17 08:23:45 +020049 t._initialized = false;
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) {
Akron24aa0052020-11-10 11:00:34 +010060 t._el = match;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000061
Akron19d97fe2016-09-06 20:47:05 +020062 // Circular reference !!
Akrone57e8802020-10-17 08:23:45 +020063 match["_match"] = t;
Nils Diewalda297f062015-04-02 00:23:46 +000064
Akron19d97fe2016-09-06 20:47:05 +020065 if (match.hasAttribute('data-text-sigle')) {
Akrone57e8802020-10-17 08:23:45 +020066 t.textSigle = match.getAttribute('data-text-sigle')
Akron19d97fe2016-09-06 20:47:05 +020067 }
68 else {
Akrone57e8802020-10-17 08:23:45 +020069 t.textSigle = match.getAttribute('data-corpus-id') +
Akron19d97fe2016-09-06 20:47:05 +020070 '/' +
71 match.getAttribute('data-doc-id') +
72 '/' +
73 match.getAttribute('data-text-id');
74 };
Akron0a6768f2016-07-13 18:00:43 +020075
Akrone57e8802020-10-17 08:23:45 +020076 t.matchID = match.getAttribute('data-match-id');
77
Akron19d97fe2016-09-06 20:47:05 +020078 // List of available annotations
Akrone57e8802020-10-17 08:23:45 +020079 t.available = match.getAttribute('data-available-info').split(' ');
Nils Diewalda297f062015-04-02 00:23:46 +000080 }
81
82 // Match as an object
83 else {
84
Akron19d97fe2016-09-06 20:47:05 +020085 // Iterate over allowed match terms
Akron678c26f2020-10-09 08:52:50 +020086 _matchTerms.forEach(function(term) {
Akron19d97fe2016-09-06 20:47:05 +020087 this[term] = match[term] !== undefined ? match[term] : undefined;
Akrone57e8802020-10-17 08:23:45 +020088 }, t);
Nils Diewalda297f062015-04-02 00:23:46 +000089 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000090
Akrone57e8802020-10-17 08:23:45 +020091 t._avail = {
Akron19d97fe2016-09-06 20:47:05 +020092 tokens : [],
93 spans : [],
94 rels : []
Nils Diewalde8518f82015-03-18 22:41:49 +000095 };
Nils Diewalda297f062015-04-02 00:23:46 +000096
97 // Iterate over info layers
Akronb50964a2020-10-12 11:44:37 +020098 let layer;
Akrone57e8802020-10-17 08:23:45 +020099 t.available.forEach(function(term){
Nils Diewalda297f062015-04-02 00:23:46 +0000100
Akron19d97fe2016-09-06 20:47:05 +0200101 // Create info layer objects
102 try {
Akronb50964a2020-10-12 11:44:37 +0200103 layer = require('match/infolayer').create(term);
Akron19d97fe2016-09-06 20:47:05 +0200104 this._avail[layer.type].push(layer);
105 }
106 catch (e) {
Akronb50964a2020-10-12 11:44:37 +0200107 return;
Akron19d97fe2016-09-06 20:47:05 +0200108 };
Akrone57e8802020-10-17 08:23:45 +0200109 }, t);
Akron3a4a08e2017-05-23 22:34:18 +0200110
Akrone57e8802020-10-17 08:23:45 +0200111 return t;
Nils Diewalde8518f82015-03-18 22:41:49 +0000112 },
113
Nils Diewalde8518f82015-03-18 22:41:49 +0000114 /**
115 * Return a list of parseable tree annotations.
116 */
117 getSpans : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000118 return this._avail.spans;
Nils Diewalde8518f82015-03-18 22:41:49 +0000119 },
120
121
122 /**
123 * Return a list of parseable token annotations.
124 */
125 getTokens : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000126 return this._avail.tokens;
Nils Diewalde8518f82015-03-18 22:41:49 +0000127 },
128
129
130 /**
131 * Return a list of parseable relation annotations.
132 */
133 getRels : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000134 return this._avail.rels;
Nils Diewalde8518f82015-03-18 22:41:49 +0000135 },
136
Akron3c390c42020-03-30 09:06:21 +0200137 /**
138 * Initialize match
139 */
140 init : function () {
Akrone57e8802020-10-17 08:23:45 +0200141 const t = this;
142 if (t._initialized)
143 return t;
Akron3c390c42020-03-30 09:06:21 +0200144
145 // Add actions unless it's already activated
Akron24aa0052020-11-10 11:00:34 +0100146 const element = t._el;
Akron3c390c42020-03-30 09:06:21 +0200147
148 // There is an element to open
149 if (element === undefined || element === null)
150 return undefined;
151
152 // Add meta button
Akrone57e8802020-10-17 08:23:45 +0200153 const refLine = element.querySelector("p.ref");
Akron3c390c42020-03-30 09:06:21 +0200154
155 // No reference found
156 if (!refLine)
157 return undefined;
158
159 // Create panel
Akrone57e8802020-10-17 08:23:45 +0200160 t.panel = matchPanelClass.create(t);
Akron3c390c42020-03-30 09:06:21 +0200161
Akron24aa0052020-11-10 11:00:34 +0100162 t._el.insertBefore(
Akrone57e8802020-10-17 08:23:45 +0200163 t.panel.element(),
Akron24aa0052020-11-10 11:00:34 +0100164 t._el.querySelector("p.ref")
Akron3c390c42020-03-30 09:06:21 +0200165 );
166
167 // Insert before reference line
168 refLine.insertBefore(
Akrone57e8802020-10-17 08:23:45 +0200169 t.panel.actions.element(),
Akron3c390c42020-03-30 09:06:21 +0200170 refLine.firstChild
171 );
172
Akrone57e8802020-10-17 08:23:45 +0200173 t._initialized = true;
174 return t;
Akron3c390c42020-03-30 09:06:21 +0200175 },
Nils Diewald7c8ced22015-04-15 19:21:00 +0000176
Nils Diewalda297f062015-04-02 00:23:46 +0000177 /**
178 * Open match
179 */
180 open : function () {
Akron7f9a6a32018-07-18 15:05:23 +0200181
Nils Diewalda297f062015-04-02 00:23:46 +0000182 // Add actions unless it's already activated
Akron24aa0052020-11-10 11:00:34 +0100183 const element = this._el;
Nils Diewalda297f062015-04-02 00:23:46 +0000184
185 // There is an element to open
Akron7f9a6a32018-07-18 15:05:23 +0200186 if (element === undefined || element === null)
Akron19d97fe2016-09-06 20:47:05 +0200187 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000188
189 // The element is already opened
190 if (element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200191 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000192
193 // Add active class to element
194 element.classList.add('active');
195
Akrone57e8802020-10-17 08:23:45 +0200196 const btn = buttonGroupClass.create(
Akronbfe912c2018-07-17 19:30:52 +0200197 ['action','button-view']
198 );
199
Akrone57e8802020-10-17 08:23:45 +0200200 const that = this;
Akron792b1a42020-09-14 18:56:38 +0200201 btn.add(loc.MINIMIZE, {'cls':['button-icon','minimize']}, function () {
Akronec6bb8e2018-08-29 13:07:56 +0200202 that.minimize();
Akronbfe912c2018-07-17 19:30:52 +0200203 });
204 element.appendChild(btn.element());
Akron3c390c42020-03-30 09:06:21 +0200205
206 if (this.init() == undefined) {
207 return false;
208 };
Akron13448c22018-07-10 13:05:46 +0200209
Nils Diewalda297f062015-04-02 00:23:46 +0000210 return true;
211 },
212
Akron8c468a12016-11-13 23:57:41 +0100213
Akrone57e8802020-10-17 08:23:45 +0200214 /**
215 * Toggle match view
216 */
Akron6a535d42015-08-26 20:16:58 +0200217 toggle : function () {
Akron24aa0052020-11-10 11:00:34 +0100218 if (this._el.classList.contains('active'))
Akronec6bb8e2018-08-29 13:07:56 +0200219 this.minimize();
Akron6a535d42015-08-26 20:16:58 +0200220 else
Akron19d97fe2016-09-06 20:47:05 +0200221 this.open();
Akron6a535d42015-08-26 20:16:58 +0200222 },
223
Nils Diewald7c8ced22015-04-15 19:21:00 +0000224
Nils Diewald8bc7e412015-03-19 22:08:27 +0000225 /**
Akronec6bb8e2018-08-29 13:07:56 +0200226 * Minimize info view
Nils Diewalda297f062015-04-02 00:23:46 +0000227 */
Akronec6bb8e2018-08-29 13:07:56 +0200228 minimize : function () {
Akron24aa0052020-11-10 11:00:34 +0100229 this._el.classList.remove('active');
Akron8b592d42018-01-26 18:33:06 +0100230 },
231
Nils Diewald7c8ced22015-04-15 19:21:00 +0000232
Nils Diewalda297f062015-04-02 00:23:46 +0000233 /**
234 * Get match element.
235 */
236 element : function () {
Akron24aa0052020-11-10 11:00:34 +0100237 return this._el; // May be null
Nils Diewalda297f062015-04-02 00:23:46 +0000238 }
239 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000240});