blob: 13904ec599186f4a47e820f6812047b92ccc7f0c [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') || "";
85
86 // Create suffix if KorAP is run in a subfolder
87 KorAP.session = sessionClass.create(
88 KorAP.URL.length > 0 ? 'kalamarJS-' + KorAP.URL.slugify() : 'kalamarJS'
89 );
90
91 // Get koralQuery response
92 const kqe = d.getElementById('koralQuery');
93 if (kqe !== null) {
94 KorAP.koralQuery = JSON.parse(kqe.getAttribute('data-koralquery') || "");
95 };
96
Helge0d3630c2024-10-16 17:19:40 +020097 let gt = document.getElementsByClassName('link-guided-tour');
98 if (gt.length != null){
99 for(let j = 0; j < gt.length; j++){
100 gt[j].setAttribute('href', '#');
101 gt[j].addEventListener('click', function(){
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200102 tourClass.gTstartSearch().start();
Helge0d3630c2024-10-16 17:19:40 +0200103
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200104 // Close the burger menu by simulating a click on the burger icon
105 const burgerIcon = document.querySelector('.burger-icon');
106 if (isBurgerMenuOpen) {
107 burgerIcon.click();
108 }
109 });
110 }
111
112 KorAP.tourshowR = function(){
113 tourClass.gTshowResults().start();
Akron006ddc62024-02-19 08:49:43 +0100114 };
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200115 }
Akron006ddc62024-02-19 08:49:43 +0100116
Nils Diewald0e6992a2015-04-14 20:13:52 +0000117 var obj = {};
Akron71b91e42016-06-01 22:12:43 +0200118
Akron4d926f12018-07-16 15:30:25 +0200119 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +0200120 var show = KorAP.session.get('show') || {};
hebasta043e96f2019-11-28 12:33:00 +0100121
122 KorAP.Panel = KorAP.Panel || {}
Nils Diewalda297f062015-04-02 00:23:46 +0000123
124 /**
Akronf55504a2015-06-18 16:42:55 +0200125 * Release notifications
126 */
Akroncb5c1712021-01-26 18:01:04 +0100127 d.querySelectorAll('#notifications div.notify').forEach(
128 function(e) {
129 let msg = e.textContent;
130
131 let src = e.getAttribute('data-src');
132 if (src) {
133 msg += '<code class="src">'+src+'</code>';
Akron8ea84292018-10-24 13:41:52 +0200134 };
Akroncb5c1712021-01-26 18:01:04 +0100135
136 let type = e.getAttribute('data-type') || "error";
137 alertifyClass.log(msg, type, 10000);
138 }
139 );
Akronf55504a2015-06-18 16:42:55 +0200140
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200141 // Responsive navbar: hide and show burger menu
142 const burgerIcon = document.querySelector('.burger-icon');
143 let isBurgerMenuOpen = false;
144
145 if (burgerIcon) {
146 burgerIcon.addEventListener('click', function() {
147 const navbar = document.querySelector('.navbar');
148 navbar.classList.toggle('show');
149
150 isBurgerMenuOpen = !isBurgerMenuOpen;
151 if (isBurgerMenuOpen) {
152 navbar.style.top = '0';
153 }
154 });
155 }
156
157 // Fallback solution for login dropdown visibility (if :focus-within is not supported)
158 document.addEventListener('DOMContentLoaded', function() {
159 const dropdown = document.querySelector('.dropdown');
160 const dropdownContent = document.querySelector('.dropdown-content');
161
162 dropdown.addEventListener('mouseenter', function() {
163 dropdownContent.style.display = 'block';
164 });
165
166 dropdown.addEventListener('mouseleave', function() {
167 // If no input inside the form is focused, then close dropdown content
168 if (!dropdown.contains(document.activeElement)) {
169 dropdownContent.style.display = 'none';
170 }
171 });
172
173 dropdownContent.addEventListener('focusin', function() {
174 dropdownContent.style.display = 'block';
175 });
176
177 dropdownContent.addEventListener('focusout', function(e) {
178 // If focus moved outside the dropdown content, then close it
179 if (!dropdownContent.contains(e.relatedTarget)) {
180 dropdownContent.style.display = 'none';
181 }
182 });
183 });
184
Akronf55504a2015-06-18 16:42:55 +0200185 /**
Akroncd42a142019-07-12 18:55:37 +0200186 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000187 */
Akron5c829e92017-05-12 18:10:00 +0200188 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200189 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200190
hebasta2758b582018-11-19 15:59:42 +0100191 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100192
Akron1f0521b2018-08-28 13:01:24 +0200193 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000194 if (input) {
195 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100196 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000197 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200198 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200199
Akron1f0521b2018-08-28 13:01:24 +0200200 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200201 // Supports "collection" for legacy reasons
202 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200203 try {
Akroncd42a142019-07-12 18:55:37 +0200204 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200205 }
206 catch (e) {
207 KorAP.log(0,e);
208 }
Akron27ae9ec2015-06-23 00:43:21 +0200209 };
210
Akron0b489ad2018-02-02 16:49:32 +0100211 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200212 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200213
Akron1f0521b2018-08-28 13:01:24 +0200214 if (vc.wasRewritten()) {
215 vcchoose.classList.add('rewritten');
216 };
217
Nils Diewald7148c6f2015-05-04 15:07:53 +0000218 input.parentNode.insertBefore(vcname, input);
219 };
220
Nils Diewald7148c6f2015-05-04 15:07:53 +0000221 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000222 * Add actions to match entries
223 */
Akronb50964a2020-10-12 11:44:37 +0200224 var matchElements = d.querySelectorAll(
Akron3c390c42020-03-30 09:06:21 +0200225 '#search > ol > li'
Nils Diewald5c5a7472015-04-02 22:13:38 +0000226 );
Akron6a535d42015-08-26 20:16:58 +0200227
Akronb50964a2020-10-12 11:44:37 +0200228 matchElements.forEach(function(e) {
Akron3c390c42020-03-30 09:06:21 +0200229
230 // Define class for active elements
231 if (e.classList.contains('active')) {
Akrond769d702021-08-16 11:09:08 +0200232 if (e._match === undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200233 // lazyLoad
Akron3c390c42020-03-30 09:06:21 +0200234 matchClass.create(e).init();
Akron19d97fe2016-09-06 20:47:05 +0200235 };
Akron3c390c42020-03-30 09:06:21 +0200236 }
237
238 // Define class for inactive elements
239 else {
240 e.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200241 if (this._match !== undefined)
Akron3c390c42020-03-30 09:06:21 +0200242 this._match.open();
Akron19d97fe2016-09-06 20:47:05 +0200243 else {
244 // lazyLoad
245 matchClass.create(this).open();
246 };
Akron3c390c42020-03-30 09:06:21 +0200247 // This would prevent the sidebar to go back
248 // e.halt();
249 });
250 e.addEventListener('keydown', function (e) {
251 var code = _codeFromEvent(e);
252
253 switch (code) {
254 case 32:
255 if (this._match !== undefined)
256 this._match.toggle();
257 else {
258 // lazyLoad
259 matchClass.create(this).open();
260 };
261 e.halt();
262 break;
263 };
264 });
265 };
Akrond769d702021-08-16 11:09:08 +0200266 });
Akron3c390c42020-03-30 09:06:21 +0200267
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200268 // Function to toggle the shifted class on elements
269 function shiftContent() {
270 // Get elements to perform content shift when sidebar is active
271 const header = document.querySelector('header');
272 const main = document.querySelector('main');
273 const footer = document.querySelector('footer');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200274 const results = document.querySelector('.found');
275 const aside = document.querySelector('aside');
276
277 if (aside && aside.classList.contains('active')) {
278 header.classList.add('shifted');
279 if (!results) {
280 main.classList.add('shifted');
281 }
282 footer.classList.add('shifted');
Akron1c18f102024-11-19 16:31:06 +0100283 adjustHintPosition();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200284 } else {
285 header.classList.remove('shifted');
286 main.classList.remove('shifted');
287 footer.classList.remove('shifted');
Akron1c18f102024-11-19 16:31:06 +0100288 adjustHintPosition();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200289 }
290 }
291
Akron1c18f102024-11-19 16:31:06 +0100292 // Function to adjust the position of the annotation assistant bar (hint),
293 // when user types into the searchbar and clicks the sidebar (or anywhere
294 // outside the searchbar) afterwards
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200295 function adjustHintPosition() {
Akron1c18f102024-11-19 16:31:06 +0100296 if (KorAP.Hint != undefined) {
297 KorAP.Hint.inputField().reposition();
298 KorAP.Hint.update();
299 };
300 };
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200301
302 // MutationObserver to detect when #hint is injected into the DOM
303 const observer = new MutationObserver((mutationsList, observer) => {
304 for (const mutation of mutationsList) {
305 if (mutation.type === 'childList') {
306 const hint = document.querySelector('#hint');
307 if (hint) {
308 shiftContent();
309 observer.disconnect();
310 }
311 }
312 }
313 });
314
315 observer.observe(document.body, { childList: true, subtree: true });
316
Akrone0c32c72017-04-25 22:38:23 +0200317 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100318 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200319
320 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200321
Akron5258d462017-04-26 23:32:57 +0200322 // Horrible lock to deal with sidebar clicks
323 var asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200324
325 shiftContent();
326
Akron1885ce92017-04-26 23:10:01 +0200327 // Make aside active on focus
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200328 aside.addEventListener('focus', function (e) {
Akrone0c32c72017-04-25 22:38:23 +0200329 this.classList.add('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200330 shiftContent();
Akrone0c32c72017-04-25 22:38:23 +0200331 });
332
Akron1885ce92017-04-26 23:10:01 +0200333 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100334 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200335 if (body !== null) {
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200336 body.addEventListener('click', function () {
Akron5258d462017-04-26 23:32:57 +0200337 if (!asideClicked) {
338 aside.classList.remove('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200339 shiftContent();
340 } else {
Akron5258d462017-04-26 23:32:57 +0200341 asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200342 }
Akron1885ce92017-04-26 23:10:01 +0200343 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200344 }
Akron1885ce92017-04-26 23:10:01 +0200345
346 /* Stop click event on aside
347 * (to not trickle down to body)
348 */
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200349 aside.addEventListener('click', function (e) {
Akron5258d462017-04-26 23:32:57 +0200350 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200351 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200352 }
Akronb9cdb102017-04-25 00:52:31 +0200353
Akron6bb71582016-06-10 20:41:08 +0200354 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100355 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200356 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100357 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100358 d.getElementById('ql-field').parentNode
Marc Kupietz95455822023-09-19 20:14:31 +0200359 ).limit(10);
Akronaba7a5a2016-08-15 21:58:33 +0200360 };
Akron6bb71582016-06-10 20:41:08 +0200361
Akron4d926f12018-07-16 15:30:25 +0200362 var resultInfo = d.getElementById('resultinfo');
363
Akron4d926f12018-07-16 15:30:25 +0200364 /**
365 * Add result panel
366 */
Akron5cb9b2b2018-07-24 17:01:09 +0200367 var resultPanel = resultPanelClass.create(show);
Akron644ad9f2021-07-26 16:12:59 +0200368
Akron4d926f12018-07-16 15:30:25 +0200369 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200370
371 // Move buttons to resultinfo
Akron37ea1192021-07-28 10:40:14 +0200372 resultInfo.appendChild(resultPanel.actions().element());
Akron4d926f12018-07-16 15:30:25 +0200373
Akrone6538cd2018-07-16 17:52:33 +0200374 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200375 var sb = d.getElementById('search');
376 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200377 };
378
379
Akron179c8ac2015-06-30 19:30:50 +0200380 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200381 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200382
383 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200384 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200385 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200386 };
Akron7716f012015-07-01 20:38:32 +0200387
Akron00cd4d12016-05-31 21:01:11 +0200388 if (KorAP.koralQuery["errors"]) {
Akron678c26f2020-10-09 08:52:50 +0200389 KorAP.koralQuery["errors"].forEach(function(e) {
Akronf0c31ed2016-06-11 11:27:01 +0200390
Akron19d97fe2016-09-06 20:47:05 +0200391 // Malformed query
Akron4a24b722020-10-13 12:44:25 +0200392 if (e[0] === 302 && e[2] !== undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200393 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200394 obj.hint.alert(e[2], e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200395 }
Akronf0c31ed2016-06-11 11:27:01 +0200396
Akron19d97fe2016-09-06 20:47:05 +0200397 // no query
Akron678c26f2020-10-09 08:52:50 +0200398 else if (e[0] === 301) {
Akron19d97fe2016-09-06 20:47:05 +0200399 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200400 obj.hint.alert(0, e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200401 }
Akron678c26f2020-10-09 08:52:50 +0200402 });
Akron00cd4d12016-05-31 21:01:11 +0200403 };
Akron179c8ac2015-06-30 19:30:50 +0200404 };
405
Akron5cb9b2b2018-07-24 17:01:09 +0200406
407 /*
408 * There is more than 0 matches, so allow for
409 * alignment toggling (left <=> right)
410 */
Akronb50964a2020-10-12 11:44:37 +0200411 if (matchElements.length > 0)
Akron5cb9b2b2018-07-24 17:01:09 +0200412 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000413
hebasta043e96f2019-11-28 12:33:00 +0100414 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200415 /*
Akroncd42a142019-07-12 18:55:37 +0200416 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000417 */
418 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200419 vc.onMinimize = function () {
420 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200421 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200422 };
Nils Diewald6283d692015-04-23 20:32:53 +0000423
Akronec6bb8e2018-08-29 13:07:56 +0200424 vc.onOpen = function () {
425 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100426
427 var view = d.getElementById('vc-view');
428 if (!view.firstChild)
429 view.appendChild(this.element());
430
Akroncd42a142019-07-12 18:55:37 +0200431 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200432 };
433
434 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200435 if (vc.isOpen()) {
436 vc.minimize()
437 }
438 else {
Akronec6bb8e2018-08-29 13:07:56 +0200439 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200440 };
Nils Diewald58141332015-04-07 16:18:45 +0000441 };
Akron04671e72017-05-11 20:47:32 +0200442
Akron179c8ac2015-06-30 19:30:50 +0200443 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200444
445 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200446 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200447 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200448 };
Nils Diewald58141332015-04-07 16:18:45 +0000449 };
450
Akron19d97fe2016-09-06 20:47:05 +0200451
Nils Diewald58141332015-04-07 16:18:45 +0000452 /**
453 * Init Tutorial view
454 */
Akron0b489ad2018-02-02 16:49:32 +0100455 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000456 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100457 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200458 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000459 );
460 obj.tutorial = window.tutorial;
461 }
Nils Diewald58141332015-04-07 16:18:45 +0000462
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000463 // Tutorial is in parent
464 else if (window.parent) {
465 obj.tutorial = window.parent.tutorial;
466 };
467
Akron0b489ad2018-02-02 16:49:32 +0100468 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200469 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100470 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000471
Akron6ed13992016-05-23 18:06:05 +0200472 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100473 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200474 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000475
Nils Diewald845282c2015-05-14 07:53:03 +0000476
Nils Diewald58141332015-04-07 16:18:45 +0000477 /**
Akronc1457bf2015-06-11 19:24:00 +0200478 * Add VC creation on submission.
479 */
Akron0b489ad2018-02-02 16:49:32 +0100480 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200481 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200482 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100483 var qf = d.getElementById('q-field');
Akron1be6c1c2020-01-07 15:29:58 +0100484
Akron19d97fe2016-09-06 20:47:05 +0200485 // No query was defined
486 if (qf.value === undefined || qf.value === '') {
487 qf.focus();
488 e.halt();
489 KorAP.log(700, "No query given");
490 return;
491 };
Akron1be6c1c2020-01-07 15:29:58 +0100492
Akron19d97fe2016-09-06 20:47:05 +0200493 // Store session information
Akronf8035592018-05-24 20:40:51 +0200494 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200495
Akron19d97fe2016-09-06 20:47:05 +0200496 if (vc !== undefined) {
497 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100498 if (input.value == '')
499 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200500 }
501 else {
Akrond7ad9072019-12-09 07:08:20 +0100502 input.removeAttribute('value');
503 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200504 };
Akron1be6c1c2020-01-07 15:29:58 +0100505
506 // This would preferably set the query to be "disabled",
507 // but in that case the query wouldn't be submitted
508 // at all.
509 // Setting the cursor to "progress" fails in current versions
510 // of webkit.
511 qf.classList.add("loading");
512 d.getElementById('qsubmit').classList.add("loading");
Akronc1457bf2015-06-11 19:24:00 +0200513 });
514 };
hebasta5df796f2019-05-21 15:27:12 +0200515
516
517 //Starts the guided tour at the next page
518 if(KorAP.session.get("tour")){
519 tourClass.gTshowResults().start();
520 }
521
Akronc1457bf2015-06-11 19:24:00 +0200522 /**
Nils Diewald58141332015-04-07 16:18:45 +0000523 * Init hint helper
524 * has to be final because of
525 * reposition
526 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000527 // Todo: Pass an element, so this works with
528 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200529 if (obj.hint === undefined)
530 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000531
Akron99713ef2017-06-28 18:19:28 +0200532 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100533 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200534
Akron2d0d96d2019-11-18 19:49:50 +0100535
536 /**
537 * Add query panel
538 */
539 var queryPanel = queryPanelClass.create();
540
541 // Get input field
Akron2d0d96d2019-11-18 19:49:50 +0100542 var vcView = d.getElementById('vc-view')
Akron96b97d62023-11-07 15:56:54 +0100543 if (form && vcView) {
Akron2d0d96d2019-11-18 19:49:50 +0100544 // The views are below the query bar
Akron96b97d62023-11-07 15:56:54 +0100545 form.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100546 KorAP.Panel['query'] = queryPanel;
Akron644ad9f2021-07-26 16:12:59 +0200547 };
548
549
550 /**
551 * Add pagination panel
552 */
553 const paginationPanel = paginationPanelClass.create();
554
555 if (paginationPanel) {
556 paginationPanel.addRandomPage();
557 KorAP.Panel['pagination'] = paginationPanel;
558 };
Akron24f48ea2020-07-01 09:37:19 +0200559
Akrona9c55802021-06-15 11:41:29 +0200560
561 /**
562 * Initialize password toggle.
563 */
564 initCopyToClipboard(d);
565
566
Akron24f48ea2020-07-01 09:37:19 +0200567 /**
Akron116eace2021-06-14 18:02:37 +0200568 * Initialize password toggle.
569 */
Akron1cfde272021-06-14 18:32:39 +0200570 initTogglePwdVisibility(d);
Akron116eace2021-06-14 18:02:37 +0200571
572 /**
Akron24f48ea2020-07-01 09:37:19 +0200573 * Initialize Plugin registry.
574 */
Akron8dda1c62021-01-20 10:27:32 +0100575 let pe;
576 if (pe = d.getElementById("kalamar-plugins")) {
577 let url = pe.getAttribute('data-plugins');
578 if (url !== undefined) {
579 KorAP.API.getPluginList(url, function (json) {
580 if (json && json.length > 0) {
Akronda32e7a2021-11-16 17:28:57 +0100581
582 // Add state manager
Akron96b97d62023-11-07 15:56:54 +0100583 form = d.getElementById("searchform");
584 if (!form) {
585 return;
586 };
587
588 const input = form.addE("input");
Akroned223be2024-12-10 13:01:46 +0100589 input.setAttribute("type","text");
Akronda32e7a2021-11-16 17:28:57 +0100590 input.setAttribute("name","state");
Akroned223be2024-12-10 13:01:46 +0100591
592 const url = new URL(window.location.href);
593
594 // Access the query parameters to check for states
595 const state = new URLSearchParams(url.search).get('state');
596 if (state != null && state != "") {
597 input.setAttribute("value", state);
598 };
599
Akronda32e7a2021-11-16 17:28:57 +0100600 KorAP.States = stateManagerClass.create(input);
601
Akron8dda1c62021-01-20 10:27:32 +0100602 // Load Plugin Server first
603 KorAP.Plugin = pluginClass.create();
Akron24f48ea2020-07-01 09:37:19 +0200604
Akron8dda1c62021-01-20 10:27:32 +0100605 // Add services container to head
606 d.head.appendChild(KorAP.Plugin.element());
Akron24f48ea2020-07-01 09:37:19 +0200607
Akron8dda1c62021-01-20 10:27:32 +0100608 // Add pipe form
609 KorAP.Pipe = pipeClass.create();
610 d.getElementById("searchform").appendChild(KorAP.Pipe.element());
Akronda32e7a2021-11-16 17:28:57 +0100611
Akron8dda1c62021-01-20 10:27:32 +0100612 try {
613
614 // Register all plugins
615 json.forEach(i => KorAP.Plugin.register(i));
616 }
617 catch (e) {
618 KorAP.log(0, e);
619 }
620 }
621 });
622 };
Akron24f48ea2020-07-01 09:37:19 +0200623 };
Akronf7f75a92024-09-24 11:15:43 +0200624
625 window.dispatchEvent(new Event("ui-ready"));
Akron8dda1c62021-01-20 10:27:32 +0100626
Nils Diewald58141332015-04-07 16:18:45 +0000627 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000628 });
hebasta75cfca52019-02-19 13:15:27 +0100629
Nils Diewald0e6992a2015-04-14 20:13:52 +0000630});