blob: 62d08009e6e605b74f9e2a298c40a9b6b8fa3358 [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 */
Akron3c390c42020-03-30 09:06:21 +0200144 var li = d.querySelectorAll(
145 '#search > ol > li'
Nils Diewald5c5a7472015-04-02 22:13:38 +0000146 );
Akron5cb9b2b2018-07-24 17:01:09 +0200147 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200148
Akron3c390c42020-03-30 09:06:21 +0200149 for (matchCount = 0; matchCount < li.length; matchCount++) {
150
151 let e = li[matchCount];
152
153 // Define class for active elements
154 if (e.classList.contains('active')) {
155 if (this._match === undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200156 // lazyLoad
Akron3c390c42020-03-30 09:06:21 +0200157 matchClass.create(e).init();
Akron19d97fe2016-09-06 20:47:05 +0200158 };
Akron3c390c42020-03-30 09:06:21 +0200159 }
160
161 // Define class for inactive elements
162 else {
163 e.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200164 if (this._match !== undefined)
Akron3c390c42020-03-30 09:06:21 +0200165 this._match.open();
Akron19d97fe2016-09-06 20:47:05 +0200166 else {
167 // lazyLoad
168 matchClass.create(this).open();
169 };
Akron3c390c42020-03-30 09:06:21 +0200170 // This would prevent the sidebar to go back
171 // e.halt();
172 });
173 e.addEventListener('keydown', function (e) {
174 var code = _codeFromEvent(e);
175
176 switch (code) {
177 case 32:
178 if (this._match !== undefined)
179 this._match.toggle();
180 else {
181 // lazyLoad
182 matchClass.create(this).open();
183 };
184 e.halt();
185 break;
186 };
187 });
188 };
Nils Diewalda297f062015-04-02 00:23:46 +0000189 };
Akron3c390c42020-03-30 09:06:21 +0200190
Akrone0c32c72017-04-25 22:38:23 +0200191 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100192 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200193
194 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200195
Akron5258d462017-04-26 23:32:57 +0200196 // Horrible lock to deal with sidebar clicks
197 var asideClicked = false;
198
Akron1885ce92017-04-26 23:10:01 +0200199 // Make aside active on focus
200 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200201 this.classList.add('active');
202 });
203
Akron1885ce92017-04-26 23:10:01 +0200204 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100205 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200206 if (body !== null) {
207 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200208 if (!asideClicked) {
209 aside.classList.remove('active');
210 }
211 else {
212 asideClicked = false;
213 };
Akron1885ce92017-04-26 23:10:01 +0200214 });
215 };
216
217 /* Stop click event on aside
218 * (to not trickle down to body)
219 */
220 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200221 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200222 });
223 };
Akronb9cdb102017-04-25 00:52:31 +0200224
225
Akron6bb71582016-06-10 20:41:08 +0200226 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100227 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200228 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100229 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100230 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200231 ).limit(5);
232 };
Akron6bb71582016-06-10 20:41:08 +0200233
Akron4d926f12018-07-16 15:30:25 +0200234 var resultInfo = d.getElementById('resultinfo');
235
Akron4d926f12018-07-16 15:30:25 +0200236 /**
237 * Add result panel
238 */
Akron5cb9b2b2018-07-24 17:01:09 +0200239 var resultPanel = resultPanelClass.create(show);
240
Akron4d926f12018-07-16 15:30:25 +0200241 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200242
243 // Move buttons to resultinfo
244 resultInfo.appendChild(resultPanel.actions.element());
245
Akrone6538cd2018-07-16 17:52:33 +0200246 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200247 var sb = d.getElementById('search');
248 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200249 };
250
251
Akron179c8ac2015-06-30 19:30:50 +0200252 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200253 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200254
255 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200256 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200257 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200258 };
Akron7716f012015-07-01 20:38:32 +0200259
Akron00cd4d12016-05-31 21:01:11 +0200260 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200261 var errors = KorAP.koralQuery["errors"];
262 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200263
Akron19d97fe2016-09-06 20:47:05 +0200264 // Malformed query
265 if (errors[i][0] === 302 && errors[i][2]) {
266 obj.hint = hintClass.create();
267 obj.hint.alert(errors[i][2], errors[i][1]);
268 break;
269 }
Akronf0c31ed2016-06-11 11:27:01 +0200270
Akron19d97fe2016-09-06 20:47:05 +0200271 // no query
272 else if (errors[i][0] === 301) {
273 obj.hint = hintClass.create();
274 obj.hint.alert(0, errors[i][1]);
275 }
276 }
Akron00cd4d12016-05-31 21:01:11 +0200277 };
Akron179c8ac2015-06-30 19:30:50 +0200278 };
279
Akron5cb9b2b2018-07-24 17:01:09 +0200280
281 /*
282 * There is more than 0 matches, so allow for
283 * alignment toggling (left <=> right)
284 */
285 if (matchCount > 0)
286 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000287
hebasta043e96f2019-11-28 12:33:00 +0100288 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200289 /*
Akroncd42a142019-07-12 18:55:37 +0200290 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000291 */
292 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200293 vc.onMinimize = function () {
294 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200295 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200296 };
Nils Diewald6283d692015-04-23 20:32:53 +0000297
Akronec6bb8e2018-08-29 13:07:56 +0200298 vc.onOpen = function () {
299 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100300
301 var view = d.getElementById('vc-view');
302 if (!view.firstChild)
303 view.appendChild(this.element());
304
Akroncd42a142019-07-12 18:55:37 +0200305 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200306 };
307
308 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200309 if (vc.isOpen()) {
310 vc.minimize()
311 }
312 else {
Akronec6bb8e2018-08-29 13:07:56 +0200313 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200314 };
Nils Diewald58141332015-04-07 16:18:45 +0000315 };
Akron04671e72017-05-11 20:47:32 +0200316
Akron179c8ac2015-06-30 19:30:50 +0200317 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200318
319 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200320 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200321 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200322 };
Nils Diewald58141332015-04-07 16:18:45 +0000323 };
324
Akron19d97fe2016-09-06 20:47:05 +0200325
Nils Diewald58141332015-04-07 16:18:45 +0000326 /**
327 * Init Tutorial view
328 */
Akron0b489ad2018-02-02 16:49:32 +0100329 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000330 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100331 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200332 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000333 );
334 obj.tutorial = window.tutorial;
335 }
Nils Diewald58141332015-04-07 16:18:45 +0000336
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000337 // Tutorial is in parent
338 else if (window.parent) {
339 obj.tutorial = window.parent.tutorial;
340 };
341
Akron0b489ad2018-02-02 16:49:32 +0100342 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200343 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100344 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000345
Akron6ed13992016-05-23 18:06:05 +0200346 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100347 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200348 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000349
Nils Diewald845282c2015-05-14 07:53:03 +0000350
Nils Diewald58141332015-04-07 16:18:45 +0000351 /**
Akronc1457bf2015-06-11 19:24:00 +0200352 * Add VC creation on submission.
353 */
Akron0b489ad2018-02-02 16:49:32 +0100354 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200355 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200356 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100357 var qf = d.getElementById('q-field');
Akron1be6c1c2020-01-07 15:29:58 +0100358
Akron19d97fe2016-09-06 20:47:05 +0200359 // No query was defined
360 if (qf.value === undefined || qf.value === '') {
361 qf.focus();
362 e.halt();
363 KorAP.log(700, "No query given");
364 return;
365 };
Akron1be6c1c2020-01-07 15:29:58 +0100366
Akron19d97fe2016-09-06 20:47:05 +0200367 // Store session information
Akronf8035592018-05-24 20:40:51 +0200368 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200369
Akron19d97fe2016-09-06 20:47:05 +0200370 if (vc !== undefined) {
371 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100372 if (input.value == '')
373 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200374 }
375 else {
Akrond7ad9072019-12-09 07:08:20 +0100376 input.removeAttribute('value');
377 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200378 };
Akron1be6c1c2020-01-07 15:29:58 +0100379
380 // This would preferably set the query to be "disabled",
381 // but in that case the query wouldn't be submitted
382 // at all.
383 // Setting the cursor to "progress" fails in current versions
384 // of webkit.
385 qf.classList.add("loading");
386 d.getElementById('qsubmit').classList.add("loading");
Akronc1457bf2015-06-11 19:24:00 +0200387 });
388 };
hebasta5df796f2019-05-21 15:27:12 +0200389
390
391 //Starts the guided tour at the next page
392 if(KorAP.session.get("tour")){
393 tourClass.gTshowResults().start();
394 }
395
Akronc1457bf2015-06-11 19:24:00 +0200396 /**
Nils Diewald58141332015-04-07 16:18:45 +0000397 * Init hint helper
398 * has to be final because of
399 * reposition
400 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000401 // Todo: Pass an element, so this works with
402 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200403 if (obj.hint === undefined)
404 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000405
Akron99713ef2017-06-28 18:19:28 +0200406 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100407 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200408
Akron2d0d96d2019-11-18 19:49:50 +0100409
410 /**
411 * Add query panel
412 */
413 var queryPanel = queryPanelClass.create();
414
415 // Get input field
416 var sform = d.getElementById("searchform");
417 var vcView = d.getElementById('vc-view')
418 if (sform && vcView) {
419 // The views are below the query bar
420 sform.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100421 KorAP.Panel['query'] = queryPanel;
422 }
423
Nils Diewald58141332015-04-07 16:18:45 +0000424 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000425 });
hebasta75cfca52019-02-19 13:15:27 +0100426
Nils Diewald0e6992a2015-04-14 20:13:52 +0000427});