blob: 761b9168d8ae67cb24265e2d348afeb6af0c91f9 [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([
Akron13448c22018-07-10 13:05:46 +020013 'buttongroup',
Akronbfe912c2018-07-17 19:30:52 +020014 'panel/match',
Akron8b592d42018-01-26 18:33:06 +010015 'util'
Akrone57e8802020-10-17 08:23:45 +020016], function (buttonGroupClass,matchPanelClass) {
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;
Akronec6bb8e2018-08-29 13:07:56 +020020 loc.MINIMIZE = loc.MINIMIZE || 'Minimize';
Nils Diewald0e6992a2015-04-14 20:13:52 +000021
Akron0a6768f2016-07-13 18:00:43 +020022 // 'corpusID', 'docID', 'textID'
Akron0b489ad2018-02-02 16:49:32 +010023 const _matchTerms = ['textSigle', 'matchID', 'available'];
24
25 const d = document;
Nils Diewalda297f062015-04-02 00:23:46 +000026
27 /**
28 * Match object
29 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000030 return {
Nils Diewalde8518f82015-03-18 22:41:49 +000031
32 /**
Akron7f9a6a32018-07-18 15:05:23 +020033 * Create a new match object.
34 * Expects an element with match descriptions.
Nils Diewalde8518f82015-03-18 22:41:49 +000035 */
Nils Diewalda297f062015-04-02 00:23:46 +000036 create : function (match) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000037 return Object.create(this)._init(match);
Nils Diewalde8518f82015-03-18 22:41:49 +000038 },
39
Nils Diewald7c8ced22015-04-15 19:21:00 +000040
Nils Diewald6e43ffd2015-03-25 18:55:39 +000041 /**
Nils Diewalda297f062015-04-02 00:23:46 +000042 * Initialize match.
Nils Diewald6e43ffd2015-03-25 18:55:39 +000043 */
Nils Diewalda297f062015-04-02 00:23:46 +000044 _init : function (match) {
Akrone57e8802020-10-17 08:23:45 +020045 const t= this;
46 t._element = null;
47 t._initialized = false;
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) {
Akrone57e8802020-10-17 08:23:45 +020058 t._element = match;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000059
Akron19d97fe2016-09-06 20:47:05 +020060 // Circular reference !!
Akrone57e8802020-10-17 08:23:45 +020061 match["_match"] = t;
Nils Diewalda297f062015-04-02 00:23:46 +000062
Akron19d97fe2016-09-06 20:47:05 +020063 if (match.hasAttribute('data-text-sigle')) {
Akrone57e8802020-10-17 08:23:45 +020064 t.textSigle = match.getAttribute('data-text-sigle')
Akron19d97fe2016-09-06 20:47:05 +020065 }
66 else {
Akrone57e8802020-10-17 08:23:45 +020067 t.textSigle = match.getAttribute('data-corpus-id') +
Akron19d97fe2016-09-06 20:47:05 +020068 '/' +
69 match.getAttribute('data-doc-id') +
70 '/' +
71 match.getAttribute('data-text-id');
72 };
Akron0a6768f2016-07-13 18:00:43 +020073
Akrone57e8802020-10-17 08:23:45 +020074 t.matchID = match.getAttribute('data-match-id');
75
Akron19d97fe2016-09-06 20:47:05 +020076 // List of available annotations
Akrone57e8802020-10-17 08:23:45 +020077 t.available = match.getAttribute('data-available-info').split(' ');
Nils Diewalda297f062015-04-02 00:23:46 +000078 }
79
80 // Match as an object
81 else {
82
Akron19d97fe2016-09-06 20:47:05 +020083 // Iterate over allowed match terms
Akron678c26f2020-10-09 08:52:50 +020084 _matchTerms.forEach(function(term) {
Akron19d97fe2016-09-06 20:47:05 +020085 this[term] = match[term] !== undefined ? match[term] : undefined;
Akrone57e8802020-10-17 08:23:45 +020086 }, t);
Nils Diewalda297f062015-04-02 00:23:46 +000087 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000088
Akrone57e8802020-10-17 08:23:45 +020089 t._avail = {
Akron19d97fe2016-09-06 20:47:05 +020090 tokens : [],
91 spans : [],
92 rels : []
Nils Diewalde8518f82015-03-18 22:41:49 +000093 };
Nils Diewalda297f062015-04-02 00:23:46 +000094
95 // Iterate over info layers
Akronb50964a2020-10-12 11:44:37 +020096 let layer;
Akrone57e8802020-10-17 08:23:45 +020097 t.available.forEach(function(term){
Nils Diewalda297f062015-04-02 00:23:46 +000098
Akron19d97fe2016-09-06 20:47:05 +020099 // Create info layer objects
100 try {
Akronb50964a2020-10-12 11:44:37 +0200101 layer = require('match/infolayer').create(term);
Akron19d97fe2016-09-06 20:47:05 +0200102 this._avail[layer.type].push(layer);
103 }
104 catch (e) {
Akronb50964a2020-10-12 11:44:37 +0200105 return;
Akron19d97fe2016-09-06 20:47:05 +0200106 };
Akrone57e8802020-10-17 08:23:45 +0200107 }, t);
Akron3a4a08e2017-05-23 22:34:18 +0200108
Akrone57e8802020-10-17 08:23:45 +0200109 return t;
Nils Diewalde8518f82015-03-18 22:41:49 +0000110 },
111
Nils Diewalde8518f82015-03-18 22:41:49 +0000112 /**
113 * Return a list of parseable tree annotations.
114 */
115 getSpans : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000116 return this._avail.spans;
Nils Diewalde8518f82015-03-18 22:41:49 +0000117 },
118
119
120 /**
121 * Return a list of parseable token annotations.
122 */
123 getTokens : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000124 return this._avail.tokens;
Nils Diewalde8518f82015-03-18 22:41:49 +0000125 },
126
127
128 /**
129 * Return a list of parseable relation annotations.
130 */
131 getRels : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000132 return this._avail.rels;
Nils Diewalde8518f82015-03-18 22:41:49 +0000133 },
134
Akron3c390c42020-03-30 09:06:21 +0200135 /**
136 * Initialize match
137 */
138 init : function () {
Akrone57e8802020-10-17 08:23:45 +0200139 const t = this;
140 if (t._initialized)
141 return t;
Akron3c390c42020-03-30 09:06:21 +0200142
143 // Add actions unless it's already activated
Akrone57e8802020-10-17 08:23:45 +0200144 const element = t._element;
Akron3c390c42020-03-30 09:06:21 +0200145
146 // There is an element to open
147 if (element === undefined || element === null)
148 return undefined;
149
150 // Add meta button
Akrone57e8802020-10-17 08:23:45 +0200151 const refLine = element.querySelector("p.ref");
Akron3c390c42020-03-30 09:06:21 +0200152
153 // No reference found
154 if (!refLine)
155 return undefined;
156
157 // Create panel
Akrone57e8802020-10-17 08:23:45 +0200158 t.panel = matchPanelClass.create(t);
Akron3c390c42020-03-30 09:06:21 +0200159
Akrone57e8802020-10-17 08:23:45 +0200160 t._element.insertBefore(
161 t.panel.element(),
162 t._element.querySelector("p.ref")
Akron3c390c42020-03-30 09:06:21 +0200163 );
164
165 // Insert before reference line
166 refLine.insertBefore(
Akrone57e8802020-10-17 08:23:45 +0200167 t.panel.actions.element(),
Akron3c390c42020-03-30 09:06:21 +0200168 refLine.firstChild
169 );
170
Akrone57e8802020-10-17 08:23:45 +0200171 t._initialized = true;
172 return t;
Akron3c390c42020-03-30 09:06:21 +0200173 },
Nils Diewald7c8ced22015-04-15 19:21:00 +0000174
Nils Diewalda297f062015-04-02 00:23:46 +0000175 /**
176 * Open match
177 */
178 open : function () {
Akron7f9a6a32018-07-18 15:05:23 +0200179
Nils Diewalda297f062015-04-02 00:23:46 +0000180 // Add actions unless it's already activated
Akrone57e8802020-10-17 08:23:45 +0200181 const element = this._element;
Nils Diewalda297f062015-04-02 00:23:46 +0000182
183 // There is an element to open
Akron7f9a6a32018-07-18 15:05:23 +0200184 if (element === undefined || element === null)
Akron19d97fe2016-09-06 20:47:05 +0200185 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000186
187 // The element is already opened
188 if (element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200189 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000190
191 // Add active class to element
192 element.classList.add('active');
193
Akrone57e8802020-10-17 08:23:45 +0200194 const btn = buttonGroupClass.create(
Akronbfe912c2018-07-17 19:30:52 +0200195 ['action','button-view']
196 );
197
Akrone57e8802020-10-17 08:23:45 +0200198 const that = this;
Akron792b1a42020-09-14 18:56:38 +0200199 btn.add(loc.MINIMIZE, {'cls':['button-icon','minimize']}, function () {
Akronec6bb8e2018-08-29 13:07:56 +0200200 that.minimize();
Akronbfe912c2018-07-17 19:30:52 +0200201 });
202 element.appendChild(btn.element());
Akron3c390c42020-03-30 09:06:21 +0200203
204 if (this.init() == undefined) {
205 return false;
206 };
Akron13448c22018-07-10 13:05:46 +0200207
Nils Diewalda297f062015-04-02 00:23:46 +0000208 return true;
209 },
210
Akron8c468a12016-11-13 23:57:41 +0100211
Akrone57e8802020-10-17 08:23:45 +0200212 /**
213 * Toggle match view
214 */
Akron6a535d42015-08-26 20:16:58 +0200215 toggle : function () {
216 if (this._element.classList.contains('active'))
Akronec6bb8e2018-08-29 13:07:56 +0200217 this.minimize();
Akron6a535d42015-08-26 20:16:58 +0200218 else
Akron19d97fe2016-09-06 20:47:05 +0200219 this.open();
Akron6a535d42015-08-26 20:16:58 +0200220 },
221
Nils Diewald7c8ced22015-04-15 19:21:00 +0000222
Nils Diewald8bc7e412015-03-19 22:08:27 +0000223 /**
Akronec6bb8e2018-08-29 13:07:56 +0200224 * Minimize info view
Nils Diewalda297f062015-04-02 00:23:46 +0000225 */
Akronec6bb8e2018-08-29 13:07:56 +0200226 minimize : function () {
Nils Diewalda297f062015-04-02 00:23:46 +0000227 this._element.classList.remove('active');
Akron8b592d42018-01-26 18:33:06 +0100228 },
229
Nils Diewald7c8ced22015-04-15 19:21:00 +0000230
Nils Diewalda297f062015-04-02 00:23:46 +0000231 /**
232 * Get match element.
233 */
234 element : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000235 return this._element; // May be null
Nils Diewalda297f062015-04-02 00:23:46 +0000236 }
237 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000238});