blob: 1a89a8fe78bffa900a46d370efa600066ef5cbb9 [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;
Akronbfe912c2018-07-17 19:30:52 +020020 // loc.SHOWINFO = loc.SHOWINFO || 'Show information';
21 // loc.ADDTREE = loc.ADDTREE || 'Relations';
22 // loc.SHOWANNO = loc.SHOWANNO || 'Tokens';
Akron24866cf2018-01-23 20:22:01 +010023 loc.CLOSE = loc.CLOSE || 'Close';
Akronbfe912c2018-07-17 19:30:52 +020024 // loc.SHOW_META = loc.SHOW_META || 'Metadata';
Nils Diewald0e6992a2015-04-14 20:13:52 +000025
Akron0a6768f2016-07-13 18:00:43 +020026 // 'corpusID', 'docID', 'textID'
Akron0b489ad2018-02-02 16:49:32 +010027 const _matchTerms = ['textSigle', 'matchID', 'available'];
28
29 const d = document;
Nils Diewalda297f062015-04-02 00:23:46 +000030
31 /**
32 * Match object
33 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000034 return {
Nils Diewalde8518f82015-03-18 22:41:49 +000035
36 /**
37 * Create a new annotation object.
38 * Expects an array of available foundry/layer=type terms.
39 * Supported types are 'spans', 'tokens' and 'rels'.
40 */
Nils Diewalda297f062015-04-02 00:23:46 +000041 create : function (match) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000042 return Object.create(this)._init(match);
Nils Diewalde8518f82015-03-18 22:41:49 +000043 },
44
Nils Diewald7c8ced22015-04-15 19:21:00 +000045
Nils Diewald6e43ffd2015-03-25 18:55:39 +000046 /**
Nils Diewalda297f062015-04-02 00:23:46 +000047 * Initialize match.
Nils Diewald6e43ffd2015-03-25 18:55:39 +000048 */
Nils Diewalda297f062015-04-02 00:23:46 +000049 _init : function (match) {
50 this._element = null;
Akronbfe912c2018-07-17 19:30:52 +020051 this._initialized = false;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000052
Nils Diewalda297f062015-04-02 00:23:46 +000053 // No match defined
54 if (arguments.length < 1 ||
Akron19d97fe2016-09-06 20:47:05 +020055 match === null ||
56 match === undefined) {
57 throw new Error('Missing parameters');
Nils Diewalda297f062015-04-02 00:23:46 +000058 }
Nils Diewald6e43ffd2015-03-25 18:55:39 +000059
Nils Diewalda297f062015-04-02 00:23:46 +000060 // Match defined as a node
61 else if (match instanceof Node) {
Akron19d97fe2016-09-06 20:47:05 +020062 this._element = match;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000063
Akron19d97fe2016-09-06 20:47:05 +020064 // Circular reference !!
65 match["_match"] = this;
Nils Diewalda297f062015-04-02 00:23:46 +000066
Akron19d97fe2016-09-06 20:47:05 +020067 /*
68 this.corpusID = match.getAttribute('data-corpus-id'),
69 this.docID = match.getAttribute('data-doc-id'),
70 this.textID = match.getAttribute('data-text-id'),
71 */
72 if (match.hasAttribute('data-text-sigle')) {
73 this.textSigle = match.getAttribute('data-text-sigle')
74 }
75 else {
76 this.textSigle = match.getAttribute('data-corpus-id') +
77 '/' +
78 match.getAttribute('data-doc-id') +
79 '/' +
80 match.getAttribute('data-text-id');
81 };
Akron0a6768f2016-07-13 18:00:43 +020082
Akron19d97fe2016-09-06 20:47:05 +020083 this.matchID = match.getAttribute('data-match-id');
Nils Diewalda297f062015-04-02 00:23:46 +000084
Akron19d97fe2016-09-06 20:47:05 +020085 // List of available annotations
86 this.available = match.getAttribute('data-available-info').split(' ');
Nils Diewalda297f062015-04-02 00:23:46 +000087 }
88
89 // Match as an object
90 else {
91
Akron19d97fe2016-09-06 20:47:05 +020092 // Iterate over allowed match terms
93 for (var i in _matchTerms) {
94 var term = _matchTerms[i];
95 this[term] = match[term] !== undefined ? match[term] : undefined;
96 };
Nils Diewalda297f062015-04-02 00:23:46 +000097 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000098
Nils Diewald7c8ced22015-04-15 19:21:00 +000099 this._avail = {
Akron19d97fe2016-09-06 20:47:05 +0200100 tokens : [],
101 spans : [],
102 rels : []
Nils Diewalde8518f82015-03-18 22:41:49 +0000103 };
Nils Diewalda297f062015-04-02 00:23:46 +0000104
105 // Iterate over info layers
106 for (var i = 0; i < this.available.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +0200107 var term = this.available[i];
Nils Diewalda297f062015-04-02 00:23:46 +0000108
Akron19d97fe2016-09-06 20:47:05 +0200109 // Create info layer objects
110 try {
111 var layer = require('match/infolayer').create(term);
112 this._avail[layer.type].push(layer);
113 }
114 catch (e) {
115 continue;
116 };
Nils Diewalde8518f82015-03-18 22:41:49 +0000117 };
Akron3a4a08e2017-05-23 22:34:18 +0200118
Nils Diewalde8518f82015-03-18 22:41:49 +0000119 return this;
120 },
121
Nils Diewalde8518f82015-03-18 22:41:49 +0000122 /**
123 * Return a list of parseable tree annotations.
124 */
125 getSpans : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000126 return this._avail.spans;
Nils Diewalde8518f82015-03-18 22:41:49 +0000127 },
128
129
130 /**
131 * Return a list of parseable token annotations.
132 */
133 getTokens : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000134 return this._avail.tokens;
Nils Diewalde8518f82015-03-18 22:41:49 +0000135 },
136
137
138 /**
139 * Return a list of parseable relation annotations.
140 */
141 getRels : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000142 return this._avail.rels;
Nils Diewalde8518f82015-03-18 22:41:49 +0000143 },
144
Nils Diewald7c8ced22015-04-15 19:21:00 +0000145
Nils Diewalda297f062015-04-02 00:23:46 +0000146 /**
147 * Open match
148 */
149 open : function () {
150
151 // Add actions unless it's already activated
152 var element = this._element;
153
154 // There is an element to open
155 if (this._element === undefined || this._element === null)
Akron19d97fe2016-09-06 20:47:05 +0200156 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000157
158 // The element is already opened
159 if (element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200160 return false;
Nils Diewalda297f062015-04-02 00:23:46 +0000161
162 // Add active class to element
163 element.classList.add('active');
164
Nils Diewald7c8ced22015-04-15 19:21:00 +0000165 // Already there
Akronbfe912c2018-07-17 19:30:52 +0200166 /*
167 if (element.classList.contains('action'))
Akron19d97fe2016-09-06 20:47:05 +0200168 return true;
Akronbfe912c2018-07-17 19:30:52 +0200169 */
170 if (this._initialized)
171 return true;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000172
Akronbfe912c2018-07-17 19:30:52 +0200173 var btn = buttonGroupClass.create(
174 ['action','button-view']
175 );
176
Nils Diewalda297f062015-04-02 00:23:46 +0000177 var that = this;
Akronbfe912c2018-07-17 19:30:52 +0200178 btn.add(loc.CLOSE, ['button-icon','close'], function () {
179 that.close();
180 });
181 element.appendChild(btn.element());
Nils Diewalda297f062015-04-02 00:23:46 +0000182
Akron24866cf2018-01-23 20:22:01 +0100183 // Add meta button
184 var refLine = element.querySelector("p.ref");
185
Akron0b489ad2018-02-02 16:49:32 +0100186 // No reference found
Akron151bc872018-02-02 14:04:15 +0100187 if (!refLine)
188 return;
Akron13448c22018-07-10 13:05:46 +0200189
Akronbfe912c2018-07-17 19:30:52 +0200190 var panel = matchPanelClass.create(this);
191
192 this._element.insertBefore(
193 panel.element(),
194 this._element.querySelector("p.ref")
195 );
196
Akron13448c22018-07-10 13:05:46 +0200197 // Insert before reference line
198 refLine.insertBefore(
Akronbfe912c2018-07-17 19:30:52 +0200199 panel.actions.element(),
Akron13448c22018-07-10 13:05:46 +0200200 refLine.firstChild
201 );
202
Akronbfe912c2018-07-17 19:30:52 +0200203 this._initialized = true;
Akron13448c22018-07-10 13:05:46 +0200204
Nils Diewalda297f062015-04-02 00:23:46 +0000205 return true;
206 },
207
Akron8c468a12016-11-13 23:57:41 +0100208
Akron6a535d42015-08-26 20:16:58 +0200209 // Todo: Test toggle
210 toggle : function () {
211 if (this._element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200212 this.close();
Akron6a535d42015-08-26 20:16:58 +0200213 else
Akron19d97fe2016-09-06 20:47:05 +0200214 this.open();
Akron6a535d42015-08-26 20:16:58 +0200215 },
216
Nils Diewald7c8ced22015-04-15 19:21:00 +0000217
Nils Diewald8bc7e412015-03-19 22:08:27 +0000218 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000219 * Close info view
220 */
221 close : function () {
222 this._element.classList.remove('active');
Akron8b592d42018-01-26 18:33:06 +0100223 },
224
Nils Diewald7c8ced22015-04-15 19:21:00 +0000225
Nils Diewalda297f062015-04-02 00:23:46 +0000226 /**
227 * Get match element.
228 */
229 element : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000230 return this._element; // May be null
Nils Diewalda297f062015-04-02 00:23:46 +0000231 }
232 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000233});