blob: 4f8c41f1a0a386de8433a38731730e97166f5682 [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');
Akronec6bb8e2018-08-29 13:07:56 +0200106 vcchoose.addT(vc.getName());
Akron27ae9ec2015-06-23 00:43:21 +0200107
Akron1f0521b2018-08-28 13:01:24 +0200108 if (vc.wasRewritten()) {
109 vcchoose.classList.add('rewritten');
110 };
111
Nils Diewald7148c6f2015-05-04 15:07:53 +0000112 input.parentNode.insertBefore(vcname, input);
113 };
114
Nils Diewald7148c6f2015-05-04 15:07:53 +0000115 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000116 * Add actions to match entries
117 */
Akron0b489ad2018-02-02 16:49:32 +0100118 var inactiveLi = d.querySelectorAll(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000119 '#search > ol > li:not(.active)'
120 );
Akron5cb9b2b2018-07-24 17:01:09 +0200121 var matchCount = 0;
Akron6a535d42015-08-26 20:16:58 +0200122
Akron5cb9b2b2018-07-24 17:01:09 +0200123 for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
Akron6112b522018-07-25 13:48:53 +0200124 inactiveLi[matchCount].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200125 if (this._match !== undefined)
126 this._match.open();
127 else {
128 // lazyLoad
129 matchClass.create(this).open();
130 };
Akrone0c32c72017-04-25 22:38:23 +0200131 // This would prevent the sidebar to go back
132 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000133 });
Akron5cb9b2b2018-07-24 17:01:09 +0200134 inactiveLi[matchCount].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200135 var code = _codeFromEvent(e);
136
137 switch (code) {
138 case 32:
139 if (this._match !== undefined)
140 this._match.toggle();
141 else {
142 // lazyLoad
143 matchClass.create(this).open();
144 };
145 e.halt();
146 break;
147 };
Akron6a535d42015-08-26 20:16:58 +0200148 });
Nils Diewalda297f062015-04-02 00:23:46 +0000149 };
150
Akrone0c32c72017-04-25 22:38:23 +0200151 // Add focus listener to aside
Akron0b489ad2018-02-02 16:49:32 +0100152 var aside = d.getElementsByTagName('aside')[0];
Akrone0c32c72017-04-25 22:38:23 +0200153
154 if (aside && aside.classList.contains('active') == false) {
Akron1885ce92017-04-26 23:10:01 +0200155
Akron5258d462017-04-26 23:32:57 +0200156 // Horrible lock to deal with sidebar clicks
157 var asideClicked = false;
158
Akron1885ce92017-04-26 23:10:01 +0200159 // Make aside active on focus
160 aside.addEventListener('focus', function(e) {
Akrone0c32c72017-04-25 22:38:23 +0200161 this.classList.add('active');
162 });
163
Akron1885ce92017-04-26 23:10:01 +0200164 // Deactivate focus when clicking anywhere else
Akron0b489ad2018-02-02 16:49:32 +0100165 var body = d.getElementsByTagName('body')[0];
Akron1885ce92017-04-26 23:10:01 +0200166 if (body !== null) {
167 body.addEventListener('click', function() {
Akron5258d462017-04-26 23:32:57 +0200168 if (!asideClicked) {
169 aside.classList.remove('active');
170 }
171 else {
172 asideClicked = false;
173 };
Akron1885ce92017-04-26 23:10:01 +0200174 });
175 };
176
177 /* Stop click event on aside
178 * (to not trickle down to body)
179 */
180 aside.addEventListener('click', function(e) {
Akron5258d462017-04-26 23:32:57 +0200181 asideClicked = true;
Akrone0c32c72017-04-25 22:38:23 +0200182 });
183 };
Akronb9cdb102017-04-25 00:52:31 +0200184
185
Akron6bb71582016-06-10 20:41:08 +0200186 // Replace QL select menus with KorAP menus
Akron0b489ad2018-02-02 16:49:32 +0100187 var qlField = d.getElementById('ql-field');
Akronaba7a5a2016-08-15 21:58:33 +0200188 if (qlField !== null) {
Akron086fe5d2017-11-13 14:01:45 +0100189 KorAP.QLmenu = selectMenuClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100190 d.getElementById('ql-field').parentNode
Akronaba7a5a2016-08-15 21:58:33 +0200191 ).limit(5);
192 };
Akron6bb71582016-06-10 20:41:08 +0200193
Akron4d926f12018-07-16 15:30:25 +0200194 var resultInfo = d.getElementById('resultinfo');
195
Akron4d926f12018-07-16 15:30:25 +0200196 /**
197 * Add result panel
198 */
Akron5cb9b2b2018-07-24 17:01:09 +0200199 var resultPanel = resultPanelClass.create(show);
200
Akron4d926f12018-07-16 15:30:25 +0200201 if (resultInfo != null) {
Akron4d926f12018-07-16 15:30:25 +0200202
203 // Move buttons to resultinfo
204 resultInfo.appendChild(resultPanel.actions.element());
205
Akrone6538cd2018-07-16 17:52:33 +0200206 // The views are at the top of the search results
Akron4d926f12018-07-16 15:30:25 +0200207 var sb = d.getElementById('search');
208 sb.insertBefore(resultPanel.element(), sb.firstChild);
Akron4d926f12018-07-16 15:30:25 +0200209 };
210
211
Akron179c8ac2015-06-30 19:30:50 +0200212 // There is a koralQuery
Akron4d926f12018-07-16 15:30:25 +0200213 if (KorAP.koralQuery !== undefined) {
Akron5cb9b2b2018-07-24 17:01:09 +0200214
215 // Add KoralQuery view to result panel
Akron4d926f12018-07-16 15:30:25 +0200216 if (resultInfo !== null) {
Akron5cb9b2b2018-07-24 17:01:09 +0200217 resultPanel.addKqAction()
Akron179c8ac2015-06-30 19:30:50 +0200218 };
Akron7716f012015-07-01 20:38:32 +0200219
Akron00cd4d12016-05-31 21:01:11 +0200220 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200221 var errors = KorAP.koralQuery["errors"];
222 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200223
Akron19d97fe2016-09-06 20:47:05 +0200224 // Malformed query
225 if (errors[i][0] === 302 && errors[i][2]) {
226 obj.hint = hintClass.create();
227 obj.hint.alert(errors[i][2], errors[i][1]);
228 break;
229 }
Akronf0c31ed2016-06-11 11:27:01 +0200230
Akron19d97fe2016-09-06 20:47:05 +0200231 // no query
232 else if (errors[i][0] === 301) {
233 obj.hint = hintClass.create();
234 obj.hint.alert(0, errors[i][1]);
235 }
236 }
Akron00cd4d12016-05-31 21:01:11 +0200237 };
Akron179c8ac2015-06-30 19:30:50 +0200238 };
239
Akron5cb9b2b2018-07-24 17:01:09 +0200240
241 /*
242 * There is more than 0 matches, so allow for
243 * alignment toggling (left <=> right)
244 */
245 if (matchCount > 0)
246 resultPanel.addAlignAction();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000247
Nils Diewald5c5a7472015-04-02 22:13:38 +0000248
Akron5cb9b2b2018-07-24 17:01:09 +0200249 /*
Nils Diewald7148c6f2015-05-04 15:07:53 +0000250 * Toggle the Virtual Collection builder
251 */
252 if (vcname) {
Akronec6bb8e2018-08-29 13:07:56 +0200253 vc.onMinimize = function () {
254 vcname.classList.remove('active');
255 delete show['collection'];
256 };
Nils Diewald6283d692015-04-23 20:32:53 +0000257
Akronec6bb8e2018-08-29 13:07:56 +0200258 vc.onOpen = function () {
259 vcname.classList.add('active');
260 show['collection'] = true;
261 };
262
263 var vcclick = function () {
264
265 if (vc.isOpen()) {
266 vc.minimize()
267 }
268 else {
269 var view = d.getElementById('vc-view');
270 if (!view.firstChild)
271 view.appendChild(vc.element());
272
273 vc.open();
Akron19d97fe2016-09-06 20:47:05 +0200274 };
Nils Diewald58141332015-04-07 16:18:45 +0000275 };
Akron04671e72017-05-11 20:47:32 +0200276
Akron179c8ac2015-06-30 19:30:50 +0200277 vcname.onclick = vcclick;
Akron5c829e92017-05-12 18:10:00 +0200278
279 // Click, if the VC should be shown
Akron7716f012015-07-01 20:38:32 +0200280 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200281 vcclick.apply();
Akron04671e72017-05-11 20:47:32 +0200282 };
Nils Diewald58141332015-04-07 16:18:45 +0000283 };
284
Akron19d97fe2016-09-06 20:47:05 +0200285
Nils Diewald58141332015-04-07 16:18:45 +0000286 /**
287 * Init Tutorial view
288 */
Akron0b489ad2018-02-02 16:49:32 +0100289 if (d.getElementById('view-tutorial')) {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000290 window.tutorial = tutClass.create(
Akron0b489ad2018-02-02 16:49:32 +0100291 d.getElementById('view-tutorial'),
Akronf8035592018-05-24 20:40:51 +0200292 KorAP.session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000293 );
294 obj.tutorial = window.tutorial;
295 }
Nils Diewald58141332015-04-07 16:18:45 +0000296
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000297 // Tutorial is in parent
298 else if (window.parent) {
299 obj.tutorial = window.parent.tutorial;
300 };
301
Akron0b489ad2018-02-02 16:49:32 +0100302 // Initialize queries for d
Akron6ed13992016-05-23 18:06:05 +0200303 if (obj.tutorial) {
Akron0b489ad2018-02-02 16:49:32 +0100304 obj.tutorial.initQueries(d);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000305
Akron6ed13992016-05-23 18:06:05 +0200306 // Initialize documentation links
Akron0b489ad2018-02-02 16:49:32 +0100307 obj.tutorial.initDocLinks(d);
Akron6ed13992016-05-23 18:06:05 +0200308 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000309
Nils Diewald845282c2015-05-14 07:53:03 +0000310
Nils Diewald58141332015-04-07 16:18:45 +0000311 /**
Akronc1457bf2015-06-11 19:24:00 +0200312 * Add VC creation on submission.
313 */
Akron0b489ad2018-02-02 16:49:32 +0100314 var form = d.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200315 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200316 form.addEventListener('submit', function (e) {
Akron0b489ad2018-02-02 16:49:32 +0100317 var qf = d.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200318
Akron19d97fe2016-09-06 20:47:05 +0200319 // No query was defined
320 if (qf.value === undefined || qf.value === '') {
321 qf.focus();
322 e.halt();
323 KorAP.log(700, "No query given");
324 return;
325 };
Akron7716f012015-07-01 20:38:32 +0200326
Akron19d97fe2016-09-06 20:47:05 +0200327 // Store session information
Akronf8035592018-05-24 20:40:51 +0200328 KorAP.session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200329
Akron19d97fe2016-09-06 20:47:05 +0200330 if (vc !== undefined) {
331 input.value = vc.toQuery();
332 }
333 else {
334 delete input['value'];
335 };
Akronc1457bf2015-06-11 19:24:00 +0200336 });
337 };
338
339 /**
Nils Diewald58141332015-04-07 16:18:45 +0000340 * Init hint helper
341 * has to be final because of
342 * reposition
343 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000344 // Todo: Pass an element, so this works with
345 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200346 if (obj.hint === undefined)
347 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000348
Akron99713ef2017-06-28 18:19:28 +0200349 // Add the hinthelper to the KorAP object to make it manipulatable globally
Akron72f73572017-12-05 12:31:09 +0100350 KorAP.Hint = obj.hint;
Akron99713ef2017-06-28 18:19:28 +0200351
Nils Diewald58141332015-04-07 16:18:45 +0000352 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000353 });
354});