blob: 1924f2ba89a42a70cd89a02fb36774344a893624 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001 /**
2 * Information about a match.
3 */
Nils Diewald7c8ced22015-04-15 19:21:00 +00004define([
5 'match/infolayer',
6 'match/table',
7 'match/tree',
8 'match/treemenu',
9 'util'
10], function (infoLayerClass,
Akron3bb91bc2016-12-02 16:43:17 +010011 matchTableClass,
12 matchTreeClass,
13 matchTreeMenuClass) {
14
Nils Diewald7148c6f2015-05-04 15:07:53 +000015 // Override
Nils Diewald0e6992a2015-04-14 20:13:52 +000016 KorAP.API.getMatchInfo = KorAP.API.getMatchInfo || function () {
17 KorAP.log(0, 'KorAP.API.getMatchInfo() not implemented')
18 return {};
19 };
20
21 var loc = KorAP.Locale;
22
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 return {
Nils Diewald7148c6f2015-05-04 15:07:53 +000024
25 /**
26 * Create new match object
27 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 create : function (match) {
29 return Object.create(this)._init(match);
30 },
31
32 /**
33 * Initialize object
34 */
35 _init : function (match) {
36 this._match = match;
37 this.opened = false;
38 return this;
39 },
40
41 /**
42 * Get match object
43 */
44 match : function () {
45 return this._match;
46 },
47
Nils Diewald7148c6f2015-05-04 15:07:53 +000048
49 /**
50 * Open the information view,
51 * if closed, otherwise close.
52 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000053 toggle : function () {
Akron3bb91bc2016-12-02 16:43:17 +010054
55 var elem = this._match.element().getElementsByClassName('match-wrap')[0];
56
57 if (this.opened == true) {
58 elem.removeChild(
59 this.element()
60 );
61 this.opened = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +000062 }
63 else {
Akron3bb91bc2016-12-02 16:43:17 +010064 // Append element to match
65 elem.appendChild(
66 this.element()
67 );
68 this.opened = true;
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 };
70
71 return this.opened;
72 },
73
74
75 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000076 * Retrieve and parse snippet for table
77 * representation
Nils Diewald0e6992a2015-04-14 20:13:52 +000078 */
79 getTable : function (tokens, cb) {
80 var focus = [];
81
82 // Get all tokens
83 if (tokens === undefined) {
Akron3bb91bc2016-12-02 16:43:17 +010084 focus = this._match.getTokens();
Nils Diewald0e6992a2015-04-14 20:13:52 +000085 }
86
87 // Get only some tokens
88 else {
89
Akron3bb91bc2016-12-02 16:43:17 +010090 // Push newly to focus array
91 for (var i = 0; i < tokens.length; i++) {
92 var term = tokens[i];
93 try {
94 // Create info layer objects
95 var layer = infoLayerClass.create(term);
96 layer.type = "tokens";
97 focus.push(layer);
98 }
99 catch (e) {
100 continue;
101 };
102 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000103 };
104
105 // No tokens chosen
106 if (focus.length == 0)
Akron3bb91bc2016-12-02 16:43:17 +0100107 cb(null);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000108
109 // Get info (may be cached)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000110 KorAP.API.getMatchInfo(
Akron3bb91bc2016-12-02 16:43:17 +0100111 this._match,
112 { 'spans' : false, 'layer' : focus },
Nils Diewald0e6992a2015-04-14 20:13:52 +0000113
Akron3bb91bc2016-12-02 16:43:17 +0100114 // Callback for retrieval
115 function (matchResponse) {
116
117 // Get snippet from match info
118 if (matchResponse["snippet"] !== undefined) {
119 this._table = matchTableClass.create(matchResponse["snippet"]);
120 cb(this._table);
121 };
122 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000123 );
124
125 /*
126 // Todo: Store the table as a hash of the focus
127 return null;
128 */
129 },
130
131
132 /**
133 * Retrieve and parse snippet for tree representation
134 */
135 getTree : function (foundry, layer, cb) {
136 var focus = [];
137
138 // TODO: Support and cache multiple trees
139 KorAP.API.getMatchInfo(
Akronc56cf2d2016-11-09 22:02:38 +0100140 this._match, {
141 'spans' : true,
142 'foundry' : foundry,
143 'layer' : layer
144 },
145 function (matchResponse) {
146 // Get snippet from match info
147 if (matchResponse["snippet"] !== undefined) {
148 // Todo: This should be cached somehow
149
150 cb(matchTreeClass.create(matchResponse["snippet"]));
151 }
152 else {
153 cb(null);
154 };
155 }.bind(this)
Nils Diewald0e6992a2015-04-14 20:13:52 +0000156 );
157 },
158
159 /**
160 * Destroy this match information view.
161 */
162 destroy : function () {
163
164 // Remove circular reference
165 if (this._treeMenu !== undefined)
166 delete this._treeMenu["info"];
167
168 this._treeMenu.destroy();
169 this._treeMenu = undefined;
170 this._match = undefined;
171
172 // Element destroy
173 },
174
175 /**
176 * Add a new tree view to the list
177 */
178 addTree : function (foundry, layer, cb) {
179 var matchtree = document.createElement('div');
180 matchtree.classList.add('matchtree');
181
182 var h6 = matchtree.appendChild(document.createElement('h6'));
183 h6.appendChild(document.createElement('span'))
184 .appendChild(document.createTextNode(foundry));
185 h6.appendChild(document.createElement('span'))
186 .appendChild(document.createTextNode(layer));
187
188 var tree = matchtree.appendChild(
Akronc56cf2d2016-11-09 22:02:38 +0100189 document.createElement('div')
Nils Diewald0e6992a2015-04-14 20:13:52 +0000190 );
191
192 this._element.insertBefore(matchtree, this._element.lastChild);
193
Akronc56cf2d2016-11-09 22:02:38 +0100194 var actions = tree.appendChild(document.createElement('ul'));
195 actions.classList.add('action', 'image');
196 var close = actions.appendChild(document.createElement('li'));
197 close.className = 'close';
198 close.appendChild(document.createElement('span'));
Nils Diewald0e6992a2015-04-14 20:13:52 +0000199 close.addEventListener(
Akronc56cf2d2016-11-09 22:02:38 +0100200 'click', function (e) {
201 matchtree.parentNode.removeChild(matchtree);
202 e.halt();
203 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000204 );
205
Nils Diewald0ec142f2015-05-05 00:29:23 +0000206 tree.classList.add('loading');
207
Nils Diewald0e6992a2015-04-14 20:13:52 +0000208 // Get tree data async
209 this.getTree(foundry, layer, function (treeObj) {
Nils Diewald0ec142f2015-05-05 00:29:23 +0000210
Akronc56cf2d2016-11-09 22:02:38 +0100211 tree.classList.remove('loading');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000212
Akronc56cf2d2016-11-09 22:02:38 +0100213 // Something went wrong - probably log!!!
Nils Diewald0ec142f2015-05-05 00:29:23 +0000214
Akronc56cf2d2016-11-09 22:02:38 +0100215 if (treeObj === null) {
216 tree.appendChild(document.createTextNode('No data available.'));
217 }
218 else {
219 tree.appendChild(treeObj.element());
220 // Reposition the view to the center
221 // (This may in a future release be a reposition
222 // to move the root into the center or the actual
223 // match)
224
225 var dl = document.createElement('li');
226 dl.className = 'download';
227 dl.addEventListener(
228 'click', function (e) {
229
230 var a = document.createElement('a');
231 a.setAttribute('href-lang', 'image/svg+xml');
232 a.setAttribute('href', 'data:image/svg+xml;base64,'+treeObj.toBase64());
233 a.setAttribute('download', 'tree.svg');
234 a.target = '_blank';
235
236 document.body.appendChild(a);
237 a.click();
238 document.body.removeChild(a)
239
240 e.halt();
241 }
242 );
243 actions.appendChild(dl);
244 treeObj.center();
245 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000246
Akronc56cf2d2016-11-09 22:02:38 +0100247 if (cb !== undefined)
248 cb(treeObj);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000249 });
250 },
251
252 /**
253 * Create match information view.
254 */
255 element : function () {
256
257 if (this._element !== undefined)
Akronc56cf2d2016-11-09 22:02:38 +0100258 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000259
260 // Create info table
261 var info = document.createElement('div');
262 info.classList.add('matchinfo');
263
264 // Append default table
265 var matchtable = document.createElement('div');
Nils Diewald0ec142f2015-05-05 00:29:23 +0000266 matchtable.classList.add('matchtable', 'loading');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000267 info.appendChild(matchtable);
268
269 // Create the table asynchronous
270 this.getTable(undefined, function (table) {
Akron3bb91bc2016-12-02 16:43:17 +0100271
272 if (table !== null) {
273 matchtable.classList.remove('loading');
274 matchtable.appendChild(table.element());
275 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000276 });
277
278 // Get spans
279 var spanLayers = this._match.getSpans().sort(
Akron3bb91bc2016-12-02 16:43:17 +0100280 function (a, b) {
281 if (a.foundry < b.foundry) {
282 return -1;
283 }
284 else if (a.foundry > b.foundry) {
285 return 1;
286 }
287 else if (a.layer < b.layer) {
288 return -1;
289 }
290 else if (a.layer > b.layer) {
291 return 1;
292 };
293 return 0;
294 });
Nils Diewald0e6992a2015-04-14 20:13:52 +0000295
296 var menuList = [];
297
298 // Show tree views
299 for (var i = 0; i < spanLayers.length; i++) {
Akron3bb91bc2016-12-02 16:43:17 +0100300 var span = spanLayers[i];
301
302 // Add foundry/layer to menu list
303 menuList.push([
304 span.foundry + '/' + span.layer,
305 span.foundry,
306 span.layer
307 ]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000308 };
309
310 // Create tree menu
311 var treemenu = this.treeMenu(menuList);
312 var span = info.appendChild(document.createElement('p'));
313 span.classList.add('addtree');
314 span.appendChild(document.createTextNode(loc.ADDTREE));
315
316 var treeElement = treemenu.element();
317 span.appendChild(treeElement);
318
319 span.addEventListener('click', function (e) {
Akron3bb91bc2016-12-02 16:43:17 +0100320 treemenu.show();
321 treemenu.focus();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000322 });
323
324 this._element = info;
325
326 return info;
327 },
328
329
330 /**
331 * Get tree menu.
332 * There is only one menu rendered
333 * - no matter how many trees exist
334 */
335 treeMenu : function (list) {
336 if (this._treeMenu !== undefined)
Akron3bb91bc2016-12-02 16:43:17 +0100337 return this._treeMenu;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000338
339 return this._treeMenu = matchTreeMenuClass.create(this, list);
340 }
341 };
342});