blob: 21887f360039604beeb4f2466e3f242a20a0b19e [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',
Nils Diewald7148c6f2015-05-04 15:07:53 +000029 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000030 'mailToChiffre',
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 'util'
32], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020033 hintClass,
34 vcClass,
35 tutClass,
36 domReady,
Akron19d97fe2016-09-06 20:47:05 +020037 vcArray,
38 alertifyClass,
39 sessionClass,
Akron4d926f12018-07-16 15:30:25 +020040 selectMenuClass,
Akron5cb9b2b2018-07-24 17:01:09 +020041 resultPanelClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000042
Akron9cc3eaf2015-06-10 22:15:52 +020043 // Localization values
Akron0b489ad2018-02-02 16:49:32 +010044 const loc = KorAP.Locale;
Akrond3903fb2017-11-13 11:13:44 +010045 loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora';
46 loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus';
Akron9a5b1e12016-12-06 18:18:23 +010047
Akron0b489ad2018-02-02 16:49:32 +010048 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020049
Akronf8035592018-05-24 20:40:51 +020050 KorAP.session = sessionClass.create('KalamarJS');
51
Nils Diewalda0defc42015-05-07 23:54:17 +000052 // Override KorAP.log
53 window.alertify = alertifyClass;
Akronc0a2da82018-07-04 15:27:37 +020054 KorAP.log = function (code, msg, src) {
55
56 if (src) {
57 msg += '<code class="src">'+src+'</code>';
58 };
Nils Diewalda0defc42015-05-07 23:54:17 +000059
60 // Use alertify to log errors
61 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020062 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020063 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000064 'error',
Akronf55504a2015-06-18 16:42:55 +020065 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000066 );
67 };
68
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 domReady(function (event) {
70 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020071
Akron4d926f12018-07-16 15:30:25 +020072 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020073 var show = KorAP.session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000074
75 /**
Akronf55504a2015-06-18 16:42:55 +020076 * Release notifications
77 */
78 if (KorAP.Notifications !== undefined) {
79 var n = KorAP.Notifications;
80 for (var i = 0; i < n.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +020081 alertifyClass.log(n[i][1], n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020082 };
83 };
84
85 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000086 * Replace Virtual Collection field
87 */
Akron5c829e92017-05-12 18:10:00 +020088 var vcname, vcchoose;
Akron0b489ad2018-02-02 16:49:32 +010089 var input = d.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000090 if (input) {
91 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +010092 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +000093 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020094 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020095
Akron27ae9ec2015-06-23 00:43:21 +020096 var currentVC = loc.VC_allCorpora;
97 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron19d97fe2016-09-06 20:47:05 +020098 currentVC = loc.VC_oneCollection;
Akron27ae9ec2015-06-23 00:43:21 +020099 };
100
Akron0b489ad2018-02-02 16:49:32 +0100101 vcchoose = vcname.addE('span');
Akron5c829e92017-05-12 18:10:00 +0200102
Akron0b489ad2018-02-02 16:49:32 +0100103 vcchoose.addT(
104 d.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +0000105 );
Akron27ae9ec2015-06-23 00:43:21 +0200106
Nils Diewald7148c6f2015-05-04 15:07:53 +0000107 input.parentNode.insertBefore(vcname, input);
108 };
109
Nils Diewald7148c6f2015-05-04 15:07:53 +0000110 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000111 * Add actions to match entries
112 */
Akron0b489ad2018-02-02 16:49:32 +0100113 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000114 '#search > ol > li:not(.active)'
115 );
Akron5cb9b2b2018-07-24 17:01:09 +0200116 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200117
Akron5cb9b2b2018-07-24 17:01:09 +0200118 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200119 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200120 if (this._match !== undefined)
121 this._match.open();
122 else {
123 // lazyLoad
124 matchClass.create(this).open();
125 };
Akrone0c32c72017-04-25 22:38:23 +0200126 // This would prevent the sidebar to go back
127 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000128 });
Akron5cb9b2b2018-07-24 17:01:09 +0200129 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200130 var code = _codeFromEvent(e);
131
132 switch (code) {
133 case 32:
134 if (this._match !== undefined)
135 this._match.toggle();
136 else {
137 // lazyLoad
138 matchClass.create(this).open();
139 };
140 e.halt();
141 break;
142 };
Akron6a535d42015-08-26 20:16:58 +0200143 });
Nils Diewalda297f062015-04-02 00:23:46 +0000144 };
145
Akrone0c32c72017-04-25 22:38:23 +0200146 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100147 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200148
149 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200150
Akron5258d462017-04-26 23:32:57 +0200151 // Horrible lock to deal with sidebar clicks
152 var asideClicked = false;
153
Akron1885ce92017-04-26 23:10:01 +0200154 // Make aside active on focus
155 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200156 this.classList.add('active');
157 });
158
Akron1885ce92017-04-26 23:10:01 +0200159 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100160 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200161 if (body !== null) {
162 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200163 if (!asideClicked) {
164 aside.classList.remove('active');
165 }
166 else {
167 asideClicked = false;
168 };
Akron1885ce92017-04-26 23:10:01 +0200169 });
170 };
171
172 /* Stop click event on aside
173 * (to not trickle down to body)
174 */
175 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200176 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200177 });
178 };
Akronb9cdb102017-04-25 00:52:31 +0200179
180
Akron6bb71582016-06-10 20:41:08 +0200181 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100182 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200183 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100184 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100185 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200186 ).limit(5);
187 };
Akron6bb71582016-06-10 20:41:08 +0200188
Akron4d926f12018-07-16 15:30:25 +0200189 var resultInfo = d.getElementById('resultinfo');
190
Akron4d926f12018-07-16 15:30:25 +0200191 /**
192 * Add result panel
193 */
Akron5cb9b2b2018-07-24 17:01:09 +0200194 var resultPanel = resultPanelClass.create(show);
195
Akron4d926f12018-07-16 15:30:25 +0200196 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200197
198 // Move buttons to resultinfo
199 resultInfo.appendChild(resultPanel.actions.element());
200
Akrone6538cd2018-07-16 17:52:33 +0200201 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200202 var sb = d.getElementById('search');
203 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200204 };
205
206
Akron179c8ac2015-06-30 19:30:50 +0200207 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200208 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200209
210 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200211 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200212 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200213 };
Akron7716f012015-07-01 20:38:32 +0200214
Akron00cd4d12016-05-31 21:01:11 +0200215 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200216 var errors = KorAP.koralQuery["errors"];
217 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200218
Akron19d97fe2016-09-06 20:47:05 +0200219 // Malformed query
220 if (errors[i][0] === 302 && errors[i][2]) {
221 obj.hint = hintClass.create();
222 obj.hint.alert(errors[i][2], errors[i][1]);
223 break;
224 }
Akronf0c31ed2016-06-11 11:27:01 +0200225
Akron19d97fe2016-09-06 20:47:05 +0200226 // no query
227 else if (errors[i][0] === 301) {
228 obj.hint = hintClass.create();
229 obj.hint.alert(0, errors[i][1]);
230 }
231 }
Akron00cd4d12016-05-31 21:01:11 +0200232 };
Akron179c8ac2015-06-30 19:30:50 +0200233 };
234
Akron5cb9b2b2018-07-24 17:01:09 +0200235
236 /*
237 * There is more than 0 matches, so allow for
238 * alignment toggling (left <=> right)
239 */
240 if (matchCount > 0)
241 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000242
Nils Diewald5c5a7472015-04-02 22:13:38 +0000243
Akron5cb9b2b2018-07-24 17:01:09 +0200244 /*
Nils Diewald7148c6f2015-05-04 15:07:53 +0000245 * Toggle the Virtual Collection builder
246 */
247 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000248 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200249 var vcclick = function () {
Akron0b489ad2018-02-02 16:49:32 +0100250 var view = d.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000251
Akron19d97fe2016-09-06 20:47:05 +0200252 // The vc is visible
253 if (vcname.classList.contains('active')) {
254 view.removeChild(vc.element());
255 vcname.classList.remove('active');
256 delete show['collection'];
257 }
Nils Diewald6283d692015-04-23 20:32:53 +0000258
Akron19d97fe2016-09-06 20:47:05 +0200259 // The vc is not visible
260 else {
261 if (vc === undefined)
262 vc = _getCurrentVC(vcClass, vcArray);
263 view.appendChild(vc.element());
264 vcname.classList.add('active');
265 show['collection'] = true;
266 };
Nils Diewald58141332015-04-07 16:18:45 +0000267 };
Akron04671e72017-05-11 20:47:32 +0200268
Akron179c8ac2015-06-30 19:30:50 +0200269 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200270
271 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200272 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200273 vcclick.apply();
Akron5c829e92017-05-12 18:10:00 +0200274 }
Akron04671e72017-05-11 20:47:32 +0200275
Akron5c829e92017-05-12 18:10:00 +0200276 // else
277 else if (_checkVCrewrite(vcClass)) {
278 vcchoose.classList.add('rewritten');
Akron04671e72017-05-11 20:47:32 +0200279 };
Nils Diewald58141332015-04-07 16:18:45 +0000280 };
281
Akron19d97fe2016-09-06 20:47:05 +0200282
Nils Diewald58141332015-04-07 16:18:45 +0000283 /**
284 * Init Tutorial view
285 */
Akron0b489ad2018-02-02 16:49:32 +0100286 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000287 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100288 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200289 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000290 );
291 obj.tutorial = window.tutorial;
292 }
Nils Diewald58141332015-04-07 16:18:45 +0000293
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000294 // Tutorial is in parent
295 else if (window.parent) {
296 obj.tutorial = window.parent.tutorial;
297 };
298
Akron0b489ad2018-02-02 16:49:32 +0100299 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200300 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100301 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000302
Akron6ed13992016-05-23 18:06:05 +0200303 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100304 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200305 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000306
Nils Diewald845282c2015-05-14 07:53:03 +0000307
Nils Diewald58141332015-04-07 16:18:45 +0000308 /**
Akronc1457bf2015-06-11 19:24:00 +0200309 * Add VC creation on submission.
310 */
Akron0b489ad2018-02-02 16:49:32 +0100311 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200312 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200313 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100314 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200315
Akron19d97fe2016-09-06 20:47:05 +0200316 // No query was defined
317 if (qf.value === undefined || qf.value === '') {
318 qf.focus();
319 e.halt();
320 KorAP.log(700, "No query given");
321 return;
322 };
Akron7716f012015-07-01 20:38:32 +0200323
Akron19d97fe2016-09-06 20:47:05 +0200324 // Store session information
Akronf8035592018-05-24 20:40:51 +0200325 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200326
Akron19d97fe2016-09-06 20:47:05 +0200327 // Set Virtual collection
328 if (vc === undefined) {
329 vc = _getCurrentVC(vcClass, vcArray);
330 };
Akron7716f012015-07-01 20:38:32 +0200331
Akron19d97fe2016-09-06 20:47:05 +0200332 if (vc !== undefined) {
333 input.value = vc.toQuery();
334 }
335 else {
336 delete input['value'];
337 };
Akronc1457bf2015-06-11 19:24:00 +0200338 });
339 };
340
341 /**
Nils Diewald58141332015-04-07 16:18:45 +0000342 * Init hint helper
343 * has to be final because of
344 * reposition
345 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000346 // Todo: Pass an element, so this works with
347 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200348 if (obj.hint === undefined)
349 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000350
Akron99713ef2017-06-28 18:19:28 +0200351 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100352 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200353
Nils Diewald58141332015-04-07 16:18:45 +0000354 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000355 });
356});
Akronc1457bf2015-06-11 19:24:00 +0200357
Akron5cb9b2b2018-07-24 17:01:09 +0200358
Akronc1457bf2015-06-11 19:24:00 +0200359// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200360function _getCurrentVC (vcClass, vcArray) {
361 var vc = vcClass.create(vcArray);
Akron1bdf5272018-07-24 18:51:30 +0200362 try {
363 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
364 vc.fromJson(KorAP.koralQuery["collection"]);
365 };
366 }
367 catch (e) {
368 KorAP.log(0, e);
369 return;
370 }
Akronc1457bf2015-06-11 19:24:00 +0200371 return vc;
372};
Akron04671e72017-05-11 20:47:32 +0200373
Akron5cb9b2b2018-07-24 17:01:09 +0200374
Akron04671e72017-05-11 20:47:32 +0200375function _checkVCrewrite (vcClass) {
376 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
377 return vcClass.checkRewrite(KorAP.koralQuery["collection"]);
378 };
379 return false;
380}