blob: 44823162acc5518b980db9c99b66ccaafcbba204 [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) {
99 var n = KorAP.Notifications;
100 for (var i = 0; i < n.length; i++) {
Akron8ea84292018-10-24 13:41:52 +0200101 var msg = n[i][1];
102 if (n[i][2]) {
103 msg += '<code class="src">'+n[i][2]+'</code>';
104 };
105 alertifyClass.log(msg, n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +0200106 };
107 };
108
109 /**
Akroncd42a142019-07-12 18:55:37 +0200110 * Replace Virtual Corpus field
Nils Diewald7148c6f2015-05-04 15:07:53 +0000111 */
Akron5c829e92017-05-12 18:10:00 +0200112 var vcname, vcchoose;
Akroncd42a142019-07-12 18:55:37 +0200113 var input = d.getElementById('cq');
Akron1f0521b2018-08-28 13:01:24 +0200114
hebasta2758b582018-11-19 15:59:42 +0100115 var vc = KorAP.vc;
hebasta48842cf2018-12-11 12:57:38 +0100116
Akron1f0521b2018-08-28 13:01:24 +0200117 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +0000118 if (input) {
119 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100120 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000121 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200122 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200123
Akron1f0521b2018-08-28 13:01:24 +0200124 // Load virtual corpus object
Akroncd42a142019-07-12 18:55:37 +0200125 // Supports "collection" for legacy reasons
126 if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) {
Akron1f0521b2018-08-28 13:01:24 +0200127 try {
Akroncd42a142019-07-12 18:55:37 +0200128 vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]);
Akron1f0521b2018-08-28 13:01:24 +0200129 }
130 catch (e) {
131 KorAP.log(0,e);
132 }
Akron27ae9ec2015-06-23 00:43:21 +0200133 };
134
Akron0b489ad2018-02-02 16:49:32 +0100135 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200136 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200137
Akron1f0521b2018-08-28 13:01:24 +0200138 if (vc.wasRewritten()) {
139 vcchoose.classList.add('rewritten');
140 };
141
Nils Diewald7148c6f2015-05-04 15:07:53 +0000142 input.parentNode.insertBefore(vcname, input);
143 };
144
Nils Diewald7148c6f2015-05-04 15:07:53 +0000145 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000146 * Add actions to match entries
147 */
Akron3c390c42020-03-30 09:06:21 +0200148 var li = d.querySelectorAll(
149 '#search > ol > li'
Nils Diewald5c5a7472015-04-02 22:13:38 +0000150 );
Akron5cb9b2b2018-07-24 17:01:09 +0200151 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200152
Akron3c390c42020-03-30 09:06:21 +0200153 for (matchCount = 0; matchCount < li.length; matchCount++) {
154
155 let e = li[matchCount];
156
157 // Define class for active elements
158 if (e.classList.contains('active')) {
159 if (this._match === undefined) {
Akron19d97fe2016-09-06 20:47:05 +0200160 // lazyLoad
Akron3c390c42020-03-30 09:06:21 +0200161 matchClass.create(e).init();
Akron19d97fe2016-09-06 20:47:05 +0200162 };
Akron3c390c42020-03-30 09:06:21 +0200163 }
164
165 // Define class for inactive elements
166 else {
167 e.addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200168 if (this._match !== undefined)
Akron3c390c42020-03-30 09:06:21 +0200169 this._match.open();
Akron19d97fe2016-09-06 20:47:05 +0200170 else {
171 // lazyLoad
172 matchClass.create(this).open();
173 };
Akron3c390c42020-03-30 09:06:21 +0200174 // This would prevent the sidebar to go back
175 // e.halt();
176 });
177 e.addEventListener('keydown', function (e) {
178 var code = _codeFromEvent(e);
179
180 switch (code) {
181 case 32:
182 if (this._match !== undefined)
183 this._match.toggle();
184 else {
185 // lazyLoad
186 matchClass.create(this).open();
187 };
188 e.halt();
189 break;
190 };
191 });
192 };
Nils Diewalda297f062015-04-02 00:23:46 +0000193 };
Akron3c390c42020-03-30 09:06:21 +0200194
Akrone0c32c72017-04-25 22:38:23 +0200195 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100196 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200197
198 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200199
Akron5258d462017-04-26 23:32:57 +0200200 // Horrible lock to deal with sidebar clicks
201 var asideClicked = false;
202
Akron1885ce92017-04-26 23:10:01 +0200203 // Make aside active on focus
204 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200205 this.classList.add('active');
206 });
207
Akron1885ce92017-04-26 23:10:01 +0200208 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100209 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200210 if (body !== null) {
211 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200212 if (!asideClicked) {
213 aside.classList.remove('active');
214 }
215 else {
216 asideClicked = false;
217 };
Akron1885ce92017-04-26 23:10:01 +0200218 });
219 };
220
221 /* Stop click event on aside
222 * (to not trickle down to body)
223 */
224 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200225 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200226 });
227 };
Akronb9cdb102017-04-25 00:52:31 +0200228
229
Akron6bb71582016-06-10 20:41:08 +0200230 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100231 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200232 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100233 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100234 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200235 ).limit(5);
236 };
Akron6bb71582016-06-10 20:41:08 +0200237
Akron4d926f12018-07-16 15:30:25 +0200238 var resultInfo = d.getElementById('resultinfo');
239
Akron4d926f12018-07-16 15:30:25 +0200240 /**
241 * Add result panel
242 */
Akron5cb9b2b2018-07-24 17:01:09 +0200243 var resultPanel = resultPanelClass.create(show);
244
Akron4d926f12018-07-16 15:30:25 +0200245 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200246
247 // Move buttons to resultinfo
248 resultInfo.appendChild(resultPanel.actions.element());
249
Akrone6538cd2018-07-16 17:52:33 +0200250 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200251 var sb = d.getElementById('search');
252 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200253 };
254
255
Akron179c8ac2015-06-30 19:30:50 +0200256 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200257 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200258
259 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200260 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200261 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200262 };
Akron7716f012015-07-01 20:38:32 +0200263
Akron00cd4d12016-05-31 21:01:11 +0200264 if (KorAP.koralQuery["errors"]) {
Akron678c26f2020-10-09 08:52:50 +0200265 KorAP.koralQuery["errors"].forEach(function(e) {
Akronf0c31ed2016-06-11 11:27:01 +0200266
Akron19d97fe2016-09-06 20:47:05 +0200267 // Malformed query
Akron678c26f2020-10-09 08:52:50 +0200268 if (e[0] === 302 && e[2]) {
Akron19d97fe2016-09-06 20:47:05 +0200269 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200270 obj.hint.alert(e[2], e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200271 }
Akronf0c31ed2016-06-11 11:27:01 +0200272
Akron19d97fe2016-09-06 20:47:05 +0200273 // no query
Akron678c26f2020-10-09 08:52:50 +0200274 else if (e[0] === 301) {
Akron19d97fe2016-09-06 20:47:05 +0200275 obj.hint = hintClass.create();
Akron678c26f2020-10-09 08:52:50 +0200276 obj.hint.alert(0, e[1]);
Akron19d97fe2016-09-06 20:47:05 +0200277 }
Akron678c26f2020-10-09 08:52:50 +0200278 });
Akron00cd4d12016-05-31 21:01:11 +0200279 };
Akron179c8ac2015-06-30 19:30:50 +0200280 };
281
Akron5cb9b2b2018-07-24 17:01:09 +0200282
283 /*
284 * There is more than 0 matches, so allow for
285 * alignment toggling (left <=> right)
286 */
287 if (matchCount > 0)
288 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000289
hebasta043e96f2019-11-28 12:33:00 +0100290 KorAP.Panel['result'] = resultPanel;
Akron5cb9b2b2018-07-24 17:01:09 +0200291 /*
Akroncd42a142019-07-12 18:55:37 +0200292 * Toggle the Virtual Corpus builder
Nils Diewald7148c6f2015-05-04 15:07:53 +0000293 */
294 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200295 vc.onMinimize = function () {
296 vcname.classList.remove('active');
Akroncd42a142019-07-12 18:55:37 +0200297 delete show['vc'];
Akronec6bb8e2018-08-29 13:07:56 +0200298 };
Nils Diewald6283d692015-04-23 20:32:53 +0000299
Akronec6bb8e2018-08-29 13:07:56 +0200300 vc.onOpen = function () {
301 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100302
303 var view = d.getElementById('vc-view');
304 if (!view.firstChild)
305 view.appendChild(this.element());
306
Akroncd42a142019-07-12 18:55:37 +0200307 show['vc'] = true;
Akronec6bb8e2018-08-29 13:07:56 +0200308 };
309
310 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200311 if (vc.isOpen()) {
312 vc.minimize()
313 }
314 else {
Akronec6bb8e2018-08-29 13:07:56 +0200315 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200316 };
Nils Diewald58141332015-04-07 16:18:45 +0000317 };
Akron04671e72017-05-11 20:47:32 +0200318
Akron179c8ac2015-06-30 19:30:50 +0200319 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200320
321 // Click, if the VC should be shown
Akroncd42a142019-07-12 18:55:37 +0200322 if (show['vc']) {
Akron19d97fe2016-09-06 20:47:05 +0200323 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200324 };
Nils Diewald58141332015-04-07 16:18:45 +0000325 };
326
Akron19d97fe2016-09-06 20:47:05 +0200327
Nils Diewald58141332015-04-07 16:18:45 +0000328 /**
329 * Init Tutorial view
330 */
Akron0b489ad2018-02-02 16:49:32 +0100331 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000332 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100333 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200334 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000335 );
336 obj.tutorial = window.tutorial;
337 }
Nils Diewald58141332015-04-07 16:18:45 +0000338
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000339 // Tutorial is in parent
340 else if (window.parent) {
341 obj.tutorial = window.parent.tutorial;
342 };
343
Akron0b489ad2018-02-02 16:49:32 +0100344 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200345 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100346 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000347
Akron6ed13992016-05-23 18:06:05 +0200348 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100349 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200350 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000351
Nils Diewald845282c2015-05-14 07:53:03 +0000352
Nils Diewald58141332015-04-07 16:18:45 +0000353 /**
Akronc1457bf2015-06-11 19:24:00 +0200354 * Add VC creation on submission.
355 */
Akron0b489ad2018-02-02 16:49:32 +0100356 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200357 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200358 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100359 var qf = d.getElementById('q-field');
Akron1be6c1c2020-01-07 15:29:58 +0100360
Akron19d97fe2016-09-06 20:47:05 +0200361 // No query was defined
362 if (qf.value === undefined || qf.value === '') {
363 qf.focus();
364 e.halt();
365 KorAP.log(700, "No query given");
366 return;
367 };
Akron1be6c1c2020-01-07 15:29:58 +0100368
Akron19d97fe2016-09-06 20:47:05 +0200369 // Store session information
Akronf8035592018-05-24 20:40:51 +0200370 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200371
Akron19d97fe2016-09-06 20:47:05 +0200372 if (vc !== undefined) {
373 input.value = vc.toQuery();
Akrond7ad9072019-12-09 07:08:20 +0100374 if (input.value == '')
375 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200376 }
377 else {
Akrond7ad9072019-12-09 07:08:20 +0100378 input.removeAttribute('value');
379 input.removeAttribute('name');
Akron19d97fe2016-09-06 20:47:05 +0200380 };
Akron1be6c1c2020-01-07 15:29:58 +0100381
382 // This would preferably set the query to be "disabled",
383 // but in that case the query wouldn't be submitted
384 // at all.
385 // Setting the cursor to "progress" fails in current versions
386 // of webkit.
387 qf.classList.add("loading");
388 d.getElementById('qsubmit').classList.add("loading");
Akronc1457bf2015-06-11 19:24:00 +0200389 });
390 };
hebasta5df796f2019-05-21 15:27:12 +0200391
392
393 //Starts the guided tour at the next page
394 if(KorAP.session.get("tour")){
395 tourClass.gTshowResults().start();
396 }
397
Akronc1457bf2015-06-11 19:24:00 +0200398 /**
Nils Diewald58141332015-04-07 16:18:45 +0000399 * Init hint helper
400 * has to be final because of
401 * reposition
402 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000403 // Todo: Pass an element, so this works with
404 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200405 if (obj.hint === undefined)
406 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000407
Akron99713ef2017-06-28 18:19:28 +0200408 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100409 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200410
Akron2d0d96d2019-11-18 19:49:50 +0100411
412 /**
413 * Add query panel
414 */
415 var queryPanel = queryPanelClass.create();
416
417 // Get input field
418 var sform = d.getElementById("searchform");
419 var vcView = d.getElementById('vc-view')
420 if (sform && vcView) {
421 // The views are below the query bar
422 sform.insertBefore(queryPanel.element(), vcView);
Akron2d0d96d2019-11-18 19:49:50 +0100423 KorAP.Panel['query'] = queryPanel;
424 }
Akron24f48ea2020-07-01 09:37:19 +0200425
426 /**
427 * Initialize Plugin registry.
428 */
429 let p = KorAP.Plugins;
430 if (p && p.length > 0) {
431 // Load Plugin Server first
432 KorAP.Plugin = pluginClass.create();
433
434 // Add services container to head
435 d.head.appendChild(KorAP.Plugin.element());
436
437 // Add pipe form
438 KorAP.Pipe = pipeClass.create();
439 d.getElementById("searchform").appendChild(KorAP.Pipe.element());
440
441 try {
442
443 // Register all plugins
444 for (var i = 0; i < p.length; i++) {
445 KorAP.Plugin.register(p[i]);
446 }
447 }
448 catch (e) {
449 KorAP.log(0, e);
450 }
451 };
452
Nils Diewald58141332015-04-07 16:18:45 +0000453 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000454 });
hebasta75cfca52019-02-19 13:15:27 +0100455
Nils Diewald0e6992a2015-04-14 20:13:52 +0000456});