blob: e98512cac5c36a4ba0fadf40d357a2e7681c154b [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
Marc Kupietze78a1ca2026-02-14 19:14:18 +010077 // Apply configured VC helper field modifications from data-vc-helper-fields attribute
78 const vcFieldsConfig = document.body ? document.body.getAttribute('data-vc-helper-fields') : null;
79 if (vcFieldsConfig) {
80 const mods = vcFieldsConfig.split(',').map(s => s.trim()).filter(s => s.length > 0);
81 mods.forEach(mod => {
82 if (mod.startsWith('-')) {
83 // Remove field by name
84 const name = mod.substring(1);
85 const idx = vcArray.findIndex(f => f[0] === name);
86 if (idx >= 0) vcArray.splice(idx, 1);
87 } else if (mod.startsWith('+')) {
88 // Add field: +name:type
89 const spec = mod.substring(1);
90 const colonIdx = spec.indexOf(':');
91 if (colonIdx > 0) {
92 const name = spec.substring(0, colonIdx);
93 const type = spec.substring(colonIdx + 1);
94 if (!vcArray.some(f => f[0] === name)) {
95 vcArray.push([name, type]);
96 }
97 }
98 }
99 });
100 // Sort alphabetically by field name
101 vcArray.sort((a, b) => a[0].localeCompare(b[0]));
102 };
103
hebasta2758b582018-11-19 15:59:42 +0100104 KorAP.vc = vcClass.create(vcArray);
Akron690066c2021-01-22 17:39:18 +0100105
Nils Diewald0e6992a2015-04-14 20:13:52 +0000106 domReady(function (event) {
Akron006ddc62024-02-19 08:49:43 +0100107
108 const d = document;
109
110 // Set base URL
111 KorAP.URL = d.body.getAttribute('data-korap-url') || "";
Akron9bbd7702024-11-21 13:01:51 +0100112
113
114
Akron006ddc62024-02-19 08:49:43 +0100115 // Create suffix if KorAP is run in a subfolder
116 KorAP.session = sessionClass.create(
Akron9bbd7702024-11-21 13:01:51 +0100117 (KorAP.URL.length > 0 ? 'kalamarJS-' + KorAP.URL.slugify() : 'kalamarJS'),
118 KorAP.URL
Akron006ddc62024-02-19 08:49:43 +0100119 );
120
121 // Get koralQuery response
122 const kqe = d.getElementById('koralQuery');
123 if (kqe !== null) {
124 KorAP.koralQuery = JSON.parse(kqe.getAttribute('data-koralquery') || "");
125 };
126
Helge0d3630c2024-10-16 17:19:40 +0200127 let gt = document.getElementsByClassName('link-guided-tour');
128 if (gt.length != null){
129 for(let j = 0; j < gt.length; j++){
130 gt[j].setAttribute('href', '#');
131 gt[j].addEventListener('click', function(){
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200132 tourClass.gTstartSearch().start();
Helge0d3630c2024-10-16 17:19:40 +0200133
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200134 // Close the burger menu by simulating a click on the burger icon
135 const burgerIcon = document.querySelector('.burger-icon');
136 if (isBurgerMenuOpen) {
137 burgerIcon.click();
138 }
139 });
140 }
141
142 KorAP.tourshowR = function(){
143 tourClass.gTshowResults().start();
Akron006ddc62024-02-19 08:49:43 +0100144 };
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200145 }
Akron006ddc62024-02-19 08:49:43 +0100146
Nils Diewald0e6992a2015-04-14 20:13:52 +0000147 var obj = {};
Akron71b91e42016-06-01 22:12:43 +0200148
Akron4d926f12018-07-16 15:30:25 +0200149 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +0200150 var show = KorAP.session.get('show') || {};
hebasta043e96f2019-11-28 12:33:00 +0100151
152 KorAP.Panel = KorAP.Panel || {}
Nils Diewalda297f062015-04-02 00:23:46 +0000153
154 /**
Akronf55504a2015-06-18 16:42:55 +0200155 * Release notifications
156 */
Akroncb5c1712021-01-26 18:01:04 +0100157 d.querySelectorAll('#notifications div.notify').forEach(
158 function(e) {
159 let msg = e.textContent;
160
161 let src = e.getAttribute('data-src');
162 if (src) {
163 msg += '<code class="src">'+src+'</code>';
Akron8ea84292018-10-24 13:41:52 +0200164 };
Akroncb5c1712021-01-26 18:01:04 +0100165
166 let type = e.getAttribute('data-type') || "error";
167 alertifyClass.log(msg, type, 10000);
168 }
169 );
Akronf55504a2015-06-18 16:42:55 +0200170
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200171 // Responsive navbar: hide and show burger menu
172 const burgerIcon = document.querySelector('.burger-icon');
Uyen-Nhu Trana17b08d2025-02-18 19:14:55 +0100173 const navbarGroup = document.querySelector('.navbar-group');
174
175 if (burgerIcon && navbarGroup) {
176 if (navbarGroup.innerHTML.trim() !== '') {
177 burgerIcon.classList.add('show');
178 }
179 }
180
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200181 let isBurgerMenuOpen = false;
182
183 if (burgerIcon) {
184 burgerIcon.addEventListener('click', function() {
185 const navbar = document.querySelector('.navbar');
186 navbar.classList.toggle('show');
187
188 isBurgerMenuOpen = !isBurgerMenuOpen;
189 if (isBurgerMenuOpen) {
190 navbar.style.top = '0';
191 }
192 });
193 }
194
195 // Fallback solution for login dropdown visibility (if :focus-within is not supported)
196 document.addEventListener('DOMContentLoaded', function() {
197 const dropdown = document.querySelector('.dropdown');
198 const dropdownContent = document.querySelector('.dropdown-content');
199
200 dropdown.addEventListener('mouseenter', function() {
201 dropdownContent.style.display = 'block';
202 });
203
204 dropdown.addEventListener('mouseleave', function() {
205 // If no input inside the form is focused, then close dropdown content
206 if (!dropdown.contains(document.activeElement)) {
207 dropdownContent.style.display = 'none';
208 }
209 });
210
211 dropdownContent.addEventListener('focusin', function() {
212 dropdownContent.style.display = 'block';
213 });
214
215 dropdownContent.addEventListener('focusout', function(e) {
216 // If focus moved outside the dropdown content, then close it
217 if (!dropdownContent.contains(e.relatedTarget)) {
218 dropdownContent.style.display = 'none';
219 }
220 });
221 });
222
Akronf55504a2015-06-18 16:42:55 +0200223 /**
Akroncd42a142019-07-12 18:55:37 +0200224 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000225 */
Akron5c829e92017-05-12 18:10:00 +0200226 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200227 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200228
hebasta2758b582018-11-19 15:59:42 +0100229 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100230
Akron1f0521b2018-08-28 13:01:24 +0200231 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000232 if (input) {
233 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100234 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000235 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200236 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200237
Akron1f0521b2018-08-28 13:01:24 +0200238 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200239 // Supports "collection" for legacy reasons
240 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200241 try {
Akroncd42a142019-07-12 18:55:37 +0200242 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200243 }
244 catch (e) {
245 KorAP.log(0,e);
246 }
Akron27ae9ec2015-06-23 00:43:21 +0200247 };
248
Akron0b489ad2018-02-02 16:49:32 +0100249 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200250 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200251
Akron1f0521b2018-08-28 13:01:24 +0200252 if (vc.wasRewritten()) {
253 vcchoose.classList.add('rewritten');
254 };
255
Nils Diewald7148c6f2015-05-04 15:07:53 +0000256 input.parentNode.insertBefore(vcname, input);
257 };
258
Nils Diewald7148c6f2015-05-04 15:07:53 +0000259 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000260 * Add actions to match entries
261 */
Akronb50964a2020-10-12 11:44:37 +0200262 var matchElements = d.querySelectorAll(
Akron3c390c42020-03-30 09:06:21 +0200263 '#search > ol > li'
Nils Diewald5c5a7472015-04-02 22:13:38 +0000264 );
Akron6a535d42015-08-26 20:16:58 +0200265
Akronb50964a2020-10-12 11:44:37 +0200266 matchElements.forEach(function(e) {
Akron3c390c42020-03-30 09:06:21 +0200267
268 // Define class for active elements
269 if (e.classList.contains('active')) {
Akrond769d702021-08-16 11:09:08 +0200270 if (e._match === undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200271 // lazyLoad
Akron3c390c42020-03-30 09:06:21 +0200272 matchClass.create(e).init();
Akron19d97fe2016-09-06 20:47:05 +0200273 };
Akron3c390c42020-03-30 09:06:21 +0200274 }
275
276 // Define class for inactive elements
277 else {
278 e.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200279 if (this._match !== undefined)
Akron3c390c42020-03-30 09:06:21 +0200280 this._match.open();
Akron19d97fe2016-09-06 20:47:05 +0200281 else {
282 // lazyLoad
283 matchClass.create(this).open();
284 };
Akron3c390c42020-03-30 09:06:21 +0200285 // This would prevent the sidebar to go back
286 // e.halt();
287 });
288 e.addEventListener('keydown', function (e) {
289 var code = _codeFromEvent(e);
290
291 switch (code) {
292 case 32:
293 if (this._match !== undefined)
294 this._match.toggle();
295 else {
296 // lazyLoad
297 matchClass.create(this).open();
298 };
299 e.halt();
300 break;
301 };
302 });
303 };
Akrond769d702021-08-16 11:09:08 +0200304 });
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100305
306 // Media Query for adjusting dynamically added elements (e.g. hint)
307 const isSmallScreen = window.matchMedia('(max-width: 768px)').matches;
Uyen-Nhu Trana7584e12025-06-17 18:45:09 +0200308
309 // Change styles for different lengths of logo add-on
310 const logoAddon = document.querySelector('.logoaddon');
Akron3c390c42020-03-30 09:06:21 +0200311
Uyen-Nhu Trana7584e12025-06-17 18:45:09 +0200312 if (logoAddon && logoAddon.textContent.length < 6) {
313 if (!isSmallScreen) {
314 logoAddon.style.right = '0.1rem';
315 } else {
316 logoAddon.style.right = '-0.3rem';
317 }
318 }
319
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200320 // Function to toggle the shifted class on elements
321 function shiftContent() {
322 // Get elements to perform content shift when sidebar is active
323 const header = document.querySelector('header');
324 const main = document.querySelector('main');
325 const footer = document.querySelector('footer');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200326 const results = document.querySelector('.found');
327 const aside = document.querySelector('aside');
328
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100329 if (aside && aside.classList.contains('active') && !isSmallScreen) {
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200330 header.classList.add('shifted');
331 if (!results) {
332 main.classList.add('shifted');
333 }
334 footer.classList.add('shifted');
Akron1c18f102024-11-19 16:31:06 +0100335 adjustHintPosition();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200336 } else {
337 header.classList.remove('shifted');
338 main.classList.remove('shifted');
339 footer.classList.remove('shifted');
Akron1c18f102024-11-19 16:31:06 +0100340 adjustHintPosition();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200341 }
342 }
343
Akron1c18f102024-11-19 16:31:06 +0100344 // Function to adjust the position of the annotation assistant bar (hint),
345 // when user types into the searchbar and clicks the sidebar (or anywhere
346 // outside the searchbar) afterwards
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200347 function adjustHintPosition() {
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100348 const hint = document.querySelector('#hint');
349 const searchInput = document.querySelector('#q-field');
350 const aside = document.querySelector('aside');
351
352 if (hint && searchInput) {
353 // Create a temporary span to measure the exact text width
354 const span = document.createElement('span');
355 span.style.visibility = 'hidden';
356 span.style.position = 'absolute';
357 span.style.whiteSpace = 'pre';
358 // Copy the input's font properties
359 const inputStyle = window.getComputedStyle(searchInput);
360 span.style.font = inputStyle.font;
361 span.style.fontSize = inputStyle.fontSize;
362 span.style.fontFamily = inputStyle.fontFamily;
363 span.textContent = searchInput.value;
364 document.body.appendChild(span);
365
366 // Get the actual width of the text
367 const inputWidth = searchInput.value.length > 0 ? span.offsetWidth : 0;
368 document.body.removeChild(span);
369 let hintLeftPosition = inputWidth;
370
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100371 if (aside && aside.classList.contains('active') && !isSmallScreen) {
Uyen-Nhu Trana17b08d2025-02-18 19:14:55 +0100372 const asideWidth = aside.getBoundingClientRect().width;
373 hintLeftPosition += asideWidth;
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100374 }
375
376 hint.style.left = `${hintLeftPosition}px`;
377 }
378 }
379 //Can be solved more elegant witch ES6 (see optional chaining operator)
380 let qlf = document.querySelector('#q-field');
381 if(qlf != null){
382 qlf.addEventListener('blur', adjustHintPosition);
383 }
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200384
385 // MutationObserver to detect when #hint is injected into the DOM
386 const observer = new MutationObserver((mutationsList, observer) => {
387 for (const mutation of mutationsList) {
388 if (mutation.type === 'childList') {
389 const hint = document.querySelector('#hint');
390 if (hint) {
Uyen-Nhu Trana17b08d2025-02-18 19:14:55 +0100391 if (window.location.pathname !== '/settings' && window.location.pathname !== '/settings/') {
392 shiftContent();
393 }
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200394 observer.disconnect();
Akronbe2f9a02025-01-14 09:36:55 +0100395 KorAP.Hint.alert().show();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200396 }
397 }
398 }
399 });
400
401 observer.observe(document.body, { childList: true, subtree: true });
402
Akrone0c32c72017-04-25 22:38:23 +0200403 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100404 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200405
406 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200407
Akron5258d462017-04-26 23:32:57 +0200408 // Horrible lock to deal with sidebar clicks
409 var asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200410
411 shiftContent();
412
Akron1885ce92017-04-26 23:10:01 +0200413 // Make aside active on focus
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200414 aside.addEventListener('focus', function (e) {
Akrone0c32c72017-04-25 22:38:23 +0200415 this.classList.add('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200416 shiftContent();
Akrone0c32c72017-04-25 22:38:23 +0200417 });
418
Akron1885ce92017-04-26 23:10:01 +0200419 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100420 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200421 if (body !== null) {
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200422 body.addEventListener('click', function () {
Akron5258d462017-04-26 23:32:57 +0200423 if (!asideClicked) {
424 aside.classList.remove('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200425 shiftContent();
426 } else {
Akron5258d462017-04-26 23:32:57 +0200427 asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200428 }
Akron1885ce92017-04-26 23:10:01 +0200429 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200430 }
Akron1885ce92017-04-26 23:10:01 +0200431
432 /* Stop click event on aside
433 * (to not trickle down to body)
434 */
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200435 aside.addEventListener('click', function (e) {
Akron5258d462017-04-26 23:32:57 +0200436 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200437 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200438 }
Akronb9cdb102017-04-25 00:52:31 +0200439
Uyen-Nhu Trana17b08d2025-02-18 19:14:55 +0100440 if (window.location.pathname === '/settings' || window.location.pathname === '/settings/') {
441 const header = document.querySelector('header');
442 const main = document.querySelector('main');
443 const footer = document.querySelector('footer');
444
445 aside.style.display = 'none';
446
447 if (header) header.style.setProperty('padding-left', '0', 'important');
448 if (main) main.style.setProperty('padding-left', '0', 'important');
449 if (footer) footer.style.setProperty('padding-left', '0', 'important');
450 }
451
Akron6bb71582016-06-10 20:41:08 +0200452 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100453 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200454 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100455 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100456 d.getElementById('ql-field').parentNode
Marc Kupietz95455822023-09-19 20:14:31 +0200457 ).limit(10);
Akronaba7a5a2016-08-15 21:58:33 +0200458 };
Akron6bb71582016-06-10 20:41:08 +0200459
Akron4d926f12018-07-16 15:30:25 +0200460 var resultInfo = d.getElementById('resultinfo');
461
Akron4d926f12018-07-16 15:30:25 +0200462 /**
463 * Add result panel
464 */
Akron5cb9b2b2018-07-24 17:01:09 +0200465 var resultPanel = resultPanelClass.create(show);
Akron644ad9f2021-07-26 16:12:59 +0200466
Akron4d926f12018-07-16 15:30:25 +0200467 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200468
469 // Move buttons to resultinfo
Akron37ea1192021-07-28 10:40:14 +0200470 resultInfo.appendChild(resultPanel.actions().element());
Akron4d926f12018-07-16 15:30:25 +0200471
Akrone6538cd2018-07-16 17:52:33 +0200472 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200473 var sb = d.getElementById('search');
474 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200475 };
476
477
Akron179c8ac2015-06-30 19:30:50 +0200478 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200479 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200480
481 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200482 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200483 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200484 };
Akron7716f012015-07-01 20:38:32 +0200485
Akron00cd4d12016-05-31 21:01:11 +0200486 if (KorAP.koralQuery["errors"]) {
Akron678c26f2020-10-09 08:52:50 +0200487 KorAP.koralQuery["errors"].forEach(function(e) {
Akronf0c31ed2016-06-11 11:27:01 +0200488
Akron19d97fe2016-09-06 20:47:05 +0200489 // Malformed query
Akron4a24b722020-10-13 12:44:25 +0200490 if (e[0] === 302 && e[2] !== undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200491 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200492 obj.hint.alert(e[2], e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200493 }
Akronf0c31ed2016-06-11 11:27:01 +0200494
Akron19d97fe2016-09-06 20:47:05 +0200495 // no query
Akron678c26f2020-10-09 08:52:50 +0200496 else if (e[0] === 301) {
Akron19d97fe2016-09-06 20:47:05 +0200497 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200498 obj.hint.alert(0, e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200499 }
Akron678c26f2020-10-09 08:52:50 +0200500 });
Akron00cd4d12016-05-31 21:01:11 +0200501 };
Akron179c8ac2015-06-30 19:30:50 +0200502 };
503
Akron5cb9b2b2018-07-24 17:01:09 +0200504
505 /*
506 * There is more than 0 matches, so allow for
507 * alignment toggling (left <=> right)
508 */
Akronb50964a2020-10-12 11:44:37 +0200509 if (matchElements.length > 0)
Akron5cb9b2b2018-07-24 17:01:09 +0200510 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000511
hebasta043e96f2019-11-28 12:33:00 +0100512 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200513 /*
Akroncd42a142019-07-12 18:55:37 +0200514 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000515 */
516 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200517 vc.onMinimize = function () {
518 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200519 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200520 };
Nils Diewald6283d692015-04-23 20:32:53 +0000521
Akronec6bb8e2018-08-29 13:07:56 +0200522 vc.onOpen = function () {
523 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100524
525 var view = d.getElementById('vc-view');
526 if (!view.firstChild)
527 view.appendChild(this.element());
528
Akroncd42a142019-07-12 18:55:37 +0200529 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200530 };
531
532 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200533 if (vc.isOpen()) {
534 vc.minimize()
535 }
536 else {
Akronec6bb8e2018-08-29 13:07:56 +0200537 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200538 };
Nils Diewald58141332015-04-07 16:18:45 +0000539 };
Akron04671e72017-05-11 20:47:32 +0200540
Akron179c8ac2015-06-30 19:30:50 +0200541 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200542
543 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200544 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200545 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200546 };
Nils Diewald58141332015-04-07 16:18:45 +0000547 };
548
Akron19d97fe2016-09-06 20:47:05 +0200549
Nils Diewald58141332015-04-07 16:18:45 +0000550 /**
551 * Init Tutorial view
552 */
Akron0b489ad2018-02-02 16:49:32 +0100553 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000554 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100555 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200556 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000557 );
558 obj.tutorial = window.tutorial;
559 }
Nils Diewald58141332015-04-07 16:18:45 +0000560
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000561 // Tutorial is in parent
562 else if (window.parent) {
563 obj.tutorial = window.parent.tutorial;
564 };
565
Akron0b489ad2018-02-02 16:49:32 +0100566 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200567 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100568 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000569
Akron6ed13992016-05-23 18:06:05 +0200570 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100571 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200572 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000573
Nils Diewald845282c2015-05-14 07:53:03 +0000574
Nils Diewald58141332015-04-07 16:18:45 +0000575 /**
Akronc1457bf2015-06-11 19:24:00 +0200576 * Add VC creation on submission.
577 */
Akron0b489ad2018-02-02 16:49:32 +0100578 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200579 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200580 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100581 var qf = d.getElementById('q-field');
Akron1be6c1c2020-01-07 15:29:58 +0100582
Akron19d97fe2016-09-06 20:47:05 +0200583 // No query was defined
584 if (qf.value === undefined || qf.value === '') {
585 qf.focus();
586 e.halt();
587 KorAP.log(700, "No query given");
588 return;
589 };
Akron1be6c1c2020-01-07 15:29:58 +0100590
Akron19d97fe2016-09-06 20:47:05 +0200591 // Store session information
Akronf8035592018-05-24 20:40:51 +0200592 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200593
Akron19d97fe2016-09-06 20:47:05 +0200594 if (vc !== undefined) {
595 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100596 if (input.value == '')
597 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200598 }
599 else {
Akrond7ad9072019-12-09 07:08:20 +0100600 input.removeAttribute('value');
601 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200602 };
Akron1be6c1c2020-01-07 15:29:58 +0100603
Akronaa3dcfe2024-12-10 15:29:40 +0100604 if (KorAP.States != null) {
605 const statesE = KorAP.States.element();
606 if (statesE.value == "")
607 statesE.removeAttribute('name');
608 };
609
610 if (KorAP.Pipe != null) {
611 const pipeE = KorAP.Pipe.element();
612 if (pipeE.value == "")
613 pipeE.removeAttribute("name");
614 };
615
Akron910828a2025-06-27 15:38:48 +0200616 if (KorAP.ResponsePipe != null) {
617 const pipeE = KorAP.ResponsePipe.element();
618 if (pipeE.value == "")
619 pipeE.removeAttribute("name");
620 };
621
622
Akron1be6c1c2020-01-07 15:29:58 +0100623 // This would preferably set the query to be "disabled",
624 // but in that case the query wouldn't be submitted
625 // at all.
626 // Setting the cursor to "progress" fails in current versions
627 // of webkit.
628 qf.classList.add("loading");
629 d.getElementById('qsubmit').classList.add("loading");
Akronaa3dcfe2024-12-10 15:29:40 +0100630
631 // Alternatively the submission could be prevented early
632 // and the formData API could be used instead:
633 // e.preventDefault();
634 // const formData = new FormData(this);
635 // const queryString = new URLSearchParams(formData).toString();
636 // window.location.href = `${this.action}?${queryString}`;
Akronc1457bf2015-06-11 19:24:00 +0200637 });
638 };
hebasta5df796f2019-05-21 15:27:12 +0200639
640
641 //Starts the guided tour at the next page
642 if(KorAP.session.get("tour")){
643 tourClass.gTshowResults().start();
644 }
645
Akronc1457bf2015-06-11 19:24:00 +0200646 /**
Nils Diewald58141332015-04-07 16:18:45 +0000647 * Init hint helper
648 * has to be final because of
649 * reposition
650 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000651 // Todo: Pass an element, so this works with
652 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200653 if (obj.hint === undefined)
654 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000655
Akron99713ef2017-06-28 18:19:28 +0200656 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100657 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200658
Akron2d0d96d2019-11-18 19:49:50 +0100659
660 /**
661 * Add query panel
662 */
663 var queryPanel = queryPanelClass.create();
664
665 // Get input field
Akron2d0d96d2019-11-18 19:49:50 +0100666 var vcView = d.getElementById('vc-view')
Akron96b97d62023-11-07 15:56:54 +0100667 if (form && vcView) {
Akron2d0d96d2019-11-18 19:49:50 +0100668 // The views are below the query bar
Akron96b97d62023-11-07 15:56:54 +0100669 form.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100670 KorAP.Panel['query'] = queryPanel;
Akron644ad9f2021-07-26 16:12:59 +0200671 };
672
673
674 /**
675 * Add pagination panel
676 */
677 const paginationPanel = paginationPanelClass.create();
678
679 if (paginationPanel) {
680 paginationPanel.addRandomPage();
681 KorAP.Panel['pagination'] = paginationPanel;
682 };
Akron24f48ea2020-07-01 09:37:19 +0200683
Akrona9c55802021-06-15 11:41:29 +0200684
685 /**
686 * Initialize password toggle.
687 */
688 initCopyToClipboard(d);
689
690
Akron24f48ea2020-07-01 09:37:19 +0200691 /**
Akron116eace2021-06-14 18:02:37 +0200692 * Initialize password toggle.
693 */
Akron1cfde272021-06-14 18:32:39 +0200694 initTogglePwdVisibility(d);
Akron116eace2021-06-14 18:02:37 +0200695
696 /**
Akron24f48ea2020-07-01 09:37:19 +0200697 * Initialize Plugin registry.
698 */
Akron8dda1c62021-01-20 10:27:32 +0100699 let pe;
700 if (pe = d.getElementById("kalamar-plugins")) {
701 let url = pe.getAttribute('data-plugins');
702 if (url !== undefined) {
703 KorAP.API.getPluginList(url, function (json) {
704 if (json && json.length > 0) {
Akronda32e7a2021-11-16 17:28:57 +0100705
706 // Add state manager
Akron96b97d62023-11-07 15:56:54 +0100707 form = d.getElementById("searchform");
708 if (!form) {
709 return;
710 };
711
712 const input = form.addE("input");
Akroned223be2024-12-10 13:01:46 +0100713 input.setAttribute("type","text");
Akronda32e7a2021-11-16 17:28:57 +0100714 input.setAttribute("name","state");
Akron3d232342025-12-03 22:46:47 +0100715 input.setAttribute("id","state");
Akroned223be2024-12-10 13:01:46 +0100716
717 const url = new URL(window.location.href);
718
719 // Access the query parameters to check for states
720 const state = new URLSearchParams(url.search).get('state');
721 if (state != null && state != "") {
722 input.setAttribute("value", state);
723 };
724
Akronda32e7a2021-11-16 17:28:57 +0100725 KorAP.States = stateManagerClass.create(input);
726
Akron8dda1c62021-01-20 10:27:32 +0100727 // Load Plugin Server first
728 KorAP.Plugin = pluginClass.create();
Akron24f48ea2020-07-01 09:37:19 +0200729
Akron8dda1c62021-01-20 10:27:32 +0100730 // Add services container to head
731 d.head.appendChild(KorAP.Plugin.element());
Akron24f48ea2020-07-01 09:37:19 +0200732
Akron8dda1c62021-01-20 10:27:32 +0100733 // Add pipe form
Akron910828a2025-06-27 15:38:48 +0200734 KorAP.Pipe = pipeClass.create("pipe");
735 let searchF = d.getElementById("searchform");
736 searchF.appendChild(KorAP.Pipe.element());
737
738 // Add pipe form
739 KorAP.ResponsePipe = pipeClass.create("response-pipe");
740 searchF.appendChild(KorAP.ResponsePipe.element());
Akronda32e7a2021-11-16 17:28:57 +0100741
Akron8dda1c62021-01-20 10:27:32 +0100742 try {
743
744 // Register all plugins
745 json.forEach(i => KorAP.Plugin.register(i));
746 }
747 catch (e) {
748 KorAP.log(0, e);
749 }
750 }
751 });
752 };
Akron24f48ea2020-07-01 09:37:19 +0200753 };
Akronf7f75a92024-09-24 11:15:43 +0200754
755 window.dispatchEvent(new Event("ui-ready"));
Akron8dda1c62021-01-20 10:27:32 +0100756
Nils Diewald58141332015-04-07 16:18:45 +0000757 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000758 });
hebasta75cfca52019-02-19 13:15:27 +0100759
Nils Diewald0e6992a2015-04-14 20:13:52 +0000760});