blob: 7be0a136112239123a1313759c0164473888c190 [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();
Akronbe2f9a02025-01-14 09:36:55 +0100310 KorAP.Hint.alert().show();
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200311 }
312 }
313 }
314 });
315
316 observer.observe(document.body, { childList: true, subtree: true });
317
Akrone0c32c72017-04-25 22:38:23 +0200318 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100319 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200320
321 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200322
Akron5258d462017-04-26 23:32:57 +0200323 // Horrible lock to deal with sidebar clicks
324 var asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200325
326 shiftContent();
327
Akron1885ce92017-04-26 23:10:01 +0200328 // Make aside active on focus
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200329 aside.addEventListener('focus', function (e) {
Akrone0c32c72017-04-25 22:38:23 +0200330 this.classList.add('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200331 shiftContent();
Akrone0c32c72017-04-25 22:38:23 +0200332 });
333
Akron1885ce92017-04-26 23:10:01 +0200334 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100335 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200336 if (body !== null) {
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200337 body.addEventListener('click', function () {
Akron5258d462017-04-26 23:32:57 +0200338 if (!asideClicked) {
339 aside.classList.remove('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200340 shiftContent();
341 } else {
Akron5258d462017-04-26 23:32:57 +0200342 asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200343 }
Akron1885ce92017-04-26 23:10:01 +0200344 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200345 }
Akron1885ce92017-04-26 23:10:01 +0200346
347 /* Stop click event on aside
348 * (to not trickle down to body)
349 */
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200350 aside.addEventListener('click', function (e) {
Akron5258d462017-04-26 23:32:57 +0200351 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200352 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200353 }
Akronb9cdb102017-04-25 00:52:31 +0200354
Akron6bb71582016-06-10 20:41:08 +0200355 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100356 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200357 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100358 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100359 d.getElementById('ql-field').parentNode
Marc Kupietz95455822023-09-19 20:14:31 +0200360 ).limit(10);
Akronaba7a5a2016-08-15 21:58:33 +0200361 };
Akron6bb71582016-06-10 20:41:08 +0200362
Akron4d926f12018-07-16 15:30:25 +0200363 var resultInfo = d.getElementById('resultinfo');
364
Akron4d926f12018-07-16 15:30:25 +0200365 /**
366 * Add result panel
367 */
Akron5cb9b2b2018-07-24 17:01:09 +0200368 var resultPanel = resultPanelClass.create(show);
Akron644ad9f2021-07-26 16:12:59 +0200369
Akron4d926f12018-07-16 15:30:25 +0200370 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200371
372 // Move buttons to resultinfo
Akron37ea1192021-07-28 10:40:14 +0200373 resultInfo.appendChild(resultPanel.actions().element());
Akron4d926f12018-07-16 15:30:25 +0200374
Akrone6538cd2018-07-16 17:52:33 +0200375 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200376 var sb = d.getElementById('search');
377 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200378 };
379
380
Akron179c8ac2015-06-30 19:30:50 +0200381 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200382 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200383
384 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200385 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200386 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200387 };
Akron7716f012015-07-01 20:38:32 +0200388
Akron00cd4d12016-05-31 21:01:11 +0200389 if (KorAP.koralQuery["errors"]) {
Akron678c26f2020-10-09 08:52:50 +0200390 KorAP.koralQuery["errors"].forEach(function(e) {
Akronf0c31ed2016-06-11 11:27:01 +0200391
Akron19d97fe2016-09-06 20:47:05 +0200392 // Malformed query
Akron4a24b722020-10-13 12:44:25 +0200393 if (e[0] === 302 && e[2] !== undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200394 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200395 obj.hint.alert(e[2], e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200396 }
Akronf0c31ed2016-06-11 11:27:01 +0200397
Akron19d97fe2016-09-06 20:47:05 +0200398 // no query
Akron678c26f2020-10-09 08:52:50 +0200399 else if (e[0] === 301) {
Akron19d97fe2016-09-06 20:47:05 +0200400 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200401 obj.hint.alert(0, e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200402 }
Akron678c26f2020-10-09 08:52:50 +0200403 });
Akron00cd4d12016-05-31 21:01:11 +0200404 };
Akron179c8ac2015-06-30 19:30:50 +0200405 };
406
Akron5cb9b2b2018-07-24 17:01:09 +0200407
408 /*
409 * There is more than 0 matches, so allow for
410 * alignment toggling (left <=> right)
411 */
Akronb50964a2020-10-12 11:44:37 +0200412 if (matchElements.length > 0)
Akron5cb9b2b2018-07-24 17:01:09 +0200413 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000414
hebasta043e96f2019-11-28 12:33:00 +0100415 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200416 /*
Akroncd42a142019-07-12 18:55:37 +0200417 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000418 */
419 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200420 vc.onMinimize = function () {
421 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200422 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200423 };
Nils Diewald6283d692015-04-23 20:32:53 +0000424
Akronec6bb8e2018-08-29 13:07:56 +0200425 vc.onOpen = function () {
426 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100427
428 var view = d.getElementById('vc-view');
429 if (!view.firstChild)
430 view.appendChild(this.element());
431
Akroncd42a142019-07-12 18:55:37 +0200432 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200433 };
434
435 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200436 if (vc.isOpen()) {
437 vc.minimize()
438 }
439 else {
Akronec6bb8e2018-08-29 13:07:56 +0200440 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200441 };
Nils Diewald58141332015-04-07 16:18:45 +0000442 };
Akron04671e72017-05-11 20:47:32 +0200443
Akron179c8ac2015-06-30 19:30:50 +0200444 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200445
446 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200447 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200448 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200449 };
Nils Diewald58141332015-04-07 16:18:45 +0000450 };
451
Akron19d97fe2016-09-06 20:47:05 +0200452
Nils Diewald58141332015-04-07 16:18:45 +0000453 /**
454 * Init Tutorial view
455 */
Akron0b489ad2018-02-02 16:49:32 +0100456 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000457 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100458 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200459 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000460 );
461 obj.tutorial = window.tutorial;
462 }
Nils Diewald58141332015-04-07 16:18:45 +0000463
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000464 // Tutorial is in parent
465 else if (window.parent) {
466 obj.tutorial = window.parent.tutorial;
467 };
468
Akron0b489ad2018-02-02 16:49:32 +0100469 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200470 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100471 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000472
Akron6ed13992016-05-23 18:06:05 +0200473 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100474 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200475 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000476
Nils Diewald845282c2015-05-14 07:53:03 +0000477
Nils Diewald58141332015-04-07 16:18:45 +0000478 /**
Akronc1457bf2015-06-11 19:24:00 +0200479 * Add VC creation on submission.
480 */
Akron0b489ad2018-02-02 16:49:32 +0100481 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200482 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200483 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100484 var qf = d.getElementById('q-field');
Akron1be6c1c2020-01-07 15:29:58 +0100485
Akron19d97fe2016-09-06 20:47:05 +0200486 // No query was defined
487 if (qf.value === undefined || qf.value === '') {
488 qf.focus();
489 e.halt();
490 KorAP.log(700, "No query given");
491 return;
492 };
Akron1be6c1c2020-01-07 15:29:58 +0100493
Akron19d97fe2016-09-06 20:47:05 +0200494 // Store session information
Akronf8035592018-05-24 20:40:51 +0200495 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200496
Akron19d97fe2016-09-06 20:47:05 +0200497 if (vc !== undefined) {
498 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100499 if (input.value == '')
500 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200501 }
502 else {
Akrond7ad9072019-12-09 07:08:20 +0100503 input.removeAttribute('value');
504 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200505 };
Akron1be6c1c2020-01-07 15:29:58 +0100506
507 // This would preferably set the query to be "disabled",
508 // but in that case the query wouldn't be submitted
509 // at all.
510 // Setting the cursor to "progress" fails in current versions
511 // of webkit.
512 qf.classList.add("loading");
513 d.getElementById('qsubmit').classList.add("loading");
Akronc1457bf2015-06-11 19:24:00 +0200514 });
515 };
hebasta5df796f2019-05-21 15:27:12 +0200516
517
518 //Starts the guided tour at the next page
519 if(KorAP.session.get("tour")){
520 tourClass.gTshowResults().start();
521 }
522
Akronc1457bf2015-06-11 19:24:00 +0200523 /**
Nils Diewald58141332015-04-07 16:18:45 +0000524 * Init hint helper
525 * has to be final because of
526 * reposition
527 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000528 // Todo: Pass an element, so this works with
529 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200530 if (obj.hint === undefined)
531 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000532
Akron99713ef2017-06-28 18:19:28 +0200533 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100534 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200535
Akron2d0d96d2019-11-18 19:49:50 +0100536
537 /**
538 * Add query panel
539 */
540 var queryPanel = queryPanelClass.create();
541
542 // Get input field
Akron2d0d96d2019-11-18 19:49:50 +0100543 var vcView = d.getElementById('vc-view')
Akron96b97d62023-11-07 15:56:54 +0100544 if (form && vcView) {
Akron2d0d96d2019-11-18 19:49:50 +0100545 // The views are below the query bar
Akron96b97d62023-11-07 15:56:54 +0100546 form.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100547 KorAP.Panel['query'] = queryPanel;
Akron644ad9f2021-07-26 16:12:59 +0200548 };
549
550
551 /**
552 * Add pagination panel
553 */
554 const paginationPanel = paginationPanelClass.create();
555
556 if (paginationPanel) {
557 paginationPanel.addRandomPage();
558 KorAP.Panel['pagination'] = paginationPanel;
559 };
Akron24f48ea2020-07-01 09:37:19 +0200560
Akrona9c55802021-06-15 11:41:29 +0200561
562 /**
563 * Initialize password toggle.
564 */
565 initCopyToClipboard(d);
566
567
Akron24f48ea2020-07-01 09:37:19 +0200568 /**
Akron116eace2021-06-14 18:02:37 +0200569 * Initialize password toggle.
570 */
Akron1cfde272021-06-14 18:32:39 +0200571 initTogglePwdVisibility(d);
Akron116eace2021-06-14 18:02:37 +0200572
573 /**
Akron24f48ea2020-07-01 09:37:19 +0200574 * Initialize Plugin registry.
575 */
Akron8dda1c62021-01-20 10:27:32 +0100576 let pe;
577 if (pe = d.getElementById("kalamar-plugins")) {
578 let url = pe.getAttribute('data-plugins');
579 if (url !== undefined) {
580 KorAP.API.getPluginList(url, function (json) {
581 if (json && json.length > 0) {
Akronda32e7a2021-11-16 17:28:57 +0100582
583 // Add state manager
Akron96b97d62023-11-07 15:56:54 +0100584 form = d.getElementById("searchform");
585 if (!form) {
586 return;
587 };
588
589 const input = form.addE("input");
Akroned223be2024-12-10 13:01:46 +0100590 input.setAttribute("type","text");
Akronda32e7a2021-11-16 17:28:57 +0100591 input.setAttribute("name","state");
Akroned223be2024-12-10 13:01:46 +0100592
593 const url = new URL(window.location.href);
594
595 // Access the query parameters to check for states
596 const state = new URLSearchParams(url.search).get('state');
597 if (state != null && state != "") {
598 input.setAttribute("value", state);
599 };
600
Akronda32e7a2021-11-16 17:28:57 +0100601 KorAP.States = stateManagerClass.create(input);
602
Akron8dda1c62021-01-20 10:27:32 +0100603 // Load Plugin Server first
604 KorAP.Plugin = pluginClass.create();
Akron24f48ea2020-07-01 09:37:19 +0200605
Akron8dda1c62021-01-20 10:27:32 +0100606 // Add services container to head
607 d.head.appendChild(KorAP.Plugin.element());
Akron24f48ea2020-07-01 09:37:19 +0200608
Akron8dda1c62021-01-20 10:27:32 +0100609 // Add pipe form
610 KorAP.Pipe = pipeClass.create();
611 d.getElementById("searchform").appendChild(KorAP.Pipe.element());
Akronda32e7a2021-11-16 17:28:57 +0100612
Akron8dda1c62021-01-20 10:27:32 +0100613 try {
614
615 // Register all plugins
616 json.forEach(i => KorAP.Plugin.register(i));
617 }
618 catch (e) {
619 KorAP.log(0, e);
620 }
621 }
622 });
623 };
Akron24f48ea2020-07-01 09:37:19 +0200624 };
Akronf7f75a92024-09-24 11:15:43 +0200625
626 window.dispatchEvent(new Event("ui-ready"));
Akron8dda1c62021-01-20 10:27:32 +0100627
Nils Diewald58141332015-04-07 16:18:45 +0000628 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000629 });
hebasta75cfca52019-02-19 13:15:27 +0100630
Nils Diewald0e6992a2015-04-14 20:13:52 +0000631});