blob: f2744c7e8267c36b2cd9089e934e6b203346ce5a [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',
Nils Diewald7148c6f2015-05-04 15:07:53 +000024 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000025 'mailToChiffre',
Nils Diewald845282c2015-05-14 07:53:03 +000026 'lib/highlight/highlight.pack',
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 'util'
28], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020029 hintClass,
30 vcClass,
31 tutClass,
Akronbec4a6a2018-07-10 14:45:15 +020032 buttonGroupClass,
Akron19d97fe2016-09-06 20:47:05 +020033 domReady,
Akron19d97fe2016-09-06 20:47:05 +020034 vcArray,
35 alertifyClass,
36 sessionClass,
Akron19d97fe2016-09-06 20:47:05 +020037 selectMenuClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000038
Akron9cc3eaf2015-06-10 22:15:52 +020039 // Localization values
Akron0b489ad2018-02-02 16:49:32 +010040 const loc = KorAP.Locale;
Akrond3903fb2017-11-13 11:13:44 +010041 loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora';
42 loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus';
43 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle alignment';
Akron7716f012015-07-01 20:38:32 +020044 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akron9a5b1e12016-12-06 18:18:23 +010045
Akron0b489ad2018-02-02 16:49:32 +010046 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020047
Akronf8035592018-05-24 20:40:51 +020048 KorAP.session = sessionClass.create('KalamarJS');
49
Nils Diewalda0defc42015-05-07 23:54:17 +000050 // Override KorAP.log
51 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020052 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000053
54 // Use alertify to log errors
55 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020056 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020057 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000058 'error',
Akronf55504a2015-06-18 16:42:55 +020059 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000060 );
61 };
62
Nils Diewald0e6992a2015-04-14 20:13:52 +000063 domReady(function (event) {
64 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020065
Akron7716f012015-07-01 20:38:32 +020066 // What should be visible?
Akronf8035592018-05-24 20:40:51 +020067 var show = KorAP.session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000068
69 /**
Akronf55504a2015-06-18 16:42:55 +020070 * Release notifications
71 */
72 if (KorAP.Notifications !== undefined) {
73 var n = KorAP.Notifications;
74 for (var i = 0; i < n.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +020075 alertifyClass.log(n[i][1], n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020076 };
77 };
78
79 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000080 * Replace Virtual Collection field
81 */
Akron5c829e92017-05-12 18:10:00 +020082 var vcname, vcchoose;
Akron0b489ad2018-02-02 16:49:32 +010083 var input = d.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000084 if (input) {
85 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +010086 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +000087 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020088 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020089
Akron27ae9ec2015-06-23 00:43:21 +020090 var currentVC = loc.VC_allCorpora;
91 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron19d97fe2016-09-06 20:47:05 +020092 currentVC = loc.VC_oneCollection;
Akron27ae9ec2015-06-23 00:43:21 +020093 };
94
Akron0b489ad2018-02-02 16:49:32 +010095 vcchoose = vcname.addE('span');
Akron5c829e92017-05-12 18:10:00 +020096
Akron0b489ad2018-02-02 16:49:32 +010097 vcchoose.addT(
98 d.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +000099 );
Akron27ae9ec2015-06-23 00:43:21 +0200100
Nils Diewald7148c6f2015-05-04 15:07:53 +0000101 input.parentNode.insertBefore(vcname, input);
102 };
103
Nils Diewald7148c6f2015-05-04 15:07:53 +0000104 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000105 * Add actions to match entries
106 */
Akron0b489ad2018-02-02 16:49:32 +0100107 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000108 '#search > ol > li:not(.active)'
109 );
Nils Diewalda297f062015-04-02 00:23:46 +0000110 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200111
Nils Diewalda297f062015-04-02 00:23:46 +0000112 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000113 inactiveLi[i].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200114 if (this._match !== undefined)
115 this._match.open();
116 else {
117 // lazyLoad
118 matchClass.create(this).open();
119 };
Akrone0c32c72017-04-25 22:38:23 +0200120 // This would prevent the sidebar to go back
121 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000122 });
Akron6a535d42015-08-26 20:16:58 +0200123 inactiveLi[i].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200124 var code = _codeFromEvent(e);
125
126 switch (code) {
127 case 32:
128 if (this._match !== undefined)
129 this._match.toggle();
130 else {
131 // lazyLoad
132 matchClass.create(this).open();
133 };
134 e.halt();
135 break;
136 };
Akron6a535d42015-08-26 20:16:58 +0200137 });
Nils Diewalda297f062015-04-02 00:23:46 +0000138 };
139
Akrone0c32c72017-04-25 22:38:23 +0200140 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100141 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200142
143 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200144
Akron5258d462017-04-26 23:32:57 +0200145 // Horrible lock to deal with sidebar clicks
146 var asideClicked = false;
147
Akron1885ce92017-04-26 23:10:01 +0200148 // Make aside active on focus
149 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200150 this.classList.add('active');
151 });
152
Akron1885ce92017-04-26 23:10:01 +0200153 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100154 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200155 if (body !== null) {
156 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200157 if (!asideClicked) {
158 aside.classList.remove('active');
159 }
160 else {
161 asideClicked = false;
162 };
Akron1885ce92017-04-26 23:10:01 +0200163 });
164 };
165
166 /* Stop click event on aside
167 * (to not trickle down to body)
168 */
169 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200170 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200171 });
172 };
Akronb9cdb102017-04-25 00:52:31 +0200173
174
Akron6bb71582016-06-10 20:41:08 +0200175 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100176 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200177 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100178 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100179 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200180 ).limit(5);
181 };
Akron6bb71582016-06-10 20:41:08 +0200182
Akron0b489ad2018-02-02 16:49:32 +0100183 var result = d.getElementById('resultinfo');
Akronbec4a6a2018-07-10 14:45:15 +0200184 var resultButton;
Akron179c8ac2015-06-30 19:30:50 +0200185 if (result != null) {
Akron9a5b1e12016-12-06 18:18:23 +0100186
Akronbec4a6a2018-07-10 14:45:15 +0200187 // Add result button group
188 resultButton = buttonGroupClass.create(['result']);
189 result.appendChild(resultButton.element());
Akron179c8ac2015-06-30 19:30:50 +0200190 };
191
192 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200193 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200194
Akronbec4a6a2018-07-10 14:45:15 +0200195 if (result !== null) {
Akron19d97fe2016-09-06 20:47:05 +0200196 var kq;
Akron179c8ac2015-06-30 19:30:50 +0200197
Akron19d97fe2016-09-06 20:47:05 +0200198 var showKQ = function () {
199 if (kq === undefined) {
Akron0b489ad2018-02-02 16:49:32 +0100200 kq = d.createElement('div');
Akron19d97fe2016-09-06 20:47:05 +0200201 kq.setAttribute('id', 'koralquery');
202 kq.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100203 var kqInner = d.createElement('div');
Akron19d97fe2016-09-06 20:47:05 +0200204 kq.appendChild(kqInner);
205 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
206 hljs.highlightBlock(kqInner);
Akron0b489ad2018-02-02 16:49:32 +0100207 var sb = d.getElementById('search');
Akron19d97fe2016-09-06 20:47:05 +0200208 sb.insertBefore(kq, sb.firstChild);
209 };
Akron7716f012015-07-01 20:38:32 +0200210
Akron19d97fe2016-09-06 20:47:05 +0200211 if (kq.style.display === 'none') {
212 kq.style.display = 'block';
213 show['kq'] = true;
214 }
215 else {
216 kq.style.display = 'none';
217 delete show['kq'];
218 };
219 };
Akron7716f012015-07-01 20:38:32 +0200220
Akronbec4a6a2018-07-10 14:45:15 +0200221 // Add KoralQuery button
222 resultButton.add(loc.SHOW_KQ, ['show-kq','button-icon'], showKQ);
Akron179c8ac2015-06-30 19:30:50 +0200223 };
Akron7716f012015-07-01 20:38:32 +0200224
Akron00cd4d12016-05-31 21:01:11 +0200225 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200226 var errors = KorAP.koralQuery["errors"];
227 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200228
Akron19d97fe2016-09-06 20:47:05 +0200229 // Malformed query
230 if (errors[i][0] === 302 && errors[i][2]) {
231 obj.hint = hintClass.create();
232 obj.hint.alert(errors[i][2], errors[i][1]);
233 break;
234 }
Akronf0c31ed2016-06-11 11:27:01 +0200235
Akron19d97fe2016-09-06 20:47:05 +0200236 // no query
237 else if (errors[i][0] === 301) {
238 obj.hint = hintClass.create();
239 obj.hint.alert(0, errors[i][1]);
240 }
241 }
Akron00cd4d12016-05-31 21:01:11 +0200242 };
243
Akron7716f012015-07-01 20:38:32 +0200244 // Session has KQ visibility stored
245 if (show["kq"])
Akron19d97fe2016-09-06 20:47:05 +0200246 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200247 };
248
Akron9a5b1e12016-12-06 18:18:23 +0100249 // There is more than 0 matches and there is a resultButton
250 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000251
Akron9a5b1e12016-12-06 18:18:23 +0100252 if (resultButton !== null) {
Akron9a5b1e12016-12-06 18:18:23 +0100253 /**
254 * Toggle the alignment (left <=> right)
255 */
Akron9a5b1e12016-12-06 18:18:23 +0100256
Akronbec4a6a2018-07-10 14:45:15 +0200257 resultButton.add(loc.TOGGLE_ALIGN, ['align','right','button-icon'], function (e) {
258 var ol = d.querySelector('#search > ol');
259 ol.toggleClass("align-left", "align-right");
260 this.toggleClass("left", "right");
261 });
Akron9a5b1e12016-12-06 18:18:23 +0100262 };
Nils Diewalda297f062015-04-02 00:23:46 +0000263 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000264
Nils Diewald7148c6f2015-05-04 15:07:53 +0000265 /**
266 * Toggle the Virtual Collection builder
267 */
268 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000269 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200270 var vcclick = function () {
Akron0b489ad2018-02-02 16:49:32 +0100271 var view = d.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000272
Akron19d97fe2016-09-06 20:47:05 +0200273 // The vc is visible
274 if (vcname.classList.contains('active')) {
275 view.removeChild(vc.element());
276 vcname.classList.remove('active');
277 delete show['collection'];
278 }
Nils Diewald6283d692015-04-23 20:32:53 +0000279
Akron19d97fe2016-09-06 20:47:05 +0200280 // The vc is not visible
281 else {
282 if (vc === undefined)
283 vc = _getCurrentVC(vcClass, vcArray);
284 view.appendChild(vc.element());
285 vcname.classList.add('active');
286 show['collection'] = true;
287 };
Nils Diewald58141332015-04-07 16:18:45 +0000288 };
Akron04671e72017-05-11 20:47:32 +0200289
Akron179c8ac2015-06-30 19:30:50 +0200290 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200291
292 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200293 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200294 vcclick.apply();
Akron5c829e92017-05-12 18:10:00 +0200295 }
Akron04671e72017-05-11 20:47:32 +0200296
Akron5c829e92017-05-12 18:10:00 +0200297 // else
298 else if (_checkVCrewrite(vcClass)) {
299 vcchoose.classList.add('rewritten');
Akron04671e72017-05-11 20:47:32 +0200300 };
Nils Diewald58141332015-04-07 16:18:45 +0000301 };
302
Akron19d97fe2016-09-06 20:47:05 +0200303
Nils Diewald58141332015-04-07 16:18:45 +0000304 /**
305 * Init Tutorial view
306 */
Akron0b489ad2018-02-02 16:49:32 +0100307 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000308 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100309 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200310 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000311 );
312 obj.tutorial = window.tutorial;
313 }
Nils Diewald58141332015-04-07 16:18:45 +0000314
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000315 // Tutorial is in parent
316 else if (window.parent) {
317 obj.tutorial = window.parent.tutorial;
318 };
319
Akron0b489ad2018-02-02 16:49:32 +0100320 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200321 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100322 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000323
Akron6ed13992016-05-23 18:06:05 +0200324 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100325 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200326 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000327
Nils Diewald845282c2015-05-14 07:53:03 +0000328
Nils Diewald58141332015-04-07 16:18:45 +0000329 /**
Akronc1457bf2015-06-11 19:24:00 +0200330 * Add VC creation on submission.
331 */
Akron0b489ad2018-02-02 16:49:32 +0100332 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200333 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200334 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100335 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200336
Akron19d97fe2016-09-06 20:47:05 +0200337 // No query was defined
338 if (qf.value === undefined || qf.value === '') {
339 qf.focus();
340 e.halt();
341 KorAP.log(700, "No query given");
342 return;
343 };
Akron7716f012015-07-01 20:38:32 +0200344
Akron19d97fe2016-09-06 20:47:05 +0200345 // Store session information
Akronf8035592018-05-24 20:40:51 +0200346 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200347
Akron19d97fe2016-09-06 20:47:05 +0200348 // Set Virtual collection
349 if (vc === undefined) {
350 vc = _getCurrentVC(vcClass, vcArray);
351 };
Akron7716f012015-07-01 20:38:32 +0200352
Akron19d97fe2016-09-06 20:47:05 +0200353 if (vc !== undefined) {
354 input.value = vc.toQuery();
355 }
356 else {
357 delete input['value'];
358 };
Akronc1457bf2015-06-11 19:24:00 +0200359 });
360 };
361
362 /**
Nils Diewald58141332015-04-07 16:18:45 +0000363 * Init hint helper
364 * has to be final because of
365 * reposition
366 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000367 // Todo: Pass an element, so this works with
368 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200369 if (obj.hint === undefined)
370 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000371
Akron99713ef2017-06-28 18:19:28 +0200372 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100373 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200374
Nils Diewald58141332015-04-07 16:18:45 +0000375 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000376 });
377});
Akronc1457bf2015-06-11 19:24:00 +0200378
379// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200380function _getCurrentVC (vcClass, vcArray) {
381 var vc = vcClass.create(vcArray);
382 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
383 vc.fromJson(KorAP.koralQuery["collection"]);
384 };
Akronc1457bf2015-06-11 19:24:00 +0200385 return vc;
386};
Akron04671e72017-05-11 20:47:32 +0200387
388function _checkVCrewrite (vcClass) {
389 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
390 return vcClass.checkRewrite(KorAP.koralQuery["collection"]);
391 };
392 return false;
393}