blob: 0e9944de5c23e910944fe0dc01378304adf7899c [file] [log] [blame]
Akron7524be12016-06-01 17:31:33 +02001/*
2 * TODO: Create lazy loading of objects including
3 * - obj.hint()
4 * - obj.alertify()
5 * - obj.session()
6 * - obj.tutorial()
7 * - obj.vc() // toggle
8 * - obj.matchCreate() (using webpack)
9 * - obj.koral() (show result, parse for errors ...)
10 * - obj.alignment() // toggle
11 */
12
Nils Diewald0e6992a2015-04-14 20:13:52 +000013define([
14 'match',
15 'hint',
16 'vc',
17 'tutorial',
Akronbec4a6a2018-07-10 14:45:15 +020018 'buttongroup',
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 'lib/domReady',
Akron27ae9ec2015-06-23 00:43:21 +020020 'vc/array',
Nils Diewald7148c6f2015-05-04 15:07:53 +000021 'lib/alertify',
Akron7716f012015-07-01 20:38:32 +020022 'session',
Akron6bb71582016-06-10 20:41:08 +020023 'selectMenu',
Akron4d926f12018-07-16 15:30:25 +020024 'panel',
25 'view/koralquery',
Nils Diewald7148c6f2015-05-04 15:07:53 +000026 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000027 'mailToChiffre',
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 'util'
29], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020030 hintClass,
31 vcClass,
32 tutClass,
Akronbec4a6a2018-07-10 14:45:15 +020033 buttonGroupClass,
Akron19d97fe2016-09-06 20:47:05 +020034 domReady,
Akron19d97fe2016-09-06 20:47:05 +020035 vcArray,
36 alertifyClass,
37 sessionClass,
Akron4d926f12018-07-16 15:30:25 +020038 selectMenuClass,
39 panelClass,
40 kqClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000041
Akron9cc3eaf2015-06-10 22:15:52 +020042 // Localization values
Akron0b489ad2018-02-02 16:49:32 +010043 const loc = KorAP.Locale;
Akrond3903fb2017-11-13 11:13:44 +010044 loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora';
45 loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus';
46 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle alignment';
Akron7716f012015-07-01 20:38:32 +020047 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akron9a5b1e12016-12-06 18:18:23 +010048
Akron0b489ad2018-02-02 16:49:32 +010049 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020050
Akronf8035592018-05-24 20:40:51 +020051 KorAP.session = sessionClass.create('KalamarJS');
52
Nils Diewalda0defc42015-05-07 23:54:17 +000053 // Override KorAP.log
54 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020055 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000056
57 // Use alertify to log errors
58 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020059 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020060 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000061 'error',
Akronf55504a2015-06-18 16:42:55 +020062 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000063 );
64 };
65
Nils Diewald0e6992a2015-04-14 20:13:52 +000066 domReady(function (event) {
67 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020068
Akron4d926f12018-07-16 15:30:25 +020069 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020070 var show = KorAP.session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000071
72 /**
Akronf55504a2015-06-18 16:42:55 +020073 * Release notifications
74 */
75 if (KorAP.Notifications !== undefined) {
76 var n = KorAP.Notifications;
77 for (var i = 0; i < n.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +020078 alertifyClass.log(n[i][1], n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020079 };
80 };
81
82 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000083 * Replace Virtual Collection field
84 */
Akron5c829e92017-05-12 18:10:00 +020085 var vcname, vcchoose;
Akron0b489ad2018-02-02 16:49:32 +010086 var input = d.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000087 if (input) {
88 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +010089 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +000090 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020091 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020092
Akron27ae9ec2015-06-23 00:43:21 +020093 var currentVC = loc.VC_allCorpora;
94 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron19d97fe2016-09-06 20:47:05 +020095 currentVC = loc.VC_oneCollection;
Akron27ae9ec2015-06-23 00:43:21 +020096 };
97
Akron0b489ad2018-02-02 16:49:32 +010098 vcchoose = vcname.addE('span');
Akron5c829e92017-05-12 18:10:00 +020099
Akron0b489ad2018-02-02 16:49:32 +0100100 vcchoose.addT(
101 d.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +0000102 );
Akron27ae9ec2015-06-23 00:43:21 +0200103
Nils Diewald7148c6f2015-05-04 15:07:53 +0000104 input.parentNode.insertBefore(vcname, input);
105 };
106
Nils Diewald7148c6f2015-05-04 15:07:53 +0000107 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000108 * Add actions to match entries
109 */
Akron0b489ad2018-02-02 16:49:32 +0100110 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000111 '#search > ol > li:not(.active)'
112 );
Nils Diewalda297f062015-04-02 00:23:46 +0000113 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200114
Nils Diewalda297f062015-04-02 00:23:46 +0000115 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000116 inactiveLi[i].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200117 if (this._match !== undefined)
118 this._match.open();
119 else {
120 // lazyLoad
121 matchClass.create(this).open();
122 };
Akrone0c32c72017-04-25 22:38:23 +0200123 // This would prevent the sidebar to go back
124 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000125 });
Akron6a535d42015-08-26 20:16:58 +0200126 inactiveLi[i].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200127 var code = _codeFromEvent(e);
128
129 switch (code) {
130 case 32:
131 if (this._match !== undefined)
132 this._match.toggle();
133 else {
134 // lazyLoad
135 matchClass.create(this).open();
136 };
137 e.halt();
138 break;
139 };
Akron6a535d42015-08-26 20:16:58 +0200140 });
Nils Diewalda297f062015-04-02 00:23:46 +0000141 };
142
Akrone0c32c72017-04-25 22:38:23 +0200143 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100144 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200145
146 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200147
Akron5258d462017-04-26 23:32:57 +0200148 // Horrible lock to deal with sidebar clicks
149 var asideClicked = false;
150
Akron1885ce92017-04-26 23:10:01 +0200151 // Make aside active on focus
152 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200153 this.classList.add('active');
154 });
155
Akron1885ce92017-04-26 23:10:01 +0200156 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100157 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200158 if (body !== null) {
159 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200160 if (!asideClicked) {
161 aside.classList.remove('active');
162 }
163 else {
164 asideClicked = false;
165 };
Akron1885ce92017-04-26 23:10:01 +0200166 });
167 };
168
169 /* Stop click event on aside
170 * (to not trickle down to body)
171 */
172 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200173 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200174 });
175 };
Akronb9cdb102017-04-25 00:52:31 +0200176
177
Akron6bb71582016-06-10 20:41:08 +0200178 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100179 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200180 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100181 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100182 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200183 ).limit(5);
184 };
Akron6bb71582016-06-10 20:41:08 +0200185
Akron4d926f12018-07-16 15:30:25 +0200186 var resultInfo = d.getElementById('resultinfo');
187
Akron4d926f12018-07-16 15:30:25 +0200188 /**
189 * Add result panel
190 */
191 var resultPanel;
192 if (resultInfo != null) {
193 resultPanel = panelClass.create(['result']);
194
195 // Move buttons to resultinfo
196 resultInfo.appendChild(resultPanel.actions.element());
197
Akrone6538cd2018-07-16 17:52:33 +0200198 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200199 var sb = d.getElementById('search');
200 sb.insertBefore(resultPanel.element(), sb.firstChild);
201
202 };
203
204
Akron179c8ac2015-06-30 19:30:50 +0200205 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200206 if (KorAP.koralQuery !== undefined) {
207
208 if (resultInfo !== null) {
Akron179c8ac2015-06-30 19:30:50 +0200209
Akrone6538cd2018-07-16 17:52:33 +0200210 // Open KoralQuery view
211 var kqButton = resultPanel.actions.add(loc.SHOW_KQ, ['show-kq','button-icon'], function () {
Akron179c8ac2015-06-30 19:30:50 +0200212
Akrone6538cd2018-07-16 17:52:33 +0200213 // Show only once - otherwise toggle
Akron4d926f12018-07-16 15:30:25 +0200214 if (this._kq && this._kq.shown()) {
Akrone6538cd2018-07-16 17:52:33 +0200215 this._kq.close();
Akron4d926f12018-07-16 15:30:25 +0200216 return;
217 };
218
219 this._kq = kqClass.create();
Akrone6538cd2018-07-16 17:52:33 +0200220
221 // On close, remove session info on KQ
222 this._kq.onClose = function () {
223 delete show['kq'];
224 };
225
226 show['kq'] = true;
Akron4d926f12018-07-16 15:30:25 +0200227 this.add(this._kq);
228 });
Akrone6538cd2018-07-16 17:52:33 +0200229
230 // Show KoralQuery in case it's meant to be shown
231 if (show['kq'])
232 kqButton.click();
Akron179c8ac2015-06-30 19:30:50 +0200233 };
Akron7716f012015-07-01 20:38:32 +0200234
Akron00cd4d12016-05-31 21:01:11 +0200235 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200236 var errors = KorAP.koralQuery["errors"];
237 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200238
Akron19d97fe2016-09-06 20:47:05 +0200239 // Malformed query
240 if (errors[i][0] === 302 && errors[i][2]) {
241 obj.hint = hintClass.create();
242 obj.hint.alert(errors[i][2], errors[i][1]);
243 break;
244 }
Akronf0c31ed2016-06-11 11:27:01 +0200245
Akron19d97fe2016-09-06 20:47:05 +0200246 // no query
247 else if (errors[i][0] === 301) {
248 obj.hint = hintClass.create();
249 obj.hint.alert(0, errors[i][1]);
250 }
251 }
Akron00cd4d12016-05-31 21:01:11 +0200252 };
Akron179c8ac2015-06-30 19:30:50 +0200253 };
254
Akron9a5b1e12016-12-06 18:18:23 +0100255 // There is more than 0 matches and there is a resultButton
256 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000257
Akron4d926f12018-07-16 15:30:25 +0200258 if (resultPanel !== null) {
Akron9a5b1e12016-12-06 18:18:23 +0100259 /**
260 * Toggle the alignment (left <=> right)
261 */
Akron4d926f12018-07-16 15:30:25 +0200262 resultPanel.actions.add(loc.TOGGLE_ALIGN, ['align','right','button-icon'], function (e) {
263 var ol = d.querySelector('#search > ol');
264 ol.toggleClass("align-left", "align-right");
265 this.button.toggleClass("left", "right");
266 });
Akron9a5b1e12016-12-06 18:18:23 +0100267 };
Nils Diewalda297f062015-04-02 00:23:46 +0000268 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000269
Nils Diewald7148c6f2015-05-04 15:07:53 +0000270 /**
271 * Toggle the Virtual Collection builder
272 */
273 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000274 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200275 var vcclick = function () {
Akron0b489ad2018-02-02 16:49:32 +0100276 var view = d.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000277
Akron19d97fe2016-09-06 20:47:05 +0200278 // The vc is visible
279 if (vcname.classList.contains('active')) {
280 view.removeChild(vc.element());
281 vcname.classList.remove('active');
282 delete show['collection'];
283 }
Nils Diewald6283d692015-04-23 20:32:53 +0000284
Akron19d97fe2016-09-06 20:47:05 +0200285 // The vc is not visible
286 else {
287 if (vc === undefined)
288 vc = _getCurrentVC(vcClass, vcArray);
289 view.appendChild(vc.element());
290 vcname.classList.add('active');
291 show['collection'] = true;
292 };
Nils Diewald58141332015-04-07 16:18:45 +0000293 };
Akron04671e72017-05-11 20:47:32 +0200294
Akron179c8ac2015-06-30 19:30:50 +0200295 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200296
297 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200298 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200299 vcclick.apply();
Akron5c829e92017-05-12 18:10:00 +0200300 }
Akron04671e72017-05-11 20:47:32 +0200301
Akron5c829e92017-05-12 18:10:00 +0200302 // else
303 else if (_checkVCrewrite(vcClass)) {
304 vcchoose.classList.add('rewritten');
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 // Set Virtual collection
354 if (vc === undefined) {
355 vc = _getCurrentVC(vcClass, vcArray);
356 };
Akron7716f012015-07-01 20:38:32 +0200357
Akron19d97fe2016-09-06 20:47:05 +0200358 if (vc !== undefined) {
359 input.value = vc.toQuery();
360 }
361 else {
362 delete input['value'];
363 };
Akronc1457bf2015-06-11 19:24:00 +0200364 });
365 };
366
367 /**
Nils Diewald58141332015-04-07 16:18:45 +0000368 * Init hint helper
369 * has to be final because of
370 * reposition
371 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000372 // Todo: Pass an element, so this works with
373 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200374 if (obj.hint === undefined)
375 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000376
Akron99713ef2017-06-28 18:19:28 +0200377 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100378 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200379
Nils Diewald58141332015-04-07 16:18:45 +0000380 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000381 });
382});
Akronc1457bf2015-06-11 19:24:00 +0200383
384// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200385function _getCurrentVC (vcClass, vcArray) {
386 var vc = vcClass.create(vcArray);
387 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
388 vc.fromJson(KorAP.koralQuery["collection"]);
389 };
Akronc1457bf2015-06-11 19:24:00 +0200390 return vc;
391};
Akron04671e72017-05-11 20:47:32 +0200392
393function _checkVCrewrite (vcClass) {
394 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
395 return vcClass.checkRewrite(KorAP.koralQuery["collection"]);
396 };
397 return false;
398}