blob: 34a838ada0585ca485b1a61e9cddfb77c8d4973d [file] [log] [blame]
Akron7524be12016-06-01 17:31:33 +02001/*
Akron5cb9b2b2018-07-24 17:01:09 +02002 * Initialize The JS frontend part and decorate
3 * the static HTML data.
4 *
5 * @author Nils Diewald
6 *
Akron7524be12016-06-01 17:31:33 +02007 * TODO: Create lazy loading of objects including
8 * - obj.hint()
9 * - obj.alertify()
10 * - obj.session()
11 * - obj.tutorial()
12 * - obj.vc() // toggle
13 * - obj.matchCreate() (using webpack)
14 * - obj.koral() (show result, parse for errors ...)
15 * - obj.alignment() // toggle
16 */
17
Nils Diewald0e6992a2015-04-14 20:13:52 +000018define([
19 'match',
20 'hint',
21 'vc',
22 'tutorial',
23 'lib/domReady',
Akron27ae9ec2015-06-23 00:43:21 +020024 'vc/array',
Nils Diewald7148c6f2015-05-04 15:07:53 +000025 'lib/alertify',
Akron7716f012015-07-01 20:38:32 +020026 'session',
Akron6bb71582016-06-10 20:41:08 +020027 'selectMenu',
Akron5cb9b2b2018-07-24 17:01:09 +020028 'panel/result',
Nils Diewald7148c6f2015-05-04 15:07:53 +000029 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000030 'mailToChiffre',
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 'util'
32], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020033 hintClass,
34 vcClass,
35 tutClass,
36 domReady,
Akron19d97fe2016-09-06 20:47:05 +020037 vcArray,
38 alertifyClass,
39 sessionClass,
Akron4d926f12018-07-16 15:30:25 +020040 selectMenuClass,
Akron5cb9b2b2018-07-24 17:01:09 +020041 resultPanelClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000042
Akron0b489ad2018-02-02 16:49:32 +010043 const d = document;
Akron9cc3eaf2015-06-10 22:15:52 +020044
Akronf8035592018-05-24 20:40:51 +020045 KorAP.session = sessionClass.create('KalamarJS');
46
Nils Diewalda0defc42015-05-07 23:54:17 +000047 // Override KorAP.log
48 window.alertify = alertifyClass;
Akronc0a2da82018-07-04 15:27:37 +020049 KorAP.log = function (code, msg, src) {
50
51 if (src) {
52 msg += '<code class="src">'+src+'</code>';
53 };
Nils Diewalda0defc42015-05-07 23:54:17 +000054
55 // Use alertify to log errors
56 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020057 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020058 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000059 'error',
Akronf55504a2015-06-18 16:42:55 +020060 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000061 );
62 };
63
hebasta2758b582018-11-19 15:59:42 +010064 KorAP.vc = vcClass.create(vcArray);
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++) {
Akron8ea84292018-10-24 13:41:52 +020078 var msg = n[i][1];
79 if (n[i][2]) {
80 msg += '<code class="src">'+n[i][2]+'</code>';
81 };
82 alertifyClass.log(msg, n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020083 };
84 };
85
86 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000087 * Replace Virtual Collection field
88 */
Akron5c829e92017-05-12 18:10:00 +020089 var vcname, vcchoose;
Akron0b489ad2018-02-02 16:49:32 +010090 var input = d.getElementById('collection');
Akron1f0521b2018-08-28 13:01:24 +020091
hebasta2758b582018-11-19 15:59:42 +010092 var vc = KorAP.vc;
Akron1f0521b2018-08-28 13:01:24 +020093
94 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +000095 if (input) {
96 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +010097 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +000098 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020099 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200100
Akron1f0521b2018-08-28 13:01:24 +0200101 // Load virtual corpus object
Akron27ae9ec2015-06-23 00:43:21 +0200102 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron1f0521b2018-08-28 13:01:24 +0200103 try {
104 vc.fromJson(KorAP.koralQuery["collection"]);
105 }
106 catch (e) {
107 KorAP.log(0,e);
108 }
Akron27ae9ec2015-06-23 00:43:21 +0200109 };
110
Akron0b489ad2018-02-02 16:49:32 +0100111 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200112 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200113
Akron1f0521b2018-08-28 13:01:24 +0200114 if (vc.wasRewritten()) {
115 vcchoose.classList.add('rewritten');
116 };
117
Nils Diewald7148c6f2015-05-04 15:07:53 +0000118 input.parentNode.insertBefore(vcname, input);
119 };
120
Nils Diewald7148c6f2015-05-04 15:07:53 +0000121 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000122 * Add actions to match entries
123 */
Akron0b489ad2018-02-02 16:49:32 +0100124 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000125 '#search > ol > li:not(.active)'
126 );
Akron5cb9b2b2018-07-24 17:01:09 +0200127 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200128
Akron5cb9b2b2018-07-24 17:01:09 +0200129 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200130 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200131 if (this._match !== undefined)
132 this._match.open();
133 else {
134 // lazyLoad
135 matchClass.create(this).open();
136 };
Akrone0c32c72017-04-25 22:38:23 +0200137 // This would prevent the sidebar to go back
138 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000139 });
Akron5cb9b2b2018-07-24 17:01:09 +0200140 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200141 var code = _codeFromEvent(e);
142
143 switch (code) {
144 case 32:
145 if (this._match !== undefined)
146 this._match.toggle();
147 else {
148 // lazyLoad
149 matchClass.create(this).open();
150 };
151 e.halt();
152 break;
153 };
Akron6a535d42015-08-26 20:16:58 +0200154 });
Nils Diewalda297f062015-04-02 00:23:46 +0000155 };
156
Akrone0c32c72017-04-25 22:38:23 +0200157 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100158 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200159
160 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200161
Akron5258d462017-04-26 23:32:57 +0200162 // Horrible lock to deal with sidebar clicks
163 var asideClicked = false;
164
Akron1885ce92017-04-26 23:10:01 +0200165 // Make aside active on focus
166 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200167 this.classList.add('active');
168 });
169
Akron1885ce92017-04-26 23:10:01 +0200170 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100171 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200172 if (body !== null) {
173 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200174 if (!asideClicked) {
175 aside.classList.remove('active');
176 }
177 else {
178 asideClicked = false;
179 };
Akron1885ce92017-04-26 23:10:01 +0200180 });
181 };
182
183 /* Stop click event on aside
184 * (to not trickle down to body)
185 */
186 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200187 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200188 });
189 };
Akronb9cdb102017-04-25 00:52:31 +0200190
191
Akron6bb71582016-06-10 20:41:08 +0200192 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100193 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200194 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100195 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100196 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200197 ).limit(5);
198 };
Akron6bb71582016-06-10 20:41:08 +0200199
Akron4d926f12018-07-16 15:30:25 +0200200 var resultInfo = d.getElementById('resultinfo');
201
Akron4d926f12018-07-16 15:30:25 +0200202 /**
203 * Add result panel
204 */
Akron5cb9b2b2018-07-24 17:01:09 +0200205 var resultPanel = resultPanelClass.create(show);
206
Akron4d926f12018-07-16 15:30:25 +0200207 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200208
209 // Move buttons to resultinfo
210 resultInfo.appendChild(resultPanel.actions.element());
211
Akrone6538cd2018-07-16 17:52:33 +0200212 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200213 var sb = d.getElementById('search');
214 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200215 };
216
217
Akron179c8ac2015-06-30 19:30:50 +0200218 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200219 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200220
221 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200222 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200223 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200224 };
Akron7716f012015-07-01 20:38:32 +0200225
Akron00cd4d12016-05-31 21:01:11 +0200226 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200227 var errors = KorAP.koralQuery["errors"];
228 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200229
Akron19d97fe2016-09-06 20:47:05 +0200230 // Malformed query
231 if (errors[i][0] === 302 && errors[i][2]) {
232 obj.hint = hintClass.create();
233 obj.hint.alert(errors[i][2], errors[i][1]);
234 break;
235 }
Akronf0c31ed2016-06-11 11:27:01 +0200236
Akron19d97fe2016-09-06 20:47:05 +0200237 // no query
238 else if (errors[i][0] === 301) {
239 obj.hint = hintClass.create();
240 obj.hint.alert(0, errors[i][1]);
241 }
242 }
Akron00cd4d12016-05-31 21:01:11 +0200243 };
Akron179c8ac2015-06-30 19:30:50 +0200244 };
245
Akron5cb9b2b2018-07-24 17:01:09 +0200246
247 /*
248 * There is more than 0 matches, so allow for
249 * alignment toggling (left <=> right)
250 */
251 if (matchCount > 0)
252 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000253
Nils Diewald5c5a7472015-04-02 22:13:38 +0000254
Akron5cb9b2b2018-07-24 17:01:09 +0200255 /*
Nils Diewald7148c6f2015-05-04 15:07:53 +0000256 * Toggle the Virtual Collection builder
257 */
258 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200259 vc.onMinimize = function () {
260 vcname.classList.remove('active');
261 delete show['collection'];
262 };
Nils Diewald6283d692015-04-23 20:32:53 +0000263
Akronec6bb8e2018-08-29 13:07:56 +0200264 vc.onOpen = function () {
265 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100266
267 var view = d.getElementById('vc-view');
268 if (!view.firstChild)
269 view.appendChild(this.element());
270
Akronec6bb8e2018-08-29 13:07:56 +0200271 show['collection'] = true;
272 };
273
274 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200275 if (vc.isOpen()) {
276 vc.minimize()
277 }
278 else {
Akronec6bb8e2018-08-29 13:07:56 +0200279 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200280 };
Nils Diewald58141332015-04-07 16:18:45 +0000281 };
Akron04671e72017-05-11 20:47:32 +0200282
Akron179c8ac2015-06-30 19:30:50 +0200283 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200284
285 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200286 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200287 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200288 };
Nils Diewald58141332015-04-07 16:18:45 +0000289 };
290
Akron19d97fe2016-09-06 20:47:05 +0200291
Nils Diewald58141332015-04-07 16:18:45 +0000292 /**
293 * Init Tutorial view
294 */
Akron0b489ad2018-02-02 16:49:32 +0100295 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000296 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100297 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200298 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000299 );
300 obj.tutorial = window.tutorial;
301 }
Nils Diewald58141332015-04-07 16:18:45 +0000302
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000303 // Tutorial is in parent
304 else if (window.parent) {
305 obj.tutorial = window.parent.tutorial;
306 };
307
Akron0b489ad2018-02-02 16:49:32 +0100308 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200309 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100310 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000311
Akron6ed13992016-05-23 18:06:05 +0200312 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100313 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200314 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000315
Nils Diewald845282c2015-05-14 07:53:03 +0000316
Nils Diewald58141332015-04-07 16:18:45 +0000317 /**
Akronc1457bf2015-06-11 19:24:00 +0200318 * Add VC creation on submission.
319 */
Akron0b489ad2018-02-02 16:49:32 +0100320 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200321 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200322 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100323 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200324
Akron19d97fe2016-09-06 20:47:05 +0200325 // No query was defined
326 if (qf.value === undefined || qf.value === '') {
327 qf.focus();
328 e.halt();
329 KorAP.log(700, "No query given");
330 return;
331 };
Akron7716f012015-07-01 20:38:32 +0200332
Akron19d97fe2016-09-06 20:47:05 +0200333 // Store session information
Akronf8035592018-05-24 20:40:51 +0200334 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200335
Akron19d97fe2016-09-06 20:47:05 +0200336 if (vc !== undefined) {
337 input.value = vc.toQuery();
338 }
339 else {
340 delete input['value'];
341 };
Akronc1457bf2015-06-11 19:24:00 +0200342 });
343 };
344
345 /**
Nils Diewald58141332015-04-07 16:18:45 +0000346 * Init hint helper
347 * has to be final because of
348 * reposition
349 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000350 // Todo: Pass an element, so this works with
351 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200352 if (obj.hint === undefined)
353 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000354
Akron99713ef2017-06-28 18:19:28 +0200355 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100356 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200357
Nils Diewald58141332015-04-07 16:18:45 +0000358 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000359 });
360});