blob: d94b1b533cfc3fe57e06b7d475a4d20b1e70fdb4 [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;
hebasta48842cf2018-12-11 12:57:38 +010093
94 document.addEventListener('vcChange', function (e) {
95 vc.checkStatActive(e.detail);
96 }, false);
Akron1f0521b2018-08-28 13:01:24 +020097
98 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +000099 if (input) {
100 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +0100101 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000102 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +0200103 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +0200104
Akron1f0521b2018-08-28 13:01:24 +0200105 // Load virtual corpus object
Akron27ae9ec2015-06-23 00:43:21 +0200106 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron1f0521b2018-08-28 13:01:24 +0200107 try {
108 vc.fromJson(KorAP.koralQuery["collection"]);
109 }
110 catch (e) {
111 KorAP.log(0,e);
112 }
Akron27ae9ec2015-06-23 00:43:21 +0200113 };
114
Akron0b489ad2018-02-02 16:49:32 +0100115 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200116 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200117
Akron1f0521b2018-08-28 13:01:24 +0200118 if (vc.wasRewritten()) {
119 vcchoose.classList.add('rewritten');
120 };
121
Nils Diewald7148c6f2015-05-04 15:07:53 +0000122 input.parentNode.insertBefore(vcname, input);
123 };
124
Nils Diewald7148c6f2015-05-04 15:07:53 +0000125 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000126 * Add actions to match entries
127 */
Akron0b489ad2018-02-02 16:49:32 +0100128 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000129 '#search > ol > li:not(.active)'
130 );
Akron5cb9b2b2018-07-24 17:01:09 +0200131 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200132
Akron5cb9b2b2018-07-24 17:01:09 +0200133 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200134 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200135 if (this._match !== undefined)
136 this._match.open();
137 else {
138 // lazyLoad
139 matchClass.create(this).open();
140 };
Akrone0c32c72017-04-25 22:38:23 +0200141 // This would prevent the sidebar to go back
142 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000143 });
Akron5cb9b2b2018-07-24 17:01:09 +0200144 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200145 var code = _codeFromEvent(e);
146
147 switch (code) {
148 case 32:
149 if (this._match !== undefined)
150 this._match.toggle();
151 else {
152 // lazyLoad
153 matchClass.create(this).open();
154 };
155 e.halt();
156 break;
157 };
Akron6a535d42015-08-26 20:16:58 +0200158 });
Nils Diewalda297f062015-04-02 00:23:46 +0000159 };
160
Akrone0c32c72017-04-25 22:38:23 +0200161 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100162 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200163
164 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200165
Akron5258d462017-04-26 23:32:57 +0200166 // Horrible lock to deal with sidebar clicks
167 var asideClicked = false;
168
Akron1885ce92017-04-26 23:10:01 +0200169 // Make aside active on focus
170 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200171 this.classList.add('active');
172 });
173
Akron1885ce92017-04-26 23:10:01 +0200174 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100175 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200176 if (body !== null) {
177 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200178 if (!asideClicked) {
179 aside.classList.remove('active');
180 }
181 else {
182 asideClicked = false;
183 };
Akron1885ce92017-04-26 23:10:01 +0200184 });
185 };
186
187 /* Stop click event on aside
188 * (to not trickle down to body)
189 */
190 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200191 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200192 });
193 };
Akronb9cdb102017-04-25 00:52:31 +0200194
195
Akron6bb71582016-06-10 20:41:08 +0200196 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100197 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200198 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100199 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100200 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200201 ).limit(5);
202 };
Akron6bb71582016-06-10 20:41:08 +0200203
Akron4d926f12018-07-16 15:30:25 +0200204 var resultInfo = d.getElementById('resultinfo');
205
Akron4d926f12018-07-16 15:30:25 +0200206 /**
207 * Add result panel
208 */
Akron5cb9b2b2018-07-24 17:01:09 +0200209 var resultPanel = resultPanelClass.create(show);
210
Akron4d926f12018-07-16 15:30:25 +0200211 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200212
213 // Move buttons to resultinfo
214 resultInfo.appendChild(resultPanel.actions.element());
215
Akrone6538cd2018-07-16 17:52:33 +0200216 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200217 var sb = d.getElementById('search');
218 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200219 };
220
221
Akron179c8ac2015-06-30 19:30:50 +0200222 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200223 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200224
225 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200226 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200227 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200228 };
Akron7716f012015-07-01 20:38:32 +0200229
Akron00cd4d12016-05-31 21:01:11 +0200230 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200231 var errors = KorAP.koralQuery["errors"];
232 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200233
Akron19d97fe2016-09-06 20:47:05 +0200234 // Malformed query
235 if (errors[i][0] === 302 && errors[i][2]) {
236 obj.hint = hintClass.create();
237 obj.hint.alert(errors[i][2], errors[i][1]);
238 break;
239 }
Akronf0c31ed2016-06-11 11:27:01 +0200240
Akron19d97fe2016-09-06 20:47:05 +0200241 // no query
242 else if (errors[i][0] === 301) {
243 obj.hint = hintClass.create();
244 obj.hint.alert(0, errors[i][1]);
245 }
246 }
Akron00cd4d12016-05-31 21:01:11 +0200247 };
Akron179c8ac2015-06-30 19:30:50 +0200248 };
249
Akron5cb9b2b2018-07-24 17:01:09 +0200250
251 /*
252 * There is more than 0 matches, so allow for
253 * alignment toggling (left <=> right)
254 */
255 if (matchCount > 0)
256 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000257
Nils Diewald5c5a7472015-04-02 22:13:38 +0000258
Akron5cb9b2b2018-07-24 17:01:09 +0200259 /*
Nils Diewald7148c6f2015-05-04 15:07:53 +0000260 * Toggle the Virtual Collection builder
261 */
262 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200263 vc.onMinimize = function () {
264 vcname.classList.remove('active');
265 delete show['collection'];
266 };
Nils Diewald6283d692015-04-23 20:32:53 +0000267
Akronec6bb8e2018-08-29 13:07:56 +0200268 vc.onOpen = function () {
269 vcname.classList.add('active');
Akroncfe8ecc2018-11-20 18:46:16 +0100270
271 var view = d.getElementById('vc-view');
272 if (!view.firstChild)
273 view.appendChild(this.element());
274
Akronec6bb8e2018-08-29 13:07:56 +0200275 show['collection'] = true;
276 };
277
278 var vcclick = function () {
Akronec6bb8e2018-08-29 13:07:56 +0200279 if (vc.isOpen()) {
280 vc.minimize()
281 }
282 else {
Akronec6bb8e2018-08-29 13:07:56 +0200283 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200284 };
Nils Diewald58141332015-04-07 16:18:45 +0000285 };
Akron04671e72017-05-11 20:47:32 +0200286
Akron179c8ac2015-06-30 19:30:50 +0200287 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200288
289 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200290 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200291 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200292 };
Nils Diewald58141332015-04-07 16:18:45 +0000293 };
294
Akron19d97fe2016-09-06 20:47:05 +0200295
Nils Diewald58141332015-04-07 16:18:45 +0000296 /**
297 * Init Tutorial view
298 */
Akron0b489ad2018-02-02 16:49:32 +0100299 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000300 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100301 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200302 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000303 );
304 obj.tutorial = window.tutorial;
305 }
Nils Diewald58141332015-04-07 16:18:45 +0000306
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000307 // Tutorial is in parent
308 else if (window.parent) {
309 obj.tutorial = window.parent.tutorial;
310 };
311
Akron0b489ad2018-02-02 16:49:32 +0100312 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200313 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100314 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000315
Akron6ed13992016-05-23 18:06:05 +0200316 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100317 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200318 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000319
Nils Diewald845282c2015-05-14 07:53:03 +0000320
Nils Diewald58141332015-04-07 16:18:45 +0000321 /**
Akronc1457bf2015-06-11 19:24:00 +0200322 * Add VC creation on submission.
323 */
Akron0b489ad2018-02-02 16:49:32 +0100324 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200325 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200326 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100327 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200328
Akron19d97fe2016-09-06 20:47:05 +0200329 // No query was defined
330 if (qf.value === undefined || qf.value === '') {
331 qf.focus();
332 e.halt();
333 KorAP.log(700, "No query given");
334 return;
335 };
Akron7716f012015-07-01 20:38:32 +0200336
Akron19d97fe2016-09-06 20:47:05 +0200337 // Store session information
Akronf8035592018-05-24 20:40:51 +0200338 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200339
Akron19d97fe2016-09-06 20:47:05 +0200340 if (vc !== undefined) {
341 input.value = vc.toQuery();
342 }
343 else {
344 delete input['value'];
345 };
Akronc1457bf2015-06-11 19:24:00 +0200346 });
347 };
348
349 /**
Nils Diewald58141332015-04-07 16:18:45 +0000350 * Init hint helper
351 * has to be final because of
352 * reposition
353 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000354 // Todo: Pass an element, so this works with
355 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200356 if (obj.hint === undefined)
357 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000358
Akron99713ef2017-06-28 18:19:28 +0200359 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100360 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200361
Nils Diewald58141332015-04-07 16:18:45 +0000362 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000363 });
364});