blob: 54dc7131998aa3a7685e3f98e69b5b0f0c6556c1 [file] [log] [blame]
Akron7524be12016-06-01 17:31:33 +02001/*
2 * TODO: Create lazy loading of objects including
3 * - obj.hint()
4 * - obj.alertify()
5 * - obj.session()
6 * - obj.tutorial()
7 * - obj.vc() // toggle
8 * - obj.matchCreate() (using webpack)
9 * - obj.koral() (show result, parse for errors ...)
10 * - obj.alignment() // toggle
11 */
12
Nils Diewald0e6992a2015-04-14 20:13:52 +000013define([
14 'match',
15 'hint',
16 'vc',
17 'tutorial',
Akronbec4a6a2018-07-10 14:45:15 +020018 'buttongroup',
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 'lib/domReady',
Akron27ae9ec2015-06-23 00:43:21 +020020 'vc/array',
Nils Diewald7148c6f2015-05-04 15:07:53 +000021 'lib/alertify',
Akron7716f012015-07-01 20:38:32 +020022 'session',
Akron6bb71582016-06-10 20:41:08 +020023 'selectMenu',
Akron4d926f12018-07-16 15:30:25 +020024 'panel',
25 'view/koralquery',
Nils Diewald7148c6f2015-05-04 15:07:53 +000026 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000027 'mailToChiffre',
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 'util'
29], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020030 hintClass,
31 vcClass,
32 tutClass,
Akronbec4a6a2018-07-10 14:45:15 +020033 buttonGroupClass,
Akron19d97fe2016-09-06 20:47:05 +020034 domReady,
Akron19d97fe2016-09-06 20:47:05 +020035 vcArray,
36 alertifyClass,
37 sessionClass,
Akron4d926f12018-07-16 15:30:25 +020038 selectMenuClass,
39 panelClass,
40 kqClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000041
Akron9cc3eaf2015-06-10 22:15:52 +020042 // Localization values
Akron0b489ad2018-02-02 16:49:32 +010043 const loc = KorAP.Locale;
Akrond3903fb2017-11-13 11:13:44 +010044 loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora';
45 loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus';
46 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle alignment';
Akron7716f012015-07-01 20:38:32 +020047 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akron9a5b1e12016-12-06 18:18:23 +010048
Akron0b489ad2018-02-02 16:49:32 +010049 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020050
Akronf8035592018-05-24 20:40:51 +020051 KorAP.session = sessionClass.create('KalamarJS');
52
Nils Diewalda0defc42015-05-07 23:54:17 +000053 // Override KorAP.log
54 window.alertify = alertifyClass;
Akronc0a2da82018-07-04 15:27:37 +020055 KorAP.log = function (code, msg, src) {
56
57 if (src) {
58 msg += '<code class="src">'+src+'</code>';
59 };
Nils Diewalda0defc42015-05-07 23:54:17 +000060
61 // Use alertify to log errors
62 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020063 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020064 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000065 'error',
Akronf55504a2015-06-18 16:42:55 +020066 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000067 );
68 };
69
Nils Diewald0e6992a2015-04-14 20:13:52 +000070 domReady(function (event) {
71 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020072
Akron4d926f12018-07-16 15:30:25 +020073 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020074 var show = KorAP.session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000075
76 /**
Akronf55504a2015-06-18 16:42:55 +020077 * Release notifications
78 */
79 if (KorAP.Notifications !== undefined) {
80 var n = KorAP.Notifications;
81 for (var i = 0; i < n.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +020082 alertifyClass.log(n[i][1], n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020083 };
84 };
85
86 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000087 * Replace Virtual Collection field
88 */
Akron5c829e92017-05-12 18:10:00 +020089 var vcname, vcchoose;
Akron0b489ad2018-02-02 16:49:32 +010090 var input = d.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000091 if (input) {
92 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +010093 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +000094 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020095 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020096
Akron27ae9ec2015-06-23 00:43:21 +020097 var currentVC = loc.VC_allCorpora;
98 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron19d97fe2016-09-06 20:47:05 +020099 currentVC = loc.VC_oneCollection;
Akron27ae9ec2015-06-23 00:43:21 +0200100 };
101
Akron0b489ad2018-02-02 16:49:32 +0100102 vcchoose = vcname.addE('span');
Akron5c829e92017-05-12 18:10:00 +0200103
Akron0b489ad2018-02-02 16:49:32 +0100104 vcchoose.addT(
105 d.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +0000106 );
Akron27ae9ec2015-06-23 00:43:21 +0200107
Nils Diewald7148c6f2015-05-04 15:07:53 +0000108 input.parentNode.insertBefore(vcname, input);
109 };
110
Nils Diewald7148c6f2015-05-04 15:07:53 +0000111 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000112 * Add actions to match entries
113 */
Akron0b489ad2018-02-02 16:49:32 +0100114 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000115 '#search > ol > li:not(.active)'
116 );
Nils Diewalda297f062015-04-02 00:23:46 +0000117 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200118
Nils Diewalda297f062015-04-02 00:23:46 +0000119 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000120 inactiveLi[i].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200121 if (this._match !== undefined)
122 this._match.open();
123 else {
124 // lazyLoad
125 matchClass.create(this).open();
126 };
Akrone0c32c72017-04-25 22:38:23 +0200127 // This would prevent the sidebar to go back
128 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000129 });
Akron6a535d42015-08-26 20:16:58 +0200130 inactiveLi[i].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200131 var code = _codeFromEvent(e);
132
133 switch (code) {
134 case 32:
135 if (this._match !== undefined)
136 this._match.toggle();
137 else {
138 // lazyLoad
139 matchClass.create(this).open();
140 };
141 e.halt();
142 break;
143 };
Akron6a535d42015-08-26 20:16:58 +0200144 });
Nils Diewalda297f062015-04-02 00:23:46 +0000145 };
146
Akrone0c32c72017-04-25 22:38:23 +0200147 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100148 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200149
150 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200151
Akron5258d462017-04-26 23:32:57 +0200152 // Horrible lock to deal with sidebar clicks
153 var asideClicked = false;
154
Akron1885ce92017-04-26 23:10:01 +0200155 // Make aside active on focus
156 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200157 this.classList.add('active');
158 });
159
Akron1885ce92017-04-26 23:10:01 +0200160 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100161 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200162 if (body !== null) {
163 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200164 if (!asideClicked) {
165 aside.classList.remove('active');
166 }
167 else {
168 asideClicked = false;
169 };
Akron1885ce92017-04-26 23:10:01 +0200170 });
171 };
172
173 /* Stop click event on aside
174 * (to not trickle down to body)
175 */
176 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200177 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200178 });
179 };
Akronb9cdb102017-04-25 00:52:31 +0200180
181
Akron6bb71582016-06-10 20:41:08 +0200182 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100183 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200184 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100185 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100186 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200187 ).limit(5);
188 };
Akron6bb71582016-06-10 20:41:08 +0200189
Akron4d926f12018-07-16 15:30:25 +0200190 var resultInfo = d.getElementById('resultinfo');
191
Akron4d926f12018-07-16 15:30:25 +0200192 /**
193 * Add result panel
194 */
195 var resultPanel;
196 if (resultInfo != null) {
197 resultPanel = panelClass.create(['result']);
198
199 // Move buttons to resultinfo
200 resultInfo.appendChild(resultPanel.actions.element());
201
Akrone6538cd2018-07-16 17:52:33 +0200202 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200203 var sb = d.getElementById('search');
204 sb.insertBefore(resultPanel.element(), sb.firstChild);
205
206 };
207
208
Akron179c8ac2015-06-30 19:30:50 +0200209 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200210 if (KorAP.koralQuery !== undefined) {
211
212 if (resultInfo !== null) {
Akron179c8ac2015-06-30 19:30:50 +0200213
Akrone6538cd2018-07-16 17:52:33 +0200214 // Open KoralQuery view
215 var kqButton = resultPanel.actions.add(loc.SHOW_KQ, ['show-kq','button-icon'], function () {
Akron179c8ac2015-06-30 19:30:50 +0200216
Akrone6538cd2018-07-16 17:52:33 +0200217 // Show only once - otherwise toggle
Akron4d926f12018-07-16 15:30:25 +0200218 if (this._kq && this._kq.shown()) {
Akrone6538cd2018-07-16 17:52:33 +0200219 this._kq.close();
Akron4d926f12018-07-16 15:30:25 +0200220 return;
221 };
222
223 this._kq = kqClass.create();
Akrone6538cd2018-07-16 17:52:33 +0200224
225 // On close, remove session info on KQ
226 this._kq.onClose = function () {
227 delete show['kq'];
228 };
229
230 show['kq'] = true;
Akron4d926f12018-07-16 15:30:25 +0200231 this.add(this._kq);
232 });
Akrone6538cd2018-07-16 17:52:33 +0200233
234 // Show KoralQuery in case it's meant to be shown
235 if (show['kq'])
236 kqButton.click();
Akron179c8ac2015-06-30 19:30:50 +0200237 };
Akron7716f012015-07-01 20:38:32 +0200238
Akron00cd4d12016-05-31 21:01:11 +0200239 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200240 var errors = KorAP.koralQuery["errors"];
241 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200242
Akron19d97fe2016-09-06 20:47:05 +0200243 // Malformed query
244 if (errors[i][0] === 302 && errors[i][2]) {
245 obj.hint = hintClass.create();
246 obj.hint.alert(errors[i][2], errors[i][1]);
247 break;
248 }
Akronf0c31ed2016-06-11 11:27:01 +0200249
Akron19d97fe2016-09-06 20:47:05 +0200250 // no query
251 else if (errors[i][0] === 301) {
252 obj.hint = hintClass.create();
253 obj.hint.alert(0, errors[i][1]);
254 }
255 }
Akron00cd4d12016-05-31 21:01:11 +0200256 };
Akron179c8ac2015-06-30 19:30:50 +0200257 };
258
Akron9a5b1e12016-12-06 18:18:23 +0100259 // There is more than 0 matches and there is a resultButton
260 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000261
Akron4d926f12018-07-16 15:30:25 +0200262 if (resultPanel !== null) {
Akron9a5b1e12016-12-06 18:18:23 +0100263 /**
264 * Toggle the alignment (left <=> right)
265 */
Akron4d926f12018-07-16 15:30:25 +0200266 resultPanel.actions.add(loc.TOGGLE_ALIGN, ['align','right','button-icon'], function (e) {
267 var ol = d.querySelector('#search > ol');
268 ol.toggleClass("align-left", "align-right");
269 this.button.toggleClass("left", "right");
270 });
Akron9a5b1e12016-12-06 18:18:23 +0100271 };
Nils Diewalda297f062015-04-02 00:23:46 +0000272 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000273
Nils Diewald7148c6f2015-05-04 15:07:53 +0000274 /**
275 * Toggle the Virtual Collection builder
276 */
277 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000278 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200279 var vcclick = function () {
Akron0b489ad2018-02-02 16:49:32 +0100280 var view = d.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000281
Akron19d97fe2016-09-06 20:47:05 +0200282 // The vc is visible
283 if (vcname.classList.contains('active')) {
284 view.removeChild(vc.element());
285 vcname.classList.remove('active');
286 delete show['collection'];
287 }
Nils Diewald6283d692015-04-23 20:32:53 +0000288
Akron19d97fe2016-09-06 20:47:05 +0200289 // The vc is not visible
290 else {
291 if (vc === undefined)
292 vc = _getCurrentVC(vcClass, vcArray);
293 view.appendChild(vc.element());
294 vcname.classList.add('active');
295 show['collection'] = true;
296 };
Nils Diewald58141332015-04-07 16:18:45 +0000297 };
Akron04671e72017-05-11 20:47:32 +0200298
Akron179c8ac2015-06-30 19:30:50 +0200299 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200300
301 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200302 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200303 vcclick.apply();
Akron5c829e92017-05-12 18:10:00 +0200304 }
Akron04671e72017-05-11 20:47:32 +0200305
Akron5c829e92017-05-12 18:10:00 +0200306 // else
307 else if (_checkVCrewrite(vcClass)) {
308 vcchoose.classList.add('rewritten');
Akron04671e72017-05-11 20:47:32 +0200309 };
Nils Diewald58141332015-04-07 16:18:45 +0000310 };
311
Akron19d97fe2016-09-06 20:47:05 +0200312
Nils Diewald58141332015-04-07 16:18:45 +0000313 /**
314 * Init Tutorial view
315 */
Akron0b489ad2018-02-02 16:49:32 +0100316 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000317 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100318 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200319 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000320 );
321 obj.tutorial = window.tutorial;
322 }
Nils Diewald58141332015-04-07 16:18:45 +0000323
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000324 // Tutorial is in parent
325 else if (window.parent) {
326 obj.tutorial = window.parent.tutorial;
327 };
328
Akron0b489ad2018-02-02 16:49:32 +0100329 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200330 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100331 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000332
Akron6ed13992016-05-23 18:06:05 +0200333 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100334 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200335 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000336
Nils Diewald845282c2015-05-14 07:53:03 +0000337
Nils Diewald58141332015-04-07 16:18:45 +0000338 /**
Akronc1457bf2015-06-11 19:24:00 +0200339 * Add VC creation on submission.
340 */
Akron0b489ad2018-02-02 16:49:32 +0100341 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200342 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200343 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100344 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200345
Akron19d97fe2016-09-06 20:47:05 +0200346 // No query was defined
347 if (qf.value === undefined || qf.value === '') {
348 qf.focus();
349 e.halt();
350 KorAP.log(700, "No query given");
351 return;
352 };
Akron7716f012015-07-01 20:38:32 +0200353
Akron19d97fe2016-09-06 20:47:05 +0200354 // Store session information
Akronf8035592018-05-24 20:40:51 +0200355 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200356
Akron19d97fe2016-09-06 20:47:05 +0200357 // Set Virtual collection
358 if (vc === undefined) {
359 vc = _getCurrentVC(vcClass, vcArray);
360 };
Akron7716f012015-07-01 20:38:32 +0200361
Akron19d97fe2016-09-06 20:47:05 +0200362 if (vc !== undefined) {
363 input.value = vc.toQuery();
364 }
365 else {
366 delete input['value'];
367 };
Akronc1457bf2015-06-11 19:24:00 +0200368 });
369 };
370
371 /**
Nils Diewald58141332015-04-07 16:18:45 +0000372 * Init hint helper
373 * has to be final because of
374 * reposition
375 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000376 // Todo: Pass an element, so this works with
377 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200378 if (obj.hint === undefined)
379 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000380
Akron99713ef2017-06-28 18:19:28 +0200381 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100382 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200383
Nils Diewald58141332015-04-07 16:18:45 +0000384 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000385 });
386});
Akronc1457bf2015-06-11 19:24:00 +0200387
388// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200389function _getCurrentVC (vcClass, vcArray) {
390 var vc = vcClass.create(vcArray);
391 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
392 vc.fromJson(KorAP.koralQuery["collection"]);
393 };
Akronc1457bf2015-06-11 19:24:00 +0200394 return vc;
395};
Akron04671e72017-05-11 20:47:32 +0200396
397function _checkVCrewrite (vcClass) {
398 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
399 return vcClass.checkRewrite(KorAP.koralQuery["collection"]);
400 };
401 return false;
402}