blob: bb61b574dc1851e1c8e908637ec775b7ad8bed14 [file] [log] [blame]
Akron7524be12016-06-01 17:31:33 +02001/*
Akron5cb9b2b2018-07-24 17:01:09 +02002 * Initialize The JS frontend part and decorate
3 * the static HTML data.
4 *
Akron2224dcf2024-11-19 13:28:44 +01005 * @author Nils Diewald, Helge Stallkamp, Uyen-Nhu Tran
Akron5cb9b2b2018-07-24 17:01:09 +02006 *
Akron7524be12016-06-01 17:31:33 +02007 * TODO: Create lazy loading of objects including
8 * - obj.hint()
9 * - obj.alertify()
10 * - obj.session()
11 * - obj.tutorial()
12 * - obj.vc() // toggle
13 * - obj.matchCreate() (using webpack)
14 * - obj.koral() (show result, parse for errors ...)
15 * - obj.alignment() // toggle
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +020016 *
17 * TODO: After upgrading to ES 6
18 * - use optional chaining operator (for example see below)
Akron7524be12016-06-01 17:31:33 +020019 */
20
Akrone51eaa32020-11-10 09:35:53 +010021"use strict";
Nils Diewald0e6992a2015-04-14 20:13:52 +000022define([
23 'match',
24 'hint',
25 'vc',
26 'tutorial',
27 'lib/domReady',
Akron27ae9ec2015-06-23 00:43:21 +020028 'vc/array',
Nils Diewald7148c6f2015-05-04 15:07:53 +000029 'lib/alertify',
Akron7716f012015-07-01 20:38:32 +020030 'session',
Akronda32e7a2021-11-16 17:28:57 +010031 'state/manager',
Akron6bb71582016-06-10 20:41:08 +020032 'selectMenu',
Akron5cb9b2b2018-07-24 17:01:09 +020033 'panel/result',
Akron2d0d96d2019-11-18 19:49:50 +010034 'panel/query',
Akron644ad9f2021-07-26 16:12:59 +020035 'panel/pagination',
hebasta75cfca52019-02-19 13:15:27 +010036 'tour/tours',
Akron24f48ea2020-07-01 09:37:19 +020037 'plugin/server',
38 'pipe',
Nils Diewald7148c6f2015-05-04 15:07:53 +000039 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000040 'mailToChiffre',
Akron858cbc82019-12-05 16:53:13 +010041 'util',
42 'state'
Nils Diewald0e6992a2015-04-14 20:13:52 +000043], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020044 hintClass,
45 vcClass,
46 tutClass,
47 domReady,
Akron19d97fe2016-09-06 20:47:05 +020048 vcArray,
49 alertifyClass,
50 sessionClass,
Akronda32e7a2021-11-16 17:28:57 +010051 stateManagerClass,
Akron4d926f12018-07-16 15:30:25 +020052 selectMenuClass,
hebasta75cfca52019-02-19 13:15:27 +010053 resultPanelClass,
Akron2d0d96d2019-11-18 19:49:50 +010054 queryPanelClass,
Akron644ad9f2021-07-26 16:12:59 +020055 paginationPanelClass,
Akron24f48ea2020-07-01 09:37:19 +020056 tourClass,
57 pluginClass,
58 pipeClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000059
Nils Diewalda0defc42015-05-07 23:54:17 +000060 // Override KorAP.log
61 window.alertify = alertifyClass;
Akrone71bd6d2024-06-11 15:47:39 +020062 KorAP.log = function (code, msg, src, type) {
Akronc0a2da82018-07-04 15:27:37 +020063
64 if (src) {
65 msg += '<code class="src">'+src+'</code>';
66 };
Nils Diewalda0defc42015-05-07 23:54:17 +000067
68 // Use alertify to log errors
69 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020070 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020071 msg,
Akrone71bd6d2024-06-11 15:47:39 +020072 (type ? type : 'error'),
Akronf55504a2015-06-18 16:42:55 +020073 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000074 );
75 };
76
hebasta2758b582018-11-19 15:59:42 +010077 KorAP.vc = vcClass.create(vcArray);
Akron690066c2021-01-22 17:39:18 +010078
Nils Diewald0e6992a2015-04-14 20:13:52 +000079 domReady(function (event) {
Akron006ddc62024-02-19 08:49:43 +010080
81 const d = document;
82
83 // Set base URL
84 KorAP.URL = d.body.getAttribute('data-korap-url') || "";
Akron9bbd7702024-11-21 13:01:51 +010085
86
87
Akron006ddc62024-02-19 08:49:43 +010088 // Create suffix if KorAP is run in a subfolder
89 KorAP.session = sessionClass.create(
Akron9bbd7702024-11-21 13:01:51 +010090 (KorAP.URL.length > 0 ? 'kalamarJS-' + KorAP.URL.slugify() : 'kalamarJS'),
91 KorAP.URL
Akron006ddc62024-02-19 08:49:43 +010092 );
93
94 // Get koralQuery response
95 const kqe = d.getElementById('koralQuery');
96 if (kqe !== null) {
97 KorAP.koralQuery = JSON.parse(kqe.getAttribute('data-koralquery') || "");
98 };
99
Helge0d3630c2024-10-16 17:19:40 +0200100 let gt = document.getElementsByClassName('link-guided-tour');
101 if (gt.length != null){
102 for(let j = 0; j < gt.length; j++){
103 gt[j].setAttribute('href', '#');
104 gt[j].addEventListener('click', function(){
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200105 tourClass.gTstartSearch().start();
Helge0d3630c2024-10-16 17:19:40 +0200106
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200107 // Close the burger menu by simulating a click on the burger icon
108 const burgerIcon = document.querySelector('.burger-icon');
109 if (isBurgerMenuOpen) {
110 burgerIcon.click();
111 }
112 });
113 }
114
115 KorAP.tourshowR = function(){
116 tourClass.gTshowResults().start();
Akron006ddc62024-02-19 08:49:43 +0100117 };
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200118 }
Akron006ddc62024-02-19 08:49:43 +0100119
Nils Diewald0e6992a2015-04-14 20:13:52 +0000120 var obj = {};
Akron71b91e42016-06-01 22:12:43 +0200121
Akron4d926f12018-07-16 15:30:25 +0200122 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +0200123 var show = KorAP.session.get('show') || {};
hebasta043e96f2019-11-28 12:33:00 +0100124
125 KorAP.Panel = KorAP.Panel || {}
Nils Diewalda297f062015-04-02 00:23:46 +0000126
127 /**
Akronf55504a2015-06-18 16:42:55 +0200128 * Release notifications
129 */
Akroncb5c1712021-01-26 18:01:04 +0100130 d.querySelectorAll('#notifications div.notify').forEach(
131 function(e) {
132 let msg = e.textContent;
133
134 let src = e.getAttribute('data-src');
135 if (src) {
136 msg += '<code class="src">'+src+'</code>';
Akron8ea84292018-10-24 13:41:52 +0200137 };
Akroncb5c1712021-01-26 18:01:04 +0100138
139 let type = e.getAttribute('data-type') || "error";
140 alertifyClass.log(msg, type, 10000);
141 }
142 );
Akronf55504a2015-06-18 16:42:55 +0200143
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200144 // Responsive navbar: hide and show burger menu
145 const burgerIcon = document.querySelector('.burger-icon');
Uyen-Nhu Trana17b08d2025-02-18 19:14:55 +0100146 const navbarGroup = document.querySelector('.navbar-group');
147
148 if (burgerIcon && navbarGroup) {
149 if (navbarGroup.innerHTML.trim() !== '') {
150 burgerIcon.classList.add('show');
151 }
152 }
153
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200154 let isBurgerMenuOpen = false;
155
156 if (burgerIcon) {
157 burgerIcon.addEventListener('click', function() {
158 const navbar = document.querySelector('.navbar');
159 navbar.classList.toggle('show');
160
161 isBurgerMenuOpen = !isBurgerMenuOpen;
162 if (isBurgerMenuOpen) {
163 navbar.style.top = '0';
164 }
165 });
166 }
167
168 // Fallback solution for login dropdown visibility (if :focus-within is not supported)
169 document.addEventListener('DOMContentLoaded', function() {
170 const dropdown = document.querySelector('.dropdown');
171 const dropdownContent = document.querySelector('.dropdown-content');
172
173 dropdown.addEventListener('mouseenter', function() {
174 dropdownContent.style.display = 'block';
175 });
176
177 dropdown.addEventListener('mouseleave', function() {
178 // If no input inside the form is focused, then close dropdown content
179 if (!dropdown.contains(document.activeElement)) {
180 dropdownContent.style.display = 'none';
181 }
182 });
183
184 dropdownContent.addEventListener('focusin', function() {
185 dropdownContent.style.display = 'block';
186 });
187
188 dropdownContent.addEventListener('focusout', function(e) {
189 // If focus moved outside the dropdown content, then close it
190 if (!dropdownContent.contains(e.relatedTarget)) {
191 dropdownContent.style.display = 'none';
192 }
193 });
194 });
195
Akronf55504a2015-06-18 16:42:55 +0200196 /**
Akroncd42a142019-07-12 18:55:37 +0200197 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000198 */
Akron5c829e92017-05-12 18:10:00 +0200199 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200200 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200201
hebasta2758b582018-11-19 15:59:42 +0100202 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100203
Akron1f0521b2018-08-28 13:01:24 +0200204 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000205 if (input) {
206 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100207 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000208 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200209 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200210
Akron1f0521b2018-08-28 13:01:24 +0200211 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200212 // Supports "collection" for legacy reasons
213 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200214 try {
Akroncd42a142019-07-12 18:55:37 +0200215 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200216 }
217 catch (e) {
218 KorAP.log(0,e);
219 }
Akron27ae9ec2015-06-23 00:43:21 +0200220 };
221
Akron0b489ad2018-02-02 16:49:32 +0100222 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200223 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200224
Akron1f0521b2018-08-28 13:01:24 +0200225 if (vc.wasRewritten()) {
226 vcchoose.classList.add('rewritten');
227 };
228
Nils Diewald7148c6f2015-05-04 15:07:53 +0000229 input.parentNode.insertBefore(vcname, input);
230 };
231
Nils Diewald7148c6f2015-05-04 15:07:53 +0000232 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000233 * Add actions to match entries
234 */
Akronb50964a2020-10-12 11:44:37 +0200235 var matchElements = d.querySelectorAll(
Akron3c390c42020-03-30 09:06:21 +0200236 '#search > ol > li'
Nils Diewald5c5a7472015-04-02 22:13:38 +0000237 );
Akron6a535d42015-08-26 20:16:58 +0200238
Akronb50964a2020-10-12 11:44:37 +0200239 matchElements.forEach(function(e) {
Akron3c390c42020-03-30 09:06:21 +0200240
241 // Define class for active elements
242 if (e.classList.contains('active')) {
Akrond769d702021-08-16 11:09:08 +0200243 if (e._match === undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200244 // lazyLoad
Akron3c390c42020-03-30 09:06:21 +0200245 matchClass.create(e).init();
Akron19d97fe2016-09-06 20:47:05 +0200246 };
Akron3c390c42020-03-30 09:06:21 +0200247 }
248
249 // Define class for inactive elements
250 else {
251 e.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200252 if (this._match !== undefined)
Akron3c390c42020-03-30 09:06:21 +0200253 this._match.open();
Akron19d97fe2016-09-06 20:47:05 +0200254 else {
255 // lazyLoad
256 matchClass.create(this).open();
257 };
Akron3c390c42020-03-30 09:06:21 +0200258 // This would prevent the sidebar to go back
259 // e.halt();
260 });
261 e.addEventListener('keydown', function (e) {
262 var code = _codeFromEvent(e);
263
264 switch (code) {
265 case 32:
266 if (this._match !== undefined)
267 this._match.toggle();
268 else {
269 // lazyLoad
270 matchClass.create(this).open();
271 };
272 e.halt();
273 break;
274 };
275 });
276 };
Akrond769d702021-08-16 11:09:08 +0200277 });
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100278
279 // Media Query for adjusting dynamically added elements (e.g. hint)
280 const isSmallScreen = window.matchMedia('(max-width: 768px)').matches;
Akron3c390c42020-03-30 09:06:21 +0200281
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200282 // Function to toggle the shifted class on elements
283 function shiftContent() {
284 // Get elements to perform content shift when sidebar is active
285 const header = document.querySelector('header');
286 const main = document.querySelector('main');
287 const footer = document.querySelector('footer');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200288 const results = document.querySelector('.found');
289 const aside = document.querySelector('aside');
290
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100291 if (aside && aside.classList.contains('active') && !isSmallScreen) {
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200292 header.classList.add('shifted');
293 if (!results) {
294 main.classList.add('shifted');
295 }
296 footer.classList.add('shifted');
Akron1c18f102024-11-19 16:31:06 +0100297 adjustHintPosition();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200298 } else {
299 header.classList.remove('shifted');
300 main.classList.remove('shifted');
301 footer.classList.remove('shifted');
Akron1c18f102024-11-19 16:31:06 +0100302 adjustHintPosition();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200303 }
304 }
305
Akron1c18f102024-11-19 16:31:06 +0100306 // Function to adjust the position of the annotation assistant bar (hint),
307 // when user types into the searchbar and clicks the sidebar (or anywhere
308 // outside the searchbar) afterwards
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200309 function adjustHintPosition() {
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100310 const hint = document.querySelector('#hint');
311 const searchInput = document.querySelector('#q-field');
312 const aside = document.querySelector('aside');
313
314 if (hint && searchInput) {
315 // Create a temporary span to measure the exact text width
316 const span = document.createElement('span');
317 span.style.visibility = 'hidden';
318 span.style.position = 'absolute';
319 span.style.whiteSpace = 'pre';
320 // Copy the input's font properties
321 const inputStyle = window.getComputedStyle(searchInput);
322 span.style.font = inputStyle.font;
323 span.style.fontSize = inputStyle.fontSize;
324 span.style.fontFamily = inputStyle.fontFamily;
325 span.textContent = searchInput.value;
326 document.body.appendChild(span);
327
328 // Get the actual width of the text
329 const inputWidth = searchInput.value.length > 0 ? span.offsetWidth : 0;
330 document.body.removeChild(span);
331 let hintLeftPosition = inputWidth;
332
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100333 if (aside && aside.classList.contains('active') && !isSmallScreen) {
Uyen-Nhu Trana17b08d2025-02-18 19:14:55 +0100334 const asideWidth = aside.getBoundingClientRect().width;
335 hintLeftPosition += asideWidth;
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100336 }
337
338 hint.style.left = `${hintLeftPosition}px`;
339 }
340 }
341 //Can be solved more elegant witch ES6 (see optional chaining operator)
342 let qlf = document.querySelector('#q-field');
343 if(qlf != null){
344 qlf.addEventListener('blur', adjustHintPosition);
345 }
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200346
347 // MutationObserver to detect when #hint is injected into the DOM
348 const observer = new MutationObserver((mutationsList, observer) => {
349 for (const mutation of mutationsList) {
350 if (mutation.type === 'childList') {
351 const hint = document.querySelector('#hint');
352 if (hint) {
Uyen-Nhu Trana17b08d2025-02-18 19:14:55 +0100353 if (window.location.pathname !== '/settings' && window.location.pathname !== '/settings/') {
354 shiftContent();
355 }
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200356 observer.disconnect();
Akronbe2f9a02025-01-14 09:36:55 +0100357 KorAP.Hint.alert().show();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200358 }
359 }
360 }
361 });
362
363 observer.observe(document.body, { childList: true, subtree: true });
364
Akrone0c32c72017-04-25 22:38:23 +0200365 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100366 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200367
368 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200369
Akron5258d462017-04-26 23:32:57 +0200370 // Horrible lock to deal with sidebar clicks
371 var asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200372
373 shiftContent();
374
Akron1885ce92017-04-26 23:10:01 +0200375 // Make aside active on focus
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200376 aside.addEventListener('focus', function (e) {
Akrone0c32c72017-04-25 22:38:23 +0200377 this.classList.add('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200378 shiftContent();
Akrone0c32c72017-04-25 22:38:23 +0200379 });
380
Akron1885ce92017-04-26 23:10:01 +0200381 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100382 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200383 if (body !== null) {
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200384 body.addEventListener('click', function () {
Akron5258d462017-04-26 23:32:57 +0200385 if (!asideClicked) {
386 aside.classList.remove('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200387 shiftContent();
388 } else {
Akron5258d462017-04-26 23:32:57 +0200389 asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200390 }
Akron1885ce92017-04-26 23:10:01 +0200391 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200392 }
Akron1885ce92017-04-26 23:10:01 +0200393
394 /* Stop click event on aside
395 * (to not trickle down to body)
396 */
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200397 aside.addEventListener('click', function (e) {
Akron5258d462017-04-26 23:32:57 +0200398 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200399 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200400 }
Akronb9cdb102017-04-25 00:52:31 +0200401
Uyen-Nhu Trana17b08d2025-02-18 19:14:55 +0100402 if (window.location.pathname === '/settings' || window.location.pathname === '/settings/') {
403 const header = document.querySelector('header');
404 const main = document.querySelector('main');
405 const footer = document.querySelector('footer');
406
407 aside.style.display = 'none';
408
409 if (header) header.style.setProperty('padding-left', '0', 'important');
410 if (main) main.style.setProperty('padding-left', '0', 'important');
411 if (footer) footer.style.setProperty('padding-left', '0', 'important');
412 }
413
Akron6bb71582016-06-10 20:41:08 +0200414 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100415 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200416 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100417 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100418 d.getElementById('ql-field').parentNode
Marc Kupietz95455822023-09-19 20:14:31 +0200419 ).limit(10);
Akronaba7a5a2016-08-15 21:58:33 +0200420 };
Akron6bb71582016-06-10 20:41:08 +0200421
Akron4d926f12018-07-16 15:30:25 +0200422 var resultInfo = d.getElementById('resultinfo');
423
Akron4d926f12018-07-16 15:30:25 +0200424 /**
425 * Add result panel
426 */
Akron5cb9b2b2018-07-24 17:01:09 +0200427 var resultPanel = resultPanelClass.create(show);
Akron644ad9f2021-07-26 16:12:59 +0200428
Akron4d926f12018-07-16 15:30:25 +0200429 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200430
431 // Move buttons to resultinfo
Akron37ea1192021-07-28 10:40:14 +0200432 resultInfo.appendChild(resultPanel.actions().element());
Akron4d926f12018-07-16 15:30:25 +0200433
Akrone6538cd2018-07-16 17:52:33 +0200434 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200435 var sb = d.getElementById('search');
436 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200437 };
438
439
Akron179c8ac2015-06-30 19:30:50 +0200440 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200441 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200442
443 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200444 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200445 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200446 };
Akron7716f012015-07-01 20:38:32 +0200447
Akron00cd4d12016-05-31 21:01:11 +0200448 if (KorAP.koralQuery["errors"]) {
Akron678c26f2020-10-09 08:52:50 +0200449 KorAP.koralQuery["errors"].forEach(function(e) {
Akronf0c31ed2016-06-11 11:27:01 +0200450
Akron19d97fe2016-09-06 20:47:05 +0200451 // Malformed query
Akron4a24b722020-10-13 12:44:25 +0200452 if (e[0] === 302 && e[2] !== undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200453 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200454 obj.hint.alert(e[2], e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200455 }
Akronf0c31ed2016-06-11 11:27:01 +0200456
Akron19d97fe2016-09-06 20:47:05 +0200457 // no query
Akron678c26f2020-10-09 08:52:50 +0200458 else if (e[0] === 301) {
Akron19d97fe2016-09-06 20:47:05 +0200459 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200460 obj.hint.alert(0, e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200461 }
Akron678c26f2020-10-09 08:52:50 +0200462 });
Akron00cd4d12016-05-31 21:01:11 +0200463 };
Akron179c8ac2015-06-30 19:30:50 +0200464 };
465
Akron5cb9b2b2018-07-24 17:01:09 +0200466
467 /*
468 * There is more than 0 matches, so allow for
469 * alignment toggling (left <=> right)
470 */
Akronb50964a2020-10-12 11:44:37 +0200471 if (matchElements.length > 0)
Akron5cb9b2b2018-07-24 17:01:09 +0200472 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000473
hebasta043e96f2019-11-28 12:33:00 +0100474 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200475 /*
Akroncd42a142019-07-12 18:55:37 +0200476 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000477 */
478 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200479 vc.onMinimize = function () {
480 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200481 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200482 };
Nils Diewald6283d692015-04-23 20:32:53 +0000483
Akronec6bb8e2018-08-29 13:07:56 +0200484 vc.onOpen = function () {
485 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100486
487 var view = d.getElementById('vc-view');
488 if (!view.firstChild)
489 view.appendChild(this.element());
490
Akroncd42a142019-07-12 18:55:37 +0200491 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200492 };
493
494 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200495 if (vc.isOpen()) {
496 vc.minimize()
497 }
498 else {
Akronec6bb8e2018-08-29 13:07:56 +0200499 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200500 };
Nils Diewald58141332015-04-07 16:18:45 +0000501 };
Akron04671e72017-05-11 20:47:32 +0200502
Akron179c8ac2015-06-30 19:30:50 +0200503 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200504
505 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200506 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200507 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200508 };
Nils Diewald58141332015-04-07 16:18:45 +0000509 };
510
Akron19d97fe2016-09-06 20:47:05 +0200511
Nils Diewald58141332015-04-07 16:18:45 +0000512 /**
513 * Init Tutorial view
514 */
Akron0b489ad2018-02-02 16:49:32 +0100515 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000516 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100517 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200518 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000519 );
520 obj.tutorial = window.tutorial;
521 }
Nils Diewald58141332015-04-07 16:18:45 +0000522
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000523 // Tutorial is in parent
524 else if (window.parent) {
525 obj.tutorial = window.parent.tutorial;
526 };
527
Akron0b489ad2018-02-02 16:49:32 +0100528 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200529 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100530 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000531
Akron6ed13992016-05-23 18:06:05 +0200532 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100533 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200534 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000535
Nils Diewald845282c2015-05-14 07:53:03 +0000536
Nils Diewald58141332015-04-07 16:18:45 +0000537 /**
Akronc1457bf2015-06-11 19:24:00 +0200538 * Add VC creation on submission.
539 */
Akron0b489ad2018-02-02 16:49:32 +0100540 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200541 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200542 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100543 var qf = d.getElementById('q-field');
Akron1be6c1c2020-01-07 15:29:58 +0100544
Akron19d97fe2016-09-06 20:47:05 +0200545 // No query was defined
546 if (qf.value === undefined || qf.value === '') {
547 qf.focus();
548 e.halt();
549 KorAP.log(700, "No query given");
550 return;
551 };
Akron1be6c1c2020-01-07 15:29:58 +0100552
Akron19d97fe2016-09-06 20:47:05 +0200553 // Store session information
Akronf8035592018-05-24 20:40:51 +0200554 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200555
Akron19d97fe2016-09-06 20:47:05 +0200556 if (vc !== undefined) {
557 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100558 if (input.value == '')
559 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200560 }
561 else {
Akrond7ad9072019-12-09 07:08:20 +0100562 input.removeAttribute('value');
563 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200564 };
Akron1be6c1c2020-01-07 15:29:58 +0100565
Akronaa3dcfe2024-12-10 15:29:40 +0100566 if (KorAP.States != null) {
567 const statesE = KorAP.States.element();
568 if (statesE.value == "")
569 statesE.removeAttribute('name');
570 };
571
572 if (KorAP.Pipe != null) {
573 const pipeE = KorAP.Pipe.element();
574 if (pipeE.value == "")
575 pipeE.removeAttribute("name");
576 };
577
Akron1be6c1c2020-01-07 15:29:58 +0100578 // This would preferably set the query to be "disabled",
579 // but in that case the query wouldn't be submitted
580 // at all.
581 // Setting the cursor to "progress" fails in current versions
582 // of webkit.
583 qf.classList.add("loading");
584 d.getElementById('qsubmit').classList.add("loading");
Akronaa3dcfe2024-12-10 15:29:40 +0100585
586 // Alternatively the submission could be prevented early
587 // and the formData API could be used instead:
588 // e.preventDefault();
589 // const formData = new FormData(this);
590 // const queryString = new URLSearchParams(formData).toString();
591 // window.location.href = `${this.action}?${queryString}`;
Akronc1457bf2015-06-11 19:24:00 +0200592 });
593 };
hebasta5df796f2019-05-21 15:27:12 +0200594
595
596 //Starts the guided tour at the next page
597 if(KorAP.session.get("tour")){
598 tourClass.gTshowResults().start();
599 }
600
Akronc1457bf2015-06-11 19:24:00 +0200601 /**
Nils Diewald58141332015-04-07 16:18:45 +0000602 * Init hint helper
603 * has to be final because of
604 * reposition
605 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000606 // Todo: Pass an element, so this works with
607 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200608 if (obj.hint === undefined)
609 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000610
Akron99713ef2017-06-28 18:19:28 +0200611 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100612 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200613
Akron2d0d96d2019-11-18 19:49:50 +0100614
615 /**
616 * Add query panel
617 */
618 var queryPanel = queryPanelClass.create();
619
620 // Get input field
Akron2d0d96d2019-11-18 19:49:50 +0100621 var vcView = d.getElementById('vc-view')
Akron96b97d62023-11-07 15:56:54 +0100622 if (form && vcView) {
Akron2d0d96d2019-11-18 19:49:50 +0100623 // The views are below the query bar
Akron96b97d62023-11-07 15:56:54 +0100624 form.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100625 KorAP.Panel['query'] = queryPanel;
Akron644ad9f2021-07-26 16:12:59 +0200626 };
627
628
629 /**
630 * Add pagination panel
631 */
632 const paginationPanel = paginationPanelClass.create();
633
634 if (paginationPanel) {
635 paginationPanel.addRandomPage();
636 KorAP.Panel['pagination'] = paginationPanel;
637 };
Akron24f48ea2020-07-01 09:37:19 +0200638
Akrona9c55802021-06-15 11:41:29 +0200639
640 /**
641 * Initialize password toggle.
642 */
643 initCopyToClipboard(d);
644
645
Akron24f48ea2020-07-01 09:37:19 +0200646 /**
Akron116eace2021-06-14 18:02:37 +0200647 * Initialize password toggle.
648 */
Akron1cfde272021-06-14 18:32:39 +0200649 initTogglePwdVisibility(d);
Akron116eace2021-06-14 18:02:37 +0200650
651 /**
Akron24f48ea2020-07-01 09:37:19 +0200652 * Initialize Plugin registry.
653 */
Akron8dda1c62021-01-20 10:27:32 +0100654 let pe;
655 if (pe = d.getElementById("kalamar-plugins")) {
656 let url = pe.getAttribute('data-plugins');
657 if (url !== undefined) {
658 KorAP.API.getPluginList(url, function (json) {
659 if (json && json.length > 0) {
Akronda32e7a2021-11-16 17:28:57 +0100660
661 // Add state manager
Akron96b97d62023-11-07 15:56:54 +0100662 form = d.getElementById("searchform");
663 if (!form) {
664 return;
665 };
666
667 const input = form.addE("input");
Akroned223be2024-12-10 13:01:46 +0100668 input.setAttribute("type","text");
Akronda32e7a2021-11-16 17:28:57 +0100669 input.setAttribute("name","state");
Akroned223be2024-12-10 13:01:46 +0100670
671 const url = new URL(window.location.href);
672
673 // Access the query parameters to check for states
674 const state = new URLSearchParams(url.search).get('state');
675 if (state != null && state != "") {
676 input.setAttribute("value", state);
677 };
678
Akronda32e7a2021-11-16 17:28:57 +0100679 KorAP.States = stateManagerClass.create(input);
680
Akron8dda1c62021-01-20 10:27:32 +0100681 // Load Plugin Server first
682 KorAP.Plugin = pluginClass.create();
Akron24f48ea2020-07-01 09:37:19 +0200683
Akron8dda1c62021-01-20 10:27:32 +0100684 // Add services container to head
685 d.head.appendChild(KorAP.Plugin.element());
Akron24f48ea2020-07-01 09:37:19 +0200686
Akron8dda1c62021-01-20 10:27:32 +0100687 // Add pipe form
688 KorAP.Pipe = pipeClass.create();
689 d.getElementById("searchform").appendChild(KorAP.Pipe.element());
Akronda32e7a2021-11-16 17:28:57 +0100690
Akron8dda1c62021-01-20 10:27:32 +0100691 try {
692
693 // Register all plugins
694 json.forEach(i => KorAP.Plugin.register(i));
695 }
696 catch (e) {
697 KorAP.log(0, e);
698 }
699 }
700 });
701 };
Akron24f48ea2020-07-01 09:37:19 +0200702 };
Akronf7f75a92024-09-24 11:15:43 +0200703
704 window.dispatchEvent(new Event("ui-ready"));
Akron8dda1c62021-01-20 10:27:32 +0100705
Nils Diewald58141332015-04-07 16:18:45 +0000706 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000707 });
hebasta75cfca52019-02-19 13:15:27 +0100708
Nils Diewald0e6992a2015-04-14 20:13:52 +0000709});