blob: 81ed0932f73140d0a4a4e514c97e172559321f24 [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;
Akron7716f012015-07-01 20:38:32 +020046 loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora';
47 loc.VC_oneCollection = loc.VC_oneCollection || 'one Collection';
48 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle Alignment';
49 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akron9a5b1e12016-12-06 18:18:23 +010050 loc.SHOW_META = loc.SHOW_META || 'show Metadata';
51
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 */
87 var vcname;
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
Nils Diewald7148c6f2015-05-04 15:07:53 +0000100 vcname.appendChild(
Akron19d97fe2016-09-06 20:47:05 +0200101 document.createTextNode(
102 document.getElementById('collection-name').value || currentVC
103 )
Nils Diewald7148c6f2015-05-04 15:07:53 +0000104 );
Akron27ae9ec2015-06-23 00:43:21 +0200105
Nils Diewald7148c6f2015-05-04 15:07:53 +0000106 input.parentNode.insertBefore(vcname, input);
107 };
108
Nils Diewald7148c6f2015-05-04 15:07:53 +0000109 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000110 * Add actions to match entries
111 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000112 var inactiveLi = document.querySelectorAll(
113 '#search > ol > li:not(.active)'
114 );
Nils Diewalda297f062015-04-02 00:23:46 +0000115 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200116
Nils Diewalda297f062015-04-02 00:23:46 +0000117 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000118 inactiveLi[i].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200119 if (this._match !== undefined)
120 this._match.open();
121 else {
122 // lazyLoad
123 matchClass.create(this).open();
124 };
Akrone0c32c72017-04-25 22:38:23 +0200125 // This would prevent the sidebar to go back
126 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000127 });
Akron6a535d42015-08-26 20:16:58 +0200128 inactiveLi[i].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200129 var code = _codeFromEvent(e);
130
131 switch (code) {
132 case 32:
133 if (this._match !== undefined)
134 this._match.toggle();
135 else {
136 // lazyLoad
137 matchClass.create(this).open();
138 };
139 e.halt();
140 break;
141 };
Akron6a535d42015-08-26 20:16:58 +0200142 });
Nils Diewalda297f062015-04-02 00:23:46 +0000143 };
144
Akrone0c32c72017-04-25 22:38:23 +0200145 // Add focus listener to aside
146 var aside = document.getElementsByTagName('aside')[0];
147
148 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200149
Akron5258d462017-04-26 23:32:57 +0200150 // Horrible lock to deal with sidebar clicks
151 var asideClicked = false;
152
Akron1885ce92017-04-26 23:10:01 +0200153 // Make aside active on focus
154 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200155 this.classList.add('active');
156 });
157
Akron1885ce92017-04-26 23:10:01 +0200158 // Deactivate focus when clicking anywhere else
159 var body = document.getElementsByTagName('body')[0];
160 if (body !== null) {
161 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200162 if (!asideClicked) {
163 aside.classList.remove('active');
164 }
165 else {
166 asideClicked = false;
167 };
Akron1885ce92017-04-26 23:10:01 +0200168 });
169 };
170
171 /* Stop click event on aside
172 * (to not trickle down to body)
173 */
174 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200175 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200176 });
177 };
Akronb9cdb102017-04-25 00:52:31 +0200178
179
Akron6bb71582016-06-10 20:41:08 +0200180 // Replace QL select menus with KorAP menus
Akronaba7a5a2016-08-15 21:58:33 +0200181 var qlField = document.getElementById('ql-field');
182 if (qlField !== null) {
183 selectMenuClass.create(
184 document.getElementById('ql-field').parentNode
185 ).limit(5);
186 };
Akron6bb71582016-06-10 20:41:08 +0200187
Akron179c8ac2015-06-30 19:30:50 +0200188 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200189 var resultButton = null;
Akron9a5b1e12016-12-06 18:18:23 +0100190 var leftButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200191 if (result != null) {
Akron9a5b1e12016-12-06 18:18:23 +0100192
193 // Add right buttons
Akron179c8ac2015-06-30 19:30:50 +0200194 resultButton = result.appendChild(document.createElement('div'));
Akron9a5b1e12016-12-06 18:18:23 +0100195 resultButton.classList.add('result', 'button');
196
197 leftButton = result.appendChild(document.createElement('div'));
198 leftButton.classList.add('result', 'button', 'left');
Akron179c8ac2015-06-30 19:30:50 +0200199 };
200
201 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200202 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200203
Akron7716f012015-07-01 20:38:32 +0200204 if (resultButton !== null) {
Akron19d97fe2016-09-06 20:47:05 +0200205 var kq;
206 var toggle = document.createElement('a');
207 toggle.setAttribute('title', loc.SHOW_KQ)
208 toggle.classList.add('show-kq', 'button');
209 toggle.appendChild(document.createElement('span'))
210 .appendChild(document.createTextNode(loc.SHOW_KQ));
211 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200212
Akron19d97fe2016-09-06 20:47:05 +0200213 var showKQ = function () {
214 if (kq === undefined) {
215 kq = document.createElement('div');
216 kq.setAttribute('id', 'koralquery');
217 kq.style.display = 'none';
218 var kqInner = document.createElement('div');
219 kq.appendChild(kqInner);
220 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
221 hljs.highlightBlock(kqInner);
222 var sb = document.getElementById('search');
223 sb.insertBefore(kq, sb.firstChild);
224 };
Akron7716f012015-07-01 20:38:32 +0200225
Akron19d97fe2016-09-06 20:47:05 +0200226 if (kq.style.display === 'none') {
227 kq.style.display = 'block';
228 show['kq'] = true;
229 }
230 else {
231 kq.style.display = 'none';
232 delete show['kq'];
233 };
234 };
Akron7716f012015-07-01 20:38:32 +0200235
Akron19d97fe2016-09-06 20:47:05 +0200236 if (toggle !== undefined) {
237
238 // Show koralquery
239 toggle.addEventListener('click', showKQ);
240 };
Akron179c8ac2015-06-30 19:30:50 +0200241 };
Akron7716f012015-07-01 20:38:32 +0200242
Akron00cd4d12016-05-31 21:01:11 +0200243 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200244 var errors = KorAP.koralQuery["errors"];
245 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200246
Akron19d97fe2016-09-06 20:47:05 +0200247 // Malformed query
248 if (errors[i][0] === 302 && errors[i][2]) {
249 obj.hint = hintClass.create();
250 obj.hint.alert(errors[i][2], errors[i][1]);
251 break;
252 }
Akronf0c31ed2016-06-11 11:27:01 +0200253
Akron19d97fe2016-09-06 20:47:05 +0200254 // no query
255 else if (errors[i][0] === 301) {
256 obj.hint = hintClass.create();
257 obj.hint.alert(0, errors[i][1]);
258 }
259 }
Akron00cd4d12016-05-31 21:01:11 +0200260 };
261
Akron7716f012015-07-01 20:38:32 +0200262 // Session has KQ visibility stored
263 if (show["kq"])
Akron19d97fe2016-09-06 20:47:05 +0200264 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200265 };
266
Akron9a5b1e12016-12-06 18:18:23 +0100267 // There is more than 0 matches and there is a resultButton
268 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000269
Akron9a5b1e12016-12-06 18:18:23 +0100270 if (resultButton !== null) {
271
272 /**
273 * Toggle the alignment (left <=> right)
274 */
275 // querySelector('div.button.right');
276
277 var toggle = document.createElement('a');
278 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
279 // Todo: Reuse old alignment from query
280 var cl = toggle.classList;
281 cl.add('align', 'right', 'button');
282 toggle.addEventListener(
283 'click',
284 function (e) {
285 var ol = document.querySelector('#search > ol');
286 ol.toggleClass("align-left", "align-right");
287 this.toggleClass("left", "right");
288 });
289 toggle.appendChild(document.createElement('span'))
290 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
291 resultButton.appendChild(toggle);
292 };
293
294 /*
295 // Not ready yet
296 if (leftButton !== null) {
297 var metaInfo = document.createElement('a');
298 metaInfo.setAttribute('title', loc.SHOW_META)
299
300 var cl = metaInfo.classList;
301 cl.add('show-meta', 'button');
302 metaInfo.appendChild(document.createElement('span'))
303 .appendChild(document.createTextNode(loc.SHOW_META));
304 leftButton.appendChild(metaInfo);
305 };
306 */
Nils Diewalda297f062015-04-02 00:23:46 +0000307 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000308
Nils Diewald7148c6f2015-05-04 15:07:53 +0000309 /**
310 * Toggle the Virtual Collection builder
311 */
312 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000313 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200314 var vcclick = function () {
Akron19d97fe2016-09-06 20:47:05 +0200315 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000316
Akron19d97fe2016-09-06 20:47:05 +0200317 // The vc is visible
318 if (vcname.classList.contains('active')) {
319 view.removeChild(vc.element());
320 vcname.classList.remove('active');
321 delete show['collection'];
322 }
Nils Diewald6283d692015-04-23 20:32:53 +0000323
Akron19d97fe2016-09-06 20:47:05 +0200324 // The vc is not visible
325 else {
326 if (vc === undefined)
327 vc = _getCurrentVC(vcClass, vcArray);
328 view.appendChild(vc.element());
329 vcname.classList.add('active');
330 show['collection'] = true;
331 };
Nils Diewald58141332015-04-07 16:18:45 +0000332 };
Akron04671e72017-05-11 20:47:32 +0200333
Akron179c8ac2015-06-30 19:30:50 +0200334 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200335 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200336 vcclick.apply();
Akron179c8ac2015-06-30 19:30:50 +0200337 };
Akron04671e72017-05-11 20:47:32 +0200338
339 // Check
340 if (_checkVCrewrite(vcClass)) {
341 console.log("Rewrite!");
342 };
Nils Diewald58141332015-04-07 16:18:45 +0000343 };
344
Akron19d97fe2016-09-06 20:47:05 +0200345
Nils Diewald58141332015-04-07 16:18:45 +0000346 /**
347 * Init Tutorial view
348 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000349 if (document.getElementById('view-tutorial')) {
350 window.tutorial = tutClass.create(
Akron19d97fe2016-09-06 20:47:05 +0200351 document.getElementById('view-tutorial'),
352 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000353 );
354 obj.tutorial = window.tutorial;
355 }
Nils Diewald58141332015-04-07 16:18:45 +0000356
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000357 // Tutorial is in parent
358 else if (window.parent) {
359 obj.tutorial = window.parent.tutorial;
360 };
361
362 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200363 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000364 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000365
Akron6ed13992016-05-23 18:06:05 +0200366 // Initialize documentation links
367 obj.tutorial.initDocLinks(document);
368 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000369
Nils Diewald845282c2015-05-14 07:53:03 +0000370
Nils Diewald58141332015-04-07 16:18:45 +0000371 /**
Akronc1457bf2015-06-11 19:24:00 +0200372 * Add VC creation on submission.
373 */
374 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200375 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200376 form.addEventListener('submit', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200377 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200378
Akron19d97fe2016-09-06 20:47:05 +0200379 // No query was defined
380 if (qf.value === undefined || qf.value === '') {
381 qf.focus();
382 e.halt();
383 KorAP.log(700, "No query given");
384 return;
385 };
Akron7716f012015-07-01 20:38:32 +0200386
Akron19d97fe2016-09-06 20:47:05 +0200387 // Store session information
388 session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200389
Akron19d97fe2016-09-06 20:47:05 +0200390 // Set Virtual collection
391 if (vc === undefined) {
392 vc = _getCurrentVC(vcClass, vcArray);
393 };
Akron7716f012015-07-01 20:38:32 +0200394
Akron19d97fe2016-09-06 20:47:05 +0200395 if (vc !== undefined) {
396 input.value = vc.toQuery();
397 }
398 else {
399 delete input['value'];
400 };
Akronc1457bf2015-06-11 19:24:00 +0200401 });
402 };
403
404 /**
Nils Diewald58141332015-04-07 16:18:45 +0000405 * Init hint helper
406 * has to be final because of
407 * reposition
408 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000409 // Todo: Pass an element, so this works with
410 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200411 if (obj.hint === undefined)
412 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000413
Nils Diewald58141332015-04-07 16:18:45 +0000414 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000415 });
416});
Akronc1457bf2015-06-11 19:24:00 +0200417
418// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200419function _getCurrentVC (vcClass, vcArray) {
420 var vc = vcClass.create(vcArray);
421 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
422 vc.fromJson(KorAP.koralQuery["collection"]);
423 };
Akronc1457bf2015-06-11 19:24:00 +0200424 return vc;
425};
Akron04671e72017-05-11 20:47:32 +0200426
427function _checkVCrewrite (vcClass) {
428 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
429 return vcClass.checkRewrite(KorAP.koralQuery["collection"]);
430 };
431 return false;
432}