blob: 0b31e237d0e487f3c187ab13c38cc3e7c653afae [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') || {};
hebasta043e96f2019-11-28 12:33:00 +010087
88 KorAP.Panel = KorAP.Panel || {}
Nils Diewalda297f062015-04-02 00:23:46 +000089
90 /**
Akronf55504a2015-06-18 16:42:55 +020091 * Release notifications
92 */
93 if (KorAP.Notifications !== undefined) {
94 var n = KorAP.Notifications;
95 for (var i = 0; i < n.length; i++) {
Akron8ea84292018-10-24 13:41:52 +020096 var msg = n[i][1];
97 if (n[i][2]) {
98 msg += '<code class="src">'+n[i][2]+'</code>';
99 };
100 alertifyClass.log(msg, n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +0200101 };
102 };
103
104 /**
Akroncd42a142019-07-12 18:55:37 +0200105 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000106 */
Akron5c829e92017-05-12 18:10:00 +0200107 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200108 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200109
hebasta2758b582018-11-19 15:59:42 +0100110 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100111
Akron1f0521b2018-08-28 13:01:24 +0200112 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000113 if (input) {
114 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100115 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000116 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200117 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200118
Akron1f0521b2018-08-28 13:01:24 +0200119 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200120 // Supports "collection" for legacy reasons
121 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200122 try {
Akroncd42a142019-07-12 18:55:37 +0200123 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200124 }
125 catch (e) {
126 KorAP.log(0,e);
127 }
Akron27ae9ec2015-06-23 00:43:21 +0200128 };
129
Akron0b489ad2018-02-02 16:49:32 +0100130 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200131 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200132
Akron1f0521b2018-08-28 13:01:24 +0200133 if (vc.wasRewritten()) {
134 vcchoose.classList.add('rewritten');
135 };
136
Nils Diewald7148c6f2015-05-04 15:07:53 +0000137 input.parentNode.insertBefore(vcname, input);
138 };
139
Nils Diewald7148c6f2015-05-04 15:07:53 +0000140 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000141 * Add actions to match entries
142 */
Akron0b489ad2018-02-02 16:49:32 +0100143 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000144 '#search > ol > li:not(.active)'
145 );
Akron5cb9b2b2018-07-24 17:01:09 +0200146 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200147
Akron5cb9b2b2018-07-24 17:01:09 +0200148 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200149 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200150 if (this._match !== undefined)
151 this._match.open();
152 else {
153 // lazyLoad
154 matchClass.create(this).open();
155 };
Akrone0c32c72017-04-25 22:38:23 +0200156 // This would prevent the sidebar to go back
157 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000158 });
Akron5cb9b2b2018-07-24 17:01:09 +0200159 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200160 var code = _codeFromEvent(e);
161
162 switch (code) {
163 case 32:
164 if (this._match !== undefined)
165 this._match.toggle();
166 else {
167 // lazyLoad
168 matchClass.create(this).open();
169 };
170 e.halt();
171 break;
172 };
Akron6a535d42015-08-26 20:16:58 +0200173 });
Nils Diewalda297f062015-04-02 00:23:46 +0000174 };
175
Akrone0c32c72017-04-25 22:38:23 +0200176 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100177 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200178
179 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200180
Akron5258d462017-04-26 23:32:57 +0200181 // Horrible lock to deal with sidebar clicks
182 var asideClicked = false;
183
Akron1885ce92017-04-26 23:10:01 +0200184 // Make aside active on focus
185 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200186 this.classList.add('active');
187 });
188
Akron1885ce92017-04-26 23:10:01 +0200189 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100190 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200191 if (body !== null) {
192 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200193 if (!asideClicked) {
194 aside.classList.remove('active');
195 }
196 else {
197 asideClicked = false;
198 };
Akron1885ce92017-04-26 23:10:01 +0200199 });
200 };
201
202 /* Stop click event on aside
203 * (to not trickle down to body)
204 */
205 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200206 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200207 });
208 };
Akronb9cdb102017-04-25 00:52:31 +0200209
210
Akron6bb71582016-06-10 20:41:08 +0200211 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100212 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200213 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100214 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100215 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200216 ).limit(5);
217 };
Akron6bb71582016-06-10 20:41:08 +0200218
Akron4d926f12018-07-16 15:30:25 +0200219 var resultInfo = d.getElementById('resultinfo');
220
Akron4d926f12018-07-16 15:30:25 +0200221 /**
222 * Add result panel
223 */
Akron5cb9b2b2018-07-24 17:01:09 +0200224 var resultPanel = resultPanelClass.create(show);
225
Akron4d926f12018-07-16 15:30:25 +0200226 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200227
228 // Move buttons to resultinfo
229 resultInfo.appendChild(resultPanel.actions.element());
230
Akrone6538cd2018-07-16 17:52:33 +0200231 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200232 var sb = d.getElementById('search');
233 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200234 };
235
236
Akron179c8ac2015-06-30 19:30:50 +0200237 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200238 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200239
240 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200241 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200242 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200243 };
Akron7716f012015-07-01 20:38:32 +0200244
Akron00cd4d12016-05-31 21:01:11 +0200245 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200246 var errors = KorAP.koralQuery["errors"];
247 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200248
Akron19d97fe2016-09-06 20:47:05 +0200249 // Malformed query
250 if (errors[i][0] === 302 && errors[i][2]) {
251 obj.hint = hintClass.create();
252 obj.hint.alert(errors[i][2], errors[i][1]);
253 break;
254 }
Akronf0c31ed2016-06-11 11:27:01 +0200255
Akron19d97fe2016-09-06 20:47:05 +0200256 // no query
257 else if (errors[i][0] === 301) {
258 obj.hint = hintClass.create();
259 obj.hint.alert(0, errors[i][1]);
260 }
261 }
Akron00cd4d12016-05-31 21:01:11 +0200262 };
Akron179c8ac2015-06-30 19:30:50 +0200263 };
264
Akron5cb9b2b2018-07-24 17:01:09 +0200265
266 /*
267 * There is more than 0 matches, so allow for
268 * alignment toggling (left <=> right)
269 */
270 if (matchCount > 0)
271 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000272
hebasta043e96f2019-11-28 12:33:00 +0100273 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200274 /*
Akroncd42a142019-07-12 18:55:37 +0200275 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000276 */
277 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200278 vc.onMinimize = function () {
279 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200280 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200281 };
Nils Diewald6283d692015-04-23 20:32:53 +0000282
Akronec6bb8e2018-08-29 13:07:56 +0200283 vc.onOpen = function () {
284 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100285
286 var view = d.getElementById('vc-view');
287 if (!view.firstChild)
288 view.appendChild(this.element());
289
Akroncd42a142019-07-12 18:55:37 +0200290 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200291 };
292
293 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200294 if (vc.isOpen()) {
295 vc.minimize()
296 }
297 else {
Akronec6bb8e2018-08-29 13:07:56 +0200298 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200299 };
Nils Diewald58141332015-04-07 16:18:45 +0000300 };
Akron04671e72017-05-11 20:47:32 +0200301
Akron179c8ac2015-06-30 19:30:50 +0200302 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200303
304 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200305 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200306 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200307 };
Nils Diewald58141332015-04-07 16:18:45 +0000308 };
309
Akron19d97fe2016-09-06 20:47:05 +0200310
Nils Diewald58141332015-04-07 16:18:45 +0000311 /**
312 * Init Tutorial view
313 */
Akron0b489ad2018-02-02 16:49:32 +0100314 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000315 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100316 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200317 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000318 );
319 obj.tutorial = window.tutorial;
320 }
Nils Diewald58141332015-04-07 16:18:45 +0000321
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000322 // Tutorial is in parent
323 else if (window.parent) {
324 obj.tutorial = window.parent.tutorial;
325 };
326
Akron0b489ad2018-02-02 16:49:32 +0100327 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200328 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100329 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000330
Akron6ed13992016-05-23 18:06:05 +0200331 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100332 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200333 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000334
Nils Diewald845282c2015-05-14 07:53:03 +0000335
Nils Diewald58141332015-04-07 16:18:45 +0000336 /**
Akronc1457bf2015-06-11 19:24:00 +0200337 * Add VC creation on submission.
338 */
Akron0b489ad2018-02-02 16:49:32 +0100339 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200340 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200341 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100342 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200343
Akron19d97fe2016-09-06 20:47:05 +0200344 // No query was defined
345 if (qf.value === undefined || qf.value === '') {
346 qf.focus();
347 e.halt();
348 KorAP.log(700, "No query given");
349 return;
350 };
Akron7716f012015-07-01 20:38:32 +0200351
Akron19d97fe2016-09-06 20:47:05 +0200352 // Store session information
Akronf8035592018-05-24 20:40:51 +0200353 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200354
Akron19d97fe2016-09-06 20:47:05 +0200355 if (vc !== undefined) {
356 input.value = vc.toQuery();
357 }
358 else {
359 delete input['value'];
360 };
Akronc1457bf2015-06-11 19:24:00 +0200361 });
362 };
hebasta5df796f2019-05-21 15:27:12 +0200363
364
365 //Starts the guided tour at the next page
366 if(KorAP.session.get("tour")){
367 tourClass.gTshowResults().start();
368 }
369
Akronc1457bf2015-06-11 19:24:00 +0200370 /**
Nils Diewald58141332015-04-07 16:18:45 +0000371 * Init hint helper
372 * has to be final because of
373 * reposition
374 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000375 // Todo: Pass an element, so this works with
376 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200377 if (obj.hint === undefined)
378 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000379
Akron99713ef2017-06-28 18:19:28 +0200380 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100381 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200382
Akron2d0d96d2019-11-18 19:49:50 +0100383
384 /**
385 * Add query panel
386 */
387 var queryPanel = queryPanelClass.create();
388
389 // Get input field
390 var sform = d.getElementById("searchform");
391 var vcView = d.getElementById('vc-view')
392 if (sform && vcView) {
393 // The views are below the query bar
394 sform.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100395 KorAP.Panel['query'] = queryPanel;
396 }
397
Nils Diewald58141332015-04-07 16:18:45 +0000398 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000399 });
hebasta75cfca52019-02-19 13:15:27 +0100400
Nils Diewald0e6992a2015-04-14 20:13:52 +0000401});