blob: d992c3f187398944902d1715f45f9e97d26bf84a [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++) {
Akron19d97fe2016-09-06 20:47:05 +020076 alertifyClass.log(n[i][1], n[i][0], 10000);
Akronf55504a2015-06-18 16:42:55 +020077 };
78 };
79
80 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000081 * Replace Virtual Collection field
82 */
Akron5c829e92017-05-12 18:10:00 +020083 var vcname, vcchoose;
Akron0b489ad2018-02-02 16:49:32 +010084 var input = d.getElementById('collection');
Akron1f0521b2018-08-28 13:01:24 +020085
86 var vc = vcClass.create(vcArray);
87
88 // Add vc name object
Nils Diewald7148c6f2015-05-04 15:07:53 +000089 if (input) {
90 input.style.display = 'none';
Akron0b489ad2018-02-02 16:49:32 +010091 vcname = d.createElement('span');
Nils Diewald7148c6f2015-05-04 15:07:53 +000092 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020093 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020094
Akron1f0521b2018-08-28 13:01:24 +020095 // Load virtual corpus object
Akron27ae9ec2015-06-23 00:43:21 +020096 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron1f0521b2018-08-28 13:01:24 +020097 try {
98 vc.fromJson(KorAP.koralQuery["collection"]);
99 }
100 catch (e) {
101 KorAP.log(0,e);
102 }
Akron27ae9ec2015-06-23 00:43:21 +0200103 };
104
Akron0b489ad2018-02-02 16:49:32 +0100105 vcchoose = vcname.addE('span');
Akron0b489ad2018-02-02 16:49:32 +0100106 vcchoose.addT(
Akron1f0521b2018-08-28 13:01:24 +0200107 vc.getName()
Nils Diewald7148c6f2015-05-04 15:07:53 +0000108 );
Akron27ae9ec2015-06-23 00:43:21 +0200109
Akron1f0521b2018-08-28 13:01:24 +0200110 if (vc.wasRewritten()) {
111 vcchoose.classList.add('rewritten');
112 };
113
Nils Diewald7148c6f2015-05-04 15:07:53 +0000114 input.parentNode.insertBefore(vcname, input);
115 };
116
Nils Diewald7148c6f2015-05-04 15:07:53 +0000117 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000118 * Add actions to match entries
119 */
Akron0b489ad2018-02-02 16:49:32 +0100120 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000121 '#search > ol > li:not(.active)'
122 );
Akron5cb9b2b2018-07-24 17:01:09 +0200123 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200124
Akron5cb9b2b2018-07-24 17:01:09 +0200125 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200126 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200127 if (this._match !== undefined)
128 this._match.open();
129 else {
130 // lazyLoad
131 matchClass.create(this).open();
132 };
Akrone0c32c72017-04-25 22:38:23 +0200133 // This would prevent the sidebar to go back
134 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000135 });
Akron5cb9b2b2018-07-24 17:01:09 +0200136 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200137 var code = _codeFromEvent(e);
138
139 switch (code) {
140 case 32:
141 if (this._match !== undefined)
142 this._match.toggle();
143 else {
144 // lazyLoad
145 matchClass.create(this).open();
146 };
147 e.halt();
148 break;
149 };
Akron6a535d42015-08-26 20:16:58 +0200150 });
Nils Diewalda297f062015-04-02 00:23:46 +0000151 };
152
Akrone0c32c72017-04-25 22:38:23 +0200153 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100154 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200155
156 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200157
Akron5258d462017-04-26 23:32:57 +0200158 // Horrible lock to deal with sidebar clicks
159 var asideClicked = false;
160
Akron1885ce92017-04-26 23:10:01 +0200161 // Make aside active on focus
162 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200163 this.classList.add('active');
164 });
165
Akron1885ce92017-04-26 23:10:01 +0200166 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100167 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200168 if (body !== null) {
169 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200170 if (!asideClicked) {
171 aside.classList.remove('active');
172 }
173 else {
174 asideClicked = false;
175 };
Akron1885ce92017-04-26 23:10:01 +0200176 });
177 };
178
179 /* Stop click event on aside
180 * (to not trickle down to body)
181 */
182 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200183 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200184 });
185 };
Akronb9cdb102017-04-25 00:52:31 +0200186
187
Akron6bb71582016-06-10 20:41:08 +0200188 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100189 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200190 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100191 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100192 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200193 ).limit(5);
194 };
Akron6bb71582016-06-10 20:41:08 +0200195
Akron4d926f12018-07-16 15:30:25 +0200196 var resultInfo = d.getElementById('resultinfo');
197
Akron4d926f12018-07-16 15:30:25 +0200198 /**
199 * Add result panel
200 */
Akron5cb9b2b2018-07-24 17:01:09 +0200201 var resultPanel = resultPanelClass.create(show);
202
Akron4d926f12018-07-16 15:30:25 +0200203 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200204
205 // Move buttons to resultinfo
206 resultInfo.appendChild(resultPanel.actions.element());
207
Akrone6538cd2018-07-16 17:52:33 +0200208 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200209 var sb = d.getElementById('search');
210 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200211 };
212
213
Akron179c8ac2015-06-30 19:30:50 +0200214 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200215 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200216
217 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200218 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200219 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200220 };
Akron7716f012015-07-01 20:38:32 +0200221
Akron00cd4d12016-05-31 21:01:11 +0200222 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200223 var errors = KorAP.koralQuery["errors"];
224 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200225
Akron19d97fe2016-09-06 20:47:05 +0200226 // Malformed query
227 if (errors[i][0] === 302 && errors[i][2]) {
228 obj.hint = hintClass.create();
229 obj.hint.alert(errors[i][2], errors[i][1]);
230 break;
231 }
Akronf0c31ed2016-06-11 11:27:01 +0200232
Akron19d97fe2016-09-06 20:47:05 +0200233 // no query
234 else if (errors[i][0] === 301) {
235 obj.hint = hintClass.create();
236 obj.hint.alert(0, errors[i][1]);
237 }
238 }
Akron00cd4d12016-05-31 21:01:11 +0200239 };
Akron179c8ac2015-06-30 19:30:50 +0200240 };
241
Akron5cb9b2b2018-07-24 17:01:09 +0200242
243 /*
244 * There is more than 0 matches, so allow for
245 * alignment toggling (left <=> right)
246 */
247 if (matchCount > 0)
248 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000249
Nils Diewald5c5a7472015-04-02 22:13:38 +0000250
Akron5cb9b2b2018-07-24 17:01:09 +0200251 /*
Nils Diewald7148c6f2015-05-04 15:07:53 +0000252 * Toggle the Virtual Collection builder
253 */
254 if (vcname) {
Akron179c8ac2015-06-30 19:30:50 +0200255 var vcclick = function () {
Akron0b489ad2018-02-02 16:49:32 +0100256 var view = d.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000257
Akron19d97fe2016-09-06 20:47:05 +0200258 // The vc is visible
259 if (vcname.classList.contains('active')) {
260 view.removeChild(vc.element());
261 vcname.classList.remove('active');
262 delete show['collection'];
263 }
Nils Diewald6283d692015-04-23 20:32:53 +0000264
Akron19d97fe2016-09-06 20:47:05 +0200265 // The vc is not visible
266 else {
Akron19d97fe2016-09-06 20:47:05 +0200267 view.appendChild(vc.element());
268 vcname.classList.add('active');
269 show['collection'] = true;
270 };
Nils Diewald58141332015-04-07 16:18:45 +0000271 };
Akron04671e72017-05-11 20:47:32 +0200272
Akron179c8ac2015-06-30 19:30:50 +0200273 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200274
275 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200276 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200277 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200278 };
Nils Diewald58141332015-04-07 16:18:45 +0000279 };
280
Akron19d97fe2016-09-06 20:47:05 +0200281
Nils Diewald58141332015-04-07 16:18:45 +0000282 /**
283 * Init Tutorial view
284 */
Akron0b489ad2018-02-02 16:49:32 +0100285 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000286 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100287 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200288 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000289 );
290 obj.tutorial = window.tutorial;
291 }
Nils Diewald58141332015-04-07 16:18:45 +0000292
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000293 // Tutorial is in parent
294 else if (window.parent) {
295 obj.tutorial = window.parent.tutorial;
296 };
297
Akron0b489ad2018-02-02 16:49:32 +0100298 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200299 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100300 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000301
Akron6ed13992016-05-23 18:06:05 +0200302 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100303 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200304 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000305
Nils Diewald845282c2015-05-14 07:53:03 +0000306
Nils Diewald58141332015-04-07 16:18:45 +0000307 /**
Akronc1457bf2015-06-11 19:24:00 +0200308 * Add VC creation on submission.
309 */
Akron0b489ad2018-02-02 16:49:32 +0100310 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200311 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200312 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100313 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200314
Akron19d97fe2016-09-06 20:47:05 +0200315 // No query was defined
316 if (qf.value === undefined || qf.value === '') {
317 qf.focus();
318 e.halt();
319 KorAP.log(700, "No query given");
320 return;
321 };
Akron7716f012015-07-01 20:38:32 +0200322
Akron19d97fe2016-09-06 20:47:05 +0200323 // Store session information
Akronf8035592018-05-24 20:40:51 +0200324 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200325
Akron19d97fe2016-09-06 20:47:05 +0200326 if (vc !== undefined) {
327 input.value = vc.toQuery();
328 }
329 else {
330 delete input['value'];
331 };
Akronc1457bf2015-06-11 19:24:00 +0200332 });
333 };
334
335 /**
Nils Diewald58141332015-04-07 16:18:45 +0000336 * Init hint helper
337 * has to be final because of
338 * reposition
339 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000340 // Todo: Pass an element, so this works with
341 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200342 if (obj.hint === undefined)
343 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000344
Akron99713ef2017-06-28 18:19:28 +0200345 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100346 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200347
Nils Diewald58141332015-04-07 16:18:45 +0000348 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000349 });
350});