blob: ec11cf5adb708c5e74135024deda2e1c92a67180 [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
hebasta5df796f2019-05-21 15:27:12 +020068 KorAP.tourshow = function(){
69 tourClass.gTstartSearch().start();
70 };
71
72 KorAP.tourshowR = function(){
73 tourClass.gTshowResults().start();
74 };
75
Nils Diewald0e6992a2015-04-14 20:13:52 +000076 domReady(function (event) {
hebasta5df796f2019-05-21 15:27:12 +020077
Nils Diewald0e6992a2015-04-14 20:13:52 +000078 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020079
Akron4d926f12018-07-16 15:30:25 +020080 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020081 var show = KorAP.session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000082
83 /**
Akronf55504a2015-06-18 16:42:55 +020084 * Release notifications
85 */
86 if (KorAP.Notifications !== undefined) {
87 var n = KorAP.Notifications;
88 for (var i = 0; i < n.length; i++) {
Akron8ea84292018-10-24 13:41:52 +020089 var msg = n[i][1];
90 if (n[i][2]) {
91 msg += '<code class="src">'+n[i][2]+'</code>';
92 };
93 alertifyClass.log(msg, n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020094 };
95 };
96
97 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000098 * Replace Virtual Collection field
99 */
Akron5c829e92017-05-12 18:10:00 +0200100 var vcname, vcchoose;
Akron0b489ad2018-02-02 16:49:32 +0100101 var input = d.getElementById('collection');
Akron1f0521b2018-08-28 13:01:24 +0200102
hebasta2758b582018-11-19 15:59:42 +0100103 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100104
Akron1f0521b2018-08-28 13:01:24 +0200105 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000106 if (input) {
107 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100108 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000109 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200110 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200111
Akron1f0521b2018-08-28 13:01:24 +0200112 // Load virtual corpus object
Akron27ae9ec2015-06-23 00:43:21 +0200113 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron1f0521b2018-08-28 13:01:24 +0200114 try {
115 vc.fromJson(KorAP.koralQuery["collection"]);
116 }
117 catch (e) {
118 KorAP.log(0,e);
119 }
Akron27ae9ec2015-06-23 00:43:21 +0200120 };
121
Akron0b489ad2018-02-02 16:49:32 +0100122 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200123 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200124
Akron1f0521b2018-08-28 13:01:24 +0200125 if (vc.wasRewritten()) {
126 vcchoose.classList.add('rewritten');
127 };
128
Nils Diewald7148c6f2015-05-04 15:07:53 +0000129 input.parentNode.insertBefore(vcname, input);
130 };
131
Nils Diewald7148c6f2015-05-04 15:07:53 +0000132 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000133 * Add actions to match entries
134 */
Akron0b489ad2018-02-02 16:49:32 +0100135 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000136 '#search > ol > li:not(.active)'
137 );
Akron5cb9b2b2018-07-24 17:01:09 +0200138 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200139
Akron5cb9b2b2018-07-24 17:01:09 +0200140 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200141 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200142 if (this._match !== undefined)
143 this._match.open();
144 else {
145 // lazyLoad
146 matchClass.create(this).open();
147 };
Akrone0c32c72017-04-25 22:38:23 +0200148 // This would prevent the sidebar to go back
149 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000150 });
Akron5cb9b2b2018-07-24 17:01:09 +0200151 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200152 var code = _codeFromEvent(e);
153
154 switch (code) {
155 case 32:
156 if (this._match !== undefined)
157 this._match.toggle();
158 else {
159 // lazyLoad
160 matchClass.create(this).open();
161 };
162 e.halt();
163 break;
164 };
Akron6a535d42015-08-26 20:16:58 +0200165 });
Nils Diewalda297f062015-04-02 00:23:46 +0000166 };
167
Akrone0c32c72017-04-25 22:38:23 +0200168 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100169 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200170
171 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200172
Akron5258d462017-04-26 23:32:57 +0200173 // Horrible lock to deal with sidebar clicks
174 var asideClicked = false;
175
Akron1885ce92017-04-26 23:10:01 +0200176 // Make aside active on focus
177 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200178 this.classList.add('active');
179 });
180
Akron1885ce92017-04-26 23:10:01 +0200181 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100182 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200183 if (body !== null) {
184 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200185 if (!asideClicked) {
186 aside.classList.remove('active');
187 }
188 else {
189 asideClicked = false;
190 };
Akron1885ce92017-04-26 23:10:01 +0200191 });
192 };
193
194 /* Stop click event on aside
195 * (to not trickle down to body)
196 */
197 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200198 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200199 });
200 };
Akronb9cdb102017-04-25 00:52:31 +0200201
202
Akron6bb71582016-06-10 20:41:08 +0200203 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100204 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200205 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100206 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100207 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200208 ).limit(5);
209 };
Akron6bb71582016-06-10 20:41:08 +0200210
Akron4d926f12018-07-16 15:30:25 +0200211 var resultInfo = d.getElementById('resultinfo');
212
Akron4d926f12018-07-16 15:30:25 +0200213 /**
214 * Add result panel
215 */
Akron5cb9b2b2018-07-24 17:01:09 +0200216 var resultPanel = resultPanelClass.create(show);
217
Akron4d926f12018-07-16 15:30:25 +0200218 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200219
220 // Move buttons to resultinfo
221 resultInfo.appendChild(resultPanel.actions.element());
222
Akrone6538cd2018-07-16 17:52:33 +0200223 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200224 var sb = d.getElementById('search');
225 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200226 };
227
228
Akron179c8ac2015-06-30 19:30:50 +0200229 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200230 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200231
232 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200233 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200234 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200235 };
Akron7716f012015-07-01 20:38:32 +0200236
Akron00cd4d12016-05-31 21:01:11 +0200237 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200238 var errors = KorAP.koralQuery["errors"];
239 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200240
Akron19d97fe2016-09-06 20:47:05 +0200241 // Malformed query
242 if (errors[i][0] === 302 && errors[i][2]) {
243 obj.hint = hintClass.create();
244 obj.hint.alert(errors[i][2], errors[i][1]);
245 break;
246 }
Akronf0c31ed2016-06-11 11:27:01 +0200247
Akron19d97fe2016-09-06 20:47:05 +0200248 // no query
249 else if (errors[i][0] === 301) {
250 obj.hint = hintClass.create();
251 obj.hint.alert(0, errors[i][1]);
252 }
253 }
Akron00cd4d12016-05-31 21:01:11 +0200254 };
Akron179c8ac2015-06-30 19:30:50 +0200255 };
256
Akron5cb9b2b2018-07-24 17:01:09 +0200257
258 /*
259 * There is more than 0 matches, so allow for
260 * alignment toggling (left <=> right)
261 */
262 if (matchCount > 0)
263 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000264
Nils Diewald5c5a7472015-04-02 22:13:38 +0000265
Akron5cb9b2b2018-07-24 17:01:09 +0200266 /*
Nils Diewald7148c6f2015-05-04 15:07:53 +0000267 * Toggle the Virtual Collection builder
268 */
269 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200270 vc.onMinimize = function () {
271 vcname.classList.remove('active');
272 delete show['collection'];
273 };
Nils Diewald6283d692015-04-23 20:32:53 +0000274
Akronec6bb8e2018-08-29 13:07:56 +0200275 vc.onOpen = function () {
276 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100277
278 var view = d.getElementById('vc-view');
279 if (!view.firstChild)
280 view.appendChild(this.element());
281
Akronec6bb8e2018-08-29 13:07:56 +0200282 show['collection'] = true;
283 };
284
285 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200286 if (vc.isOpen()) {
287 vc.minimize()
288 }
289 else {
Akronec6bb8e2018-08-29 13:07:56 +0200290 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200291 };
Nils Diewald58141332015-04-07 16:18:45 +0000292 };
Akron04671e72017-05-11 20:47:32 +0200293
Akron179c8ac2015-06-30 19:30:50 +0200294 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200295
296 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200297 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200298 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200299 };
Nils Diewald58141332015-04-07 16:18:45 +0000300 };
301
Akron19d97fe2016-09-06 20:47:05 +0200302
Nils Diewald58141332015-04-07 16:18:45 +0000303 /**
304 * Init Tutorial view
305 */
Akron0b489ad2018-02-02 16:49:32 +0100306 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000307 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100308 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200309 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000310 );
311 obj.tutorial = window.tutorial;
312 }
Nils Diewald58141332015-04-07 16:18:45 +0000313
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000314 // Tutorial is in parent
315 else if (window.parent) {
316 obj.tutorial = window.parent.tutorial;
317 };
318
Akron0b489ad2018-02-02 16:49:32 +0100319 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200320 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100321 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000322
Akron6ed13992016-05-23 18:06:05 +0200323 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100324 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200325 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000326
Nils Diewald845282c2015-05-14 07:53:03 +0000327
Nils Diewald58141332015-04-07 16:18:45 +0000328 /**
Akronc1457bf2015-06-11 19:24:00 +0200329 * Add VC creation on submission.
330 */
Akron0b489ad2018-02-02 16:49:32 +0100331 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200332 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200333 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100334 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200335
Akron19d97fe2016-09-06 20:47:05 +0200336 // No query was defined
337 if (qf.value === undefined || qf.value === '') {
338 qf.focus();
339 e.halt();
340 KorAP.log(700, "No query given");
341 return;
342 };
Akron7716f012015-07-01 20:38:32 +0200343
Akron19d97fe2016-09-06 20:47:05 +0200344 // Store session information
Akronf8035592018-05-24 20:40:51 +0200345 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200346
Akron19d97fe2016-09-06 20:47:05 +0200347 if (vc !== undefined) {
348 input.value = vc.toQuery();
349 }
350 else {
351 delete input['value'];
352 };
Akronc1457bf2015-06-11 19:24:00 +0200353 });
354 };
hebasta5df796f2019-05-21 15:27:12 +0200355
356
357 //Starts the guided tour at the next page
358 if(KorAP.session.get("tour")){
359 tourClass.gTshowResults().start();
360 }
361
Akronc1457bf2015-06-11 19:24:00 +0200362 /**
Nils Diewald58141332015-04-07 16:18:45 +0000363 * Init hint helper
364 * has to be final because of
365 * reposition
366 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000367 // Todo: Pass an element, so this works with
368 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200369 if (obj.hint === undefined)
370 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000371
Akron99713ef2017-06-28 18:19:28 +0200372 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100373 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200374
Nils Diewald58141332015-04-07 16:18:45 +0000375 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000376 });
hebasta75cfca52019-02-19 13:15:27 +0100377
Nils Diewald0e6992a2015-04-14 20:13:52 +0000378});