blob: beb4bcaa03077dc8952523cc0454503a6f233190 [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
Nils Diewalde8518f82015-03-18 22:41:49 +000010 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000011define([
12 'match/infolayer',
13 'match/info',
14 'util'
15], function (infoLayerClass,
16 infoClass) {
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
24 // KorAP._AvailableRE = new RegExp("^([^\/]+?)\/([^=]+?)(?:=(spans|rels|tokens))?$");
25 // KorAP._TermRE = new RegExp("^(?:([^\/]+?)\/)?([^:]+?):(.+?)$");
26 var _matchTerms = ['corpusID', 'docID', 'textID', 'matchID', 'available'];
Nils Diewalda297f062015-04-02 00:23:46 +000027
28 /**
29 * Match object
30 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 return {
Nils Diewalde8518f82015-03-18 22:41:49 +000032
33 /**
34 * Create a new annotation object.
35 * Expects an array of available foundry/layer=type terms.
36 * Supported types are 'spans', 'tokens' and 'rels'.
37 */
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 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 ||
50 match === null ||
51 match === undefined) {
52 throw new Error('Missing parameters');
53 }
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) {
57 this._element = match;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000058
Nils Diewalda297f062015-04-02 00:23:46 +000059 // Circular reference !!
60 match["_match"] = this;
61
62 this.corpusID = match.getAttribute('data-corpus-id'),
63 this.docID = match.getAttribute('data-doc-id'),
64 this.textID = match.getAttribute('data-text-id'),
65 this.matchID = match.getAttribute('data-match-id')
66
67 // List of available annotations
68 this.available = match.getAttribute('data-available-info').split(' ');
69 }
70
71 // Match as an object
72 else {
73
74 // Iterate over allowed match terms
Nils Diewald0e6992a2015-04-14 20:13:52 +000075 for (var i in _matchTerms) {
76 var term = _matchTerms[i];
Nils Diewalda297f062015-04-02 00:23:46 +000077 if (match[term] !== undefined) {
78 this[term] = match[term];
79 }
80 else {
81 this[term] = undefined;
82 }
83 };
84 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000085
Nils Diewalde8518f82015-03-18 22:41:49 +000086 this._available = {
87 tokens : [],
Nils Diewalda297f062015-04-02 00:23:46 +000088 spans : [],
89 rels : []
Nils Diewalde8518f82015-03-18 22:41:49 +000090 };
Nils Diewalda297f062015-04-02 00:23:46 +000091
92 // Iterate over info layers
93 for (var i = 0; i < this.available.length; i++) {
94 var term = this.available[i];
95
Nils Diewalde8518f82015-03-18 22:41:49 +000096 // Create info layer objects
97 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +000098 var layer = infoLayerClass.create(term);
Nils Diewalde8518f82015-03-18 22:41:49 +000099 this._available[layer.type].push(layer);
100 }
101 catch (e) {
102 continue;
103 };
104 };
Nils Diewalda297f062015-04-02 00:23:46 +0000105
Nils Diewalde8518f82015-03-18 22:41:49 +0000106 return this;
107 },
108
109
110 /**
111 * Return a list of parseable tree annotations.
112 */
113 getSpans : function () {
114 return this._available.spans;
115 },
116
117
118 /**
119 * Return a list of parseable token annotations.
120 */
121 getTokens : function () {
122 return this._available.tokens;
123 },
124
125
126 /**
127 * Return a list of parseable relation annotations.
128 */
129 getRels : function () {
130 return this._available.rels;
131 },
132
Nils Diewalda297f062015-04-02 00:23:46 +0000133 /**
134 * Open match
135 */
136 open : function () {
137
138 // Add actions unless it's already activated
139 var element = this._element;
140
141 // There is an element to open
142 if (this._element === undefined || this._element === null)
143 return false;
144
145 // The element is already opened
146 if (element.classList.contains('active'))
147 return false;
148
149 // Add active class to element
150 element.classList.add('active');
151
152 // Create action buttons
153 var ul = document.createElement('ul');
154 ul.classList.add('action', 'right');
155 element.appendChild(ul);
156
157 // Use localization
158 var loc = KorAP.Locale;
159
160 // Add close button
161 var close = document.createElement('li');
162 close.appendChild(document.createElement('span'))
163 .appendChild(document.createTextNode(loc.CLOSE));
164 close.classList.add('close');
165 close.setAttribute('title', loc.CLOSE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000166
Nils Diewalda297f062015-04-02 00:23:46 +0000167 // Add info button
168 var info = document.createElement('li');
169 info.appendChild(document.createElement('span'))
170 .appendChild(document.createTextNode(loc.SHOWINFO));
171 info.classList.add('info');
172 info.setAttribute('title', loc.SHOWINFO);
173
174 var that = this;
175
176 // Close match
177 close.addEventListener('click', function (e) {
178 e.halt();
179 that.close()
180 });
181
182 // Add information, unless it already exists
183 info.addEventListener('click', function (e) {
184 e.halt();
Nils Diewald5c5a7472015-04-02 22:13:38 +0000185 that.info().toggle();
Nils Diewalda297f062015-04-02 00:23:46 +0000186 });
187
188 ul.appendChild(close);
189 ul.appendChild(info);
190
191 return true;
192 },
193
Nils Diewald8bc7e412015-03-19 22:08:27 +0000194 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000195 * Close info view
196 */
197 close : function () {
198 this._element.classList.remove('active');
199
200/*
201 if (this._info !== undefined) {
202 this._info.destroy();
203 };
204*/
205 },
206
207
Nils Diewalda297f062015-04-02 00:23:46 +0000208 /**
209 * Get and open associated match info.
210 */
211 info : function () {
212
213 // Create match info
214 if (this._info === undefined)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000215 this._info = infoClass.create(this);
Nils Diewalda297f062015-04-02 00:23:46 +0000216
217 // There is an element to append
218 if (this._element === undefined ||
219 this._element === null)
220 return this._info;
221
222 // Info is already activated
Nils Diewald5c5a7472015-04-02 22:13:38 +0000223 if (this._info._element !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +0000224 return this._info;
225
Nils Diewalda297f062015-04-02 00:23:46 +0000226 return this._info;
227 },
228
229
230 /**
231 * Get match element.
232 */
233 element : function () {
234
235 // May be null
236 return this._element;
237 }
238 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000239});