blob: bafa473d7f58f116c5270fc60ffb084954515de8 [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
Akron0c4cd222019-07-19 16:33:34 +020047 // Create suffix if KorAP is run in a subfolder
48 KorAP.session = sessionClass.create(
49 KorAP.URL.length > 0 ? 'kalamarJS-' + KorAP.URL.slugify() : 'kalamarJS'
50 );
Akronf8035592018-05-24 20:40:51 +020051
Nils Diewalda0defc42015-05-07 23:54:17 +000052 // Override KorAP.log
53 window.alertify = alertifyClass;
Akronc0a2da82018-07-04 15:27:37 +020054 KorAP.log = function (code, msg, src) {
55
56 if (src) {
57 msg += '<code class="src">'+src+'</code>';
58 };
Nils Diewalda0defc42015-05-07 23:54:17 +000059
60 // Use alertify to log errors
61 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020062 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020063 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000064 'error',
Akronf55504a2015-06-18 16:42:55 +020065 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000066 );
67 };
68
hebasta2758b582018-11-19 15:59:42 +010069 KorAP.vc = vcClass.create(vcArray);
70
hebasta5df796f2019-05-21 15:27:12 +020071 KorAP.tourshow = function(){
72 tourClass.gTstartSearch().start();
73 };
74
75 KorAP.tourshowR = function(){
76 tourClass.gTshowResults().start();
77 };
78
Nils Diewald0e6992a2015-04-14 20:13:52 +000079 domReady(function (event) {
hebasta5df796f2019-05-21 15:27:12 +020080
Nils Diewald0e6992a2015-04-14 20:13:52 +000081 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020082
Akron4d926f12018-07-16 15:30:25 +020083 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020084 var show = KorAP.session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000085
86 /**
Akronf55504a2015-06-18 16:42:55 +020087 * Release notifications
88 */
89 if (KorAP.Notifications !== undefined) {
90 var n = KorAP.Notifications;
91 for (var i = 0; i < n.length; i++) {
Akron8ea84292018-10-24 13:41:52 +020092 var msg = n[i][1];
93 if (n[i][2]) {
94 msg += '<code class="src">'+n[i][2]+'</code>';
95 };
96 alertifyClass.log(msg, n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020097 };
98 };
99
100 /**
Akroncd42a142019-07-12 18:55:37 +0200101 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000102 */
Akron5c829e92017-05-12 18:10:00 +0200103 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200104 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200105
hebasta2758b582018-11-19 15:59:42 +0100106 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100107
Akron1f0521b2018-08-28 13:01:24 +0200108 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000109 if (input) {
110 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100111 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000112 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200113 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200114
Akron1f0521b2018-08-28 13:01:24 +0200115 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200116 // Supports "collection" for legacy reasons
117 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200118 try {
Akroncd42a142019-07-12 18:55:37 +0200119 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200120 }
121 catch (e) {
122 KorAP.log(0,e);
123 }
Akron27ae9ec2015-06-23 00:43:21 +0200124 };
125
Akron0b489ad2018-02-02 16:49:32 +0100126 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200127 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200128
Akron1f0521b2018-08-28 13:01:24 +0200129 if (vc.wasRewritten()) {
130 vcchoose.classList.add('rewritten');
131 };
132
Nils Diewald7148c6f2015-05-04 15:07:53 +0000133 input.parentNode.insertBefore(vcname, input);
134 };
135
Nils Diewald7148c6f2015-05-04 15:07:53 +0000136 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000137 * Add actions to match entries
138 */
Akron0b489ad2018-02-02 16:49:32 +0100139 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000140 '#search > ol > li:not(.active)'
141 );
Akron5cb9b2b2018-07-24 17:01:09 +0200142 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200143
Akron5cb9b2b2018-07-24 17:01:09 +0200144 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200145 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200146 if (this._match !== undefined)
147 this._match.open();
148 else {
149 // lazyLoad
150 matchClass.create(this).open();
151 };
Akrone0c32c72017-04-25 22:38:23 +0200152 // This would prevent the sidebar to go back
153 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000154 });
Akron5cb9b2b2018-07-24 17:01:09 +0200155 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200156 var code = _codeFromEvent(e);
157
158 switch (code) {
159 case 32:
160 if (this._match !== undefined)
161 this._match.toggle();
162 else {
163 // lazyLoad
164 matchClass.create(this).open();
165 };
166 e.halt();
167 break;
168 };
Akron6a535d42015-08-26 20:16:58 +0200169 });
Nils Diewalda297f062015-04-02 00:23:46 +0000170 };
171
Akrone0c32c72017-04-25 22:38:23 +0200172 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100173 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200174
175 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200176
Akron5258d462017-04-26 23:32:57 +0200177 // Horrible lock to deal with sidebar clicks
178 var asideClicked = false;
179
Akron1885ce92017-04-26 23:10:01 +0200180 // Make aside active on focus
181 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200182 this.classList.add('active');
183 });
184
Akron1885ce92017-04-26 23:10:01 +0200185 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100186 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200187 if (body !== null) {
188 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200189 if (!asideClicked) {
190 aside.classList.remove('active');
191 }
192 else {
193 asideClicked = false;
194 };
Akron1885ce92017-04-26 23:10:01 +0200195 });
196 };
197
198 /* Stop click event on aside
199 * (to not trickle down to body)
200 */
201 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200202 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200203 });
204 };
Akronb9cdb102017-04-25 00:52:31 +0200205
206
Akron6bb71582016-06-10 20:41:08 +0200207 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100208 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200209 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100210 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100211 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200212 ).limit(5);
213 };
Akron6bb71582016-06-10 20:41:08 +0200214
Akron4d926f12018-07-16 15:30:25 +0200215 var resultInfo = d.getElementById('resultinfo');
216
Akron4d926f12018-07-16 15:30:25 +0200217 /**
218 * Add result panel
219 */
Akron5cb9b2b2018-07-24 17:01:09 +0200220 var resultPanel = resultPanelClass.create(show);
221
Akron4d926f12018-07-16 15:30:25 +0200222 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200223
224 // Move buttons to resultinfo
225 resultInfo.appendChild(resultPanel.actions.element());
226
Akrone6538cd2018-07-16 17:52:33 +0200227 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200228 var sb = d.getElementById('search');
229 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200230 };
231
232
Akron179c8ac2015-06-30 19:30:50 +0200233 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200234 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200235
236 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200237 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200238 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200239 };
Akron7716f012015-07-01 20:38:32 +0200240
Akron00cd4d12016-05-31 21:01:11 +0200241 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200242 var errors = KorAP.koralQuery["errors"];
243 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200244
Akron19d97fe2016-09-06 20:47:05 +0200245 // Malformed query
246 if (errors[i][0] === 302 && errors[i][2]) {
247 obj.hint = hintClass.create();
248 obj.hint.alert(errors[i][2], errors[i][1]);
249 break;
250 }
Akronf0c31ed2016-06-11 11:27:01 +0200251
Akron19d97fe2016-09-06 20:47:05 +0200252 // no query
253 else if (errors[i][0] === 301) {
254 obj.hint = hintClass.create();
255 obj.hint.alert(0, errors[i][1]);
256 }
257 }
Akron00cd4d12016-05-31 21:01:11 +0200258 };
Akron179c8ac2015-06-30 19:30:50 +0200259 };
260
Akron5cb9b2b2018-07-24 17:01:09 +0200261
262 /*
263 * There is more than 0 matches, so allow for
264 * alignment toggling (left <=> right)
265 */
266 if (matchCount > 0)
267 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000268
Nils Diewald5c5a7472015-04-02 22:13:38 +0000269
Akron5cb9b2b2018-07-24 17:01:09 +0200270 /*
Akroncd42a142019-07-12 18:55:37 +0200271 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000272 */
273 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200274 vc.onMinimize = function () {
275 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200276 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200277 };
Nils Diewald6283d692015-04-23 20:32:53 +0000278
Akronec6bb8e2018-08-29 13:07:56 +0200279 vc.onOpen = function () {
280 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100281
282 var view = d.getElementById('vc-view');
283 if (!view.firstChild)
284 view.appendChild(this.element());
285
Akroncd42a142019-07-12 18:55:37 +0200286 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200287 };
288
289 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200290 if (vc.isOpen()) {
291 vc.minimize()
292 }
293 else {
Akronec6bb8e2018-08-29 13:07:56 +0200294 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200295 };
Nils Diewald58141332015-04-07 16:18:45 +0000296 };
Akron04671e72017-05-11 20:47:32 +0200297
Akron179c8ac2015-06-30 19:30:50 +0200298 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200299
300 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200301 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200302 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200303 };
Nils Diewald58141332015-04-07 16:18:45 +0000304 };
305
Akron19d97fe2016-09-06 20:47:05 +0200306
Nils Diewald58141332015-04-07 16:18:45 +0000307 /**
308 * Init Tutorial view
309 */
Akron0b489ad2018-02-02 16:49:32 +0100310 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000311 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100312 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200313 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000314 );
315 obj.tutorial = window.tutorial;
316 }
Nils Diewald58141332015-04-07 16:18:45 +0000317
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000318 // Tutorial is in parent
319 else if (window.parent) {
320 obj.tutorial = window.parent.tutorial;
321 };
322
Akron0b489ad2018-02-02 16:49:32 +0100323 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200324 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100325 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000326
Akron6ed13992016-05-23 18:06:05 +0200327 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100328 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200329 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000330
Nils Diewald845282c2015-05-14 07:53:03 +0000331
Nils Diewald58141332015-04-07 16:18:45 +0000332 /**
Akronc1457bf2015-06-11 19:24:00 +0200333 * Add VC creation on submission.
334 */
Akron0b489ad2018-02-02 16:49:32 +0100335 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200336 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200337 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100338 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200339
Akron19d97fe2016-09-06 20:47:05 +0200340 // No query was defined
341 if (qf.value === undefined || qf.value === '') {
342 qf.focus();
343 e.halt();
344 KorAP.log(700, "No query given");
345 return;
346 };
Akron7716f012015-07-01 20:38:32 +0200347
Akron19d97fe2016-09-06 20:47:05 +0200348 // Store session information
Akronf8035592018-05-24 20:40:51 +0200349 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200350
Akron19d97fe2016-09-06 20:47:05 +0200351 if (vc !== undefined) {
352 input.value = vc.toQuery();
353 }
354 else {
355 delete input['value'];
356 };
Akronc1457bf2015-06-11 19:24:00 +0200357 });
358 };
hebasta5df796f2019-05-21 15:27:12 +0200359
360
361 //Starts the guided tour at the next page
362 if(KorAP.session.get("tour")){
363 tourClass.gTshowResults().start();
364 }
365
Akronc1457bf2015-06-11 19:24:00 +0200366 /**
Nils Diewald58141332015-04-07 16:18:45 +0000367 * Init hint helper
368 * has to be final because of
369 * reposition
370 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000371 // Todo: Pass an element, so this works with
372 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200373 if (obj.hint === undefined)
374 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000375
Akron99713ef2017-06-28 18:19:28 +0200376 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100377 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200378
Nils Diewald58141332015-04-07 16:18:45 +0000379 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000380 });
hebasta75cfca52019-02-19 13:15:27 +0100381
Nils Diewald0e6992a2015-04-14 20:13:52 +0000382});