blob: 1666588fb8b75c8e1128c5771e8cbb3a344e5686 [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
Nils Diewald0e6992a2015-04-14 20:13:52 +000064 domReady(function (event) {
65 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020066
Akron4d926f12018-07-16 15:30:25 +020067 // What should be visible in the beginning?
Akronf8035592018-05-24 20:40:51 +020068 var show = KorAP.session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000069
70 /**
Akronf55504a2015-06-18 16:42:55 +020071 * Release notifications
72 */
73 if (KorAP.Notifications !== undefined) {
74 var n = KorAP.Notifications;
75 for (var i = 0; i < n.length; i++) {
Akron8ea84292018-10-24 13:41:52 +020076 var msg = n[i][1];
77 if (n[i][2]) {
78 msg += '<code class="src">'+n[i][2]+'</code>';
79 };
80 alertifyClass.log(msg, 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 */
Akron5c829e92017-05-12 18:10:00 +020087 var vcname, vcchoose;
Akron0b489ad2018-02-02 16:49:32 +010088 var input = d.getElementById('collection');
Akron1f0521b2018-08-28 13:01:24 +020089
90 var vc = vcClass.create(vcArray);
91
92 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +000093 if (input) {
94 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +010095 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +000096 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020097 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020098
Akron1f0521b2018-08-28 13:01:24 +020099 // Load virtual corpus object
Akron27ae9ec2015-06-23 00:43:21 +0200100 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron1f0521b2018-08-28 13:01:24 +0200101 try {
102 vc.fromJson(KorAP.koralQuery["collection"]);
103 }
104 catch (e) {
105 KorAP.log(0,e);
106 }
Akron27ae9ec2015-06-23 00:43:21 +0200107 };
108
Akron0b489ad2018-02-02 16:49:32 +0100109 vcchoose = vcname.addE('span');
Akronec6bb8e2018-08-29 13:07:56 +0200110 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200111
Akron1f0521b2018-08-28 13:01:24 +0200112 if (vc.wasRewritten()) {
113 vcchoose.classList.add('rewritten');
114 };
115
Nils Diewald7148c6f2015-05-04 15:07:53 +0000116 input.parentNode.insertBefore(vcname, input);
117 };
118
Nils Diewald7148c6f2015-05-04 15:07:53 +0000119 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000120 * Add actions to match entries
121 */
Akron0b489ad2018-02-02 16:49:32 +0100122 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000123 '#search > ol > li:not(.active)'
124 );
Akron5cb9b2b2018-07-24 17:01:09 +0200125 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200126
Akron5cb9b2b2018-07-24 17:01:09 +0200127 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200128 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200129 if (this._match !== undefined)
130 this._match.open();
131 else {
132 // lazyLoad
133 matchClass.create(this).open();
134 };
Akrone0c32c72017-04-25 22:38:23 +0200135 // This would prevent the sidebar to go back
136 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000137 });
Akron5cb9b2b2018-07-24 17:01:09 +0200138 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200139 var code = _codeFromEvent(e);
140
141 switch (code) {
142 case 32:
143 if (this._match !== undefined)
144 this._match.toggle();
145 else {
146 // lazyLoad
147 matchClass.create(this).open();
148 };
149 e.halt();
150 break;
151 };
Akron6a535d42015-08-26 20:16:58 +0200152 });
Nils Diewalda297f062015-04-02 00:23:46 +0000153 };
154
Akrone0c32c72017-04-25 22:38:23 +0200155 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100156 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200157
158 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200159
Akron5258d462017-04-26 23:32:57 +0200160 // Horrible lock to deal with sidebar clicks
161 var asideClicked = false;
162
Akron1885ce92017-04-26 23:10:01 +0200163 // Make aside active on focus
164 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200165 this.classList.add('active');
166 });
167
Akron1885ce92017-04-26 23:10:01 +0200168 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100169 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200170 if (body !== null) {
171 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200172 if (!asideClicked) {
173 aside.classList.remove('active');
174 }
175 else {
176 asideClicked = false;
177 };
Akron1885ce92017-04-26 23:10:01 +0200178 });
179 };
180
181 /* Stop click event on aside
182 * (to not trickle down to body)
183 */
184 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200185 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200186 });
187 };
Akronb9cdb102017-04-25 00:52:31 +0200188
189
Akron6bb71582016-06-10 20:41:08 +0200190 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100191 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200192 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100193 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100194 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200195 ).limit(5);
196 };
Akron6bb71582016-06-10 20:41:08 +0200197
Akron4d926f12018-07-16 15:30:25 +0200198 var resultInfo = d.getElementById('resultinfo');
199
Akron4d926f12018-07-16 15:30:25 +0200200 /**
201 * Add result panel
202 */
Akron5cb9b2b2018-07-24 17:01:09 +0200203 var resultPanel = resultPanelClass.create(show);
204
Akron4d926f12018-07-16 15:30:25 +0200205 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200206
207 // Move buttons to resultinfo
208 resultInfo.appendChild(resultPanel.actions.element());
209
Akrone6538cd2018-07-16 17:52:33 +0200210 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200211 var sb = d.getElementById('search');
212 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200213 };
214
215
Akron179c8ac2015-06-30 19:30:50 +0200216 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200217 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200218
219 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200220 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200221 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200222 };
Akron7716f012015-07-01 20:38:32 +0200223
Akron00cd4d12016-05-31 21:01:11 +0200224 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200225 var errors = KorAP.koralQuery["errors"];
226 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200227
Akron19d97fe2016-09-06 20:47:05 +0200228 // Malformed query
229 if (errors[i][0] === 302 && errors[i][2]) {
230 obj.hint = hintClass.create();
231 obj.hint.alert(errors[i][2], errors[i][1]);
232 break;
233 }
Akronf0c31ed2016-06-11 11:27:01 +0200234
Akron19d97fe2016-09-06 20:47:05 +0200235 // no query
236 else if (errors[i][0] === 301) {
237 obj.hint = hintClass.create();
238 obj.hint.alert(0, errors[i][1]);
239 }
240 }
Akron00cd4d12016-05-31 21:01:11 +0200241 };
Akron179c8ac2015-06-30 19:30:50 +0200242 };
243
Akron5cb9b2b2018-07-24 17:01:09 +0200244
245 /*
246 * There is more than 0 matches, so allow for
247 * alignment toggling (left <=> right)
248 */
249 if (matchCount > 0)
250 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000251
Nils Diewald5c5a7472015-04-02 22:13:38 +0000252
Akron5cb9b2b2018-07-24 17:01:09 +0200253 /*
Nils Diewald7148c6f2015-05-04 15:07:53 +0000254 * Toggle the Virtual Collection builder
255 */
256 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200257 vc.onMinimize = function () {
258 vcname.classList.remove('active');
259 delete show['collection'];
260 };
Nils Diewald6283d692015-04-23 20:32:53 +0000261
Akronec6bb8e2018-08-29 13:07:56 +0200262 vc.onOpen = function () {
263 vcname.classList.add('active');
264 show['collection'] = true;
265 };
266
267 var vcclick = function () {
268
269 if (vc.isOpen()) {
270 vc.minimize()
271 }
272 else {
273 var view = d.getElementById('vc-view');
274 if (!view.firstChild)
275 view.appendChild(vc.element());
276
277 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200278 };
Nils Diewald58141332015-04-07 16:18:45 +0000279 };
Akron04671e72017-05-11 20:47:32 +0200280
Akron179c8ac2015-06-30 19:30:50 +0200281 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200282
283 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200284 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200285 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200286 };
Nils Diewald58141332015-04-07 16:18:45 +0000287 };
288
Akron19d97fe2016-09-06 20:47:05 +0200289
Nils Diewald58141332015-04-07 16:18:45 +0000290 /**
291 * Init Tutorial view
292 */
Akron0b489ad2018-02-02 16:49:32 +0100293 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000294 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100295 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200296 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000297 );
298 obj.tutorial = window.tutorial;
299 }
Nils Diewald58141332015-04-07 16:18:45 +0000300
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000301 // Tutorial is in parent
302 else if (window.parent) {
303 obj.tutorial = window.parent.tutorial;
304 };
305
Akron0b489ad2018-02-02 16:49:32 +0100306 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200307 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100308 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000309
Akron6ed13992016-05-23 18:06:05 +0200310 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100311 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200312 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000313
Nils Diewald845282c2015-05-14 07:53:03 +0000314
Nils Diewald58141332015-04-07 16:18:45 +0000315 /**
Akronc1457bf2015-06-11 19:24:00 +0200316 * Add VC creation on submission.
317 */
Akron0b489ad2018-02-02 16:49:32 +0100318 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200319 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200320 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100321 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200322
Akron19d97fe2016-09-06 20:47:05 +0200323 // No query was defined
324 if (qf.value === undefined || qf.value === '') {
325 qf.focus();
326 e.halt();
327 KorAP.log(700, "No query given");
328 return;
329 };
Akron7716f012015-07-01 20:38:32 +0200330
Akron19d97fe2016-09-06 20:47:05 +0200331 // Store session information
Akronf8035592018-05-24 20:40:51 +0200332 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200333
Akron19d97fe2016-09-06 20:47:05 +0200334 if (vc !== undefined) {
335 input.value = vc.toQuery();
336 }
337 else {
338 delete input['value'];
339 };
Akronc1457bf2015-06-11 19:24:00 +0200340 });
341 };
342
343 /**
Nils Diewald58141332015-04-07 16:18:45 +0000344 * Init hint helper
345 * has to be final because of
346 * reposition
347 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000348 // Todo: Pass an element, so this works with
349 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200350 if (obj.hint === undefined)
351 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000352
Akron99713ef2017-06-28 18:19:28 +0200353 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100354 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200355
Nils Diewald58141332015-04-07 16:18:45 +0000356 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000357 });
358});