blob: 82d9de9dbf71fcb62090faae4c164689ceec2be6 [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',
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 'util'
34], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020035 hintClass,
36 vcClass,
37 tutClass,
38 domReady,
Akron19d97fe2016-09-06 20:47:05 +020039 vcArray,
40 alertifyClass,
41 sessionClass,
Akron4d926f12018-07-16 15:30:25 +020042 selectMenuClass,
hebasta75cfca52019-02-19 13:15:27 +010043 resultPanelClass,
Akron2d0d96d2019-11-18 19:49:50 +010044 queryPanelClass,
hebasta75cfca52019-02-19 13:15:27 +010045 tourClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000046
Akron0b489ad2018-02-02 16:49:32 +010047 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020048
Akron0c4cd222019-07-19 16:33:34 +020049 // Create suffix if KorAP is run in a subfolder
50 KorAP.session = sessionClass.create(
51 KorAP.URL.length > 0 ? 'kalamarJS-' + KorAP.URL.slugify() : 'kalamarJS'
52 );
Akronf8035592018-05-24 20:40:51 +020053
Nils Diewalda0defc42015-05-07 23:54:17 +000054 // Override KorAP.log
55 window.alertify = alertifyClass;
Akronc0a2da82018-07-04 15:27:37 +020056 KorAP.log = function (code, msg, src) {
57
58 if (src) {
59 msg += '<code class="src">'+src+'</code>';
60 };
Nils Diewalda0defc42015-05-07 23:54:17 +000061
62 // Use alertify to log errors
63 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020064 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020065 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000066 'error',
Akronf55504a2015-06-18 16:42:55 +020067 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000068 );
69 };
70
hebasta2758b582018-11-19 15:59:42 +010071 KorAP.vc = vcClass.create(vcArray);
72
hebasta5df796f2019-05-21 15:27:12 +020073 KorAP.tourshow = function(){
74 tourClass.gTstartSearch().start();
75 };
76
77 KorAP.tourshowR = function(){
78 tourClass.gTshowResults().start();
79 };
80
Nils Diewald0e6992a2015-04-14 20:13:52 +000081 domReady(function (event) {
hebasta5df796f2019-05-21 15:27:12 +020082
Nils Diewald0e6992a2015-04-14 20:13:52 +000083 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020084
Akron4d926f12018-07-16 15:30:25 +020085 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020086 var show = KorAP.session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000087
88 /**
Akronf55504a2015-06-18 16:42:55 +020089 * Release notifications
90 */
91 if (KorAP.Notifications !== undefined) {
92 var n = KorAP.Notifications;
93 for (var i = 0; i < n.length; i++) {
Akron8ea84292018-10-24 13:41:52 +020094 var msg = n[i][1];
95 if (n[i][2]) {
96 msg += '<code class="src">'+n[i][2]+'</code>';
97 };
98 alertifyClass.log(msg, n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020099 };
100 };
101
102 /**
Akroncd42a142019-07-12 18:55:37 +0200103 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000104 */
Akron5c829e92017-05-12 18:10:00 +0200105 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200106 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200107
hebasta2758b582018-11-19 15:59:42 +0100108 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100109
Akron1f0521b2018-08-28 13:01:24 +0200110 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000111 if (input) {
112 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100113 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000114 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200115 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200116
Akron1f0521b2018-08-28 13:01:24 +0200117 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200118 // Supports "collection" for legacy reasons
119 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200120 try {
Akroncd42a142019-07-12 18:55:37 +0200121 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200122 }
123 catch (e) {
124 KorAP.log(0,e);
125 }
Akron27ae9ec2015-06-23 00:43:21 +0200126 };
127
Akron0b489ad2018-02-02 16:49:32 +0100128 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200129 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200130
Akron1f0521b2018-08-28 13:01:24 +0200131 if (vc.wasRewritten()) {
132 vcchoose.classList.add('rewritten');
133 };
134
Nils Diewald7148c6f2015-05-04 15:07:53 +0000135 input.parentNode.insertBefore(vcname, input);
136 };
137
Nils Diewald7148c6f2015-05-04 15:07:53 +0000138 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000139 * Add actions to match entries
140 */
Akron0b489ad2018-02-02 16:49:32 +0100141 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000142 '#search > ol > li:not(.active)'
143 );
Akron5cb9b2b2018-07-24 17:01:09 +0200144 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200145
Akron5cb9b2b2018-07-24 17:01:09 +0200146 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200147 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200148 if (this._match !== undefined)
149 this._match.open();
150 else {
151 // lazyLoad
152 matchClass.create(this).open();
153 };
Akrone0c32c72017-04-25 22:38:23 +0200154 // This would prevent the sidebar to go back
155 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000156 });
Akron5cb9b2b2018-07-24 17:01:09 +0200157 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200158 var code = _codeFromEvent(e);
159
160 switch (code) {
161 case 32:
162 if (this._match !== undefined)
163 this._match.toggle();
164 else {
165 // lazyLoad
166 matchClass.create(this).open();
167 };
168 e.halt();
169 break;
170 };
Akron6a535d42015-08-26 20:16:58 +0200171 });
Nils Diewalda297f062015-04-02 00:23:46 +0000172 };
173
Akrone0c32c72017-04-25 22:38:23 +0200174 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100175 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200176
177 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200178
Akron5258d462017-04-26 23:32:57 +0200179 // Horrible lock to deal with sidebar clicks
180 var asideClicked = false;
181
Akron1885ce92017-04-26 23:10:01 +0200182 // Make aside active on focus
183 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200184 this.classList.add('active');
185 });
186
Akron1885ce92017-04-26 23:10:01 +0200187 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100188 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200189 if (body !== null) {
190 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200191 if (!asideClicked) {
192 aside.classList.remove('active');
193 }
194 else {
195 asideClicked = false;
196 };
Akron1885ce92017-04-26 23:10:01 +0200197 });
198 };
199
200 /* Stop click event on aside
201 * (to not trickle down to body)
202 */
203 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200204 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200205 });
206 };
Akronb9cdb102017-04-25 00:52:31 +0200207
208
Akron6bb71582016-06-10 20:41:08 +0200209 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100210 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200211 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100212 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100213 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200214 ).limit(5);
215 };
Akron6bb71582016-06-10 20:41:08 +0200216
Akron4d926f12018-07-16 15:30:25 +0200217 var resultInfo = d.getElementById('resultinfo');
218
Akron4d926f12018-07-16 15:30:25 +0200219 /**
220 * Add result panel
221 */
Akron5cb9b2b2018-07-24 17:01:09 +0200222 var resultPanel = resultPanelClass.create(show);
223
Akron4d926f12018-07-16 15:30:25 +0200224 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200225
226 // Move buttons to resultinfo
227 resultInfo.appendChild(resultPanel.actions.element());
228
Akrone6538cd2018-07-16 17:52:33 +0200229 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200230 var sb = d.getElementById('search');
231 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200232 };
233
234
Akron179c8ac2015-06-30 19:30:50 +0200235 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200236 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200237
238 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200239 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200240 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200241 };
Akron7716f012015-07-01 20:38:32 +0200242
Akron00cd4d12016-05-31 21:01:11 +0200243 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200244 var errors = KorAP.koralQuery["errors"];
245 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200246
Akron19d97fe2016-09-06 20:47:05 +0200247 // Malformed query
248 if (errors[i][0] === 302 && errors[i][2]) {
249 obj.hint = hintClass.create();
250 obj.hint.alert(errors[i][2], errors[i][1]);
251 break;
252 }
Akronf0c31ed2016-06-11 11:27:01 +0200253
Akron19d97fe2016-09-06 20:47:05 +0200254 // no query
255 else if (errors[i][0] === 301) {
256 obj.hint = hintClass.create();
257 obj.hint.alert(0, errors[i][1]);
258 }
259 }
Akron00cd4d12016-05-31 21:01:11 +0200260 };
Akron179c8ac2015-06-30 19:30:50 +0200261 };
262
Akron5cb9b2b2018-07-24 17:01:09 +0200263
264 /*
265 * There is more than 0 matches, so allow for
266 * alignment toggling (left <=> right)
267 */
268 if (matchCount > 0)
269 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000270
Nils Diewald5c5a7472015-04-02 22:13:38 +0000271
Akron5cb9b2b2018-07-24 17:01:09 +0200272 /*
Akroncd42a142019-07-12 18:55:37 +0200273 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000274 */
275 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200276 vc.onMinimize = function () {
277 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200278 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200279 };
Nils Diewald6283d692015-04-23 20:32:53 +0000280
Akronec6bb8e2018-08-29 13:07:56 +0200281 vc.onOpen = function () {
282 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100283
284 var view = d.getElementById('vc-view');
285 if (!view.firstChild)
286 view.appendChild(this.element());
287
Akroncd42a142019-07-12 18:55:37 +0200288 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200289 };
290
291 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200292 if (vc.isOpen()) {
293 vc.minimize()
294 }
295 else {
Akronec6bb8e2018-08-29 13:07:56 +0200296 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200297 };
Nils Diewald58141332015-04-07 16:18:45 +0000298 };
Akron04671e72017-05-11 20:47:32 +0200299
Akron179c8ac2015-06-30 19:30:50 +0200300 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200301
302 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200303 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200304 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200305 };
Nils Diewald58141332015-04-07 16:18:45 +0000306 };
307
Akron19d97fe2016-09-06 20:47:05 +0200308
Nils Diewald58141332015-04-07 16:18:45 +0000309 /**
310 * Init Tutorial view
311 */
Akron0b489ad2018-02-02 16:49:32 +0100312 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000313 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100314 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200315 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000316 );
317 obj.tutorial = window.tutorial;
318 }
Nils Diewald58141332015-04-07 16:18:45 +0000319
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000320 // Tutorial is in parent
321 else if (window.parent) {
322 obj.tutorial = window.parent.tutorial;
323 };
324
Akron0b489ad2018-02-02 16:49:32 +0100325 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200326 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100327 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000328
Akron6ed13992016-05-23 18:06:05 +0200329 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100330 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200331 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000332
Nils Diewald845282c2015-05-14 07:53:03 +0000333
Nils Diewald58141332015-04-07 16:18:45 +0000334 /**
Akronc1457bf2015-06-11 19:24:00 +0200335 * Add VC creation on submission.
336 */
Akron0b489ad2018-02-02 16:49:32 +0100337 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200338 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200339 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100340 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200341
Akron19d97fe2016-09-06 20:47:05 +0200342 // No query was defined
343 if (qf.value === undefined || qf.value === '') {
344 qf.focus();
345 e.halt();
346 KorAP.log(700, "No query given");
347 return;
348 };
Akron7716f012015-07-01 20:38:32 +0200349
Akron19d97fe2016-09-06 20:47:05 +0200350 // Store session information
Akronf8035592018-05-24 20:40:51 +0200351 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200352
Akron19d97fe2016-09-06 20:47:05 +0200353 if (vc !== undefined) {
354 input.value = vc.toQuery();
355 }
356 else {
357 delete input['value'];
358 };
Akronc1457bf2015-06-11 19:24:00 +0200359 });
360 };
hebasta5df796f2019-05-21 15:27:12 +0200361
362
363 //Starts the guided tour at the next page
364 if(KorAP.session.get("tour")){
365 tourClass.gTshowResults().start();
366 }
367
Akronc1457bf2015-06-11 19:24:00 +0200368 /**
Nils Diewald58141332015-04-07 16:18:45 +0000369 * Init hint helper
370 * has to be final because of
371 * reposition
372 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000373 // Todo: Pass an element, so this works with
374 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200375 if (obj.hint === undefined)
376 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000377
Akron99713ef2017-06-28 18:19:28 +0200378 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100379 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200380
Akron2d0d96d2019-11-18 19:49:50 +0100381
382 /**
383 * Add query panel
384 */
385 var queryPanel = queryPanelClass.create();
386
387 // Get input field
388 var sform = d.getElementById("searchform");
389 var vcView = d.getElementById('vc-view')
390 if (sform && vcView) {
391 // The views are below the query bar
392 sform.insertBefore(queryPanel.element(), vcView);
393 KorAP.Panel = KorAP.Panel || {};
394 KorAP.Panel['query'] = queryPanel;
395 }
396
Nils Diewald58141332015-04-07 16:18:45 +0000397 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000398 });
hebasta75cfca52019-02-19 13:15:27 +0100399
Nils Diewald0e6992a2015-04-14 20:13:52 +0000400});