blob: b5f0d5af900ab15be0a8e4b5f983adbb438fd32a [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([
Nils Diewald0e6992a2015-04-14 20:13:52 +000013 'match/info',
Akron30ee5142015-06-26 01:50:14 +020014 'match/reference',
Nils Diewald0e6992a2015-04-14 20:13:52 +000015 'util'
Akron30ee5142015-06-26 01:50:14 +020016], function (infoClass, 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;
Nils Diewalda297f062015-04-02 00:23:46 +000020 loc.ADDTREE = loc.ADDTREE || 'Add tree view';
21 loc.SHOWINFO = loc.SHOWINFO || 'Show information';
22 loc.CLOSE = loc.CLOSE || 'Close';
Nils Diewald0e6992a2015-04-14 20:13:52 +000023
Akron0a6768f2016-07-13 18:00:43 +020024 // 'corpusID', 'docID', 'textID'
25 var _matchTerms = ['textSigle', 'matchID', 'available'];
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 /**
33 * Create a new annotation object.
34 * Expects an array of available foundry/layer=type terms.
35 * Supported types are 'spans', 'tokens' and 'rels'.
36 */
Nils Diewalda297f062015-04-02 00:23:46 +000037 create : function (match) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000038 return Object.create(this)._init(match);
Nils Diewalde8518f82015-03-18 22:41:49 +000039 },
40
Nils Diewald7c8ced22015-04-15 19:21:00 +000041
Nils Diewald6e43ffd2015-03-25 18:55:39 +000042 /**
Nils Diewalda297f062015-04-02 00:23:46 +000043 * Initialize match.
Nils Diewald6e43ffd2015-03-25 18:55:39 +000044 */
Nils Diewalda297f062015-04-02 00:23:46 +000045 _init : function (match) {
46 this._element = null;
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 };
Nils Diewalda297f062015-04-02 00:23:46 +0000113
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 () {
145
146 // Add actions unless it's already activated
147 var element = this._element;
148
149 // There is an element to open
150 if (this._element === undefined || this._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
161 if (element.classList.contains('action'))
Akron19d97fe2016-09-06 20:47:05 +0200162 return true;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000163
Nils Diewalda297f062015-04-02 00:23:46 +0000164 // Create action buttons
165 var ul = document.createElement('ul');
166 ul.classList.add('action', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +0000167
Nils Diewald7c8ced22015-04-15 19:21:00 +0000168 element.appendChild(ul);
169 element.classList.add('action');
170
171 // Todo: Open in new frame
Nils Diewalda297f062015-04-02 00:23:46 +0000172
173 // Add close button
174 var close = document.createElement('li');
175 close.appendChild(document.createElement('span'))
Akron19d97fe2016-09-06 20:47:05 +0200176 .appendChild(document.createTextNode(loc.CLOSE));
Nils Diewalda297f062015-04-02 00:23:46 +0000177 close.classList.add('close');
178 close.setAttribute('title', loc.CLOSE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000179
Nils Diewalda297f062015-04-02 00:23:46 +0000180 // Add info button
181 var info = document.createElement('li');
182 info.appendChild(document.createElement('span'))
Akron19d97fe2016-09-06 20:47:05 +0200183 .appendChild(document.createTextNode(loc.SHOWINFO));
Nils Diewalda297f062015-04-02 00:23:46 +0000184 info.classList.add('info');
185 info.setAttribute('title', loc.SHOWINFO);
186
187 var that = this;
188
189 // Close match
190 close.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200191 e.halt();
192 that.close()
Nils Diewalda297f062015-04-02 00:23:46 +0000193 });
194
195 // Add information, unless it already exists
196 info.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200197 e.halt();
198 that.info().toggle();
Nils Diewalda297f062015-04-02 00:23:46 +0000199 });
200
201 ul.appendChild(close);
202 ul.appendChild(info);
203
204 return true;
205 },
206
Akron8c468a12016-11-13 23:57:41 +0100207
208 /**
209 * Return a list of meta data.
210 */
211 showMeta : function () {
212 this.metaInfo = this._element.getAttribute('data-info');
Akron1f0d04c2016-11-18 22:51:20 +0100213 if (metaInfo)
214 metaInfo = JSON.parse(metaInfo);
215 if (metaInfo) {
216
217 // TODO: Meta fields should be separated
218 for (i in metaInfo) {
219 if (i !== "UID" &&
220 i !== "corpusID" &&
221 i !== "docID" &&
222 i !== "textID" &&
223 i !== "corpusSigle" &&
224 i !== "docSigle" &&
225 i !== "textSigle" &&
226 i !== "layerInfos") {
227 console.log(i);
228 };
229 };
230 };
Akron8c468a12016-11-13 23:57:41 +0100231 },
232
Akron6a535d42015-08-26 20:16:58 +0200233 // Todo: Test toggle
234 toggle : function () {
235 if (this._element.classList.contains('active'))
Akron19d97fe2016-09-06 20:47:05 +0200236 this.close();
Akron6a535d42015-08-26 20:16:58 +0200237 else
Akron19d97fe2016-09-06 20:47:05 +0200238 this.open();
Akron6a535d42015-08-26 20:16:58 +0200239 },
240
Nils Diewald7c8ced22015-04-15 19:21:00 +0000241
Nils Diewald8bc7e412015-03-19 22:08:27 +0000242 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000243 * Close info view
244 */
245 close : function () {
246 this._element.classList.remove('active');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000247 /* if (this._info !== undefined) {
248 * this._info.destroy();
249 * };
250 */
Nils Diewalda297f062015-04-02 00:23:46 +0000251 },
252
253
Nils Diewalda297f062015-04-02 00:23:46 +0000254 /**
255 * Get and open associated match info.
256 */
257 info : function () {
258
259 // Create match info
260 if (this._info === undefined)
Akron19d97fe2016-09-06 20:47:05 +0200261 this._info = infoClass.create(this);
Nils Diewalda297f062015-04-02 00:23:46 +0000262
263 // There is an element to append
264 if (this._element === undefined ||
Akron19d97fe2016-09-06 20:47:05 +0200265 this._element === null)
266 return this._info;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000267
Nils Diewalda297f062015-04-02 00:23:46 +0000268 // Info is already activated
Nils Diewald5c5a7472015-04-02 22:13:38 +0000269 if (this._info._element !== undefined)
Akron19d97fe2016-09-06 20:47:05 +0200270 return this._info;
Nils Diewalda297f062015-04-02 00:23:46 +0000271
Nils Diewalda297f062015-04-02 00:23:46 +0000272 return this._info;
273 },
274
Nils Diewald7c8ced22015-04-15 19:21:00 +0000275
Nils Diewalda297f062015-04-02 00:23:46 +0000276 /**
277 * Get match element.
278 */
279 element : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000280 return this._element; // May be null
Nils Diewalda297f062015-04-02 00:23:46 +0000281 }
282 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000283});