blob: b78e87005700cd943100efca4626160e3cda93e0 [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
Nils Diewald0e6992a2015-04-14 20:13:52 +000018define([
19 'match',
20 'hint',
21 'vc',
22 'tutorial',
23 'lib/domReady',
Akron27ae9ec2015-06-23 00:43:21 +020024 'vc/array',
Nils Diewald7148c6f2015-05-04 15:07:53 +000025 'lib/alertify',
Akron7716f012015-07-01 20:38:32 +020026 'session',
Akron6bb71582016-06-10 20:41:08 +020027 'selectMenu',
Akron5cb9b2b2018-07-24 17:01:09 +020028 'panel/result',
hebasta75cfca52019-02-19 13:15:27 +010029 'tour/tours',
Nils Diewald7148c6f2015-05-04 15:07:53 +000030 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000031 'mailToChiffre',
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 'util'
33], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020034 hintClass,
35 vcClass,
36 tutClass,
37 domReady,
Akron19d97fe2016-09-06 20:47:05 +020038 vcArray,
39 alertifyClass,
40 sessionClass,
Akron4d926f12018-07-16 15:30:25 +020041 selectMenuClass,
hebasta75cfca52019-02-19 13:15:27 +010042 resultPanelClass,
43 tourClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000044
Akron0b489ad2018-02-02 16:49:32 +010045 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020046
Akronf8035592018-05-24 20:40:51 +020047 KorAP.session = sessionClass.create('KalamarJS');
48
Nils Diewalda0defc42015-05-07 23:54:17 +000049 // Override KorAP.log
50 window.alertify = alertifyClass;
Akronc0a2da82018-07-04 15:27:37 +020051 KorAP.log = function (code, msg, src) {
52
53 if (src) {
54 msg += '<code class="src">'+src+'</code>';
55 };
Nils Diewalda0defc42015-05-07 23:54:17 +000056
57 // Use alertify to log errors
58 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020059 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020060 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000061 'error',
Akronf55504a2015-06-18 16:42:55 +020062 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000063 );
64 };
65
hebasta2758b582018-11-19 15:59:42 +010066 KorAP.vc = vcClass.create(vcArray);
67
Nils Diewald0e6992a2015-04-14 20:13:52 +000068 domReady(function (event) {
69 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020070
Akron4d926f12018-07-16 15:30:25 +020071 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020072 var show = KorAP.session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000073
74 /**
Akronf55504a2015-06-18 16:42:55 +020075 * Release notifications
76 */
77 if (KorAP.Notifications !== undefined) {
78 var n = KorAP.Notifications;
79 for (var i = 0; i < n.length; i++) {
Akron8ea84292018-10-24 13:41:52 +020080 var msg = n[i][1];
81 if (n[i][2]) {
82 msg += '<code class="src">'+n[i][2]+'</code>';
83 };
84 alertifyClass.log(msg, n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020085 };
86 };
87
88 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000089 * Replace Virtual Collection field
90 */
Akron5c829e92017-05-12 18:10:00 +020091 var vcname, vcchoose;
Akron0b489ad2018-02-02 16:49:32 +010092 var input = d.getElementById('collection');
Akron1f0521b2018-08-28 13:01:24 +020093
hebasta2758b582018-11-19 15:59:42 +010094 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +010095
Akron1f0521b2018-08-28 13:01:24 +020096 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +000097 if (input) {
98 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +010099 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000100 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200101 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200102
Akron1f0521b2018-08-28 13:01:24 +0200103 // Load virtual corpus object
Akron27ae9ec2015-06-23 00:43:21 +0200104 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron1f0521b2018-08-28 13:01:24 +0200105 try {
106 vc.fromJson(KorAP.koralQuery["collection"]);
107 }
108 catch (e) {
109 KorAP.log(0,e);
110 }
Akron27ae9ec2015-06-23 00:43:21 +0200111 };
112
Akron0b489ad2018-02-02 16:49:32 +0100113 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200114 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200115
Akron1f0521b2018-08-28 13:01:24 +0200116 if (vc.wasRewritten()) {
117 vcchoose.classList.add('rewritten');
118 };
119
Nils Diewald7148c6f2015-05-04 15:07:53 +0000120 input.parentNode.insertBefore(vcname, input);
121 };
122
Nils Diewald7148c6f2015-05-04 15:07:53 +0000123 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000124 * Add actions to match entries
125 */
Akron0b489ad2018-02-02 16:49:32 +0100126 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000127 '#search > ol > li:not(.active)'
128 );
Akron5cb9b2b2018-07-24 17:01:09 +0200129 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200130
Akron5cb9b2b2018-07-24 17:01:09 +0200131 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200132 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200133 if (this._match !== undefined)
134 this._match.open();
135 else {
136 // lazyLoad
137 matchClass.create(this).open();
138 };
Akrone0c32c72017-04-25 22:38:23 +0200139 // This would prevent the sidebar to go back
140 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000141 });
Akron5cb9b2b2018-07-24 17:01:09 +0200142 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200143 var code = _codeFromEvent(e);
144
145 switch (code) {
146 case 32:
147 if (this._match !== undefined)
148 this._match.toggle();
149 else {
150 // lazyLoad
151 matchClass.create(this).open();
152 };
153 e.halt();
154 break;
155 };
Akron6a535d42015-08-26 20:16:58 +0200156 });
Nils Diewalda297f062015-04-02 00:23:46 +0000157 };
158
Akrone0c32c72017-04-25 22:38:23 +0200159 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100160 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200161
162 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200163
Akron5258d462017-04-26 23:32:57 +0200164 // Horrible lock to deal with sidebar clicks
165 var asideClicked = false;
166
Akron1885ce92017-04-26 23:10:01 +0200167 // Make aside active on focus
168 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200169 this.classList.add('active');
170 });
171
Akron1885ce92017-04-26 23:10:01 +0200172 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100173 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200174 if (body !== null) {
175 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200176 if (!asideClicked) {
177 aside.classList.remove('active');
178 }
179 else {
180 asideClicked = false;
181 };
Akron1885ce92017-04-26 23:10:01 +0200182 });
183 };
184
185 /* Stop click event on aside
186 * (to not trickle down to body)
187 */
188 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200189 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200190 });
191 };
Akronb9cdb102017-04-25 00:52:31 +0200192
193
Akron6bb71582016-06-10 20:41:08 +0200194 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100195 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200196 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100197 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100198 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200199 ).limit(5);
200 };
Akron6bb71582016-06-10 20:41:08 +0200201
Akron4d926f12018-07-16 15:30:25 +0200202 var resultInfo = d.getElementById('resultinfo');
203
Akron4d926f12018-07-16 15:30:25 +0200204 /**
205 * Add result panel
206 */
Akron5cb9b2b2018-07-24 17:01:09 +0200207 var resultPanel = resultPanelClass.create(show);
208
Akron4d926f12018-07-16 15:30:25 +0200209 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200210
211 // Move buttons to resultinfo
212 resultInfo.appendChild(resultPanel.actions.element());
213
Akrone6538cd2018-07-16 17:52:33 +0200214 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200215 var sb = d.getElementById('search');
216 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200217 };
218
219
Akron179c8ac2015-06-30 19:30:50 +0200220 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200221 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200222
223 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200224 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200225 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200226 };
Akron7716f012015-07-01 20:38:32 +0200227
Akron00cd4d12016-05-31 21:01:11 +0200228 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200229 var errors = KorAP.koralQuery["errors"];
230 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200231
Akron19d97fe2016-09-06 20:47:05 +0200232 // Malformed query
233 if (errors[i][0] === 302 && errors[i][2]) {
234 obj.hint = hintClass.create();
235 obj.hint.alert(errors[i][2], errors[i][1]);
236 break;
237 }
Akronf0c31ed2016-06-11 11:27:01 +0200238
Akron19d97fe2016-09-06 20:47:05 +0200239 // no query
240 else if (errors[i][0] === 301) {
241 obj.hint = hintClass.create();
242 obj.hint.alert(0, errors[i][1]);
243 }
244 }
Akron00cd4d12016-05-31 21:01:11 +0200245 };
Akron179c8ac2015-06-30 19:30:50 +0200246 };
247
Akron5cb9b2b2018-07-24 17:01:09 +0200248
249 /*
250 * There is more than 0 matches, so allow for
251 * alignment toggling (left <=> right)
252 */
253 if (matchCount > 0)
254 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000255
Nils Diewald5c5a7472015-04-02 22:13:38 +0000256
Akron5cb9b2b2018-07-24 17:01:09 +0200257 /*
Nils Diewald7148c6f2015-05-04 15:07:53 +0000258 * Toggle the Virtual Collection builder
259 */
260 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200261 vc.onMinimize = function () {
262 vcname.classList.remove('active');
263 delete show['collection'];
264 };
Nils Diewald6283d692015-04-23 20:32:53 +0000265
Akronec6bb8e2018-08-29 13:07:56 +0200266 vc.onOpen = function () {
267 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100268
269 var view = d.getElementById('vc-view');
270 if (!view.firstChild)
271 view.appendChild(this.element());
272
Akronec6bb8e2018-08-29 13:07:56 +0200273 show['collection'] = true;
274 };
275
276 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200277 if (vc.isOpen()) {
278 vc.minimize()
279 }
280 else {
Akronec6bb8e2018-08-29 13:07:56 +0200281 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200282 };
Nils Diewald58141332015-04-07 16:18:45 +0000283 };
Akron04671e72017-05-11 20:47:32 +0200284
Akron179c8ac2015-06-30 19:30:50 +0200285 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200286
287 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200288 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200289 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200290 };
Nils Diewald58141332015-04-07 16:18:45 +0000291 };
292
Akron19d97fe2016-09-06 20:47:05 +0200293
Nils Diewald58141332015-04-07 16:18:45 +0000294 /**
295 * Init Tutorial view
296 */
Akron0b489ad2018-02-02 16:49:32 +0100297 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000298 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100299 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200300 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000301 );
302 obj.tutorial = window.tutorial;
303 }
Nils Diewald58141332015-04-07 16:18:45 +0000304
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000305 // Tutorial is in parent
306 else if (window.parent) {
307 obj.tutorial = window.parent.tutorial;
308 };
309
Akron0b489ad2018-02-02 16:49:32 +0100310 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200311 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100312 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000313
Akron6ed13992016-05-23 18:06:05 +0200314 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100315 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200316 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000317
Nils Diewald845282c2015-05-14 07:53:03 +0000318
Nils Diewald58141332015-04-07 16:18:45 +0000319 /**
Akronc1457bf2015-06-11 19:24:00 +0200320 * Add VC creation on submission.
321 */
Akron0b489ad2018-02-02 16:49:32 +0100322 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200323 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200324 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100325 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200326
Akron19d97fe2016-09-06 20:47:05 +0200327 // No query was defined
328 if (qf.value === undefined || qf.value === '') {
329 qf.focus();
330 e.halt();
331 KorAP.log(700, "No query given");
332 return;
333 };
Akron7716f012015-07-01 20:38:32 +0200334
Akron19d97fe2016-09-06 20:47:05 +0200335 // Store session information
Akronf8035592018-05-24 20:40:51 +0200336 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200337
Akron19d97fe2016-09-06 20:47:05 +0200338 if (vc !== undefined) {
339 input.value = vc.toQuery();
340 }
341 else {
342 delete input['value'];
343 };
Akronc1457bf2015-06-11 19:24:00 +0200344 });
345 };
346
347 /**
Nils Diewald58141332015-04-07 16:18:45 +0000348 * Init hint helper
349 * has to be final because of
350 * reposition
351 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000352 // Todo: Pass an element, so this works with
353 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200354 if (obj.hint === undefined)
355 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000356
Akron99713ef2017-06-28 18:19:28 +0200357 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100358 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200359
Nils Diewald58141332015-04-07 16:18:45 +0000360 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000361 });
hebasta75cfca52019-02-19 13:15:27 +0100362
363
364 tourshow = function(){
365 let tour = tourClass.guidedTour();
366 tour.start();
367 };
368
369
Nils Diewald0e6992a2015-04-14 20:13:52 +0000370});