blob: d3aee6773888e4937e95316542b3150c616ff179 [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([
Nils Diewald0e6992a2015-04-14 20:13:52 +000012 'match/info',
13 'util'
Nils Diewald7c8ced22015-04-15 19:21:00 +000014], function (infoClass) {
Nils Diewald4f6521a2015-03-20 21:30:13 +000015
Nils Diewald6e43ffd2015-03-25 18:55:39 +000016 // Localization values
Nils Diewald0e6992a2015-04-14 20:13:52 +000017 var loc = KorAP.Locale;
Nils Diewalda297f062015-04-02 00:23:46 +000018 loc.ADDTREE = loc.ADDTREE || 'Add tree view';
19 loc.SHOWINFO = loc.SHOWINFO || 'Show information';
20 loc.CLOSE = loc.CLOSE || 'Close';
Nils Diewald0e6992a2015-04-14 20:13:52 +000021
Nils Diewald0e6992a2015-04-14 20:13:52 +000022 var _matchTerms = ['corpusID', 'docID', 'textID', 'matchID', 'available'];
Nils Diewalda297f062015-04-02 00:23:46 +000023
24 /**
25 * Match object
26 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 return {
Nils Diewalde8518f82015-03-18 22:41:49 +000028
29 /**
30 * Create a new annotation object.
31 * Expects an array of available foundry/layer=type terms.
32 * Supported types are 'spans', 'tokens' and 'rels'.
33 */
Nils Diewalda297f062015-04-02 00:23:46 +000034 create : function (match) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000035 return Object.create(this)._init(match);
Nils Diewalde8518f82015-03-18 22:41:49 +000036 },
37
Nils Diewald7c8ced22015-04-15 19:21:00 +000038
Nils Diewald6e43ffd2015-03-25 18:55:39 +000039 /**
Nils Diewalda297f062015-04-02 00:23:46 +000040 * Initialize match.
Nils Diewald6e43ffd2015-03-25 18:55:39 +000041 */
Nils Diewalda297f062015-04-02 00:23:46 +000042 _init : function (match) {
43 this._element = null;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000044
Nils Diewalda297f062015-04-02 00:23:46 +000045 // No match defined
46 if (arguments.length < 1 ||
47 match === null ||
48 match === undefined) {
49 throw new Error('Missing parameters');
50 }
Nils Diewald6e43ffd2015-03-25 18:55:39 +000051
Nils Diewalda297f062015-04-02 00:23:46 +000052 // Match defined as a node
53 else if (match instanceof Node) {
54 this._element = match;
Nils Diewald6e43ffd2015-03-25 18:55:39 +000055
Nils Diewalda297f062015-04-02 00:23:46 +000056 // Circular reference !!
57 match["_match"] = this;
58
59 this.corpusID = match.getAttribute('data-corpus-id'),
60 this.docID = match.getAttribute('data-doc-id'),
61 this.textID = match.getAttribute('data-text-id'),
62 this.matchID = match.getAttribute('data-match-id')
63
64 // List of available annotations
65 this.available = match.getAttribute('data-available-info').split(' ');
66 }
67
68 // Match as an object
69 else {
70
71 // Iterate over allowed match terms
Nils Diewald0e6992a2015-04-14 20:13:52 +000072 for (var i in _matchTerms) {
73 var term = _matchTerms[i];
Nils Diewald7c8ced22015-04-15 19:21:00 +000074 this[term] = match[term] !== undefined ? match[term] : undefined;
Nils Diewalda297f062015-04-02 00:23:46 +000075 };
76 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000077
Nils Diewald7c8ced22015-04-15 19:21:00 +000078 this._avail = {
Nils Diewalde8518f82015-03-18 22:41:49 +000079 tokens : [],
Nils Diewalda297f062015-04-02 00:23:46 +000080 spans : [],
81 rels : []
Nils Diewalde8518f82015-03-18 22:41:49 +000082 };
Nils Diewalda297f062015-04-02 00:23:46 +000083
84 // Iterate over info layers
85 for (var i = 0; i < this.available.length; i++) {
86 var term = this.available[i];
87
Nils Diewalde8518f82015-03-18 22:41:49 +000088 // Create info layer objects
89 try {
Nils Diewald7c8ced22015-04-15 19:21:00 +000090 var layer = require('match/infolayer').create(term);
91 this._avail[layer.type].push(layer);
Nils Diewalde8518f82015-03-18 22:41:49 +000092 }
93 catch (e) {
94 continue;
95 };
96 };
Nils Diewalda297f062015-04-02 00:23:46 +000097
Nils Diewalde8518f82015-03-18 22:41:49 +000098 return this;
99 },
100
101
102 /**
103 * Return a list of parseable tree annotations.
104 */
105 getSpans : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000106 return this._avail.spans;
Nils Diewalde8518f82015-03-18 22:41:49 +0000107 },
108
109
110 /**
111 * Return a list of parseable token annotations.
112 */
113 getTokens : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000114 return this._avail.tokens;
Nils Diewalde8518f82015-03-18 22:41:49 +0000115 },
116
117
118 /**
119 * Return a list of parseable relation annotations.
120 */
121 getRels : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000122 return this._avail.rels;
Nils Diewalde8518f82015-03-18 22:41:49 +0000123 },
124
Nils Diewald7c8ced22015-04-15 19:21:00 +0000125
Nils Diewalda297f062015-04-02 00:23:46 +0000126 /**
127 * Open match
128 */
129 open : function () {
130
131 // Add actions unless it's already activated
132 var element = this._element;
133
134 // There is an element to open
135 if (this._element === undefined || this._element === null)
136 return false;
137
138 // The element is already opened
139 if (element.classList.contains('active'))
140 return false;
141
142 // Add active class to element
143 element.classList.add('active');
144
Nils Diewald7c8ced22015-04-15 19:21:00 +0000145 // Already there
146 if (element.classList.contains('action'))
147 return true;
148
Nils Diewalda297f062015-04-02 00:23:46 +0000149 // Create action buttons
150 var ul = document.createElement('ul');
151 ul.classList.add('action', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +0000152
Nils Diewald7c8ced22015-04-15 19:21:00 +0000153 element.appendChild(ul);
154 element.classList.add('action');
155
156 // Todo: Open in new frame
Nils Diewalda297f062015-04-02 00:23:46 +0000157
158 // Add close button
159 var close = document.createElement('li');
160 close.appendChild(document.createElement('span'))
161 .appendChild(document.createTextNode(loc.CLOSE));
162 close.classList.add('close');
163 close.setAttribute('title', loc.CLOSE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000164
Nils Diewalda297f062015-04-02 00:23:46 +0000165 // Add info button
166 var info = document.createElement('li');
167 info.appendChild(document.createElement('span'))
168 .appendChild(document.createTextNode(loc.SHOWINFO));
169 info.classList.add('info');
170 info.setAttribute('title', loc.SHOWINFO);
171
172 var that = this;
173
174 // Close match
175 close.addEventListener('click', function (e) {
176 e.halt();
177 that.close()
178 });
179
180 // Add information, unless it already exists
181 info.addEventListener('click', function (e) {
182 e.halt();
Nils Diewald5c5a7472015-04-02 22:13:38 +0000183 that.info().toggle();
Nils Diewalda297f062015-04-02 00:23:46 +0000184 });
185
186 ul.appendChild(close);
187 ul.appendChild(info);
188
189 return true;
190 },
191
Nils Diewald7c8ced22015-04-15 19:21:00 +0000192
Nils Diewald8bc7e412015-03-19 22:08:27 +0000193 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000194 * Close info view
195 */
196 close : function () {
197 this._element.classList.remove('active');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000198 /* if (this._info !== undefined) {
199 * this._info.destroy();
200 * };
201 */
Nils Diewalda297f062015-04-02 00:23:46 +0000202 },
203
204
Nils Diewalda297f062015-04-02 00:23:46 +0000205 /**
206 * Get and open associated match info.
207 */
208 info : function () {
209
210 // Create match info
211 if (this._info === undefined)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000212 this._info = infoClass.create(this);
Nils Diewalda297f062015-04-02 00:23:46 +0000213
214 // There is an element to append
215 if (this._element === undefined ||
216 this._element === null)
217 return this._info;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000218
Nils Diewalda297f062015-04-02 00:23:46 +0000219 // Info is already activated
Nils Diewald5c5a7472015-04-02 22:13:38 +0000220 if (this._info._element !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +0000221 return this._info;
222
Nils Diewalda297f062015-04-02 00:23:46 +0000223 return this._info;
224 },
225
Nils Diewald7c8ced22015-04-15 19:21:00 +0000226
Nils Diewalda297f062015-04-02 00:23:46 +0000227 /**
228 * Get match element.
229 */
230 element : function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000231 return this._element; // May be null
Nils Diewalda297f062015-04-02 00:23:46 +0000232 }
233 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000234});