blob: d842a1b54574379833c0020e477adb8f53fa73ef [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';
Akron9a5b1e12016-12-06 18:18:23 +010045
Akron9cc3eaf2015-06-10 22:15:52 +020046
Nils Diewalda0defc42015-05-07 23:54:17 +000047 // Override KorAP.log
48 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020049 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000050
51 // Use alertify to log errors
52 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020053 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020054 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000055 'error',
Akronf55504a2015-06-18 16:42:55 +020056 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000057 );
58 };
59
Nils Diewald0e6992a2015-04-14 20:13:52 +000060 domReady(function (event) {
61 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020062
Akron7716f012015-07-01 20:38:32 +020063 var session = sessionClass.create('KalamarJS');
64
65 // What should be visible?
66 var show = session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000067
68 /**
Akronf55504a2015-06-18 16:42:55 +020069 * Release notifications
70 */
71 if (KorAP.Notifications !== undefined) {
72 var n = KorAP.Notifications;
73 for (var i = 0; i < n.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +020074 alertifyClass.log(n[i][1], n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020075 };
76 };
77
78 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000079 * Replace Virtual Collection field
80 */
Akron5c829e92017-05-12 18:10:00 +020081 var vcname, vcchoose;
Akronc1457bf2015-06-11 19:24:00 +020082 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000083 if (input) {
84 input.style.display = 'none';
85 vcname = document.createElement('span');
86 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020087 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020088
Akron27ae9ec2015-06-23 00:43:21 +020089 var currentVC = loc.VC_allCorpora;
90 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron19d97fe2016-09-06 20:47:05 +020091 currentVC = loc.VC_oneCollection;
Akron27ae9ec2015-06-23 00:43:21 +020092 };
93
Akron5c829e92017-05-12 18:10:00 +020094 vcchoose = vcname.appendChild(document.createElement('span'));
95
96 vcchoose.appendChild(
Akron19d97fe2016-09-06 20:47:05 +020097 document.createTextNode(
98 document.getElementById('collection-name').value || currentVC
99 )
Nils Diewald7148c6f2015-05-04 15:07:53 +0000100 );
Akron27ae9ec2015-06-23 00:43:21 +0200101
Nils Diewald7148c6f2015-05-04 15:07:53 +0000102 input.parentNode.insertBefore(vcname, input);
103 };
104
Nils Diewald7148c6f2015-05-04 15:07:53 +0000105 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000106 * Add actions to match entries
107 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000108 var inactiveLi = document.querySelectorAll(
109 '#search > ol > li:not(.active)'
110 );
Nils Diewalda297f062015-04-02 00:23:46 +0000111 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200112
Nils Diewalda297f062015-04-02 00:23:46 +0000113 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000114 inactiveLi[i].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200115 if (this._match !== undefined)
116 this._match.open();
117 else {
118 // lazyLoad
119 matchClass.create(this).open();
120 };
Akrone0c32c72017-04-25 22:38:23 +0200121 // This would prevent the sidebar to go back
122 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000123 });
Akron6a535d42015-08-26 20:16:58 +0200124 inactiveLi[i].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200125 var code = _codeFromEvent(e);
126
127 switch (code) {
128 case 32:
129 if (this._match !== undefined)
130 this._match.toggle();
131 else {
132 // lazyLoad
133 matchClass.create(this).open();
134 };
135 e.halt();
136 break;
137 };
Akron6a535d42015-08-26 20:16:58 +0200138 });
Nils Diewalda297f062015-04-02 00:23:46 +0000139 };
140
Akrone0c32c72017-04-25 22:38:23 +0200141 // Add focus listener to aside
142 var aside = document.getElementsByTagName('aside')[0];
143
144 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200145
Akron5258d462017-04-26 23:32:57 +0200146 // Horrible lock to deal with sidebar clicks
147 var asideClicked = false;
148
Akron1885ce92017-04-26 23:10:01 +0200149 // Make aside active on focus
150 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200151 this.classList.add('active');
152 });
153
Akron1885ce92017-04-26 23:10:01 +0200154 // Deactivate focus when clicking anywhere else
155 var body = document.getElementsByTagName('body')[0];
156 if (body !== null) {
157 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200158 if (!asideClicked) {
159 aside.classList.remove('active');
160 }
161 else {
162 asideClicked = false;
163 };
Akron1885ce92017-04-26 23:10:01 +0200164 });
165 };
166
167 /* Stop click event on aside
168 * (to not trickle down to body)
169 */
170 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200171 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200172 });
173 };
Akronb9cdb102017-04-25 00:52:31 +0200174
175
Akron6bb71582016-06-10 20:41:08 +0200176 // Replace QL select menus with KorAP menus
Akronaba7a5a2016-08-15 21:58:33 +0200177 var qlField = document.getElementById('ql-field');
178 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100179 KorAP.QLmenu = selectMenuClass.create(
Akronaba7a5a2016-08-15 21:58:33 +0200180 document.getElementById('ql-field').parentNode
181 ).limit(5);
182 };
Akron6bb71582016-06-10 20:41:08 +0200183
Akron179c8ac2015-06-30 19:30:50 +0200184 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200185 var resultButton = null;
Akron9a5b1e12016-12-06 18:18:23 +0100186 var leftButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200187 if (result != null) {
Akron9a5b1e12016-12-06 18:18:23 +0100188
189 // Add right buttons
Akron179c8ac2015-06-30 19:30:50 +0200190 resultButton = result.appendChild(document.createElement('div'));
Akron9a5b1e12016-12-06 18:18:23 +0100191 resultButton.classList.add('result', 'button');
192
193 leftButton = result.appendChild(document.createElement('div'));
194 leftButton.classList.add('result', 'button', 'left');
Akron179c8ac2015-06-30 19:30:50 +0200195 };
196
197 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200198 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200199
Akron7716f012015-07-01 20:38:32 +0200200 if (resultButton !== null) {
Akron19d97fe2016-09-06 20:47:05 +0200201 var kq;
202 var toggle = document.createElement('a');
203 toggle.setAttribute('title', loc.SHOW_KQ)
204 toggle.classList.add('show-kq', 'button');
205 toggle.appendChild(document.createElement('span'))
206 .appendChild(document.createTextNode(loc.SHOW_KQ));
207 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200208
Akron19d97fe2016-09-06 20:47:05 +0200209 var showKQ = function () {
210 if (kq === undefined) {
211 kq = document.createElement('div');
212 kq.setAttribute('id', 'koralquery');
213 kq.style.display = 'none';
214 var kqInner = document.createElement('div');
215 kq.appendChild(kqInner);
216 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
217 hljs.highlightBlock(kqInner);
218 var sb = document.getElementById('search');
219 sb.insertBefore(kq, sb.firstChild);
220 };
Akron7716f012015-07-01 20:38:32 +0200221
Akron19d97fe2016-09-06 20:47:05 +0200222 if (kq.style.display === 'none') {
223 kq.style.display = 'block';
224 show['kq'] = true;
225 }
226 else {
227 kq.style.display = 'none';
228 delete show['kq'];
229 };
230 };
Akron7716f012015-07-01 20:38:32 +0200231
Akron19d97fe2016-09-06 20:47:05 +0200232 if (toggle !== undefined) {
233
234 // Show koralquery
235 toggle.addEventListener('click', showKQ);
236 };
Akron179c8ac2015-06-30 19:30:50 +0200237 };
Akron7716f012015-07-01 20:38:32 +0200238
Akron00cd4d12016-05-31 21:01:11 +0200239 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200240 var errors = KorAP.koralQuery["errors"];
241 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200242
Akron19d97fe2016-09-06 20:47:05 +0200243 // Malformed query
244 if (errors[i][0] === 302 && errors[i][2]) {
245 obj.hint = hintClass.create();
246 obj.hint.alert(errors[i][2], errors[i][1]);
247 break;
248 }
Akronf0c31ed2016-06-11 11:27:01 +0200249
Akron19d97fe2016-09-06 20:47:05 +0200250 // no query
251 else if (errors[i][0] === 301) {
252 obj.hint = hintClass.create();
253 obj.hint.alert(0, errors[i][1]);
254 }
255 }
Akron00cd4d12016-05-31 21:01:11 +0200256 };
257
Akron7716f012015-07-01 20:38:32 +0200258 // Session has KQ visibility stored
259 if (show["kq"])
Akron19d97fe2016-09-06 20:47:05 +0200260 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200261 };
262
Akron9a5b1e12016-12-06 18:18:23 +0100263 // There is more than 0 matches and there is a resultButton
264 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000265
Akron9a5b1e12016-12-06 18:18:23 +0100266 if (resultButton !== null) {
267
268 /**
269 * Toggle the alignment (left <=> right)
270 */
271 // querySelector('div.button.right');
272
273 var toggle = document.createElement('a');
274 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
275 // Todo: Reuse old alignment from query
276 var cl = toggle.classList;
277 cl.add('align', 'right', 'button');
278 toggle.addEventListener(
279 'click',
280 function (e) {
281 var ol = document.querySelector('#search > ol');
282 ol.toggleClass("align-left", "align-right");
283 this.toggleClass("left", "right");
284 });
285 toggle.appendChild(document.createElement('span'))
286 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
287 resultButton.appendChild(toggle);
288 };
289
Akron9a5b1e12016-12-06 18:18:23 +0100290 // Not ready yet
Akron24866cf2018-01-23 20:22:01 +0100291 /*
Akron9a5b1e12016-12-06 18:18:23 +0100292 if (leftButton !== null) {
293 var metaInfo = document.createElement('a');
294 metaInfo.setAttribute('title', loc.SHOW_META)
295
296 var cl = metaInfo.classList;
297 cl.add('show-meta', 'button');
298 metaInfo.appendChild(document.createElement('span'))
299 .appendChild(document.createTextNode(loc.SHOW_META));
300 leftButton.appendChild(metaInfo);
301 };
302 */
Nils Diewalda297f062015-04-02 00:23:46 +0000303 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000304
Nils Diewald7148c6f2015-05-04 15:07:53 +0000305 /**
306 * Toggle the Virtual Collection builder
307 */
308 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000309 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200310 var vcclick = function () {
Akron19d97fe2016-09-06 20:47:05 +0200311 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000312
Akron19d97fe2016-09-06 20:47:05 +0200313 // The vc is visible
314 if (vcname.classList.contains('active')) {
315 view.removeChild(vc.element());
316 vcname.classList.remove('active');
317 delete show['collection'];
318 }
Nils Diewald6283d692015-04-23 20:32:53 +0000319
Akron19d97fe2016-09-06 20:47:05 +0200320 // The vc is not visible
321 else {
322 if (vc === undefined)
323 vc = _getCurrentVC(vcClass, vcArray);
324 view.appendChild(vc.element());
325 vcname.classList.add('active');
326 show['collection'] = true;
327 };
Nils Diewald58141332015-04-07 16:18:45 +0000328 };
Akron04671e72017-05-11 20:47:32 +0200329
Akron179c8ac2015-06-30 19:30:50 +0200330 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200331
332 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200333 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200334 vcclick.apply();
Akron5c829e92017-05-12 18:10:00 +0200335 }
Akron04671e72017-05-11 20:47:32 +0200336
Akron5c829e92017-05-12 18:10:00 +0200337 // else
338 else if (_checkVCrewrite(vcClass)) {
339 vcchoose.classList.add('rewritten');
Akron04671e72017-05-11 20:47:32 +0200340 };
Nils Diewald58141332015-04-07 16:18:45 +0000341 };
342
Akron19d97fe2016-09-06 20:47:05 +0200343
Nils Diewald58141332015-04-07 16:18:45 +0000344 /**
345 * Init Tutorial view
346 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000347 if (document.getElementById('view-tutorial')) {
348 window.tutorial = tutClass.create(
Akron19d97fe2016-09-06 20:47:05 +0200349 document.getElementById('view-tutorial'),
350 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000351 );
352 obj.tutorial = window.tutorial;
353 }
Nils Diewald58141332015-04-07 16:18:45 +0000354
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000355 // Tutorial is in parent
356 else if (window.parent) {
357 obj.tutorial = window.parent.tutorial;
358 };
359
360 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200361 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000362 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000363
Akron6ed13992016-05-23 18:06:05 +0200364 // Initialize documentation links
365 obj.tutorial.initDocLinks(document);
366 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000367
Nils Diewald845282c2015-05-14 07:53:03 +0000368
Nils Diewald58141332015-04-07 16:18:45 +0000369 /**
Akronc1457bf2015-06-11 19:24:00 +0200370 * Add VC creation on submission.
371 */
372 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200373 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200374 form.addEventListener('submit', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200375 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200376
Akron19d97fe2016-09-06 20:47:05 +0200377 // No query was defined
378 if (qf.value === undefined || qf.value === '') {
379 qf.focus();
380 e.halt();
381 KorAP.log(700, "No query given");
382 return;
383 };
Akron7716f012015-07-01 20:38:32 +0200384
Akron19d97fe2016-09-06 20:47:05 +0200385 // Store session information
386 session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200387
Akron19d97fe2016-09-06 20:47:05 +0200388 // Set Virtual collection
389 if (vc === undefined) {
390 vc = _getCurrentVC(vcClass, vcArray);
391 };
Akron7716f012015-07-01 20:38:32 +0200392
Akron19d97fe2016-09-06 20:47:05 +0200393 if (vc !== undefined) {
394 input.value = vc.toQuery();
395 }
396 else {
397 delete input['value'];
398 };
Akronc1457bf2015-06-11 19:24:00 +0200399 });
400 };
401
402 /**
Nils Diewald58141332015-04-07 16:18:45 +0000403 * Init hint helper
404 * has to be final because of
405 * reposition
406 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000407 // Todo: Pass an element, so this works with
408 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200409 if (obj.hint === undefined)
410 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000411
Akron99713ef2017-06-28 18:19:28 +0200412 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100413 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200414
Nils Diewald58141332015-04-07 16:18:45 +0000415 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000416 });
417});
Akronc1457bf2015-06-11 19:24:00 +0200418
419// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200420function _getCurrentVC (vcClass, vcArray) {
421 var vc = vcClass.create(vcArray);
422 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
423 vc.fromJson(KorAP.koralQuery["collection"]);
424 };
Akronc1457bf2015-06-11 19:24:00 +0200425 return vc;
426};
Akron04671e72017-05-11 20:47:32 +0200427
428function _checkVCrewrite (vcClass) {
429 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
430 return vcClass.checkRewrite(KorAP.koralQuery["collection"]);
431 };
432 return false;
433}