blob: 58c6472571cf14db8c91f39e7c00a7699fd9b0fd [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 *
5 * @author Nils Diewald
6 *
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
16 */
17
Akrone51eaa32020-11-10 09:35:53 +010018"use strict";
Nils Diewald0e6992a2015-04-14 20:13:52 +000019define([
20 'match',
21 'hint',
22 'vc',
23 'tutorial',
24 'lib/domReady',
Akron27ae9ec2015-06-23 00:43:21 +020025 'vc/array',
Nils Diewald7148c6f2015-05-04 15:07:53 +000026 'lib/alertify',
Akron7716f012015-07-01 20:38:32 +020027 'session',
Akron6bb71582016-06-10 20:41:08 +020028 'selectMenu',
Akron5cb9b2b2018-07-24 17:01:09 +020029 'panel/result',
Akron2d0d96d2019-11-18 19:49:50 +010030 'panel/query',
hebasta75cfca52019-02-19 13:15:27 +010031 'tour/tours',
Akron24f48ea2020-07-01 09:37:19 +020032 'plugin/server',
33 'pipe',
Nils Diewald7148c6f2015-05-04 15:07:53 +000034 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000035 'mailToChiffre',
Akron858cbc82019-12-05 16:53:13 +010036 'util',
37 'state'
Nils Diewald0e6992a2015-04-14 20:13:52 +000038], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020039 hintClass,
40 vcClass,
41 tutClass,
42 domReady,
Akron19d97fe2016-09-06 20:47:05 +020043 vcArray,
44 alertifyClass,
45 sessionClass,
Akron4d926f12018-07-16 15:30:25 +020046 selectMenuClass,
hebasta75cfca52019-02-19 13:15:27 +010047 resultPanelClass,
Akron2d0d96d2019-11-18 19:49:50 +010048 queryPanelClass,
Akron24f48ea2020-07-01 09:37:19 +020049 tourClass,
50 pluginClass,
51 pipeClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000052
Akron0b489ad2018-02-02 16:49:32 +010053 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020054
Akron0c4cd222019-07-19 16:33:34 +020055 // Create suffix if KorAP is run in a subfolder
56 KorAP.session = sessionClass.create(
57 KorAP.URL.length > 0 ? 'kalamarJS-' + KorAP.URL.slugify() : 'kalamarJS'
58 );
Akronf8035592018-05-24 20:40:51 +020059
Nils Diewalda0defc42015-05-07 23:54:17 +000060 // Override KorAP.log
61 window.alertify = alertifyClass;
Akronc0a2da82018-07-04 15:27:37 +020062 KorAP.log = function (code, msg, src) {
63
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,
Nils Diewalda0defc42015-05-07 23:54:17 +000072 '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);
78
hebasta5df796f2019-05-21 15:27:12 +020079 KorAP.tourshow = function(){
80 tourClass.gTstartSearch().start();
81 };
82
83 KorAP.tourshowR = function(){
84 tourClass.gTshowResults().start();
85 };
86
Nils Diewald0e6992a2015-04-14 20:13:52 +000087 domReady(function (event) {
hebasta5df796f2019-05-21 15:27:12 +020088
Nils Diewald0e6992a2015-04-14 20:13:52 +000089 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020090
Akron4d926f12018-07-16 15:30:25 +020091 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020092 var show = KorAP.session.get('show') || {};
hebasta043e96f2019-11-28 12:33:00 +010093
94 KorAP.Panel = KorAP.Panel || {}
Nils Diewalda297f062015-04-02 00:23:46 +000095
96 /**
Akronf55504a2015-06-18 16:42:55 +020097 * Release notifications
98 */
99 if (KorAP.Notifications !== undefined) {
Akronb50964a2020-10-12 11:44:37 +0200100 KorAP.Notifications.forEach(function(n) {
101 var msg = n[1];
102 if (n[2]) {
103 msg += '<code class="src">'+n[2]+'</code>';
Akron8ea84292018-10-24 13:41:52 +0200104 };
Akronb50964a2020-10-12 11:44:37 +0200105 alertifyClass.log(msg, n[0], 10000);
106 });
Akronf55504a2015-06-18 16:42:55 +0200107 };
108
109 /**
Akroncd42a142019-07-12 18:55:37 +0200110 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000111 */
Akron5c829e92017-05-12 18:10:00 +0200112 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200113 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200114
hebasta2758b582018-11-19 15:59:42 +0100115 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100116
Akron1f0521b2018-08-28 13:01:24 +0200117 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000118 if (input) {
119 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100120 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000121 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200122 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200123
Akron1f0521b2018-08-28 13:01:24 +0200124 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200125 // Supports "collection" for legacy reasons
126 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200127 try {
Akroncd42a142019-07-12 18:55:37 +0200128 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200129 }
130 catch (e) {
131 KorAP.log(0,e);
132 }
Akron27ae9ec2015-06-23 00:43:21 +0200133 };
134
Akron0b489ad2018-02-02 16:49:32 +0100135 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200136 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200137
Akron1f0521b2018-08-28 13:01:24 +0200138 if (vc.wasRewritten()) {
139 vcchoose.classList.add('rewritten');
140 };
141
Nils Diewald7148c6f2015-05-04 15:07:53 +0000142 input.parentNode.insertBefore(vcname, input);
143 };
144
Nils Diewald7148c6f2015-05-04 15:07:53 +0000145 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000146 * Add actions to match entries
147 */
Akronb50964a2020-10-12 11:44:37 +0200148 var matchElements = d.querySelectorAll(
Akron3c390c42020-03-30 09:06:21 +0200149 '#search > ol > li'
Nils Diewald5c5a7472015-04-02 22:13:38 +0000150 );
Akron6a535d42015-08-26 20:16:58 +0200151
Akronb50964a2020-10-12 11:44:37 +0200152 matchElements.forEach(function(e) {
Akron3c390c42020-03-30 09:06:21 +0200153
154 // Define class for active elements
155 if (e.classList.contains('active')) {
156 if (this._match === undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200157 // lazyLoad
Akron3c390c42020-03-30 09:06:21 +0200158 matchClass.create(e).init();
Akron19d97fe2016-09-06 20:47:05 +0200159 };
Akron3c390c42020-03-30 09:06:21 +0200160 }
161
162 // Define class for inactive elements
163 else {
164 e.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200165 if (this._match !== undefined)
Akron3c390c42020-03-30 09:06:21 +0200166 this._match.open();
Akron19d97fe2016-09-06 20:47:05 +0200167 else {
168 // lazyLoad
169 matchClass.create(this).open();
170 };
Akron3c390c42020-03-30 09:06:21 +0200171 // This would prevent the sidebar to go back
172 // e.halt();
173 });
174 e.addEventListener('keydown', function (e) {
175 var code = _codeFromEvent(e);
176
177 switch (code) {
178 case 32:
179 if (this._match !== undefined)
180 this._match.toggle();
181 else {
182 // lazyLoad
183 matchClass.create(this).open();
184 };
185 e.halt();
186 break;
187 };
188 });
189 };
Akronb50964a2020-10-12 11:44:37 +0200190 }, this);
Akron3c390c42020-03-30 09:06:21 +0200191
Akrone0c32c72017-04-25 22:38:23 +0200192 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100193 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200194
195 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200196
Akron5258d462017-04-26 23:32:57 +0200197 // Horrible lock to deal with sidebar clicks
198 var asideClicked = false;
199
Akron1885ce92017-04-26 23:10:01 +0200200 // Make aside active on focus
201 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200202 this.classList.add('active');
203 });
204
Akron1885ce92017-04-26 23:10:01 +0200205 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100206 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200207 if (body !== null) {
208 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200209 if (!asideClicked) {
210 aside.classList.remove('active');
211 }
212 else {
213 asideClicked = false;
214 };
Akron1885ce92017-04-26 23:10:01 +0200215 });
216 };
217
218 /* Stop click event on aside
219 * (to not trickle down to body)
220 */
221 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200222 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200223 });
224 };
Akronb9cdb102017-04-25 00:52:31 +0200225
226
Akron6bb71582016-06-10 20:41:08 +0200227 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100228 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200229 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100230 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100231 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200232 ).limit(5);
233 };
Akron6bb71582016-06-10 20:41:08 +0200234
Akron4d926f12018-07-16 15:30:25 +0200235 var resultInfo = d.getElementById('resultinfo');
236
Akron4d926f12018-07-16 15:30:25 +0200237 /**
238 * Add result panel
239 */
Akron5cb9b2b2018-07-24 17:01:09 +0200240 var resultPanel = resultPanelClass.create(show);
241
Akron4d926f12018-07-16 15:30:25 +0200242 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200243
244 // Move buttons to resultinfo
245 resultInfo.appendChild(resultPanel.actions.element());
246
Akrone6538cd2018-07-16 17:52:33 +0200247 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200248 var sb = d.getElementById('search');
249 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200250 };
251
252
Akron179c8ac2015-06-30 19:30:50 +0200253 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200254 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200255
256 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200257 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200258 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200259 };
Akron7716f012015-07-01 20:38:32 +0200260
Akron00cd4d12016-05-31 21:01:11 +0200261 if (KorAP.koralQuery["errors"]) {
Akron678c26f2020-10-09 08:52:50 +0200262 KorAP.koralQuery["errors"].forEach(function(e) {
Akronf0c31ed2016-06-11 11:27:01 +0200263
Akron19d97fe2016-09-06 20:47:05 +0200264 // Malformed query
Akron4a24b722020-10-13 12:44:25 +0200265 if (e[0] === 302 && e[2] !== undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200266 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200267 obj.hint.alert(e[2], e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200268 }
Akronf0c31ed2016-06-11 11:27:01 +0200269
Akron19d97fe2016-09-06 20:47:05 +0200270 // no query
Akron678c26f2020-10-09 08:52:50 +0200271 else if (e[0] === 301) {
Akron19d97fe2016-09-06 20:47:05 +0200272 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200273 obj.hint.alert(0, e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200274 }
Akron678c26f2020-10-09 08:52:50 +0200275 });
Akron00cd4d12016-05-31 21:01:11 +0200276 };
Akron179c8ac2015-06-30 19:30:50 +0200277 };
278
Akron5cb9b2b2018-07-24 17:01:09 +0200279
280 /*
281 * There is more than 0 matches, so allow for
282 * alignment toggling (left <=> right)
283 */
Akronb50964a2020-10-12 11:44:37 +0200284 if (matchElements.length > 0)
Akron5cb9b2b2018-07-24 17:01:09 +0200285 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000286
hebasta043e96f2019-11-28 12:33:00 +0100287 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200288 /*
Akroncd42a142019-07-12 18:55:37 +0200289 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000290 */
291 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200292 vc.onMinimize = function () {
293 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200294 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200295 };
Nils Diewald6283d692015-04-23 20:32:53 +0000296
Akronec6bb8e2018-08-29 13:07:56 +0200297 vc.onOpen = function () {
298 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100299
300 var view = d.getElementById('vc-view');
301 if (!view.firstChild)
302 view.appendChild(this.element());
303
Akroncd42a142019-07-12 18:55:37 +0200304 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200305 };
306
307 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200308 if (vc.isOpen()) {
309 vc.minimize()
310 }
311 else {
Akronec6bb8e2018-08-29 13:07:56 +0200312 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200313 };
Nils Diewald58141332015-04-07 16:18:45 +0000314 };
Akron04671e72017-05-11 20:47:32 +0200315
Akron179c8ac2015-06-30 19:30:50 +0200316 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200317
318 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200319 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200320 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200321 };
Nils Diewald58141332015-04-07 16:18:45 +0000322 };
323
Akron19d97fe2016-09-06 20:47:05 +0200324
Nils Diewald58141332015-04-07 16:18:45 +0000325 /**
326 * Init Tutorial view
327 */
Akron0b489ad2018-02-02 16:49:32 +0100328 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000329 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100330 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200331 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000332 );
333 obj.tutorial = window.tutorial;
334 }
Nils Diewald58141332015-04-07 16:18:45 +0000335
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000336 // Tutorial is in parent
337 else if (window.parent) {
338 obj.tutorial = window.parent.tutorial;
339 };
340
Akron0b489ad2018-02-02 16:49:32 +0100341 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200342 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100343 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000344
Akron6ed13992016-05-23 18:06:05 +0200345 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100346 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200347 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000348
Nils Diewald845282c2015-05-14 07:53:03 +0000349
Nils Diewald58141332015-04-07 16:18:45 +0000350 /**
Akronc1457bf2015-06-11 19:24:00 +0200351 * Add VC creation on submission.
352 */
Akron0b489ad2018-02-02 16:49:32 +0100353 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200354 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200355 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100356 var qf = d.getElementById('q-field');
Akron1be6c1c2020-01-07 15:29:58 +0100357
Akron19d97fe2016-09-06 20:47:05 +0200358 // No query was defined
359 if (qf.value === undefined || qf.value === '') {
360 qf.focus();
361 e.halt();
362 KorAP.log(700, "No query given");
363 return;
364 };
Akron1be6c1c2020-01-07 15:29:58 +0100365
Akron19d97fe2016-09-06 20:47:05 +0200366 // Store session information
Akronf8035592018-05-24 20:40:51 +0200367 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200368
Akron19d97fe2016-09-06 20:47:05 +0200369 if (vc !== undefined) {
370 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100371 if (input.value == '')
372 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200373 }
374 else {
Akrond7ad9072019-12-09 07:08:20 +0100375 input.removeAttribute('value');
376 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200377 };
Akron1be6c1c2020-01-07 15:29:58 +0100378
379 // This would preferably set the query to be "disabled",
380 // but in that case the query wouldn't be submitted
381 // at all.
382 // Setting the cursor to "progress" fails in current versions
383 // of webkit.
384 qf.classList.add("loading");
385 d.getElementById('qsubmit').classList.add("loading");
Akronc1457bf2015-06-11 19:24:00 +0200386 });
387 };
hebasta5df796f2019-05-21 15:27:12 +0200388
389
390 //Starts the guided tour at the next page
391 if(KorAP.session.get("tour")){
392 tourClass.gTshowResults().start();
393 }
394
Akronc1457bf2015-06-11 19:24:00 +0200395 /**
Nils Diewald58141332015-04-07 16:18:45 +0000396 * Init hint helper
397 * has to be final because of
398 * reposition
399 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000400 // Todo: Pass an element, so this works with
401 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200402 if (obj.hint === undefined)
403 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000404
Akron99713ef2017-06-28 18:19:28 +0200405 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100406 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200407
Akron2d0d96d2019-11-18 19:49:50 +0100408
409 /**
410 * Add query panel
411 */
412 var queryPanel = queryPanelClass.create();
413
414 // Get input field
415 var sform = d.getElementById("searchform");
416 var vcView = d.getElementById('vc-view')
417 if (sform && vcView) {
418 // The views are below the query bar
419 sform.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100420 KorAP.Panel['query'] = queryPanel;
421 }
Akron24f48ea2020-07-01 09:37:19 +0200422
423 /**
424 * Initialize Plugin registry.
425 */
426 let p = KorAP.Plugins;
427 if (p && p.length > 0) {
428 // Load Plugin Server first
429 KorAP.Plugin = pluginClass.create();
430
431 // Add services container to head
432 d.head.appendChild(KorAP.Plugin.element());
433
434 // Add pipe form
435 KorAP.Pipe = pipeClass.create();
436 d.getElementById("searchform").appendChild(KorAP.Pipe.element());
437
438 try {
439
440 // Register all plugins
Akronb50964a2020-10-12 11:44:37 +0200441 p.forEach(i => KorAP.Plugin.register(i));
Akron24f48ea2020-07-01 09:37:19 +0200442 }
443 catch (e) {
444 KorAP.log(0, e);
445 }
446 };
447
Nils Diewald58141332015-04-07 16:18:45 +0000448 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000449 });
hebasta75cfca52019-02-19 13:15:27 +0100450
Nils Diewald0e6992a2015-04-14 20:13:52 +0000451});