blob: 109fd487e0a132153e1b694a647c2c88682d7580 [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',
Akron2d0d96d2019-11-18 19:49:50 +010029 'panel/query',
hebasta75cfca52019-02-19 13:15:27 +010030 'tour/tours',
Nils Diewald7148c6f2015-05-04 15:07:53 +000031 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000032 'mailToChiffre',
Akron858cbc82019-12-05 16:53:13 +010033 'util',
34 'state'
Nils Diewald0e6992a2015-04-14 20:13:52 +000035], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020036 hintClass,
37 vcClass,
38 tutClass,
39 domReady,
Akron19d97fe2016-09-06 20:47:05 +020040 vcArray,
41 alertifyClass,
42 sessionClass,
Akron4d926f12018-07-16 15:30:25 +020043 selectMenuClass,
hebasta75cfca52019-02-19 13:15:27 +010044 resultPanelClass,
Akron2d0d96d2019-11-18 19:49:50 +010045 queryPanelClass,
hebasta75cfca52019-02-19 13:15:27 +010046 tourClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000047
Akron0b489ad2018-02-02 16:49:32 +010048 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020049
Akron0c4cd222019-07-19 16:33:34 +020050 // Create suffix if KorAP is run in a subfolder
51 KorAP.session = sessionClass.create(
52 KorAP.URL.length > 0 ? 'kalamarJS-' + KorAP.URL.slugify() : 'kalamarJS'
53 );
Akronf8035592018-05-24 20:40:51 +020054
Nils Diewalda0defc42015-05-07 23:54:17 +000055 // Override KorAP.log
56 window.alertify = alertifyClass;
Akronc0a2da82018-07-04 15:27:37 +020057 KorAP.log = function (code, msg, src) {
58
59 if (src) {
60 msg += '<code class="src">'+src+'</code>';
61 };
Nils Diewalda0defc42015-05-07 23:54:17 +000062
63 // Use alertify to log errors
64 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020065 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020066 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000067 'error',
Akronf55504a2015-06-18 16:42:55 +020068 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000069 );
70 };
71
hebasta2758b582018-11-19 15:59:42 +010072 KorAP.vc = vcClass.create(vcArray);
73
hebasta5df796f2019-05-21 15:27:12 +020074 KorAP.tourshow = function(){
75 tourClass.gTstartSearch().start();
76 };
77
78 KorAP.tourshowR = function(){
79 tourClass.gTshowResults().start();
80 };
81
Nils Diewald0e6992a2015-04-14 20:13:52 +000082 domReady(function (event) {
hebasta5df796f2019-05-21 15:27:12 +020083
Nils Diewald0e6992a2015-04-14 20:13:52 +000084 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020085
Akron4d926f12018-07-16 15:30:25 +020086 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020087 var show = KorAP.session.get('show') || {};
hebasta043e96f2019-11-28 12:33:00 +010088
89 KorAP.Panel = KorAP.Panel || {}
Nils Diewalda297f062015-04-02 00:23:46 +000090
91 /**
Akronf55504a2015-06-18 16:42:55 +020092 * Release notifications
93 */
94 if (KorAP.Notifications !== undefined) {
95 var n = KorAP.Notifications;
96 for (var i = 0; i < n.length; i++) {
Akron8ea84292018-10-24 13:41:52 +020097 var msg = n[i][1];
98 if (n[i][2]) {
99 msg += '<code class="src">'+n[i][2]+'</code>';
100 };
101 alertifyClass.log(msg, n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +0200102 };
103 };
104
105 /**
Akroncd42a142019-07-12 18:55:37 +0200106 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000107 */
Akron5c829e92017-05-12 18:10:00 +0200108 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200109 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200110
hebasta2758b582018-11-19 15:59:42 +0100111 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100112
Akron1f0521b2018-08-28 13:01:24 +0200113 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000114 if (input) {
115 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100116 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000117 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200118 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200119
Akron1f0521b2018-08-28 13:01:24 +0200120 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200121 // Supports "collection" for legacy reasons
122 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200123 try {
Akroncd42a142019-07-12 18:55:37 +0200124 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200125 }
126 catch (e) {
127 KorAP.log(0,e);
128 }
Akron27ae9ec2015-06-23 00:43:21 +0200129 };
130
Akron0b489ad2018-02-02 16:49:32 +0100131 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200132 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200133
Akron1f0521b2018-08-28 13:01:24 +0200134 if (vc.wasRewritten()) {
135 vcchoose.classList.add('rewritten');
136 };
137
Nils Diewald7148c6f2015-05-04 15:07:53 +0000138 input.parentNode.insertBefore(vcname, input);
139 };
140
Nils Diewald7148c6f2015-05-04 15:07:53 +0000141 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000142 * Add actions to match entries
143 */
Akron0b489ad2018-02-02 16:49:32 +0100144 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000145 '#search > ol > li:not(.active)'
146 );
Akron5cb9b2b2018-07-24 17:01:09 +0200147 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200148
Akron5cb9b2b2018-07-24 17:01:09 +0200149 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200150 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200151 if (this._match !== undefined)
152 this._match.open();
153 else {
154 // lazyLoad
155 matchClass.create(this).open();
156 };
Akrone0c32c72017-04-25 22:38:23 +0200157 // This would prevent the sidebar to go back
158 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000159 });
Akron5cb9b2b2018-07-24 17:01:09 +0200160 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200161 var code = _codeFromEvent(e);
162
163 switch (code) {
164 case 32:
165 if (this._match !== undefined)
166 this._match.toggle();
167 else {
168 // lazyLoad
169 matchClass.create(this).open();
170 };
171 e.halt();
172 break;
173 };
Akron6a535d42015-08-26 20:16:58 +0200174 });
Nils Diewalda297f062015-04-02 00:23:46 +0000175 };
176
Akrone0c32c72017-04-25 22:38:23 +0200177 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100178 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200179
180 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200181
Akron5258d462017-04-26 23:32:57 +0200182 // Horrible lock to deal with sidebar clicks
183 var asideClicked = false;
184
Akron1885ce92017-04-26 23:10:01 +0200185 // Make aside active on focus
186 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200187 this.classList.add('active');
188 });
189
Akron1885ce92017-04-26 23:10:01 +0200190 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100191 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200192 if (body !== null) {
193 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200194 if (!asideClicked) {
195 aside.classList.remove('active');
196 }
197 else {
198 asideClicked = false;
199 };
Akron1885ce92017-04-26 23:10:01 +0200200 });
201 };
202
203 /* Stop click event on aside
204 * (to not trickle down to body)
205 */
206 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200207 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200208 });
209 };
Akronb9cdb102017-04-25 00:52:31 +0200210
211
Akron6bb71582016-06-10 20:41:08 +0200212 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100213 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200214 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100215 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100216 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200217 ).limit(5);
218 };
Akron6bb71582016-06-10 20:41:08 +0200219
Akron4d926f12018-07-16 15:30:25 +0200220 var resultInfo = d.getElementById('resultinfo');
221
Akron4d926f12018-07-16 15:30:25 +0200222 /**
223 * Add result panel
224 */
Akron5cb9b2b2018-07-24 17:01:09 +0200225 var resultPanel = resultPanelClass.create(show);
226
Akron4d926f12018-07-16 15:30:25 +0200227 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200228
229 // Move buttons to resultinfo
230 resultInfo.appendChild(resultPanel.actions.element());
231
Akrone6538cd2018-07-16 17:52:33 +0200232 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200233 var sb = d.getElementById('search');
234 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200235 };
236
237
Akron179c8ac2015-06-30 19:30:50 +0200238 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200239 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200240
241 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200242 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200243 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200244 };
Akron7716f012015-07-01 20:38:32 +0200245
Akron00cd4d12016-05-31 21:01:11 +0200246 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200247 var errors = KorAP.koralQuery["errors"];
248 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200249
Akron19d97fe2016-09-06 20:47:05 +0200250 // Malformed query
251 if (errors[i][0] === 302 && errors[i][2]) {
252 obj.hint = hintClass.create();
253 obj.hint.alert(errors[i][2], errors[i][1]);
254 break;
255 }
Akronf0c31ed2016-06-11 11:27:01 +0200256
Akron19d97fe2016-09-06 20:47:05 +0200257 // no query
258 else if (errors[i][0] === 301) {
259 obj.hint = hintClass.create();
260 obj.hint.alert(0, errors[i][1]);
261 }
262 }
Akron00cd4d12016-05-31 21:01:11 +0200263 };
Akron179c8ac2015-06-30 19:30:50 +0200264 };
265
Akron5cb9b2b2018-07-24 17:01:09 +0200266
267 /*
268 * There is more than 0 matches, so allow for
269 * alignment toggling (left <=> right)
270 */
271 if (matchCount > 0)
272 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000273
hebasta043e96f2019-11-28 12:33:00 +0100274 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200275 /*
Akroncd42a142019-07-12 18:55:37 +0200276 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000277 */
278 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200279 vc.onMinimize = function () {
280 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200281 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200282 };
Nils Diewald6283d692015-04-23 20:32:53 +0000283
Akronec6bb8e2018-08-29 13:07:56 +0200284 vc.onOpen = function () {
285 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100286
287 var view = d.getElementById('vc-view');
288 if (!view.firstChild)
289 view.appendChild(this.element());
290
Akroncd42a142019-07-12 18:55:37 +0200291 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200292 };
293
294 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200295 if (vc.isOpen()) {
296 vc.minimize()
297 }
298 else {
Akronec6bb8e2018-08-29 13:07:56 +0200299 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200300 };
Nils Diewald58141332015-04-07 16:18:45 +0000301 };
Akron04671e72017-05-11 20:47:32 +0200302
Akron179c8ac2015-06-30 19:30:50 +0200303 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200304
305 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200306 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200307 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200308 };
Nils Diewald58141332015-04-07 16:18:45 +0000309 };
310
Akron19d97fe2016-09-06 20:47:05 +0200311
Nils Diewald58141332015-04-07 16:18:45 +0000312 /**
313 * Init Tutorial view
314 */
Akron0b489ad2018-02-02 16:49:32 +0100315 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000316 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100317 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200318 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000319 );
320 obj.tutorial = window.tutorial;
321 }
Nils Diewald58141332015-04-07 16:18:45 +0000322
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000323 // Tutorial is in parent
324 else if (window.parent) {
325 obj.tutorial = window.parent.tutorial;
326 };
327
Akron0b489ad2018-02-02 16:49:32 +0100328 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200329 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100330 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000331
Akron6ed13992016-05-23 18:06:05 +0200332 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100333 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200334 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000335
Nils Diewald845282c2015-05-14 07:53:03 +0000336
Nils Diewald58141332015-04-07 16:18:45 +0000337 /**
Akronc1457bf2015-06-11 19:24:00 +0200338 * Add VC creation on submission.
339 */
Akron0b489ad2018-02-02 16:49:32 +0100340 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200341 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200342 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100343 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200344
Akron19d97fe2016-09-06 20:47:05 +0200345 // No query was defined
346 if (qf.value === undefined || qf.value === '') {
347 qf.focus();
348 e.halt();
349 KorAP.log(700, "No query given");
350 return;
351 };
Akron7716f012015-07-01 20:38:32 +0200352
Akron19d97fe2016-09-06 20:47:05 +0200353 // Store session information
Akronf8035592018-05-24 20:40:51 +0200354 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200355
Akron19d97fe2016-09-06 20:47:05 +0200356 if (vc !== undefined) {
357 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100358 if (input.value == '')
359 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200360 }
361 else {
Akrond7ad9072019-12-09 07:08:20 +0100362 input.removeAttribute('value');
363 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200364 };
Akronc1457bf2015-06-11 19:24:00 +0200365 });
366 };
hebasta5df796f2019-05-21 15:27:12 +0200367
368
369 //Starts the guided tour at the next page
370 if(KorAP.session.get("tour")){
371 tourClass.gTshowResults().start();
372 }
373
Akronc1457bf2015-06-11 19:24:00 +0200374 /**
Nils Diewald58141332015-04-07 16:18:45 +0000375 * Init hint helper
376 * has to be final because of
377 * reposition
378 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000379 // Todo: Pass an element, so this works with
380 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200381 if (obj.hint === undefined)
382 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000383
Akron99713ef2017-06-28 18:19:28 +0200384 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100385 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200386
Akron2d0d96d2019-11-18 19:49:50 +0100387
388 /**
389 * Add query panel
390 */
391 var queryPanel = queryPanelClass.create();
392
393 // Get input field
394 var sform = d.getElementById("searchform");
395 var vcView = d.getElementById('vc-view')
396 if (sform && vcView) {
397 // The views are below the query bar
398 sform.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100399 KorAP.Panel['query'] = queryPanel;
400 }
401
Nils Diewald58141332015-04-07 16:18:45 +0000402 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000403 });
hebasta75cfca52019-02-19 13:15:27 +0100404
Nils Diewald0e6992a2015-04-14 20:13:52 +0000405});