blob: 3c44656c7adc63537fb4b35efe17b7cc993e20a8 [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'
Akronbfe912c2018-07-17 19:30:52 +020016], function (buttonGroupClass,matchPanelClass) { //, 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;
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) {
45 this._element = null;
Akronbfe912c2018-07-17 19:30:52 +020046 this._initialized = false;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000047
Nils Diewalda297f062015-04-02 00:23:46 +000048 // No match defined
49 if (arguments.length < 1 ||
Akron19d97fe2016-09-06 20:47:05 +020050 match === null ||
51 match === undefined) {
52 throw new Error('Missing parameters');
Nils Diewalda297f062015-04-02 00:23:46 +000053 }
Nils Diewald6e43ffd2015-03-25 18:55:39 +000054
Nils Diewalda297f062015-04-02 00:23:46 +000055 // Match defined as a node
56 else if (match instanceof Node) {
Akron19d97fe2016-09-06 20:47:05 +020057 this._element = match;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000058
Akron19d97fe2016-09-06 20:47:05 +020059 // Circular reference !!
60 match["_match"] = this;
Nils Diewalda297f062015-04-02 00:23:46 +000061
Akron19d97fe2016-09-06 20:47:05 +020062 /*
63 this.corpusID = match.getAttribute('data-corpus-id'),
64 this.docID = match.getAttribute('data-doc-id'),
65 this.textID = match.getAttribute('data-text-id'),
66 */
67 if (match.hasAttribute('data-text-sigle')) {
68 this.textSigle = match.getAttribute('data-text-sigle')
69 }
70 else {
71 this.textSigle = match.getAttribute('data-corpus-id') +
72 '/' +
73 match.getAttribute('data-doc-id') +
74 '/' +
75 match.getAttribute('data-text-id');
76 };
Akron0a6768f2016-07-13 18:00:43 +020077
Akron19d97fe2016-09-06 20:47:05 +020078 this.matchID = match.getAttribute('data-match-id');
Nils Diewalda297f062015-04-02 00:23:46 +000079
Akron19d97fe2016-09-06 20:47:05 +020080 // List of available annotations
81 this.available = match.getAttribute('data-available-info').split(' ');
Nils Diewalda297f062015-04-02 00:23:46 +000082 }
83
84 // Match as an object
85 else {
86
Akron19d97fe2016-09-06 20:47:05 +020087 // Iterate over allowed match terms
88 for (var i in _matchTerms) {
89 var term = _matchTerms[i];
90 this[term] = match[term] !== undefined ? match[term] : undefined;
91 };
Nils Diewalda297f062015-04-02 00:23:46 +000092 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000093
Nils Diewald7c8ced22015-04-15 19:21:00 +000094 this._avail = {
Akron19d97fe2016-09-06 20:47:05 +020095 tokens : [],
96 spans : [],
97 rels : []
Nils Diewalde8518f82015-03-18 22:41:49 +000098 };
Nils Diewalda297f062015-04-02 00:23:46 +000099
100 // Iterate over info layers
101 for (var i = 0; i < this.available.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +0200102 var term = this.available[i];
Nils Diewalda297f062015-04-02 00:23:46 +0000103
Akron19d97fe2016-09-06 20:47:05 +0200104 // Create info layer objects
105 try {
106 var layer = require('match/infolayer').create(term);
107 this._avail[layer.type].push(layer);
108 }
109 catch (e) {
110 continue;
111 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000112 };
Akron3a4a08e2017-05-23 22:34:18 +0200113
Nils Diewalde8518f82015-03-18 22:41:49 +0000114 return this;
115 },
116
Nils Diewalde8518f82015-03-18 22:41:49 +0000117 /**
118 * Return a list of parseable tree annotations.
119 */
120 getSpans : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000121 return this._avail.spans;
Nils Diewalde8518f82015-03-18 22:41:49 +0000122 },
123
124
125 /**
126 * Return a list of parseable token annotations.
127 */
128 getTokens : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000129 return this._avail.tokens;
Nils Diewalde8518f82015-03-18 22:41:49 +0000130 },
131
132
133 /**
134 * Return a list of parseable relation annotations.
135 */
136 getRels : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000137 return this._avail.rels;
Nils Diewalde8518f82015-03-18 22:41:49 +0000138 },
139
Nils Diewald7c8ced22015-04-15 19:21:00 +0000140
Nils Diewalda297f062015-04-02 00:23:46 +0000141 /**
142 * Open match
143 */
144 open : function () {
Akron7f9a6a32018-07-18 15:05:23 +0200145
Nils Diewalda297f062015-04-02 00:23:46 +0000146 // Add actions unless it's already activated
147 var element = this._element;
148
149 // There is an element to open
Akron7f9a6a32018-07-18 15:05:23 +0200150 if (element === undefined || element === null)
Akron19d97fe2016-09-06 20:47:05 +0200151 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000152
153 // The element is already opened
154 if (element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200155 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000156
157 // Add active class to element
158 element.classList.add('active');
159
Nils Diewald7c8ced22015-04-15 19:21:00 +0000160 // Already there
Akronbfe912c2018-07-17 19:30:52 +0200161 /*
162 if (element.classList.contains('action'))
Akron19d97fe2016-09-06 20:47:05 +0200163 return true;
Akronbfe912c2018-07-17 19:30:52 +0200164 */
165 if (this._initialized)
166 return true;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000167
Akronbfe912c2018-07-17 19:30:52 +0200168 var btn = buttonGroupClass.create(
169 ['action','button-view']
170 );
171
Nils Diewalda297f062015-04-02 00:23:46 +0000172 var that = this;
Akronec6bb8e2018-08-29 13:07:56 +0200173 btn.add(loc.MINIMIZE, ['button-icon','minimize'], function () {
174 that.minimize();
Akronbfe912c2018-07-17 19:30:52 +0200175 });
176 element.appendChild(btn.element());
Nils Diewalda297f062015-04-02 00:23:46 +0000177
Akron24866cf2018-01-23 20:22:01 +0100178 // Add meta button
179 var refLine = element.querySelector("p.ref");
180
Akron0b489ad2018-02-02 16:49:32 +0100181 // No reference found
Akron151bc872018-02-02 14:04:15 +0100182 if (!refLine)
183 return;
Akrone1c27f62018-07-20 11:42:59 +0200184
185 // Create panel
Akron7f9a6a32018-07-18 15:05:23 +0200186 this.panel = matchPanelClass.create(this);
Akronbfe912c2018-07-17 19:30:52 +0200187
188 this._element.insertBefore(
Akron7f9a6a32018-07-18 15:05:23 +0200189 this.panel.element(),
Akronbfe912c2018-07-17 19:30:52 +0200190 this._element.querySelector("p.ref")
191 );
192
Akron13448c22018-07-10 13:05:46 +0200193 // Insert before reference line
194 refLine.insertBefore(
Akron7f9a6a32018-07-18 15:05:23 +0200195 this.panel.actions.element(),
Akron13448c22018-07-10 13:05:46 +0200196 refLine.firstChild
197 );
198
Akronbfe912c2018-07-17 19:30:52 +0200199 this._initialized = true;
Akron13448c22018-07-10 13:05:46 +0200200
Nils Diewalda297f062015-04-02 00:23:46 +0000201 return true;
202 },
203
Akron8c468a12016-11-13 23:57:41 +0100204
Akron6a535d42015-08-26 20:16:58 +0200205 // Todo: Test toggle
206 toggle : function () {
207 if (this._element.classList.contains('active'))
Akronec6bb8e2018-08-29 13:07:56 +0200208 this.minimize();
Akron6a535d42015-08-26 20:16:58 +0200209 else
Akron19d97fe2016-09-06 20:47:05 +0200210 this.open();
Akron6a535d42015-08-26 20:16:58 +0200211 },
212
Nils Diewald7c8ced22015-04-15 19:21:00 +0000213
Nils Diewald8bc7e412015-03-19 22:08:27 +0000214 /**
Akronec6bb8e2018-08-29 13:07:56 +0200215 * Minimize info view
Nils Diewalda297f062015-04-02 00:23:46 +0000216 */
Akronec6bb8e2018-08-29 13:07:56 +0200217 minimize : function () {
Nils Diewalda297f062015-04-02 00:23:46 +0000218 this._element.classList.remove('active');
Akron8b592d42018-01-26 18:33:06 +0100219 },
220
Nils Diewald7c8ced22015-04-15 19:21:00 +0000221
Nils Diewalda297f062015-04-02 00:23:46 +0000222 /**
223 * Get match element.
224 */
225 element : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000226 return this._element; // May be null
Nils Diewalda297f062015-04-02 00:23:46 +0000227 }
228 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000229});