blob: 6eee4f8fe15038461d8190b5efd2ab5789947921 [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',
18 'lib/domReady',
Nils Diewald7148c6f2015-05-04 15:07:53 +000019 'hint/array',
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',
Akronbe105b92016-01-04 14:13:20 +010023 'tagger',
Akron6bb71582016-06-10 20:41:08 +020024 'selectMenu',
Nils Diewald7148c6f2015-05-04 15:07:53 +000025 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000026 'mailToChiffre',
Nils Diewald845282c2015-05-14 07:53:03 +000027 'lib/highlight/highlight.pack',
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 'util'
29], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020030 hintClass,
31 vcClass,
32 tutClass,
33 domReady,
34 hintArray,
35 vcArray,
36 alertifyClass,
37 sessionClass,
38 tagger,
39 selectMenuClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000040
41 // Set hint array for hint helper
42 KorAP.hintArray = hintArray;
43
Akron9cc3eaf2015-06-10 22:15:52 +020044 // Localization values
45 var loc = KorAP.Locale;
Akrond3903fb2017-11-13 11:13:44 +010046 loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora';
47 loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus';
48 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle alignment';
Akron7716f012015-07-01 20:38:32 +020049 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akrond3903fb2017-11-13 11:13:44 +010050 loc.SHOW_META = loc.SHOW_META || 'show metadata';
Akron9a5b1e12016-12-06 18:18:23 +010051
Akron9cc3eaf2015-06-10 22:15:52 +020052
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
Akron7716f012015-07-01 20:38:32 +020069 var session = sessionClass.create('KalamarJS');
70
71 // What should be visible?
72 var show = session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000073
74 /**
Akronf55504a2015-06-18 16:42:55 +020075 * Release notifications
76 */
77 if (KorAP.Notifications !== undefined) {
78 var n = KorAP.Notifications;
79 for (var i = 0; i < n.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +020080 alertifyClass.log(n[i][1], n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020081 };
82 };
83
84 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000085 * Replace Virtual Collection field
86 */
Akron5c829e92017-05-12 18:10:00 +020087 var vcname, vcchoose;
Akronc1457bf2015-06-11 19:24:00 +020088 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000089 if (input) {
90 input.style.display = 'none';
91 vcname = document.createElement('span');
92 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020093 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020094
Akron27ae9ec2015-06-23 00:43:21 +020095 var currentVC = loc.VC_allCorpora;
96 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron19d97fe2016-09-06 20:47:05 +020097 currentVC = loc.VC_oneCollection;
Akron27ae9ec2015-06-23 00:43:21 +020098 };
99
Akron5c829e92017-05-12 18:10:00 +0200100 vcchoose = vcname.appendChild(document.createElement('span'));
101
102 vcchoose.appendChild(
Akron19d97fe2016-09-06 20:47:05 +0200103 document.createTextNode(
104 document.getElementById('collection-name').value || currentVC
105 )
Nils Diewald7148c6f2015-05-04 15:07:53 +0000106 );
Akron27ae9ec2015-06-23 00:43:21 +0200107
Nils Diewald7148c6f2015-05-04 15:07:53 +0000108 input.parentNode.insertBefore(vcname, input);
109 };
110
Nils Diewald7148c6f2015-05-04 15:07:53 +0000111 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000112 * Add actions to match entries
113 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000114 var inactiveLi = document.querySelectorAll(
115 '#search > ol > li:not(.active)'
116 );
Nils Diewalda297f062015-04-02 00:23:46 +0000117 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200118
Nils Diewalda297f062015-04-02 00:23:46 +0000119 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000120 inactiveLi[i].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200121 if (this._match !== undefined)
122 this._match.open();
123 else {
124 // lazyLoad
125 matchClass.create(this).open();
126 };
Akrone0c32c72017-04-25 22:38:23 +0200127 // This would prevent the sidebar to go back
128 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000129 });
Akron6a535d42015-08-26 20:16:58 +0200130 inactiveLi[i].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200131 var code = _codeFromEvent(e);
132
133 switch (code) {
134 case 32:
135 if (this._match !== undefined)
136 this._match.toggle();
137 else {
138 // lazyLoad
139 matchClass.create(this).open();
140 };
141 e.halt();
142 break;
143 };
Akron6a535d42015-08-26 20:16:58 +0200144 });
Nils Diewalda297f062015-04-02 00:23:46 +0000145 };
146
Akrone0c32c72017-04-25 22:38:23 +0200147 // Add focus listener to aside
148 var aside = document.getElementsByTagName('aside')[0];
149
150 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200151
Akron5258d462017-04-26 23:32:57 +0200152 // Horrible lock to deal with sidebar clicks
153 var asideClicked = false;
154
Akron1885ce92017-04-26 23:10:01 +0200155 // Make aside active on focus
156 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200157 this.classList.add('active');
158 });
159
Akron1885ce92017-04-26 23:10:01 +0200160 // Deactivate focus when clicking anywhere else
161 var body = document.getElementsByTagName('body')[0];
162 if (body !== null) {
163 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200164 if (!asideClicked) {
165 aside.classList.remove('active');
166 }
167 else {
168 asideClicked = false;
169 };
Akron1885ce92017-04-26 23:10:01 +0200170 });
171 };
172
173 /* Stop click event on aside
174 * (to not trickle down to body)
175 */
176 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200177 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200178 });
179 };
Akronb9cdb102017-04-25 00:52:31 +0200180
181
Akron6bb71582016-06-10 20:41:08 +0200182 // Replace QL select menus with KorAP menus
Akronaba7a5a2016-08-15 21:58:33 +0200183 var qlField = document.getElementById('ql-field');
184 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100185 KorAP.QLmenu = selectMenuClass.create(
Akronaba7a5a2016-08-15 21:58:33 +0200186 document.getElementById('ql-field').parentNode
187 ).limit(5);
188 };
Akron6bb71582016-06-10 20:41:08 +0200189
Akron179c8ac2015-06-30 19:30:50 +0200190 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200191 var resultButton = null;
Akron9a5b1e12016-12-06 18:18:23 +0100192 var leftButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200193 if (result != null) {
Akron9a5b1e12016-12-06 18:18:23 +0100194
195 // Add right buttons
Akron179c8ac2015-06-30 19:30:50 +0200196 resultButton = result.appendChild(document.createElement('div'));
Akron9a5b1e12016-12-06 18:18:23 +0100197 resultButton.classList.add('result', 'button');
198
199 leftButton = result.appendChild(document.createElement('div'));
200 leftButton.classList.add('result', 'button', 'left');
Akron179c8ac2015-06-30 19:30:50 +0200201 };
202
203 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200204 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200205
Akron7716f012015-07-01 20:38:32 +0200206 if (resultButton !== null) {
Akron19d97fe2016-09-06 20:47:05 +0200207 var kq;
208 var toggle = document.createElement('a');
209 toggle.setAttribute('title', loc.SHOW_KQ)
210 toggle.classList.add('show-kq', 'button');
211 toggle.appendChild(document.createElement('span'))
212 .appendChild(document.createTextNode(loc.SHOW_KQ));
213 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200214
Akron19d97fe2016-09-06 20:47:05 +0200215 var showKQ = function () {
216 if (kq === undefined) {
217 kq = document.createElement('div');
218 kq.setAttribute('id', 'koralquery');
219 kq.style.display = 'none';
220 var kqInner = document.createElement('div');
221 kq.appendChild(kqInner);
222 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
223 hljs.highlightBlock(kqInner);
224 var sb = document.getElementById('search');
225 sb.insertBefore(kq, sb.firstChild);
226 };
Akron7716f012015-07-01 20:38:32 +0200227
Akron19d97fe2016-09-06 20:47:05 +0200228 if (kq.style.display === 'none') {
229 kq.style.display = 'block';
230 show['kq'] = true;
231 }
232 else {
233 kq.style.display = 'none';
234 delete show['kq'];
235 };
236 };
Akron7716f012015-07-01 20:38:32 +0200237
Akron19d97fe2016-09-06 20:47:05 +0200238 if (toggle !== undefined) {
239
240 // Show koralquery
241 toggle.addEventListener('click', showKQ);
242 };
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 };
263
Akron7716f012015-07-01 20:38:32 +0200264 // Session has KQ visibility stored
265 if (show["kq"])
Akron19d97fe2016-09-06 20:47:05 +0200266 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200267 };
268
Akron9a5b1e12016-12-06 18:18:23 +0100269 // There is more than 0 matches and there is a resultButton
270 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000271
Akron9a5b1e12016-12-06 18:18:23 +0100272 if (resultButton !== null) {
273
274 /**
275 * Toggle the alignment (left <=> right)
276 */
277 // querySelector('div.button.right');
278
279 var toggle = document.createElement('a');
280 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
281 // Todo: Reuse old alignment from query
282 var cl = toggle.classList;
283 cl.add('align', 'right', 'button');
284 toggle.addEventListener(
285 'click',
286 function (e) {
287 var ol = document.querySelector('#search > ol');
288 ol.toggleClass("align-left", "align-right");
289 this.toggleClass("left", "right");
290 });
291 toggle.appendChild(document.createElement('span'))
292 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
293 resultButton.appendChild(toggle);
294 };
295
296 /*
297 // Not ready yet
298 if (leftButton !== null) {
299 var metaInfo = document.createElement('a');
300 metaInfo.setAttribute('title', loc.SHOW_META)
301
302 var cl = metaInfo.classList;
303 cl.add('show-meta', 'button');
304 metaInfo.appendChild(document.createElement('span'))
305 .appendChild(document.createTextNode(loc.SHOW_META));
306 leftButton.appendChild(metaInfo);
307 };
308 */
Nils Diewalda297f062015-04-02 00:23:46 +0000309 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000310
Nils Diewald7148c6f2015-05-04 15:07:53 +0000311 /**
312 * Toggle the Virtual Collection builder
313 */
314 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000315 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200316 var vcclick = function () {
Akron19d97fe2016-09-06 20:47:05 +0200317 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000318
Akron19d97fe2016-09-06 20:47:05 +0200319 // The vc is visible
320 if (vcname.classList.contains('active')) {
321 view.removeChild(vc.element());
322 vcname.classList.remove('active');
323 delete show['collection'];
324 }
Nils Diewald6283d692015-04-23 20:32:53 +0000325
Akron19d97fe2016-09-06 20:47:05 +0200326 // The vc is not visible
327 else {
328 if (vc === undefined)
329 vc = _getCurrentVC(vcClass, vcArray);
330 view.appendChild(vc.element());
331 vcname.classList.add('active');
332 show['collection'] = true;
333 };
Nils Diewald58141332015-04-07 16:18:45 +0000334 };
Akron04671e72017-05-11 20:47:32 +0200335
Akron179c8ac2015-06-30 19:30:50 +0200336 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200337
338 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200339 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200340 vcclick.apply();
Akron5c829e92017-05-12 18:10:00 +0200341 }
Akron04671e72017-05-11 20:47:32 +0200342
Akron5c829e92017-05-12 18:10:00 +0200343 // else
344 else if (_checkVCrewrite(vcClass)) {
345 vcchoose.classList.add('rewritten');
Akron04671e72017-05-11 20:47:32 +0200346 };
Nils Diewald58141332015-04-07 16:18:45 +0000347 };
348
Akron19d97fe2016-09-06 20:47:05 +0200349
Nils Diewald58141332015-04-07 16:18:45 +0000350 /**
351 * Init Tutorial view
352 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000353 if (document.getElementById('view-tutorial')) {
354 window.tutorial = tutClass.create(
Akron19d97fe2016-09-06 20:47:05 +0200355 document.getElementById('view-tutorial'),
356 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000357 );
358 obj.tutorial = window.tutorial;
359 }
Nils Diewald58141332015-04-07 16:18:45 +0000360
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000361 // Tutorial is in parent
362 else if (window.parent) {
363 obj.tutorial = window.parent.tutorial;
364 };
365
366 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200367 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000368 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000369
Akron6ed13992016-05-23 18:06:05 +0200370 // Initialize documentation links
371 obj.tutorial.initDocLinks(document);
372 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000373
Nils Diewald845282c2015-05-14 07:53:03 +0000374
Nils Diewald58141332015-04-07 16:18:45 +0000375 /**
Akronc1457bf2015-06-11 19:24:00 +0200376 * Add VC creation on submission.
377 */
378 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200379 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200380 form.addEventListener('submit', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200381 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200382
Akron19d97fe2016-09-06 20:47:05 +0200383 // No query was defined
384 if (qf.value === undefined || qf.value === '') {
385 qf.focus();
386 e.halt();
387 KorAP.log(700, "No query given");
388 return;
389 };
Akron7716f012015-07-01 20:38:32 +0200390
Akron19d97fe2016-09-06 20:47:05 +0200391 // Store session information
392 session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200393
Akron19d97fe2016-09-06 20:47:05 +0200394 // Set Virtual collection
395 if (vc === undefined) {
396 vc = _getCurrentVC(vcClass, vcArray);
397 };
Akron7716f012015-07-01 20:38:32 +0200398
Akron19d97fe2016-09-06 20:47:05 +0200399 if (vc !== undefined) {
400 input.value = vc.toQuery();
401 }
402 else {
403 delete input['value'];
404 };
Akronc1457bf2015-06-11 19:24:00 +0200405 });
406 };
407
408 /**
Nils Diewald58141332015-04-07 16:18:45 +0000409 * Init hint helper
410 * has to be final because of
411 * reposition
412 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000413 // Todo: Pass an element, so this works with
414 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200415 if (obj.hint === undefined)
416 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000417
Akron99713ef2017-06-28 18:19:28 +0200418 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100419 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200420
Nils Diewald58141332015-04-07 16:18:45 +0000421 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000422 });
423});
Akronc1457bf2015-06-11 19:24:00 +0200424
425// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200426function _getCurrentVC (vcClass, vcArray) {
427 var vc = vcClass.create(vcArray);
428 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
429 vc.fromJson(KorAP.koralQuery["collection"]);
430 };
Akronc1457bf2015-06-11 19:24:00 +0200431 return vc;
432};
Akron04671e72017-05-11 20:47:32 +0200433
434function _checkVCrewrite (vcClass) {
435 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
436 return vcClass.checkRewrite(KorAP.koralQuery["collection"]);
437 };
438 return false;
439}