blob: df56b80477be7f366b7a2031f5ed35d4ea89db03 [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',
Akron24f48ea2020-07-01 09:37:19 +020031 'plugin/server',
32 'pipe',
Nils Diewald7148c6f2015-05-04 15:07:53 +000033 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000034 'mailToChiffre',
Akron858cbc82019-12-05 16:53:13 +010035 'util',
36 'state'
Nils Diewald0e6992a2015-04-14 20:13:52 +000037], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020038 hintClass,
39 vcClass,
40 tutClass,
41 domReady,
Akron19d97fe2016-09-06 20:47:05 +020042 vcArray,
43 alertifyClass,
44 sessionClass,
Akron4d926f12018-07-16 15:30:25 +020045 selectMenuClass,
hebasta75cfca52019-02-19 13:15:27 +010046 resultPanelClass,
Akron2d0d96d2019-11-18 19:49:50 +010047 queryPanelClass,
Akron24f48ea2020-07-01 09:37:19 +020048 tourClass,
49 pluginClass,
50 pipeClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000051
Akron0b489ad2018-02-02 16:49:32 +010052 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020053
Akron0c4cd222019-07-19 16:33:34 +020054 // Create suffix if KorAP is run in a subfolder
55 KorAP.session = sessionClass.create(
56 KorAP.URL.length > 0 ? 'kalamarJS-' + KorAP.URL.slugify() : 'kalamarJS'
57 );
Akronf8035592018-05-24 20:40:51 +020058
Nils Diewalda0defc42015-05-07 23:54:17 +000059 // Override KorAP.log
60 window.alertify = alertifyClass;
Akronc0a2da82018-07-04 15:27:37 +020061 KorAP.log = function (code, msg, src) {
62
63 if (src) {
64 msg += '<code class="src">'+src+'</code>';
65 };
Nils Diewalda0defc42015-05-07 23:54:17 +000066
67 // Use alertify to log errors
68 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020069 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020070 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000071 'error',
Akronf55504a2015-06-18 16:42:55 +020072 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000073 );
74 };
75
hebasta2758b582018-11-19 15:59:42 +010076 KorAP.vc = vcClass.create(vcArray);
77
hebasta5df796f2019-05-21 15:27:12 +020078 KorAP.tourshow = function(){
79 tourClass.gTstartSearch().start();
80 };
81
82 KorAP.tourshowR = function(){
83 tourClass.gTshowResults().start();
84 };
85
Nils Diewald0e6992a2015-04-14 20:13:52 +000086 domReady(function (event) {
hebasta5df796f2019-05-21 15:27:12 +020087
Nils Diewald0e6992a2015-04-14 20:13:52 +000088 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020089
Akron4d926f12018-07-16 15:30:25 +020090 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020091 var show = KorAP.session.get('show') || {};
hebasta043e96f2019-11-28 12:33:00 +010092
93 KorAP.Panel = KorAP.Panel || {}
Nils Diewalda297f062015-04-02 00:23:46 +000094
95 /**
Akronf55504a2015-06-18 16:42:55 +020096 * Release notifications
97 */
98 if (KorAP.Notifications !== undefined) {
Akronb50964a2020-10-12 11:44:37 +020099 KorAP.Notifications.forEach(function(n) {
100 var msg = n[1];
101 if (n[2]) {
102 msg += '<code class="src">'+n[2]+'</code>';
Akron8ea84292018-10-24 13:41:52 +0200103 };
Akronb50964a2020-10-12 11:44:37 +0200104 alertifyClass.log(msg, n[0], 10000);
105 });
Akronf55504a2015-06-18 16:42:55 +0200106 };
107
108 /**
Akroncd42a142019-07-12 18:55:37 +0200109 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000110 */
Akron5c829e92017-05-12 18:10:00 +0200111 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200112 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200113
hebasta2758b582018-11-19 15:59:42 +0100114 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100115
Akron1f0521b2018-08-28 13:01:24 +0200116 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000117 if (input) {
118 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100119 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000120 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200121 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200122
Akron1f0521b2018-08-28 13:01:24 +0200123 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200124 // Supports "collection" for legacy reasons
125 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200126 try {
Akroncd42a142019-07-12 18:55:37 +0200127 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200128 }
129 catch (e) {
130 KorAP.log(0,e);
131 }
Akron27ae9ec2015-06-23 00:43:21 +0200132 };
133
Akron0b489ad2018-02-02 16:49:32 +0100134 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200135 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200136
Akron1f0521b2018-08-28 13:01:24 +0200137 if (vc.wasRewritten()) {
138 vcchoose.classList.add('rewritten');
139 };
140
Nils Diewald7148c6f2015-05-04 15:07:53 +0000141 input.parentNode.insertBefore(vcname, input);
142 };
143
Nils Diewald7148c6f2015-05-04 15:07:53 +0000144 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000145 * Add actions to match entries
146 */
Akronb50964a2020-10-12 11:44:37 +0200147 var matchElements = d.querySelectorAll(
Akron3c390c42020-03-30 09:06:21 +0200148 '#search > ol > li'
Nils Diewald5c5a7472015-04-02 22:13:38 +0000149 );
Akron6a535d42015-08-26 20:16:58 +0200150
Akronb50964a2020-10-12 11:44:37 +0200151 matchElements.forEach(function(e) {
Akron3c390c42020-03-30 09:06:21 +0200152
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 };
Akronb50964a2020-10-12 11:44:37 +0200189 }, this);
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"]) {
Akron678c26f2020-10-09 08:52:50 +0200261 KorAP.koralQuery["errors"].forEach(function(e) {
Akronf0c31ed2016-06-11 11:27:01 +0200262
Akron19d97fe2016-09-06 20:47:05 +0200263 // Malformed query
Akron4a24b722020-10-13 12:44:25 +0200264 if (e[0] === 302 && e[2] !== undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200265 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200266 obj.hint.alert(e[2], e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200267 }
Akronf0c31ed2016-06-11 11:27:01 +0200268
Akron19d97fe2016-09-06 20:47:05 +0200269 // no query
Akron678c26f2020-10-09 08:52:50 +0200270 else if (e[0] === 301) {
Akron19d97fe2016-09-06 20:47:05 +0200271 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200272 obj.hint.alert(0, e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200273 }
Akron678c26f2020-10-09 08:52:50 +0200274 });
Akron00cd4d12016-05-31 21:01:11 +0200275 };
Akron179c8ac2015-06-30 19:30:50 +0200276 };
277
Akron5cb9b2b2018-07-24 17:01:09 +0200278
279 /*
280 * There is more than 0 matches, so allow for
281 * alignment toggling (left <=> right)
282 */
Akronb50964a2020-10-12 11:44:37 +0200283 if (matchElements.length > 0)
Akron5cb9b2b2018-07-24 17:01:09 +0200284 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000285
hebasta043e96f2019-11-28 12:33:00 +0100286 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200287 /*
Akroncd42a142019-07-12 18:55:37 +0200288 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000289 */
290 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200291 vc.onMinimize = function () {
292 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200293 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200294 };
Nils Diewald6283d692015-04-23 20:32:53 +0000295
Akronec6bb8e2018-08-29 13:07:56 +0200296 vc.onOpen = function () {
297 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100298
299 var view = d.getElementById('vc-view');
300 if (!view.firstChild)
301 view.appendChild(this.element());
302
Akroncd42a142019-07-12 18:55:37 +0200303 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200304 };
305
306 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200307 if (vc.isOpen()) {
308 vc.minimize()
309 }
310 else {
Akronec6bb8e2018-08-29 13:07:56 +0200311 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200312 };
Nils Diewald58141332015-04-07 16:18:45 +0000313 };
Akron04671e72017-05-11 20:47:32 +0200314
Akron179c8ac2015-06-30 19:30:50 +0200315 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200316
317 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200318 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200319 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200320 };
Nils Diewald58141332015-04-07 16:18:45 +0000321 };
322
Akron19d97fe2016-09-06 20:47:05 +0200323
Nils Diewald58141332015-04-07 16:18:45 +0000324 /**
325 * Init Tutorial view
326 */
Akron0b489ad2018-02-02 16:49:32 +0100327 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000328 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100329 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200330 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000331 );
332 obj.tutorial = window.tutorial;
333 }
Nils Diewald58141332015-04-07 16:18:45 +0000334
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000335 // Tutorial is in parent
336 else if (window.parent) {
337 obj.tutorial = window.parent.tutorial;
338 };
339
Akron0b489ad2018-02-02 16:49:32 +0100340 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200341 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100342 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000343
Akron6ed13992016-05-23 18:06:05 +0200344 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100345 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200346 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000347
Nils Diewald845282c2015-05-14 07:53:03 +0000348
Nils Diewald58141332015-04-07 16:18:45 +0000349 /**
Akronc1457bf2015-06-11 19:24:00 +0200350 * Add VC creation on submission.
351 */
Akron0b489ad2018-02-02 16:49:32 +0100352 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200353 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200354 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100355 var qf = d.getElementById('q-field');
Akron1be6c1c2020-01-07 15:29:58 +0100356
Akron19d97fe2016-09-06 20:47:05 +0200357 // No query was defined
358 if (qf.value === undefined || qf.value === '') {
359 qf.focus();
360 e.halt();
361 KorAP.log(700, "No query given");
362 return;
363 };
Akron1be6c1c2020-01-07 15:29:58 +0100364
Akron19d97fe2016-09-06 20:47:05 +0200365 // Store session information
Akronf8035592018-05-24 20:40:51 +0200366 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200367
Akron19d97fe2016-09-06 20:47:05 +0200368 if (vc !== undefined) {
369 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100370 if (input.value == '')
371 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200372 }
373 else {
Akrond7ad9072019-12-09 07:08:20 +0100374 input.removeAttribute('value');
375 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200376 };
Akron1be6c1c2020-01-07 15:29:58 +0100377
378 // This would preferably set the query to be "disabled",
379 // but in that case the query wouldn't be submitted
380 // at all.
381 // Setting the cursor to "progress" fails in current versions
382 // of webkit.
383 qf.classList.add("loading");
384 d.getElementById('qsubmit').classList.add("loading");
Akronc1457bf2015-06-11 19:24:00 +0200385 });
386 };
hebasta5df796f2019-05-21 15:27:12 +0200387
388
389 //Starts the guided tour at the next page
390 if(KorAP.session.get("tour")){
391 tourClass.gTshowResults().start();
392 }
393
Akronc1457bf2015-06-11 19:24:00 +0200394 /**
Nils Diewald58141332015-04-07 16:18:45 +0000395 * Init hint helper
396 * has to be final because of
397 * reposition
398 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000399 // Todo: Pass an element, so this works with
400 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200401 if (obj.hint === undefined)
402 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000403
Akron99713ef2017-06-28 18:19:28 +0200404 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100405 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200406
Akron2d0d96d2019-11-18 19:49:50 +0100407
408 /**
409 * Add query panel
410 */
411 var queryPanel = queryPanelClass.create();
412
413 // Get input field
414 var sform = d.getElementById("searchform");
415 var vcView = d.getElementById('vc-view')
416 if (sform && vcView) {
417 // The views are below the query bar
418 sform.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100419 KorAP.Panel['query'] = queryPanel;
420 }
Akron24f48ea2020-07-01 09:37:19 +0200421
422 /**
423 * Initialize Plugin registry.
424 */
425 let p = KorAP.Plugins;
426 if (p && p.length > 0) {
427 // Load Plugin Server first
428 KorAP.Plugin = pluginClass.create();
429
430 // Add services container to head
431 d.head.appendChild(KorAP.Plugin.element());
432
433 // Add pipe form
434 KorAP.Pipe = pipeClass.create();
435 d.getElementById("searchform").appendChild(KorAP.Pipe.element());
436
437 try {
438
439 // Register all plugins
Akronb50964a2020-10-12 11:44:37 +0200440 p.forEach(i => KorAP.Plugin.register(i));
Akron24f48ea2020-07-01 09:37:19 +0200441 }
442 catch (e) {
443 KorAP.log(0, e);
444 }
445 };
446
Nils Diewald58141332015-04-07 16:18:45 +0000447 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000448 });
hebasta75cfca52019-02-19 13:15:27 +0100449
Nils Diewald0e6992a2015-04-14 20:13:52 +0000450});