blob: a668b77102c60ac08e6455c5585423098454da6d [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');
274 const hint = document.querySelector('#hint');
275 const results = document.querySelector('.found');
276 const aside = document.querySelector('aside');
277
278 if (aside && aside.classList.contains('active')) {
279 header.classList.add('shifted');
280 if (!results) {
281 main.classList.add('shifted');
282 }
283 footer.classList.add('shifted');
284 if (hint) {
285 hint.classList.add('shifted');
286 adjustHintPosition();
287 }
288 } else {
289 header.classList.remove('shifted');
290 main.classList.remove('shifted');
291 footer.classList.remove('shifted');
292 if (hint) {
293 hint.classList.remove('shifted');
294 adjustHintPosition();
295 }
296 }
297 }
298
299 // Function to adjust the position of the annotation assistant bar (hint), when user types into the searchbar and clicks the sidebar (or anywhere outside the searchbar) afterwards
300 function adjustHintPosition() {
301 const hint = document.querySelector('#hint');
302 const searchInput = document.querySelector('#q-field');
303 const aside = document.querySelector('aside');
304
305 if (hint && searchInput) {
306 // Create a temporary span to measure the exact text width
307 const span = document.createElement('span');
308 span.style.visibility = 'hidden';
309 span.style.position = 'absolute';
310 span.style.whiteSpace = 'pre';
311 // Copy the input's font properties
312 const inputStyle = window.getComputedStyle(searchInput);
313 span.style.font = inputStyle.font;
314 span.style.fontSize = inputStyle.fontSize;
315 span.style.fontFamily = inputStyle.fontFamily;
316 span.textContent = searchInput.value;
317 document.body.appendChild(span);
318
319 // Get the actual width of the text
320 const inputWidth = searchInput.value.length > 0 ? span.offsetWidth : 0;
321 document.body.removeChild(span);
322 let hintLeftPosition = inputWidth;
323
324 // TODO: This shouldn't be a fixed position!
325 if (aside && aside.classList.contains('active')) {
326 hintLeftPosition += 230;
327 }
328
329 hint.style.left = `${hintLeftPosition}px`;
330 }
331 }
332 //Can be solved more elegant witch ES6 (see optional chaining operator)
333 let qlf = document.querySelector('#q-field');
334 if(qlf != null){
335 qlf.addEventListener('blur', adjustHintPosition);
336 }
337
338 // MutationObserver to detect when #hint is injected into the DOM
339 const observer = new MutationObserver((mutationsList, observer) => {
340 for (const mutation of mutationsList) {
341 if (mutation.type === 'childList') {
342 const hint = document.querySelector('#hint');
343 if (hint) {
344 shiftContent();
345 observer.disconnect();
346 }
347 }
348 }
349 });
350
351 observer.observe(document.body, { childList: true, subtree: true });
352
Akrone0c32c72017-04-25 22:38:23 +0200353 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100354 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200355
356 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200357
Akron5258d462017-04-26 23:32:57 +0200358 // Horrible lock to deal with sidebar clicks
359 var asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200360
361 shiftContent();
362
Akron1885ce92017-04-26 23:10:01 +0200363 // Make aside active on focus
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200364 aside.addEventListener('focus', function (e) {
Akrone0c32c72017-04-25 22:38:23 +0200365 this.classList.add('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200366 shiftContent();
Akrone0c32c72017-04-25 22:38:23 +0200367 });
368
Akron1885ce92017-04-26 23:10:01 +0200369 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100370 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200371 if (body !== null) {
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200372 body.addEventListener('click', function () {
Akron5258d462017-04-26 23:32:57 +0200373 if (!asideClicked) {
374 aside.classList.remove('active');
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200375 shiftContent();
376 } else {
Akron5258d462017-04-26 23:32:57 +0200377 asideClicked = false;
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200378 }
Akron1885ce92017-04-26 23:10:01 +0200379 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200380 }
Akron1885ce92017-04-26 23:10:01 +0200381
382 /* Stop click event on aside
383 * (to not trickle down to body)
384 */
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200385 aside.addEventListener('click', function (e) {
Akron5258d462017-04-26 23:32:57 +0200386 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200387 });
Uyen-Nhu Tran243fe732024-04-10 01:17:24 +0200388 }
Akronb9cdb102017-04-25 00:52:31 +0200389
Akron6bb71582016-06-10 20:41:08 +0200390 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100391 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200392 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100393 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100394 d.getElementById('ql-field').parentNode
Marc Kupietz95455822023-09-19 20:14:31 +0200395 ).limit(10);
Akronaba7a5a2016-08-15 21:58:33 +0200396 };
Akron6bb71582016-06-10 20:41:08 +0200397
Akron4d926f12018-07-16 15:30:25 +0200398 var resultInfo = d.getElementById('resultinfo');
399
Akron4d926f12018-07-16 15:30:25 +0200400 /**
401 * Add result panel
402 */
Akron5cb9b2b2018-07-24 17:01:09 +0200403 var resultPanel = resultPanelClass.create(show);
Akron644ad9f2021-07-26 16:12:59 +0200404
Akron4d926f12018-07-16 15:30:25 +0200405 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200406
407 // Move buttons to resultinfo
Akron37ea1192021-07-28 10:40:14 +0200408 resultInfo.appendChild(resultPanel.actions().element());
Akron4d926f12018-07-16 15:30:25 +0200409
Akrone6538cd2018-07-16 17:52:33 +0200410 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200411 var sb = d.getElementById('search');
412 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200413 };
414
415
Akron179c8ac2015-06-30 19:30:50 +0200416 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200417 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200418
419 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200420 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200421 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200422 };
Akron7716f012015-07-01 20:38:32 +0200423
Akron00cd4d12016-05-31 21:01:11 +0200424 if (KorAP.koralQuery["errors"]) {
Akron678c26f2020-10-09 08:52:50 +0200425 KorAP.koralQuery["errors"].forEach(function(e) {
Akronf0c31ed2016-06-11 11:27:01 +0200426
Akron19d97fe2016-09-06 20:47:05 +0200427 // Malformed query
Akron4a24b722020-10-13 12:44:25 +0200428 if (e[0] === 302 && e[2] !== undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200429 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200430 obj.hint.alert(e[2], e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200431 }
Akronf0c31ed2016-06-11 11:27:01 +0200432
Akron19d97fe2016-09-06 20:47:05 +0200433 // no query
Akron678c26f2020-10-09 08:52:50 +0200434 else if (e[0] === 301) {
Akron19d97fe2016-09-06 20:47:05 +0200435 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200436 obj.hint.alert(0, e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200437 }
Akron678c26f2020-10-09 08:52:50 +0200438 });
Akron00cd4d12016-05-31 21:01:11 +0200439 };
Akron179c8ac2015-06-30 19:30:50 +0200440 };
441
Akron5cb9b2b2018-07-24 17:01:09 +0200442
443 /*
444 * There is more than 0 matches, so allow for
445 * alignment toggling (left <=> right)
446 */
Akronb50964a2020-10-12 11:44:37 +0200447 if (matchElements.length > 0)
Akron5cb9b2b2018-07-24 17:01:09 +0200448 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000449
hebasta043e96f2019-11-28 12:33:00 +0100450 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200451 /*
Akroncd42a142019-07-12 18:55:37 +0200452 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000453 */
454 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200455 vc.onMinimize = function () {
456 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200457 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200458 };
Nils Diewald6283d692015-04-23 20:32:53 +0000459
Akronec6bb8e2018-08-29 13:07:56 +0200460 vc.onOpen = function () {
461 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100462
463 var view = d.getElementById('vc-view');
464 if (!view.firstChild)
465 view.appendChild(this.element());
466
Akroncd42a142019-07-12 18:55:37 +0200467 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200468 };
469
470 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200471 if (vc.isOpen()) {
472 vc.minimize()
473 }
474 else {
Akronec6bb8e2018-08-29 13:07:56 +0200475 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200476 };
Nils Diewald58141332015-04-07 16:18:45 +0000477 };
Akron04671e72017-05-11 20:47:32 +0200478
Akron179c8ac2015-06-30 19:30:50 +0200479 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200480
481 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200482 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200483 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200484 };
Nils Diewald58141332015-04-07 16:18:45 +0000485 };
486
Akron19d97fe2016-09-06 20:47:05 +0200487
Nils Diewald58141332015-04-07 16:18:45 +0000488 /**
489 * Init Tutorial view
490 */
Akron0b489ad2018-02-02 16:49:32 +0100491 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000492 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100493 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200494 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000495 );
496 obj.tutorial = window.tutorial;
497 }
Nils Diewald58141332015-04-07 16:18:45 +0000498
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000499 // Tutorial is in parent
500 else if (window.parent) {
501 obj.tutorial = window.parent.tutorial;
502 };
503
Akron0b489ad2018-02-02 16:49:32 +0100504 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200505 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100506 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000507
Akron6ed13992016-05-23 18:06:05 +0200508 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100509 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200510 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000511
Nils Diewald845282c2015-05-14 07:53:03 +0000512
Nils Diewald58141332015-04-07 16:18:45 +0000513 /**
Akronc1457bf2015-06-11 19:24:00 +0200514 * Add VC creation on submission.
515 */
Akron0b489ad2018-02-02 16:49:32 +0100516 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200517 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200518 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100519 var qf = d.getElementById('q-field');
Akron1be6c1c2020-01-07 15:29:58 +0100520
Akron19d97fe2016-09-06 20:47:05 +0200521 // No query was defined
522 if (qf.value === undefined || qf.value === '') {
523 qf.focus();
524 e.halt();
525 KorAP.log(700, "No query given");
526 return;
527 };
Akron1be6c1c2020-01-07 15:29:58 +0100528
Akron19d97fe2016-09-06 20:47:05 +0200529 // Store session information
Akronf8035592018-05-24 20:40:51 +0200530 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200531
Akron19d97fe2016-09-06 20:47:05 +0200532 if (vc !== undefined) {
533 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100534 if (input.value == '')
535 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200536 }
537 else {
Akrond7ad9072019-12-09 07:08:20 +0100538 input.removeAttribute('value');
539 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200540 };
Akron1be6c1c2020-01-07 15:29:58 +0100541
542 // This would preferably set the query to be "disabled",
543 // but in that case the query wouldn't be submitted
544 // at all.
545 // Setting the cursor to "progress" fails in current versions
546 // of webkit.
547 qf.classList.add("loading");
548 d.getElementById('qsubmit').classList.add("loading");
Akronc1457bf2015-06-11 19:24:00 +0200549 });
550 };
hebasta5df796f2019-05-21 15:27:12 +0200551
552
553 //Starts the guided tour at the next page
554 if(KorAP.session.get("tour")){
555 tourClass.gTshowResults().start();
556 }
557
Akronc1457bf2015-06-11 19:24:00 +0200558 /**
Nils Diewald58141332015-04-07 16:18:45 +0000559 * Init hint helper
560 * has to be final because of
561 * reposition
562 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000563 // Todo: Pass an element, so this works with
564 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200565 if (obj.hint === undefined)
566 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000567
Akron99713ef2017-06-28 18:19:28 +0200568 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100569 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200570
Akron2d0d96d2019-11-18 19:49:50 +0100571
572 /**
573 * Add query panel
574 */
575 var queryPanel = queryPanelClass.create();
576
577 // Get input field
Akron2d0d96d2019-11-18 19:49:50 +0100578 var vcView = d.getElementById('vc-view')
Akron96b97d62023-11-07 15:56:54 +0100579 if (form && vcView) {
Akron2d0d96d2019-11-18 19:49:50 +0100580 // The views are below the query bar
Akron96b97d62023-11-07 15:56:54 +0100581 form.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100582 KorAP.Panel['query'] = queryPanel;
Akron644ad9f2021-07-26 16:12:59 +0200583 };
584
585
586 /**
587 * Add pagination panel
588 */
589 const paginationPanel = paginationPanelClass.create();
590
591 if (paginationPanel) {
592 paginationPanel.addRandomPage();
593 KorAP.Panel['pagination'] = paginationPanel;
594 };
Akron24f48ea2020-07-01 09:37:19 +0200595
Akrona9c55802021-06-15 11:41:29 +0200596
597 /**
598 * Initialize password toggle.
599 */
600 initCopyToClipboard(d);
601
602
Akron24f48ea2020-07-01 09:37:19 +0200603 /**
Akron116eace2021-06-14 18:02:37 +0200604 * Initialize password toggle.
605 */
Akron1cfde272021-06-14 18:32:39 +0200606 initTogglePwdVisibility(d);
Akron116eace2021-06-14 18:02:37 +0200607
608 /**
Akron24f48ea2020-07-01 09:37:19 +0200609 * Initialize Plugin registry.
610 */
Akron8dda1c62021-01-20 10:27:32 +0100611 let pe;
612 if (pe = d.getElementById("kalamar-plugins")) {
613 let url = pe.getAttribute('data-plugins');
614 if (url !== undefined) {
615 KorAP.API.getPluginList(url, function (json) {
616 if (json && json.length > 0) {
Akronda32e7a2021-11-16 17:28:57 +0100617
618 // Add state manager
Akron96b97d62023-11-07 15:56:54 +0100619 form = d.getElementById("searchform");
620 if (!form) {
621 return;
622 };
623
624 const input = form.addE("input");
Akronda32e7a2021-11-16 17:28:57 +0100625 input.setAttribute("name","state");
626 KorAP.States = stateManagerClass.create(input);
627
Akron8dda1c62021-01-20 10:27:32 +0100628 // Load Plugin Server first
629 KorAP.Plugin = pluginClass.create();
Akron24f48ea2020-07-01 09:37:19 +0200630
Akron8dda1c62021-01-20 10:27:32 +0100631 // Add services container to head
632 d.head.appendChild(KorAP.Plugin.element());
Akron24f48ea2020-07-01 09:37:19 +0200633
Akron8dda1c62021-01-20 10:27:32 +0100634 // Add pipe form
635 KorAP.Pipe = pipeClass.create();
636 d.getElementById("searchform").appendChild(KorAP.Pipe.element());
Akronda32e7a2021-11-16 17:28:57 +0100637
Akron8dda1c62021-01-20 10:27:32 +0100638 try {
639
640 // Register all plugins
641 json.forEach(i => KorAP.Plugin.register(i));
642 }
643 catch (e) {
644 KorAP.log(0, e);
645 }
646 }
647 });
648 };
Akron24f48ea2020-07-01 09:37:19 +0200649 };
Akronf7f75a92024-09-24 11:15:43 +0200650
651 window.dispatchEvent(new Event("ui-ready"));
Akron8dda1c62021-01-20 10:27:32 +0100652
Nils Diewald58141332015-04-07 16:18:45 +0000653 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000654 });
hebasta75cfca52019-02-19 13:15:27 +0100655
Nils Diewald0e6992a2015-04-14 20:13:52 +0000656});