blob: 6de66d8d0aaa9d978d5abeb7693ece9783a6a18a [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 /**
Akroncd42a142019-07-12 18:55:37 +020098 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +000099 */
Akron5c829e92017-05-12 18:10:00 +0200100 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200101 var input = d.getElementById('cq');
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
Akroncd42a142019-07-12 18:55:37 +0200113 // Supports "collection" for legacy reasons
114 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200115 try {
Akroncd42a142019-07-12 18:55:37 +0200116 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200117 }
118 catch (e) {
119 KorAP.log(0,e);
120 }
Akron27ae9ec2015-06-23 00:43:21 +0200121 };
122
Akron0b489ad2018-02-02 16:49:32 +0100123 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200124 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200125
Akron1f0521b2018-08-28 13:01:24 +0200126 if (vc.wasRewritten()) {
127 vcchoose.classList.add('rewritten');
128 };
129
Nils Diewald7148c6f2015-05-04 15:07:53 +0000130 input.parentNode.insertBefore(vcname, input);
131 };
132
Nils Diewald7148c6f2015-05-04 15:07:53 +0000133 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000134 * Add actions to match entries
135 */
Akron0b489ad2018-02-02 16:49:32 +0100136 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000137 '#search > ol > li:not(.active)'
138 );
Akron5cb9b2b2018-07-24 17:01:09 +0200139 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200140
Akron5cb9b2b2018-07-24 17:01:09 +0200141 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200142 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200143 if (this._match !== undefined)
144 this._match.open();
145 else {
146 // lazyLoad
147 matchClass.create(this).open();
148 };
Akrone0c32c72017-04-25 22:38:23 +0200149 // This would prevent the sidebar to go back
150 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000151 });
Akron5cb9b2b2018-07-24 17:01:09 +0200152 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200153 var code = _codeFromEvent(e);
154
155 switch (code) {
156 case 32:
157 if (this._match !== undefined)
158 this._match.toggle();
159 else {
160 // lazyLoad
161 matchClass.create(this).open();
162 };
163 e.halt();
164 break;
165 };
Akron6a535d42015-08-26 20:16:58 +0200166 });
Nils Diewalda297f062015-04-02 00:23:46 +0000167 };
168
Akrone0c32c72017-04-25 22:38:23 +0200169 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100170 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200171
172 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200173
Akron5258d462017-04-26 23:32:57 +0200174 // Horrible lock to deal with sidebar clicks
175 var asideClicked = false;
176
Akron1885ce92017-04-26 23:10:01 +0200177 // Make aside active on focus
178 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200179 this.classList.add('active');
180 });
181
Akron1885ce92017-04-26 23:10:01 +0200182 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100183 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200184 if (body !== null) {
185 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200186 if (!asideClicked) {
187 aside.classList.remove('active');
188 }
189 else {
190 asideClicked = false;
191 };
Akron1885ce92017-04-26 23:10:01 +0200192 });
193 };
194
195 /* Stop click event on aside
196 * (to not trickle down to body)
197 */
198 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200199 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200200 });
201 };
Akronb9cdb102017-04-25 00:52:31 +0200202
203
Akron6bb71582016-06-10 20:41:08 +0200204 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100205 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200206 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100207 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100208 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200209 ).limit(5);
210 };
Akron6bb71582016-06-10 20:41:08 +0200211
Akron4d926f12018-07-16 15:30:25 +0200212 var resultInfo = d.getElementById('resultinfo');
213
Akron4d926f12018-07-16 15:30:25 +0200214 /**
215 * Add result panel
216 */
Akron5cb9b2b2018-07-24 17:01:09 +0200217 var resultPanel = resultPanelClass.create(show);
218
Akron4d926f12018-07-16 15:30:25 +0200219 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200220
221 // Move buttons to resultinfo
222 resultInfo.appendChild(resultPanel.actions.element());
223
Akrone6538cd2018-07-16 17:52:33 +0200224 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200225 var sb = d.getElementById('search');
226 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200227 };
228
229
Akron179c8ac2015-06-30 19:30:50 +0200230 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200231 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200232
233 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200234 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200235 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200236 };
Akron7716f012015-07-01 20:38:32 +0200237
Akron00cd4d12016-05-31 21:01:11 +0200238 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200239 var errors = KorAP.koralQuery["errors"];
240 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200241
Akron19d97fe2016-09-06 20:47:05 +0200242 // Malformed query
243 if (errors[i][0] === 302 && errors[i][2]) {
244 obj.hint = hintClass.create();
245 obj.hint.alert(errors[i][2], errors[i][1]);
246 break;
247 }
Akronf0c31ed2016-06-11 11:27:01 +0200248
Akron19d97fe2016-09-06 20:47:05 +0200249 // no query
250 else if (errors[i][0] === 301) {
251 obj.hint = hintClass.create();
252 obj.hint.alert(0, errors[i][1]);
253 }
254 }
Akron00cd4d12016-05-31 21:01:11 +0200255 };
Akron179c8ac2015-06-30 19:30:50 +0200256 };
257
Akron5cb9b2b2018-07-24 17:01:09 +0200258
259 /*
260 * There is more than 0 matches, so allow for
261 * alignment toggling (left <=> right)
262 */
263 if (matchCount > 0)
264 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000265
Nils Diewald5c5a7472015-04-02 22:13:38 +0000266
Akron5cb9b2b2018-07-24 17:01:09 +0200267 /*
Akroncd42a142019-07-12 18:55:37 +0200268 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000269 */
270 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200271 vc.onMinimize = function () {
272 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200273 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200274 };
Nils Diewald6283d692015-04-23 20:32:53 +0000275
Akronec6bb8e2018-08-29 13:07:56 +0200276 vc.onOpen = function () {
277 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100278
279 var view = d.getElementById('vc-view');
280 if (!view.firstChild)
281 view.appendChild(this.element());
282
Akroncd42a142019-07-12 18:55:37 +0200283 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200284 };
285
286 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200287 if (vc.isOpen()) {
288 vc.minimize()
289 }
290 else {
Akronec6bb8e2018-08-29 13:07:56 +0200291 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200292 };
Nils Diewald58141332015-04-07 16:18:45 +0000293 };
Akron04671e72017-05-11 20:47:32 +0200294
Akron179c8ac2015-06-30 19:30:50 +0200295 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200296
297 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200298 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200299 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200300 };
Nils Diewald58141332015-04-07 16:18:45 +0000301 };
302
Akron19d97fe2016-09-06 20:47:05 +0200303
Nils Diewald58141332015-04-07 16:18:45 +0000304 /**
305 * Init Tutorial view
306 */
Akron0b489ad2018-02-02 16:49:32 +0100307 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000308 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100309 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200310 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000311 );
312 obj.tutorial = window.tutorial;
313 }
Nils Diewald58141332015-04-07 16:18:45 +0000314
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000315 // Tutorial is in parent
316 else if (window.parent) {
317 obj.tutorial = window.parent.tutorial;
318 };
319
Akron0b489ad2018-02-02 16:49:32 +0100320 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200321 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100322 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000323
Akron6ed13992016-05-23 18:06:05 +0200324 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100325 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200326 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000327
Nils Diewald845282c2015-05-14 07:53:03 +0000328
Nils Diewald58141332015-04-07 16:18:45 +0000329 /**
Akronc1457bf2015-06-11 19:24:00 +0200330 * Add VC creation on submission.
331 */
Akron0b489ad2018-02-02 16:49:32 +0100332 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200333 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200334 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100335 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200336
Akron19d97fe2016-09-06 20:47:05 +0200337 // No query was defined
338 if (qf.value === undefined || qf.value === '') {
339 qf.focus();
340 e.halt();
341 KorAP.log(700, "No query given");
342 return;
343 };
Akron7716f012015-07-01 20:38:32 +0200344
Akron19d97fe2016-09-06 20:47:05 +0200345 // Store session information
Akronf8035592018-05-24 20:40:51 +0200346 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200347
Akron19d97fe2016-09-06 20:47:05 +0200348 if (vc !== undefined) {
349 input.value = vc.toQuery();
350 }
351 else {
352 delete input['value'];
353 };
Akronc1457bf2015-06-11 19:24:00 +0200354 });
355 };
hebasta5df796f2019-05-21 15:27:12 +0200356
357
358 //Starts the guided tour at the next page
359 if(KorAP.session.get("tour")){
360 tourClass.gTshowResults().start();
361 }
362
Akronc1457bf2015-06-11 19:24:00 +0200363 /**
Nils Diewald58141332015-04-07 16:18:45 +0000364 * Init hint helper
365 * has to be final because of
366 * reposition
367 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000368 // Todo: Pass an element, so this works with
369 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200370 if (obj.hint === undefined)
371 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000372
Akron99713ef2017-06-28 18:19:28 +0200373 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100374 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200375
Nils Diewald58141332015-04-07 16:18:45 +0000376 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000377 });
hebasta75cfca52019-02-19 13:15:27 +0100378
Nils Diewald0e6992a2015-04-14 20:13:52 +0000379});