blob: fdc2fabc6c852572b74bcd2846189a2fc54d2437 [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',
Akron27ae9ec2015-06-23 00:43:21 +020019 'vc/array',
Nils Diewald7148c6f2015-05-04 15:07:53 +000020 'lib/alertify',
Akron7716f012015-07-01 20:38:32 +020021 'session',
Akronbe105b92016-01-04 14:13:20 +010022 'tagger',
Akron6bb71582016-06-10 20:41:08 +020023 'selectMenu',
Nils Diewald7148c6f2015-05-04 15:07:53 +000024 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000025 'mailToChiffre',
Nils Diewald845282c2015-05-14 07:53:03 +000026 'lib/highlight/highlight.pack',
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 'util'
28], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020029 hintClass,
30 vcClass,
31 tutClass,
32 domReady,
Akron19d97fe2016-09-06 20:47:05 +020033 vcArray,
34 alertifyClass,
35 sessionClass,
36 tagger,
37 selectMenuClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000038
Akron9cc3eaf2015-06-10 22:15:52 +020039 // Localization values
40 var loc = KorAP.Locale;
Akrond3903fb2017-11-13 11:13:44 +010041 loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora';
42 loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus';
43 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle alignment';
Akron7716f012015-07-01 20:38:32 +020044 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akrond3903fb2017-11-13 11:13:44 +010045 loc.SHOW_META = loc.SHOW_META || 'show metadata';
Akron9a5b1e12016-12-06 18:18:23 +010046
Akron9cc3eaf2015-06-10 22:15:52 +020047
Nils Diewalda0defc42015-05-07 23:54:17 +000048 // Override KorAP.log
49 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020050 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000051
52 // Use alertify to log errors
53 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020054 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020055 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000056 'error',
Akronf55504a2015-06-18 16:42:55 +020057 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000058 );
59 };
60
Nils Diewald0e6992a2015-04-14 20:13:52 +000061 domReady(function (event) {
62 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020063
Akron7716f012015-07-01 20:38:32 +020064 var session = sessionClass.create('KalamarJS');
65
66 // What should be visible?
67 var show = session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000068
69 /**
Akronf55504a2015-06-18 16:42:55 +020070 * Release notifications
71 */
72 if (KorAP.Notifications !== undefined) {
73 var n = KorAP.Notifications;
74 for (var i = 0; i < n.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +020075 alertifyClass.log(n[i][1], n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020076 };
77 };
78
79 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000080 * Replace Virtual Collection field
81 */
Akron5c829e92017-05-12 18:10:00 +020082 var vcname, vcchoose;
Akronc1457bf2015-06-11 19:24:00 +020083 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000084 if (input) {
85 input.style.display = 'none';
86 vcname = document.createElement('span');
87 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020088 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020089
Akron27ae9ec2015-06-23 00:43:21 +020090 var currentVC = loc.VC_allCorpora;
91 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron19d97fe2016-09-06 20:47:05 +020092 currentVC = loc.VC_oneCollection;
Akron27ae9ec2015-06-23 00:43:21 +020093 };
94
Akron5c829e92017-05-12 18:10:00 +020095 vcchoose = vcname.appendChild(document.createElement('span'));
96
97 vcchoose.appendChild(
Akron19d97fe2016-09-06 20:47:05 +020098 document.createTextNode(
99 document.getElementById('collection-name').value || currentVC
100 )
Nils Diewald7148c6f2015-05-04 15:07:53 +0000101 );
Akron27ae9ec2015-06-23 00:43:21 +0200102
Nils Diewald7148c6f2015-05-04 15:07:53 +0000103 input.parentNode.insertBefore(vcname, input);
104 };
105
Nils Diewald7148c6f2015-05-04 15:07:53 +0000106 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000107 * Add actions to match entries
108 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000109 var inactiveLi = document.querySelectorAll(
110 '#search > ol > li:not(.active)'
111 );
Nils Diewalda297f062015-04-02 00:23:46 +0000112 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200113
Nils Diewalda297f062015-04-02 00:23:46 +0000114 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000115 inactiveLi[i].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200116 if (this._match !== undefined)
117 this._match.open();
118 else {
119 // lazyLoad
120 matchClass.create(this).open();
121 };
Akrone0c32c72017-04-25 22:38:23 +0200122 // This would prevent the sidebar to go back
123 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000124 });
Akron6a535d42015-08-26 20:16:58 +0200125 inactiveLi[i].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200126 var code = _codeFromEvent(e);
127
128 switch (code) {
129 case 32:
130 if (this._match !== undefined)
131 this._match.toggle();
132 else {
133 // lazyLoad
134 matchClass.create(this).open();
135 };
136 e.halt();
137 break;
138 };
Akron6a535d42015-08-26 20:16:58 +0200139 });
Nils Diewalda297f062015-04-02 00:23:46 +0000140 };
141
Akrone0c32c72017-04-25 22:38:23 +0200142 // Add focus listener to aside
143 var aside = document.getElementsByTagName('aside')[0];
144
145 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200146
Akron5258d462017-04-26 23:32:57 +0200147 // Horrible lock to deal with sidebar clicks
148 var asideClicked = false;
149
Akron1885ce92017-04-26 23:10:01 +0200150 // Make aside active on focus
151 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200152 this.classList.add('active');
153 });
154
Akron1885ce92017-04-26 23:10:01 +0200155 // Deactivate focus when clicking anywhere else
156 var body = document.getElementsByTagName('body')[0];
157 if (body !== null) {
158 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200159 if (!asideClicked) {
160 aside.classList.remove('active');
161 }
162 else {
163 asideClicked = false;
164 };
Akron1885ce92017-04-26 23:10:01 +0200165 });
166 };
167
168 /* Stop click event on aside
169 * (to not trickle down to body)
170 */
171 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200172 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200173 });
174 };
Akronb9cdb102017-04-25 00:52:31 +0200175
176
Akron6bb71582016-06-10 20:41:08 +0200177 // Replace QL select menus with KorAP menus
Akronaba7a5a2016-08-15 21:58:33 +0200178 var qlField = document.getElementById('ql-field');
179 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100180 KorAP.QLmenu = selectMenuClass.create(
Akronaba7a5a2016-08-15 21:58:33 +0200181 document.getElementById('ql-field').parentNode
182 ).limit(5);
183 };
Akron6bb71582016-06-10 20:41:08 +0200184
Akron179c8ac2015-06-30 19:30:50 +0200185 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200186 var resultButton = null;
Akron9a5b1e12016-12-06 18:18:23 +0100187 var leftButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200188 if (result != null) {
Akron9a5b1e12016-12-06 18:18:23 +0100189
190 // Add right buttons
Akron179c8ac2015-06-30 19:30:50 +0200191 resultButton = result.appendChild(document.createElement('div'));
Akron9a5b1e12016-12-06 18:18:23 +0100192 resultButton.classList.add('result', 'button');
193
194 leftButton = result.appendChild(document.createElement('div'));
195 leftButton.classList.add('result', 'button', 'left');
Akron179c8ac2015-06-30 19:30:50 +0200196 };
197
198 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200199 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200200
Akron7716f012015-07-01 20:38:32 +0200201 if (resultButton !== null) {
Akron19d97fe2016-09-06 20:47:05 +0200202 var kq;
203 var toggle = document.createElement('a');
204 toggle.setAttribute('title', loc.SHOW_KQ)
205 toggle.classList.add('show-kq', 'button');
206 toggle.appendChild(document.createElement('span'))
207 .appendChild(document.createTextNode(loc.SHOW_KQ));
208 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200209
Akron19d97fe2016-09-06 20:47:05 +0200210 var showKQ = function () {
211 if (kq === undefined) {
212 kq = document.createElement('div');
213 kq.setAttribute('id', 'koralquery');
214 kq.style.display = 'none';
215 var kqInner = document.createElement('div');
216 kq.appendChild(kqInner);
217 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
218 hljs.highlightBlock(kqInner);
219 var sb = document.getElementById('search');
220 sb.insertBefore(kq, sb.firstChild);
221 };
Akron7716f012015-07-01 20:38:32 +0200222
Akron19d97fe2016-09-06 20:47:05 +0200223 if (kq.style.display === 'none') {
224 kq.style.display = 'block';
225 show['kq'] = true;
226 }
227 else {
228 kq.style.display = 'none';
229 delete show['kq'];
230 };
231 };
Akron7716f012015-07-01 20:38:32 +0200232
Akron19d97fe2016-09-06 20:47:05 +0200233 if (toggle !== undefined) {
234
235 // Show koralquery
236 toggle.addEventListener('click', showKQ);
237 };
Akron179c8ac2015-06-30 19:30:50 +0200238 };
Akron7716f012015-07-01 20:38:32 +0200239
Akron00cd4d12016-05-31 21:01:11 +0200240 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200241 var errors = KorAP.koralQuery["errors"];
242 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200243
Akron19d97fe2016-09-06 20:47:05 +0200244 // Malformed query
245 if (errors[i][0] === 302 && errors[i][2]) {
246 obj.hint = hintClass.create();
247 obj.hint.alert(errors[i][2], errors[i][1]);
248 break;
249 }
Akronf0c31ed2016-06-11 11:27:01 +0200250
Akron19d97fe2016-09-06 20:47:05 +0200251 // no query
252 else if (errors[i][0] === 301) {
253 obj.hint = hintClass.create();
254 obj.hint.alert(0, errors[i][1]);
255 }
256 }
Akron00cd4d12016-05-31 21:01:11 +0200257 };
258
Akron7716f012015-07-01 20:38:32 +0200259 // Session has KQ visibility stored
260 if (show["kq"])
Akron19d97fe2016-09-06 20:47:05 +0200261 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200262 };
263
Akron9a5b1e12016-12-06 18:18:23 +0100264 // There is more than 0 matches and there is a resultButton
265 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000266
Akron9a5b1e12016-12-06 18:18:23 +0100267 if (resultButton !== null) {
268
269 /**
270 * Toggle the alignment (left <=> right)
271 */
272 // querySelector('div.button.right');
273
274 var toggle = document.createElement('a');
275 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
276 // Todo: Reuse old alignment from query
277 var cl = toggle.classList;
278 cl.add('align', 'right', 'button');
279 toggle.addEventListener(
280 'click',
281 function (e) {
282 var ol = document.querySelector('#search > ol');
283 ol.toggleClass("align-left", "align-right");
284 this.toggleClass("left", "right");
285 });
286 toggle.appendChild(document.createElement('span'))
287 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
288 resultButton.appendChild(toggle);
289 };
290
291 /*
292 // Not ready yet
293 if (leftButton !== null) {
294 var metaInfo = document.createElement('a');
295 metaInfo.setAttribute('title', loc.SHOW_META)
296
297 var cl = metaInfo.classList;
298 cl.add('show-meta', 'button');
299 metaInfo.appendChild(document.createElement('span'))
300 .appendChild(document.createTextNode(loc.SHOW_META));
301 leftButton.appendChild(metaInfo);
302 };
303 */
Nils Diewalda297f062015-04-02 00:23:46 +0000304 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000305
Nils Diewald7148c6f2015-05-04 15:07:53 +0000306 /**
307 * Toggle the Virtual Collection builder
308 */
309 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000310 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200311 var vcclick = function () {
Akron19d97fe2016-09-06 20:47:05 +0200312 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000313
Akron19d97fe2016-09-06 20:47:05 +0200314 // The vc is visible
315 if (vcname.classList.contains('active')) {
316 view.removeChild(vc.element());
317 vcname.classList.remove('active');
318 delete show['collection'];
319 }
Nils Diewald6283d692015-04-23 20:32:53 +0000320
Akron19d97fe2016-09-06 20:47:05 +0200321 // The vc is not visible
322 else {
323 if (vc === undefined)
324 vc = _getCurrentVC(vcClass, vcArray);
325 view.appendChild(vc.element());
326 vcname.classList.add('active');
327 show['collection'] = true;
328 };
Nils Diewald58141332015-04-07 16:18:45 +0000329 };
Akron04671e72017-05-11 20:47:32 +0200330
Akron179c8ac2015-06-30 19:30:50 +0200331 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200332
333 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200334 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200335 vcclick.apply();
Akron5c829e92017-05-12 18:10:00 +0200336 }
Akron04671e72017-05-11 20:47:32 +0200337
Akron5c829e92017-05-12 18:10:00 +0200338 // else
339 else if (_checkVCrewrite(vcClass)) {
340 vcchoose.classList.add('rewritten');
Akron04671e72017-05-11 20:47:32 +0200341 };
Nils Diewald58141332015-04-07 16:18:45 +0000342 };
343
Akron19d97fe2016-09-06 20:47:05 +0200344
Nils Diewald58141332015-04-07 16:18:45 +0000345 /**
346 * Init Tutorial view
347 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000348 if (document.getElementById('view-tutorial')) {
349 window.tutorial = tutClass.create(
Akron19d97fe2016-09-06 20:47:05 +0200350 document.getElementById('view-tutorial'),
351 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000352 );
353 obj.tutorial = window.tutorial;
354 }
Nils Diewald58141332015-04-07 16:18:45 +0000355
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000356 // Tutorial is in parent
357 else if (window.parent) {
358 obj.tutorial = window.parent.tutorial;
359 };
360
361 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200362 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000363 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000364
Akron6ed13992016-05-23 18:06:05 +0200365 // Initialize documentation links
366 obj.tutorial.initDocLinks(document);
367 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000368
Nils Diewald845282c2015-05-14 07:53:03 +0000369
Nils Diewald58141332015-04-07 16:18:45 +0000370 /**
Akronc1457bf2015-06-11 19:24:00 +0200371 * Add VC creation on submission.
372 */
373 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200374 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200375 form.addEventListener('submit', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200376 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200377
Akron19d97fe2016-09-06 20:47:05 +0200378 // No query was defined
379 if (qf.value === undefined || qf.value === '') {
380 qf.focus();
381 e.halt();
382 KorAP.log(700, "No query given");
383 return;
384 };
Akron7716f012015-07-01 20:38:32 +0200385
Akron19d97fe2016-09-06 20:47:05 +0200386 // Store session information
387 session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200388
Akron19d97fe2016-09-06 20:47:05 +0200389 // Set Virtual collection
390 if (vc === undefined) {
391 vc = _getCurrentVC(vcClass, vcArray);
392 };
Akron7716f012015-07-01 20:38:32 +0200393
Akron19d97fe2016-09-06 20:47:05 +0200394 if (vc !== undefined) {
395 input.value = vc.toQuery();
396 }
397 else {
398 delete input['value'];
399 };
Akronc1457bf2015-06-11 19:24:00 +0200400 });
401 };
402
403 /**
Nils Diewald58141332015-04-07 16:18:45 +0000404 * Init hint helper
405 * has to be final because of
406 * reposition
407 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000408 // Todo: Pass an element, so this works with
409 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200410 if (obj.hint === undefined)
411 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000412
Akron99713ef2017-06-28 18:19:28 +0200413 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100414 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200415
Nils Diewald58141332015-04-07 16:18:45 +0000416 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000417 });
418});
Akronc1457bf2015-06-11 19:24:00 +0200419
420// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200421function _getCurrentVC (vcClass, vcArray) {
422 var vc = vcClass.create(vcArray);
423 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
424 vc.fromJson(KorAP.koralQuery["collection"]);
425 };
Akronc1457bf2015-06-11 19:24:00 +0200426 return vc;
427};
Akron04671e72017-05-11 20:47:32 +0200428
429function _checkVCrewrite (vcClass) {
430 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
431 return vcClass.checkRewrite(KorAP.koralQuery["collection"]);
432 };
433 return false;
434}