blob: b4621d0920b56734a23bcd654c749223594a934c [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',
18 'lib/domReady',
Akron27ae9ec2015-06-23 00:43:21 +020019 'vc/array',
Nils Diewald7148c6f2015-05-04 15:07:53 +000020 'lib/alertify',
Akron7716f012015-07-01 20:38:32 +020021 'session',
Akronbe105b92016-01-04 14:13:20 +010022 'tagger',
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,
32 domReady,
Akron19d97fe2016-09-06 20:47:05 +020033 vcArray,
34 alertifyClass,
35 sessionClass,
36 tagger,
37 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');
Akron6ed13992016-05-23 18:06:05 +0200184 var resultButton = null;
Akron9a5b1e12016-12-06 18:18:23 +0100185 var leftButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200186 if (result != null) {
Akron9a5b1e12016-12-06 18:18:23 +0100187
188 // Add right buttons
Akron0b489ad2018-02-02 16:49:32 +0100189 resultButton = result.addE('div');
Akron9a5b1e12016-12-06 18:18:23 +0100190 resultButton.classList.add('result', 'button');
191
Akron0b489ad2018-02-02 16:49:32 +0100192 leftButton = result.addE('div');
Akron9a5b1e12016-12-06 18:18:23 +0100193 leftButton.classList.add('result', 'button', 'left');
Akron179c8ac2015-06-30 19:30:50 +0200194 };
195
196 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200197 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200198
Akron7716f012015-07-01 20:38:32 +0200199 if (resultButton !== null) {
Akron19d97fe2016-09-06 20:47:05 +0200200 var kq;
Akron0b489ad2018-02-02 16:49:32 +0100201 var toggle = d.createElement('a');
Akron19d97fe2016-09-06 20:47:05 +0200202 toggle.setAttribute('title', loc.SHOW_KQ)
203 toggle.classList.add('show-kq', 'button');
Akron0b489ad2018-02-02 16:49:32 +0100204 toggle.addE('span')
205 .addT(loc.SHOW_KQ);
Akron19d97fe2016-09-06 20:47:05 +0200206 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200207
Akron19d97fe2016-09-06 20:47:05 +0200208 var showKQ = function () {
209 if (kq === undefined) {
Akron0b489ad2018-02-02 16:49:32 +0100210 kq = d.createElement('div');
Akron19d97fe2016-09-06 20:47:05 +0200211 kq.setAttribute('id', 'koralquery');
212 kq.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100213 var kqInner = d.createElement('div');
Akron19d97fe2016-09-06 20:47:05 +0200214 kq.appendChild(kqInner);
215 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
216 hljs.highlightBlock(kqInner);
Akron0b489ad2018-02-02 16:49:32 +0100217 var sb = d.getElementById('search');
Akron19d97fe2016-09-06 20:47:05 +0200218 sb.insertBefore(kq, sb.firstChild);
219 };
Akron7716f012015-07-01 20:38:32 +0200220
Akron19d97fe2016-09-06 20:47:05 +0200221 if (kq.style.display === 'none') {
222 kq.style.display = 'block';
223 show['kq'] = true;
224 }
225 else {
226 kq.style.display = 'none';
227 delete show['kq'];
228 };
229 };
Akron7716f012015-07-01 20:38:32 +0200230
Akron19d97fe2016-09-06 20:47:05 +0200231 if (toggle !== undefined) {
232
233 // Show koralquery
234 toggle.addEventListener('click', showKQ);
235 };
Akron179c8ac2015-06-30 19:30:50 +0200236 };
Akron7716f012015-07-01 20:38:32 +0200237
Akron00cd4d12016-05-31 21:01:11 +0200238 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200239 var errors = KorAP.koralQuery["errors"];
240 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200241
Akron19d97fe2016-09-06 20:47:05 +0200242 // Malformed query
243 if (errors[i][0] === 302 && errors[i][2]) {
244 obj.hint = hintClass.create();
245 obj.hint.alert(errors[i][2], errors[i][1]);
246 break;
247 }
Akronf0c31ed2016-06-11 11:27:01 +0200248
Akron19d97fe2016-09-06 20:47:05 +0200249 // no query
250 else if (errors[i][0] === 301) {
251 obj.hint = hintClass.create();
252 obj.hint.alert(0, errors[i][1]);
253 }
254 }
Akron00cd4d12016-05-31 21:01:11 +0200255 };
256
Akron7716f012015-07-01 20:38:32 +0200257 // Session has KQ visibility stored
258 if (show["kq"])
Akron19d97fe2016-09-06 20:47:05 +0200259 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200260 };
261
Akron9a5b1e12016-12-06 18:18:23 +0100262 // There is more than 0 matches and there is a resultButton
263 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000264
Akron9a5b1e12016-12-06 18:18:23 +0100265 if (resultButton !== null) {
266
267 /**
268 * Toggle the alignment (left <=> right)
269 */
270 // querySelector('div.button.right');
271
Akron0b489ad2018-02-02 16:49:32 +0100272 var toggle = d.createElement('a');
Akron9a5b1e12016-12-06 18:18:23 +0100273 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
274 // Todo: Reuse old alignment from query
275 var cl = toggle.classList;
276 cl.add('align', 'right', 'button');
277 toggle.addEventListener(
278 'click',
279 function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100280 var ol = d.querySelector('#search > ol');
Akron9a5b1e12016-12-06 18:18:23 +0100281 ol.toggleClass("align-left", "align-right");
282 this.toggleClass("left", "right");
283 });
Akron0b489ad2018-02-02 16:49:32 +0100284 toggle.addE('span').addT(loc.TOGGLE_ALIGN);
Akron9a5b1e12016-12-06 18:18:23 +0100285 resultButton.appendChild(toggle);
286 };
287
Akron9a5b1e12016-12-06 18:18:23 +0100288 // Not ready yet
Akron24866cf2018-01-23 20:22:01 +0100289 /*
Akron9a5b1e12016-12-06 18:18:23 +0100290 if (leftButton !== null) {
Akron0b489ad2018-02-02 16:49:32 +0100291 var metaInfo = d.createElement('a');
Akron9a5b1e12016-12-06 18:18:23 +0100292 metaInfo.setAttribute('title', loc.SHOW_META)
293
294 var cl = metaInfo.classList;
295 cl.add('show-meta', 'button');
Akron0b489ad2018-02-02 16:49:32 +0100296 metaInfo.appendChild(d.createElement('span'))
297 .appendChild(d.createTextNode(loc.SHOW_META));
Akron9a5b1e12016-12-06 18:18:23 +0100298 leftButton.appendChild(metaInfo);
299 };
300 */
Nils Diewalda297f062015-04-02 00:23:46 +0000301 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000302
Nils Diewald7148c6f2015-05-04 15:07:53 +0000303 /**
304 * Toggle the Virtual Collection builder
305 */
306 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000307 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200308 var vcclick = function () {
Akron0b489ad2018-02-02 16:49:32 +0100309 var view = d.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000310
Akron19d97fe2016-09-06 20:47:05 +0200311 // The vc is visible
312 if (vcname.classList.contains('active')) {
313 view.removeChild(vc.element());
314 vcname.classList.remove('active');
315 delete show['collection'];
316 }
Nils Diewald6283d692015-04-23 20:32:53 +0000317
Akron19d97fe2016-09-06 20:47:05 +0200318 // The vc is not visible
319 else {
320 if (vc === undefined)
321 vc = _getCurrentVC(vcClass, vcArray);
322 view.appendChild(vc.element());
323 vcname.classList.add('active');
324 show['collection'] = true;
325 };
Nils Diewald58141332015-04-07 16:18:45 +0000326 };
Akron04671e72017-05-11 20:47:32 +0200327
Akron179c8ac2015-06-30 19:30:50 +0200328 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200329
330 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200331 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200332 vcclick.apply();
Akron5c829e92017-05-12 18:10:00 +0200333 }
Akron04671e72017-05-11 20:47:32 +0200334
Akron5c829e92017-05-12 18:10:00 +0200335 // else
336 else if (_checkVCrewrite(vcClass)) {
337 vcchoose.classList.add('rewritten');
Akron04671e72017-05-11 20:47:32 +0200338 };
Nils Diewald58141332015-04-07 16:18:45 +0000339 };
340
Akron19d97fe2016-09-06 20:47:05 +0200341
Nils Diewald58141332015-04-07 16:18:45 +0000342 /**
343 * Init Tutorial view
344 */
Akron0b489ad2018-02-02 16:49:32 +0100345 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000346 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100347 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200348 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000349 );
350 obj.tutorial = window.tutorial;
351 }
Nils Diewald58141332015-04-07 16:18:45 +0000352
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000353 // Tutorial is in parent
354 else if (window.parent) {
355 obj.tutorial = window.parent.tutorial;
356 };
357
Akron0b489ad2018-02-02 16:49:32 +0100358 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200359 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100360 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000361
Akron6ed13992016-05-23 18:06:05 +0200362 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100363 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200364 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000365
Nils Diewald845282c2015-05-14 07:53:03 +0000366
Nils Diewald58141332015-04-07 16:18:45 +0000367 /**
Akronc1457bf2015-06-11 19:24:00 +0200368 * Add VC creation on submission.
369 */
Akron0b489ad2018-02-02 16:49:32 +0100370 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200371 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200372 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100373 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200374
Akron19d97fe2016-09-06 20:47:05 +0200375 // No query was defined
376 if (qf.value === undefined || qf.value === '') {
377 qf.focus();
378 e.halt();
379 KorAP.log(700, "No query given");
380 return;
381 };
Akron7716f012015-07-01 20:38:32 +0200382
Akron19d97fe2016-09-06 20:47:05 +0200383 // Store session information
Akronf8035592018-05-24 20:40:51 +0200384 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200385
Akron19d97fe2016-09-06 20:47:05 +0200386 // Set Virtual collection
387 if (vc === undefined) {
388 vc = _getCurrentVC(vcClass, vcArray);
389 };
Akron7716f012015-07-01 20:38:32 +0200390
Akron19d97fe2016-09-06 20:47:05 +0200391 if (vc !== undefined) {
392 input.value = vc.toQuery();
393 }
394 else {
395 delete input['value'];
396 };
Akronc1457bf2015-06-11 19:24:00 +0200397 });
398 };
399
400 /**
Nils Diewald58141332015-04-07 16:18:45 +0000401 * Init hint helper
402 * has to be final because of
403 * reposition
404 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000405 // Todo: Pass an element, so this works with
406 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200407 if (obj.hint === undefined)
408 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000409
Akron99713ef2017-06-28 18:19:28 +0200410 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100411 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200412
Nils Diewald58141332015-04-07 16:18:45 +0000413 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000414 });
415});
Akronc1457bf2015-06-11 19:24:00 +0200416
417// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200418function _getCurrentVC (vcClass, vcArray) {
419 var vc = vcClass.create(vcArray);
420 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
421 vc.fromJson(KorAP.koralQuery["collection"]);
422 };
Akronc1457bf2015-06-11 19:24:00 +0200423 return vc;
424};
Akron04671e72017-05-11 20:47:32 +0200425
426function _checkVCrewrite (vcClass) {
427 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
428 return vcClass.checkRewrite(KorAP.koralQuery["collection"]);
429 };
430 return false;
431}