blob: 2380db861eb73369f8dc8210e1456b98ff3d4ee2 [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 Diewald845282c2015-05-14 07:53:03 +000028 'lib/highlight/highlight.pack',
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 'util'
30], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020031 hintClass,
32 vcClass,
33 tutClass,
Akronbec4a6a2018-07-10 14:45:15 +020034 buttonGroupClass,
Akron19d97fe2016-09-06 20:47:05 +020035 domReady,
Akron19d97fe2016-09-06 20:47:05 +020036 vcArray,
37 alertifyClass,
38 sessionClass,
Akron4d926f12018-07-16 15:30:25 +020039 selectMenuClass,
40 panelClass,
41 kqClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000042
Akron9cc3eaf2015-06-10 22:15:52 +020043 // Localization values
Akron0b489ad2018-02-02 16:49:32 +010044 const loc = KorAP.Locale;
Akrond3903fb2017-11-13 11:13:44 +010045 loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora';
46 loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus';
47 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle alignment';
Akron7716f012015-07-01 20:38:32 +020048 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akron9a5b1e12016-12-06 18:18:23 +010049
Akron0b489ad2018-02-02 16:49:32 +010050 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020051
Akronf8035592018-05-24 20:40:51 +020052 KorAP.session = sessionClass.create('KalamarJS');
53
Nils Diewalda0defc42015-05-07 23:54:17 +000054 // Override KorAP.log
55 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020056 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000057
58 // Use alertify to log errors
59 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020060 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020061 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000062 'error',
Akronf55504a2015-06-18 16:42:55 +020063 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000064 );
65 };
66
Nils Diewald0e6992a2015-04-14 20:13:52 +000067 domReady(function (event) {
68 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020069
Akron4d926f12018-07-16 15:30:25 +020070 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020071 var show = KorAP.session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000072
73 /**
Akronf55504a2015-06-18 16:42:55 +020074 * Release notifications
75 */
76 if (KorAP.Notifications !== undefined) {
77 var n = KorAP.Notifications;
78 for (var i = 0; i < n.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +020079 alertifyClass.log(n[i][1], n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020080 };
81 };
82
83 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000084 * Replace Virtual Collection field
85 */
Akron5c829e92017-05-12 18:10:00 +020086 var vcname, vcchoose;
Akron0b489ad2018-02-02 16:49:32 +010087 var input = d.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000088 if (input) {
89 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +010090 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +000091 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020092 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020093
Akron27ae9ec2015-06-23 00:43:21 +020094 var currentVC = loc.VC_allCorpora;
95 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron19d97fe2016-09-06 20:47:05 +020096 currentVC = loc.VC_oneCollection;
Akron27ae9ec2015-06-23 00:43:21 +020097 };
98
Akron0b489ad2018-02-02 16:49:32 +010099 vcchoose = vcname.addE('span');
Akron5c829e92017-05-12 18:10:00 +0200100
Akron0b489ad2018-02-02 16:49:32 +0100101 vcchoose.addT(
102 d.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +0000103 );
Akron27ae9ec2015-06-23 00:43:21 +0200104
Nils Diewald7148c6f2015-05-04 15:07:53 +0000105 input.parentNode.insertBefore(vcname, input);
106 };
107
Nils Diewald7148c6f2015-05-04 15:07:53 +0000108 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000109 * Add actions to match entries
110 */
Akron0b489ad2018-02-02 16:49:32 +0100111 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000112 '#search > ol > li:not(.active)'
113 );
Nils Diewalda297f062015-04-02 00:23:46 +0000114 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200115
Nils Diewalda297f062015-04-02 00:23:46 +0000116 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000117 inactiveLi[i].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200118 if (this._match !== undefined)
119 this._match.open();
120 else {
121 // lazyLoad
122 matchClass.create(this).open();
123 };
Akrone0c32c72017-04-25 22:38:23 +0200124 // This would prevent the sidebar to go back
125 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000126 });
Akron6a535d42015-08-26 20:16:58 +0200127 inactiveLi[i].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200128 var code = _codeFromEvent(e);
129
130 switch (code) {
131 case 32:
132 if (this._match !== undefined)
133 this._match.toggle();
134 else {
135 // lazyLoad
136 matchClass.create(this).open();
137 };
138 e.halt();
139 break;
140 };
Akron6a535d42015-08-26 20:16:58 +0200141 });
Nils Diewalda297f062015-04-02 00:23:46 +0000142 };
143
Akrone0c32c72017-04-25 22:38:23 +0200144 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100145 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200146
147 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200148
Akron5258d462017-04-26 23:32:57 +0200149 // Horrible lock to deal with sidebar clicks
150 var asideClicked = false;
151
Akron1885ce92017-04-26 23:10:01 +0200152 // Make aside active on focus
153 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200154 this.classList.add('active');
155 });
156
Akron1885ce92017-04-26 23:10:01 +0200157 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100158 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200159 if (body !== null) {
160 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200161 if (!asideClicked) {
162 aside.classList.remove('active');
163 }
164 else {
165 asideClicked = false;
166 };
Akron1885ce92017-04-26 23:10:01 +0200167 });
168 };
169
170 /* Stop click event on aside
171 * (to not trickle down to body)
172 */
173 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200174 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200175 });
176 };
Akronb9cdb102017-04-25 00:52:31 +0200177
178
Akron6bb71582016-06-10 20:41:08 +0200179 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100180 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200181 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100182 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100183 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200184 ).limit(5);
185 };
Akron6bb71582016-06-10 20:41:08 +0200186
Akron4d926f12018-07-16 15:30:25 +0200187 var resultInfo = d.getElementById('resultinfo');
188
189 /*
Akronbec4a6a2018-07-10 14:45:15 +0200190 var resultButton;
Akron179c8ac2015-06-30 19:30:50 +0200191 if (result != null) {
Akron9a5b1e12016-12-06 18:18:23 +0100192
Akronbec4a6a2018-07-10 14:45:15 +0200193 // Add result button group
194 resultButton = buttonGroupClass.create(['result']);
195 result.appendChild(resultButton.element());
Akron179c8ac2015-06-30 19:30:50 +0200196 };
Akron4d926f12018-07-16 15:30:25 +0200197 */
Akron179c8ac2015-06-30 19:30:50 +0200198
Akron4d926f12018-07-16 15:30:25 +0200199 /**
200 * Add result panel
201 */
202 var resultPanel;
203 if (resultInfo != null) {
204 resultPanel = panelClass.create(['result']);
205
206 // Move buttons to resultinfo
207 resultInfo.appendChild(resultPanel.actions.element());
208
209
210 var sb = d.getElementById('search');
211 sb.insertBefore(resultPanel.element(), sb.firstChild);
212
213 };
214
215
Akron179c8ac2015-06-30 19:30:50 +0200216 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200217 if (KorAP.koralQuery !== undefined) {
218
219 if (resultInfo !== null) {
Akron179c8ac2015-06-30 19:30:50 +0200220
Akron4d926f12018-07-16 15:30:25 +0200221 /*
Akron19d97fe2016-09-06 20:47:05 +0200222 var kq;
Akron179c8ac2015-06-30 19:30:50 +0200223
Akron19d97fe2016-09-06 20:47:05 +0200224 var showKQ = function () {
225 if (kq === undefined) {
Akron0b489ad2018-02-02 16:49:32 +0100226 kq = d.createElement('div');
Akron19d97fe2016-09-06 20:47:05 +0200227 kq.setAttribute('id', 'koralquery');
228 kq.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100229 var kqInner = d.createElement('div');
Akron19d97fe2016-09-06 20:47:05 +0200230 kq.appendChild(kqInner);
231 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
232 hljs.highlightBlock(kqInner);
Akron0b489ad2018-02-02 16:49:32 +0100233 var sb = d.getElementById('search');
Akron19d97fe2016-09-06 20:47:05 +0200234 sb.insertBefore(kq, sb.firstChild);
235 };
Akron7716f012015-07-01 20:38:32 +0200236
Akron19d97fe2016-09-06 20:47:05 +0200237 if (kq.style.display === 'none') {
238 kq.style.display = 'block';
239 show['kq'] = true;
240 }
241 else {
242 kq.style.display = 'none';
243 delete show['kq'];
244 };
245 };
Akron7716f012015-07-01 20:38:32 +0200246
Akronbec4a6a2018-07-10 14:45:15 +0200247 // Add KoralQuery button
248 resultButton.add(loc.SHOW_KQ, ['show-kq','button-icon'], showKQ);
Akron4d926f12018-07-16 15:30:25 +0200249 */
250
251 resultPanel.actions.add(loc.SHOW_KQ, ['show-kq','button-icon'], function () {
252
253 // Show only once
254 if (this._kq && this._kq.shown()) {
255 return;
256 };
257
258 this._kq = kqClass.create();
259 this.add(this._kq);
260 });
Akron179c8ac2015-06-30 19:30:50 +0200261 };
Akron7716f012015-07-01 20:38:32 +0200262
Akron00cd4d12016-05-31 21:01:11 +0200263 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200264 var errors = KorAP.koralQuery["errors"];
265 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200266
Akron19d97fe2016-09-06 20:47:05 +0200267 // Malformed query
268 if (errors[i][0] === 302 && errors[i][2]) {
269 obj.hint = hintClass.create();
270 obj.hint.alert(errors[i][2], errors[i][1]);
271 break;
272 }
Akronf0c31ed2016-06-11 11:27:01 +0200273
Akron19d97fe2016-09-06 20:47:05 +0200274 // no query
275 else if (errors[i][0] === 301) {
276 obj.hint = hintClass.create();
277 obj.hint.alert(0, errors[i][1]);
278 }
279 }
Akron00cd4d12016-05-31 21:01:11 +0200280 };
281
Akron7716f012015-07-01 20:38:32 +0200282 // Session has KQ visibility stored
Akron4d926f12018-07-16 15:30:25 +0200283 /**
284 * TODO:
Akron7716f012015-07-01 20:38:32 +0200285 if (show["kq"])
Akron19d97fe2016-09-06 20:47:05 +0200286 showKQ.apply();
Akron4d926f12018-07-16 15:30:25 +0200287 */
Akron179c8ac2015-06-30 19:30:50 +0200288 };
289
Akron9a5b1e12016-12-06 18:18:23 +0100290 // There is more than 0 matches and there is a resultButton
291 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000292
Akron4d926f12018-07-16 15:30:25 +0200293 if (resultPanel !== null) {
Akron9a5b1e12016-12-06 18:18:23 +0100294 /**
295 * Toggle the alignment (left <=> right)
296 */
Akron4d926f12018-07-16 15:30:25 +0200297 /*
Akronbec4a6a2018-07-10 14:45:15 +0200298 resultButton.add(loc.TOGGLE_ALIGN, ['align','right','button-icon'], function (e) {
299 var ol = d.querySelector('#search > ol');
300 ol.toggleClass("align-left", "align-right");
301 this.toggleClass("left", "right");
302 });
Akron4d926f12018-07-16 15:30:25 +0200303 */
304
305 resultPanel.actions.add(loc.TOGGLE_ALIGN, ['align','right','button-icon'], function (e) {
306 var ol = d.querySelector('#search > ol');
307 ol.toggleClass("align-left", "align-right");
308 this.button.toggleClass("left", "right");
309 });
Akron9a5b1e12016-12-06 18:18:23 +0100310 };
Nils Diewalda297f062015-04-02 00:23:46 +0000311 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000312
Nils Diewald7148c6f2015-05-04 15:07:53 +0000313 /**
314 * Toggle the Virtual Collection builder
315 */
316 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000317 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200318 var vcclick = function () {
Akron0b489ad2018-02-02 16:49:32 +0100319 var view = d.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000320
Akron19d97fe2016-09-06 20:47:05 +0200321 // The vc is visible
322 if (vcname.classList.contains('active')) {
323 view.removeChild(vc.element());
324 vcname.classList.remove('active');
325 delete show['collection'];
326 }
Nils Diewald6283d692015-04-23 20:32:53 +0000327
Akron19d97fe2016-09-06 20:47:05 +0200328 // The vc is not visible
329 else {
330 if (vc === undefined)
331 vc = _getCurrentVC(vcClass, vcArray);
332 view.appendChild(vc.element());
333 vcname.classList.add('active');
334 show['collection'] = true;
335 };
Nils Diewald58141332015-04-07 16:18:45 +0000336 };
Akron04671e72017-05-11 20:47:32 +0200337
Akron179c8ac2015-06-30 19:30:50 +0200338 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200339
340 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200341 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200342 vcclick.apply();
Akron5c829e92017-05-12 18:10:00 +0200343 }
Akron04671e72017-05-11 20:47:32 +0200344
Akron5c829e92017-05-12 18:10:00 +0200345 // else
346 else if (_checkVCrewrite(vcClass)) {
347 vcchoose.classList.add('rewritten');
Akron04671e72017-05-11 20:47:32 +0200348 };
Nils Diewald58141332015-04-07 16:18:45 +0000349 };
350
Akron19d97fe2016-09-06 20:47:05 +0200351
Nils Diewald58141332015-04-07 16:18:45 +0000352 /**
353 * Init Tutorial view
354 */
Akron0b489ad2018-02-02 16:49:32 +0100355 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000356 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100357 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200358 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000359 );
360 obj.tutorial = window.tutorial;
361 }
Nils Diewald58141332015-04-07 16:18:45 +0000362
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000363 // Tutorial is in parent
364 else if (window.parent) {
365 obj.tutorial = window.parent.tutorial;
366 };
367
Akron0b489ad2018-02-02 16:49:32 +0100368 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200369 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100370 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000371
Akron6ed13992016-05-23 18:06:05 +0200372 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100373 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200374 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000375
Nils Diewald845282c2015-05-14 07:53:03 +0000376
Nils Diewald58141332015-04-07 16:18:45 +0000377 /**
Akronc1457bf2015-06-11 19:24:00 +0200378 * Add VC creation on submission.
379 */
Akron0b489ad2018-02-02 16:49:32 +0100380 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200381 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200382 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100383 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200384
Akron19d97fe2016-09-06 20:47:05 +0200385 // No query was defined
386 if (qf.value === undefined || qf.value === '') {
387 qf.focus();
388 e.halt();
389 KorAP.log(700, "No query given");
390 return;
391 };
Akron7716f012015-07-01 20:38:32 +0200392
Akron19d97fe2016-09-06 20:47:05 +0200393 // Store session information
Akronf8035592018-05-24 20:40:51 +0200394 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200395
Akron19d97fe2016-09-06 20:47:05 +0200396 // Set Virtual collection
397 if (vc === undefined) {
398 vc = _getCurrentVC(vcClass, vcArray);
399 };
Akron7716f012015-07-01 20:38:32 +0200400
Akron19d97fe2016-09-06 20:47:05 +0200401 if (vc !== undefined) {
402 input.value = vc.toQuery();
403 }
404 else {
405 delete input['value'];
406 };
Akronc1457bf2015-06-11 19:24:00 +0200407 });
408 };
409
410 /**
Nils Diewald58141332015-04-07 16:18:45 +0000411 * Init hint helper
412 * has to be final because of
413 * reposition
414 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000415 // Todo: Pass an element, so this works with
416 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200417 if (obj.hint === undefined)
418 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000419
Akron99713ef2017-06-28 18:19:28 +0200420 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100421 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200422
Nils Diewald58141332015-04-07 16:18:45 +0000423 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000424 });
425});
Akronc1457bf2015-06-11 19:24:00 +0200426
427// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200428function _getCurrentVC (vcClass, vcArray) {
429 var vc = vcClass.create(vcArray);
430 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
431 vc.fromJson(KorAP.koralQuery["collection"]);
432 };
Akronc1457bf2015-06-11 19:24:00 +0200433 return vc;
434};
Akron04671e72017-05-11 20:47:32 +0200435
436function _checkVCrewrite (vcClass) {
437 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
438 return vcClass.checkRewrite(KorAP.koralQuery["collection"]);
439 };
440 return false;
441}