blob: 25fb06b5d153b026126b3090c25e2b7f16884e1e [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');
146 let isBurgerMenuOpen = false;
147
148 if (burgerIcon) {
149 burgerIcon.addEventListener('click', function() {
150 const navbar = document.querySelector('.navbar');
151 navbar.classList.toggle('show');
152
153 isBurgerMenuOpen = !isBurgerMenuOpen;
154 if (isBurgerMenuOpen) {
155 navbar.style.top = '0';
156 }
157 });
158 }
159
160 // Fallback solution for login dropdown visibility (if :focus-within is not supported)
161 document.addEventListener('DOMContentLoaded', function() {
162 const dropdown = document.querySelector('.dropdown');
163 const dropdownContent = document.querySelector('.dropdown-content');
164
165 dropdown.addEventListener('mouseenter', function() {
166 dropdownContent.style.display = 'block';
167 });
168
169 dropdown.addEventListener('mouseleave', function() {
170 // If no input inside the form is focused, then close dropdown content
171 if (!dropdown.contains(document.activeElement)) {
172 dropdownContent.style.display = 'none';
173 }
174 });
175
176 dropdownContent.addEventListener('focusin', function() {
177 dropdownContent.style.display = 'block';
178 });
179
180 dropdownContent.addEventListener('focusout', function(e) {
181 // If focus moved outside the dropdown content, then close it
182 if (!dropdownContent.contains(e.relatedTarget)) {
183 dropdownContent.style.display = 'none';
184 }
185 });
186 });
187
Akronf55504a2015-06-18 16:42:55 +0200188 /**
Akroncd42a142019-07-12 18:55:37 +0200189 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000190 */
Akron5c829e92017-05-12 18:10:00 +0200191 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200192 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200193
hebasta2758b582018-11-19 15:59:42 +0100194 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100195
Akron1f0521b2018-08-28 13:01:24 +0200196 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000197 if (input) {
198 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100199 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000200 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200201 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200202
Akron1f0521b2018-08-28 13:01:24 +0200203 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200204 // Supports "collection" for legacy reasons
205 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200206 try {
Akroncd42a142019-07-12 18:55:37 +0200207 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200208 }
209 catch (e) {
210 KorAP.log(0,e);
211 }
Akron27ae9ec2015-06-23 00:43:21 +0200212 };
213
Akron0b489ad2018-02-02 16:49:32 +0100214 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200215 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200216
Akron1f0521b2018-08-28 13:01:24 +0200217 if (vc.wasRewritten()) {
218 vcchoose.classList.add('rewritten');
219 };
220
Nils Diewald7148c6f2015-05-04 15:07:53 +0000221 input.parentNode.insertBefore(vcname, input);
222 };
223
Nils Diewald7148c6f2015-05-04 15:07:53 +0000224 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000225 * Add actions to match entries
226 */
Akronb50964a2020-10-12 11:44:37 +0200227 var matchElements = d.querySelectorAll(
Akron3c390c42020-03-30 09:06:21 +0200228 '#search > ol > li'
Nils Diewald5c5a7472015-04-02 22:13:38 +0000229 );
Akron6a535d42015-08-26 20:16:58 +0200230
Akronb50964a2020-10-12 11:44:37 +0200231 matchElements.forEach(function(e) {
Akron3c390c42020-03-30 09:06:21 +0200232
233 // Define class for active elements
234 if (e.classList.contains('active')) {
Akrond769d702021-08-16 11:09:08 +0200235 if (e._match === undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200236 // lazyLoad
Akron3c390c42020-03-30 09:06:21 +0200237 matchClass.create(e).init();
Akron19d97fe2016-09-06 20:47:05 +0200238 };
Akron3c390c42020-03-30 09:06:21 +0200239 }
240
241 // Define class for inactive elements
242 else {
243 e.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200244 if (this._match !== undefined)
Akron3c390c42020-03-30 09:06:21 +0200245 this._match.open();
Akron19d97fe2016-09-06 20:47:05 +0200246 else {
247 // lazyLoad
248 matchClass.create(this).open();
249 };
Akron3c390c42020-03-30 09:06:21 +0200250 // This would prevent the sidebar to go back
251 // e.halt();
252 });
253 e.addEventListener('keydown', function (e) {
254 var code = _codeFromEvent(e);
255
256 switch (code) {
257 case 32:
258 if (this._match !== undefined)
259 this._match.toggle();
260 else {
261 // lazyLoad
262 matchClass.create(this).open();
263 };
264 e.halt();
265 break;
266 };
267 });
268 };
Akrond769d702021-08-16 11:09:08 +0200269 });
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100270
271 // Media Query for adjusting dynamically added elements (e.g. hint)
272 const isSmallScreen = window.matchMedia('(max-width: 768px)').matches;
Akron3c390c42020-03-30 09:06:21 +0200273
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200274 // Function to toggle the shifted class on elements
275 function shiftContent() {
276 // Get elements to perform content shift when sidebar is active
277 const header = document.querySelector('header');
278 const main = document.querySelector('main');
279 const footer = document.querySelector('footer');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200280 const results = document.querySelector('.found');
281 const aside = document.querySelector('aside');
282
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100283 if (aside && aside.classList.contains('active') && !isSmallScreen) {
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200284 header.classList.add('shifted');
285 if (!results) {
286 main.classList.add('shifted');
287 }
288 footer.classList.add('shifted');
Akron1c18f102024-11-19 16:31:06 +0100289 adjustHintPosition();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200290 } else {
291 header.classList.remove('shifted');
292 main.classList.remove('shifted');
293 footer.classList.remove('shifted');
Akron1c18f102024-11-19 16:31:06 +0100294 adjustHintPosition();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200295 }
296 }
297
Akron1c18f102024-11-19 16:31:06 +0100298 // Function to adjust the position of the annotation assistant bar (hint),
299 // when user types into the searchbar and clicks the sidebar (or anywhere
300 // outside the searchbar) afterwards
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200301 function adjustHintPosition() {
Uyen-Nhu Trane1eba0c2024-12-10 17:06:39 +0100302 const hint = document.querySelector('#hint');
303 const searchInput = document.querySelector('#q-field');
304 const aside = document.querySelector('aside');
305
306 if (hint && searchInput) {
307 // Create a temporary span to measure the exact text width
308 const span = document.createElement('span');
309 span.style.visibility = 'hidden';
310 span.style.position = 'absolute';
311 span.style.whiteSpace = 'pre';
312 // Copy the input's font properties
313 const inputStyle = window.getComputedStyle(searchInput);
314 span.style.font = inputStyle.font;
315 span.style.fontSize = inputStyle.fontSize;
316 span.style.fontFamily = inputStyle.fontFamily;
317 span.textContent = searchInput.value;
318 document.body.appendChild(span);
319
320 // Get the actual width of the text
321 const inputWidth = searchInput.value.length > 0 ? span.offsetWidth : 0;
322 document.body.removeChild(span);
323 let hintLeftPosition = inputWidth;
324
325 // TODO: This shouldn't be a fixed position!
326 if (aside && aside.classList.contains('active') && !isSmallScreen) {
327 hintLeftPosition += 230;
328 }
329
330 hint.style.left = `${hintLeftPosition}px`;
331 }
332 }
333 //Can be solved more elegant witch ES6 (see optional chaining operator)
334 let qlf = document.querySelector('#q-field');
335 if(qlf != null){
336 qlf.addEventListener('blur', adjustHintPosition);
337 }
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200338
339 // MutationObserver to detect when #hint is injected into the DOM
340 const observer = new MutationObserver((mutationsList, observer) => {
341 for (const mutation of mutationsList) {
342 if (mutation.type === 'childList') {
343 const hint = document.querySelector('#hint');
344 if (hint) {
345 shiftContent();
346 observer.disconnect();
Akronbe2f9a02025-01-14 09:36:55 +0100347 KorAP.Hint.alert().show();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200348 }
349 }
350 }
351 });
352
353 observer.observe(document.body, { childList: true, subtree: true });
354
Akrone0c32c72017-04-25 22:38:23 +0200355 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100356 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200357
358 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200359
Akron5258d462017-04-26 23:32:57 +0200360 // Horrible lock to deal with sidebar clicks
361 var asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200362
363 shiftContent();
364
Akron1885ce92017-04-26 23:10:01 +0200365 // Make aside active on focus
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200366 aside.addEventListener('focus', function (e) {
Akrone0c32c72017-04-25 22:38:23 +0200367 this.classList.add('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200368 shiftContent();
Akrone0c32c72017-04-25 22:38:23 +0200369 });
370
Akron1885ce92017-04-26 23:10:01 +0200371 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100372 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200373 if (body !== null) {
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200374 body.addEventListener('click', function () {
Akron5258d462017-04-26 23:32:57 +0200375 if (!asideClicked) {
376 aside.classList.remove('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200377 shiftContent();
378 } else {
Akron5258d462017-04-26 23:32:57 +0200379 asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200380 }
Akron1885ce92017-04-26 23:10:01 +0200381 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200382 }
Akron1885ce92017-04-26 23:10:01 +0200383
384 /* Stop click event on aside
385 * (to not trickle down to body)
386 */
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200387 aside.addEventListener('click', function (e) {
Akron5258d462017-04-26 23:32:57 +0200388 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200389 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200390 }
Akronb9cdb102017-04-25 00:52:31 +0200391
Akron6bb71582016-06-10 20:41:08 +0200392 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100393 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200394 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100395 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100396 d.getElementById('ql-field').parentNode
Marc Kupietz95455822023-09-19 20:14:31 +0200397 ).limit(10);
Akronaba7a5a2016-08-15 21:58:33 +0200398 };
Akron6bb71582016-06-10 20:41:08 +0200399
Akron4d926f12018-07-16 15:30:25 +0200400 var resultInfo = d.getElementById('resultinfo');
401
Akron4d926f12018-07-16 15:30:25 +0200402 /**
403 * Add result panel
404 */
Akron5cb9b2b2018-07-24 17:01:09 +0200405 var resultPanel = resultPanelClass.create(show);
Akron644ad9f2021-07-26 16:12:59 +0200406
Akron4d926f12018-07-16 15:30:25 +0200407 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200408
409 // Move buttons to resultinfo
Akron37ea1192021-07-28 10:40:14 +0200410 resultInfo.appendChild(resultPanel.actions().element());
Akron4d926f12018-07-16 15:30:25 +0200411
Akrone6538cd2018-07-16 17:52:33 +0200412 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200413 var sb = d.getElementById('search');
414 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200415 };
416
417
Akron179c8ac2015-06-30 19:30:50 +0200418 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200419 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200420
421 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200422 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200423 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200424 };
Akron7716f012015-07-01 20:38:32 +0200425
Akron00cd4d12016-05-31 21:01:11 +0200426 if (KorAP.koralQuery["errors"]) {
Akron678c26f2020-10-09 08:52:50 +0200427 KorAP.koralQuery["errors"].forEach(function(e) {
Akronf0c31ed2016-06-11 11:27:01 +0200428
Akron19d97fe2016-09-06 20:47:05 +0200429 // Malformed query
Akron4a24b722020-10-13 12:44:25 +0200430 if (e[0] === 302 && e[2] !== undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200431 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200432 obj.hint.alert(e[2], e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200433 }
Akronf0c31ed2016-06-11 11:27:01 +0200434
Akron19d97fe2016-09-06 20:47:05 +0200435 // no query
Akron678c26f2020-10-09 08:52:50 +0200436 else if (e[0] === 301) {
Akron19d97fe2016-09-06 20:47:05 +0200437 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200438 obj.hint.alert(0, e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200439 }
Akron678c26f2020-10-09 08:52:50 +0200440 });
Akron00cd4d12016-05-31 21:01:11 +0200441 };
Akron179c8ac2015-06-30 19:30:50 +0200442 };
443
Akron5cb9b2b2018-07-24 17:01:09 +0200444
445 /*
446 * There is more than 0 matches, so allow for
447 * alignment toggling (left <=> right)
448 */
Akronb50964a2020-10-12 11:44:37 +0200449 if (matchElements.length > 0)
Akron5cb9b2b2018-07-24 17:01:09 +0200450 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000451
hebasta043e96f2019-11-28 12:33:00 +0100452 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200453 /*
Akroncd42a142019-07-12 18:55:37 +0200454 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000455 */
456 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200457 vc.onMinimize = function () {
458 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200459 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200460 };
Nils Diewald6283d692015-04-23 20:32:53 +0000461
Akronec6bb8e2018-08-29 13:07:56 +0200462 vc.onOpen = function () {
463 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100464
465 var view = d.getElementById('vc-view');
466 if (!view.firstChild)
467 view.appendChild(this.element());
468
Akroncd42a142019-07-12 18:55:37 +0200469 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200470 };
471
472 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200473 if (vc.isOpen()) {
474 vc.minimize()
475 }
476 else {
Akronec6bb8e2018-08-29 13:07:56 +0200477 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200478 };
Nils Diewald58141332015-04-07 16:18:45 +0000479 };
Akron04671e72017-05-11 20:47:32 +0200480
Akron179c8ac2015-06-30 19:30:50 +0200481 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200482
483 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200484 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200485 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200486 };
Nils Diewald58141332015-04-07 16:18:45 +0000487 };
488
Akron19d97fe2016-09-06 20:47:05 +0200489
Nils Diewald58141332015-04-07 16:18:45 +0000490 /**
491 * Init Tutorial view
492 */
Akron0b489ad2018-02-02 16:49:32 +0100493 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000494 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100495 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200496 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000497 );
498 obj.tutorial = window.tutorial;
499 }
Nils Diewald58141332015-04-07 16:18:45 +0000500
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000501 // Tutorial is in parent
502 else if (window.parent) {
503 obj.tutorial = window.parent.tutorial;
504 };
505
Akron0b489ad2018-02-02 16:49:32 +0100506 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200507 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100508 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000509
Akron6ed13992016-05-23 18:06:05 +0200510 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100511 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200512 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000513
Nils Diewald845282c2015-05-14 07:53:03 +0000514
Nils Diewald58141332015-04-07 16:18:45 +0000515 /**
Akronc1457bf2015-06-11 19:24:00 +0200516 * Add VC creation on submission.
517 */
Akron0b489ad2018-02-02 16:49:32 +0100518 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200519 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200520 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100521 var qf = d.getElementById('q-field');
Akron1be6c1c2020-01-07 15:29:58 +0100522
Akron19d97fe2016-09-06 20:47:05 +0200523 // No query was defined
524 if (qf.value === undefined || qf.value === '') {
525 qf.focus();
526 e.halt();
527 KorAP.log(700, "No query given");
528 return;
529 };
Akron1be6c1c2020-01-07 15:29:58 +0100530
Akron19d97fe2016-09-06 20:47:05 +0200531 // Store session information
Akronf8035592018-05-24 20:40:51 +0200532 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200533
Akron19d97fe2016-09-06 20:47:05 +0200534 if (vc !== undefined) {
535 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100536 if (input.value == '')
537 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200538 }
539 else {
Akrond7ad9072019-12-09 07:08:20 +0100540 input.removeAttribute('value');
541 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200542 };
Akron1be6c1c2020-01-07 15:29:58 +0100543
Akronaa3dcfe2024-12-10 15:29:40 +0100544 if (KorAP.States != null) {
545 const statesE = KorAP.States.element();
546 if (statesE.value == "")
547 statesE.removeAttribute('name');
548 };
549
550 if (KorAP.Pipe != null) {
551 const pipeE = KorAP.Pipe.element();
552 if (pipeE.value == "")
553 pipeE.removeAttribute("name");
554 };
555
Akron1be6c1c2020-01-07 15:29:58 +0100556 // This would preferably set the query to be "disabled",
557 // but in that case the query wouldn't be submitted
558 // at all.
559 // Setting the cursor to "progress" fails in current versions
560 // of webkit.
561 qf.classList.add("loading");
562 d.getElementById('qsubmit').classList.add("loading");
Akronaa3dcfe2024-12-10 15:29:40 +0100563
564 // Alternatively the submission could be prevented early
565 // and the formData API could be used instead:
566 // e.preventDefault();
567 // const formData = new FormData(this);
568 // const queryString = new URLSearchParams(formData).toString();
569 // window.location.href = `${this.action}?${queryString}`;
Akronc1457bf2015-06-11 19:24:00 +0200570 });
571 };
hebasta5df796f2019-05-21 15:27:12 +0200572
573
574 //Starts the guided tour at the next page
575 if(KorAP.session.get("tour")){
576 tourClass.gTshowResults().start();
577 }
578
Akronc1457bf2015-06-11 19:24:00 +0200579 /**
Nils Diewald58141332015-04-07 16:18:45 +0000580 * Init hint helper
581 * has to be final because of
582 * reposition
583 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000584 // Todo: Pass an element, so this works with
585 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200586 if (obj.hint === undefined)
587 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000588
Akron99713ef2017-06-28 18:19:28 +0200589 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100590 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200591
Akron2d0d96d2019-11-18 19:49:50 +0100592
593 /**
594 * Add query panel
595 */
596 var queryPanel = queryPanelClass.create();
597
598 // Get input field
Akron2d0d96d2019-11-18 19:49:50 +0100599 var vcView = d.getElementById('vc-view')
Akron96b97d62023-11-07 15:56:54 +0100600 if (form && vcView) {
Akron2d0d96d2019-11-18 19:49:50 +0100601 // The views are below the query bar
Akron96b97d62023-11-07 15:56:54 +0100602 form.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100603 KorAP.Panel['query'] = queryPanel;
Akron644ad9f2021-07-26 16:12:59 +0200604 };
605
606
607 /**
608 * Add pagination panel
609 */
610 const paginationPanel = paginationPanelClass.create();
611
612 if (paginationPanel) {
613 paginationPanel.addRandomPage();
614 KorAP.Panel['pagination'] = paginationPanel;
615 };
Akron24f48ea2020-07-01 09:37:19 +0200616
Akrona9c55802021-06-15 11:41:29 +0200617
618 /**
619 * Initialize password toggle.
620 */
621 initCopyToClipboard(d);
622
623
Akron24f48ea2020-07-01 09:37:19 +0200624 /**
Akron116eace2021-06-14 18:02:37 +0200625 * Initialize password toggle.
626 */
Akron1cfde272021-06-14 18:32:39 +0200627 initTogglePwdVisibility(d);
Akron116eace2021-06-14 18:02:37 +0200628
629 /**
Akron24f48ea2020-07-01 09:37:19 +0200630 * Initialize Plugin registry.
631 */
Akron8dda1c62021-01-20 10:27:32 +0100632 let pe;
633 if (pe = d.getElementById("kalamar-plugins")) {
634 let url = pe.getAttribute('data-plugins');
635 if (url !== undefined) {
636 KorAP.API.getPluginList(url, function (json) {
637 if (json && json.length > 0) {
Akronda32e7a2021-11-16 17:28:57 +0100638
639 // Add state manager
Akron96b97d62023-11-07 15:56:54 +0100640 form = d.getElementById("searchform");
641 if (!form) {
642 return;
643 };
644
645 const input = form.addE("input");
Akroned223be2024-12-10 13:01:46 +0100646 input.setAttribute("type","text");
Akronda32e7a2021-11-16 17:28:57 +0100647 input.setAttribute("name","state");
Akroned223be2024-12-10 13:01:46 +0100648
649 const url = new URL(window.location.href);
650
651 // Access the query parameters to check for states
652 const state = new URLSearchParams(url.search).get('state');
653 if (state != null && state != "") {
654 input.setAttribute("value", state);
655 };
656
Akronda32e7a2021-11-16 17:28:57 +0100657 KorAP.States = stateManagerClass.create(input);
658
Akron8dda1c62021-01-20 10:27:32 +0100659 // Load Plugin Server first
660 KorAP.Plugin = pluginClass.create();
Akron24f48ea2020-07-01 09:37:19 +0200661
Akron8dda1c62021-01-20 10:27:32 +0100662 // Add services container to head
663 d.head.appendChild(KorAP.Plugin.element());
Akron24f48ea2020-07-01 09:37:19 +0200664
Akron8dda1c62021-01-20 10:27:32 +0100665 // Add pipe form
666 KorAP.Pipe = pipeClass.create();
667 d.getElementById("searchform").appendChild(KorAP.Pipe.element());
Akronda32e7a2021-11-16 17:28:57 +0100668
Akron8dda1c62021-01-20 10:27:32 +0100669 try {
670
671 // Register all plugins
672 json.forEach(i => KorAP.Plugin.register(i));
673 }
674 catch (e) {
675 KorAP.log(0, e);
676 }
677 }
678 });
679 };
Akron24f48ea2020-07-01 09:37:19 +0200680 };
Akronf7f75a92024-09-24 11:15:43 +0200681
682 window.dispatchEvent(new Event("ui-ready"));
Akron8dda1c62021-01-20 10:27:32 +0100683
Nils Diewald58141332015-04-07 16:18:45 +0000684 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000685 });
hebasta75cfca52019-02-19 13:15:27 +0100686
Nils Diewald0e6992a2015-04-14 20:13:52 +0000687});