blob: 1a2992b05639738eb39d4f2d0d94abdcb2e18711 [file] [log] [blame]
Akron7524be12016-06-01 17:31:33 +02001/*
2 * TODO: Create lazy loading of objects including
3 * - obj.hint()
4 * - obj.alertify()
5 * - obj.session()
6 * - obj.tutorial()
7 * - obj.vc() // toggle
8 * - obj.matchCreate() (using webpack)
9 * - obj.koral() (show result, parse for errors ...)
10 * - obj.alignment() // toggle
11 */
12
Nils Diewald0e6992a2015-04-14 20:13:52 +000013define([
14 'match',
15 'hint',
16 'vc',
17 'tutorial',
18 'lib/domReady',
Nils Diewald7148c6f2015-05-04 15:07:53 +000019 'hint/array',
Akron27ae9ec2015-06-23 00:43:21 +020020 'vc/array',
Nils Diewald7148c6f2015-05-04 15:07:53 +000021 'lib/alertify',
Akron7716f012015-07-01 20:38:32 +020022 'session',
Akronbe105b92016-01-04 14:13:20 +010023 'tagger',
Akron6bb71582016-06-10 20:41:08 +020024 'selectMenu',
Nils Diewald7148c6f2015-05-04 15:07:53 +000025 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000026 'mailToChiffre',
Nils Diewald845282c2015-05-14 07:53:03 +000027 'lib/highlight/highlight.pack',
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 'util'
29], function (matchClass,
Akron19d97fe2016-09-06 20:47:05 +020030 hintClass,
31 vcClass,
32 tutClass,
33 domReady,
34 hintArray,
35 vcArray,
36 alertifyClass,
37 sessionClass,
38 tagger,
39 selectMenuClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000040
41 // Set hint array for hint helper
42 KorAP.hintArray = hintArray;
43
Akron9cc3eaf2015-06-10 22:15:52 +020044 // Localization values
45 var loc = KorAP.Locale;
Akron7716f012015-07-01 20:38:32 +020046 loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora';
47 loc.VC_oneCollection = loc.VC_oneCollection || 'one Collection';
48 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle Alignment';
49 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akron9a5b1e12016-12-06 18:18:23 +010050 loc.SHOW_META = loc.SHOW_META || 'show Metadata';
51
Akron9cc3eaf2015-06-10 22:15:52 +020052
Nils Diewalda0defc42015-05-07 23:54:17 +000053 // Override KorAP.log
54 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020055 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000056
57 // Use alertify to log errors
58 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020059 (code === 0 ? '' : code + ': ') +
Akron19d97fe2016-09-06 20:47:05 +020060 msg,
Nils Diewalda0defc42015-05-07 23:54:17 +000061 'error',
Akronf55504a2015-06-18 16:42:55 +020062 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000063 );
64 };
65
Nils Diewald0e6992a2015-04-14 20:13:52 +000066 domReady(function (event) {
67 var obj = {};
Akron71b91e42016-06-01 22:12:43 +020068
Akron7716f012015-07-01 20:38:32 +020069 var session = sessionClass.create('KalamarJS');
70
71 // What should be visible?
72 var show = session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000073
74 /**
Akronf55504a2015-06-18 16:42:55 +020075 * Release notifications
76 */
77 if (KorAP.Notifications !== undefined) {
78 var n = KorAP.Notifications;
79 for (var i = 0; i < n.length; i++) {
Akron19d97fe2016-09-06 20:47:05 +020080 alertifyClass.log(n[i][1], 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 */
87 var vcname;
Akronc1457bf2015-06-11 19:24:00 +020088 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000089 if (input) {
90 input.style.display = 'none';
91 vcname = document.createElement('span');
92 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020093 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020094
Akron27ae9ec2015-06-23 00:43:21 +020095 var currentVC = loc.VC_allCorpora;
96 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
Akron19d97fe2016-09-06 20:47:05 +020097 currentVC = loc.VC_oneCollection;
Akron27ae9ec2015-06-23 00:43:21 +020098 };
99
Nils Diewald7148c6f2015-05-04 15:07:53 +0000100 vcname.appendChild(
Akron19d97fe2016-09-06 20:47:05 +0200101 document.createTextNode(
102 document.getElementById('collection-name').value || currentVC
103 )
Nils Diewald7148c6f2015-05-04 15:07:53 +0000104 );
Akron27ae9ec2015-06-23 00:43:21 +0200105
Nils Diewald7148c6f2015-05-04 15:07:53 +0000106 input.parentNode.insertBefore(vcname, input);
107 };
108
Nils Diewald7148c6f2015-05-04 15:07:53 +0000109 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000110 * Add actions to match entries
111 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000112 var inactiveLi = document.querySelectorAll(
113 '#search > ol > li:not(.active)'
114 );
Nils Diewalda297f062015-04-02 00:23:46 +0000115 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200116
Nils Diewalda297f062015-04-02 00:23:46 +0000117 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000118 inactiveLi[i].addEventListener('click', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200119 if (this._match !== undefined)
120 this._match.open();
121 else {
122 // lazyLoad
123 matchClass.create(this).open();
124 };
Akrone0c32c72017-04-25 22:38:23 +0200125 // This would prevent the sidebar to go back
126 // e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000127 });
Akron6a535d42015-08-26 20:16:58 +0200128 inactiveLi[i].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200129 var code = _codeFromEvent(e);
130
131 switch (code) {
132 case 32:
133 if (this._match !== undefined)
134 this._match.toggle();
135 else {
136 // lazyLoad
137 matchClass.create(this).open();
138 };
139 e.halt();
140 break;
141 };
Akron6a535d42015-08-26 20:16:58 +0200142 });
Nils Diewalda297f062015-04-02 00:23:46 +0000143 };
144
Akrone0c32c72017-04-25 22:38:23 +0200145 // Add focus listener to aside
146 var aside = document.getElementsByTagName('aside')[0];
147
148 if (aside && aside.classList.contains('active') == false) {
149 aside.addEventListener('focus', function() {
150 this.classList.add('active');
151 });
152
153 var main = document.getElementsByTagName('main')[0];
154 main.addEventListener('click', function() {
155 aside.classList.remove('active');
156 });
157 };
Akronb9cdb102017-04-25 00:52:31 +0200158
159
Akron6bb71582016-06-10 20:41:08 +0200160 // Replace QL select menus with KorAP menus
Akronaba7a5a2016-08-15 21:58:33 +0200161 var qlField = document.getElementById('ql-field');
162 if (qlField !== null) {
163 selectMenuClass.create(
164 document.getElementById('ql-field').parentNode
165 ).limit(5);
166 };
Akron6bb71582016-06-10 20:41:08 +0200167
Akron179c8ac2015-06-30 19:30:50 +0200168 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200169 var resultButton = null;
Akron9a5b1e12016-12-06 18:18:23 +0100170 var leftButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200171 if (result != null) {
Akron9a5b1e12016-12-06 18:18:23 +0100172
173 // Add right buttons
Akron179c8ac2015-06-30 19:30:50 +0200174 resultButton = result.appendChild(document.createElement('div'));
Akron9a5b1e12016-12-06 18:18:23 +0100175 resultButton.classList.add('result', 'button');
176
177 leftButton = result.appendChild(document.createElement('div'));
178 leftButton.classList.add('result', 'button', 'left');
Akron179c8ac2015-06-30 19:30:50 +0200179 };
180
181 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200182 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200183
Akron7716f012015-07-01 20:38:32 +0200184 if (resultButton !== null) {
Akron19d97fe2016-09-06 20:47:05 +0200185 var kq;
186 var toggle = document.createElement('a');
187 toggle.setAttribute('title', loc.SHOW_KQ)
188 toggle.classList.add('show-kq', 'button');
189 toggle.appendChild(document.createElement('span'))
190 .appendChild(document.createTextNode(loc.SHOW_KQ));
191 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200192
Akron19d97fe2016-09-06 20:47:05 +0200193 var showKQ = function () {
194 if (kq === undefined) {
195 kq = document.createElement('div');
196 kq.setAttribute('id', 'koralquery');
197 kq.style.display = 'none';
198 var kqInner = document.createElement('div');
199 kq.appendChild(kqInner);
200 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
201 hljs.highlightBlock(kqInner);
202 var sb = document.getElementById('search');
203 sb.insertBefore(kq, sb.firstChild);
204 };
Akron7716f012015-07-01 20:38:32 +0200205
Akron19d97fe2016-09-06 20:47:05 +0200206 if (kq.style.display === 'none') {
207 kq.style.display = 'block';
208 show['kq'] = true;
209 }
210 else {
211 kq.style.display = 'none';
212 delete show['kq'];
213 };
214 };
Akron7716f012015-07-01 20:38:32 +0200215
Akron19d97fe2016-09-06 20:47:05 +0200216 if (toggle !== undefined) {
217
218 // Show koralquery
219 toggle.addEventListener('click', showKQ);
220 };
Akron179c8ac2015-06-30 19:30:50 +0200221 };
Akron7716f012015-07-01 20:38:32 +0200222
Akron00cd4d12016-05-31 21:01:11 +0200223 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200224 var errors = KorAP.koralQuery["errors"];
225 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200226
Akron19d97fe2016-09-06 20:47:05 +0200227 // Malformed query
228 if (errors[i][0] === 302 && errors[i][2]) {
229 obj.hint = hintClass.create();
230 obj.hint.alert(errors[i][2], errors[i][1]);
231 break;
232 }
Akronf0c31ed2016-06-11 11:27:01 +0200233
Akron19d97fe2016-09-06 20:47:05 +0200234 // no query
235 else if (errors[i][0] === 301) {
236 obj.hint = hintClass.create();
237 obj.hint.alert(0, errors[i][1]);
238 }
239 }
Akron00cd4d12016-05-31 21:01:11 +0200240 };
241
Akron7716f012015-07-01 20:38:32 +0200242 // Session has KQ visibility stored
243 if (show["kq"])
Akron19d97fe2016-09-06 20:47:05 +0200244 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200245 };
246
Akron9a5b1e12016-12-06 18:18:23 +0100247 // There is more than 0 matches and there is a resultButton
248 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000249
Akron9a5b1e12016-12-06 18:18:23 +0100250 if (resultButton !== null) {
251
252 /**
253 * Toggle the alignment (left <=> right)
254 */
255 // querySelector('div.button.right');
256
257 var toggle = document.createElement('a');
258 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
259 // Todo: Reuse old alignment from query
260 var cl = toggle.classList;
261 cl.add('align', 'right', 'button');
262 toggle.addEventListener(
263 'click',
264 function (e) {
265 var ol = document.querySelector('#search > ol');
266 ol.toggleClass("align-left", "align-right");
267 this.toggleClass("left", "right");
268 });
269 toggle.appendChild(document.createElement('span'))
270 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
271 resultButton.appendChild(toggle);
272 };
273
274 /*
275 // Not ready yet
276 if (leftButton !== null) {
277 var metaInfo = document.createElement('a');
278 metaInfo.setAttribute('title', loc.SHOW_META)
279
280 var cl = metaInfo.classList;
281 cl.add('show-meta', 'button');
282 metaInfo.appendChild(document.createElement('span'))
283 .appendChild(document.createTextNode(loc.SHOW_META));
284 leftButton.appendChild(metaInfo);
285 };
286 */
Nils Diewalda297f062015-04-02 00:23:46 +0000287 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000288
Nils Diewald7148c6f2015-05-04 15:07:53 +0000289 /**
290 * Toggle the Virtual Collection builder
291 */
292 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000293 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200294 var vcclick = function () {
Akron19d97fe2016-09-06 20:47:05 +0200295 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000296
Akron19d97fe2016-09-06 20:47:05 +0200297 // The vc is visible
298 if (vcname.classList.contains('active')) {
299 view.removeChild(vc.element());
300 vcname.classList.remove('active');
301 delete show['collection'];
302 }
Nils Diewald6283d692015-04-23 20:32:53 +0000303
Akron19d97fe2016-09-06 20:47:05 +0200304 // The vc is not visible
305 else {
306 if (vc === undefined)
307 vc = _getCurrentVC(vcClass, vcArray);
308 view.appendChild(vc.element());
309 vcname.classList.add('active');
310 show['collection'] = true;
311 };
Nils Diewald58141332015-04-07 16:18:45 +0000312 };
Akron179c8ac2015-06-30 19:30:50 +0200313 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200314 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200315 vcclick.apply();
Akron179c8ac2015-06-30 19:30:50 +0200316 };
Nils Diewald58141332015-04-07 16:18:45 +0000317 };
318
Akron19d97fe2016-09-06 20:47:05 +0200319
Nils Diewald58141332015-04-07 16:18:45 +0000320 /**
321 * Init Tutorial view
322 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000323 if (document.getElementById('view-tutorial')) {
324 window.tutorial = tutClass.create(
Akron19d97fe2016-09-06 20:47:05 +0200325 document.getElementById('view-tutorial'),
326 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000327 );
328 obj.tutorial = window.tutorial;
329 }
Nils Diewald58141332015-04-07 16:18:45 +0000330
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000331 // Tutorial is in parent
332 else if (window.parent) {
333 obj.tutorial = window.parent.tutorial;
334 };
335
336 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200337 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000338 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000339
Akron6ed13992016-05-23 18:06:05 +0200340 // Initialize documentation links
341 obj.tutorial.initDocLinks(document);
342 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000343
Nils Diewald845282c2015-05-14 07:53:03 +0000344
Nils Diewald58141332015-04-07 16:18:45 +0000345 /**
Akronc1457bf2015-06-11 19:24:00 +0200346 * Add VC creation on submission.
347 */
348 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200349 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200350 form.addEventListener('submit', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200351 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200352
Akron19d97fe2016-09-06 20:47:05 +0200353 // No query was defined
354 if (qf.value === undefined || qf.value === '') {
355 qf.focus();
356 e.halt();
357 KorAP.log(700, "No query given");
358 return;
359 };
Akron7716f012015-07-01 20:38:32 +0200360
Akron19d97fe2016-09-06 20:47:05 +0200361 // Store session information
362 session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200363
Akron19d97fe2016-09-06 20:47:05 +0200364 // Set Virtual collection
365 if (vc === undefined) {
366 vc = _getCurrentVC(vcClass, vcArray);
367 };
Akron7716f012015-07-01 20:38:32 +0200368
Akron19d97fe2016-09-06 20:47:05 +0200369 if (vc !== undefined) {
370 input.value = vc.toQuery();
371 }
372 else {
373 delete input['value'];
374 };
Akronc1457bf2015-06-11 19:24:00 +0200375 });
376 };
377
378 /**
Nils Diewald58141332015-04-07 16:18:45 +0000379 * Init hint helper
380 * has to be final because of
381 * reposition
382 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000383 // Todo: Pass an element, so this works with
384 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200385 if (obj.hint === undefined)
386 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000387
Nils Diewald58141332015-04-07 16:18:45 +0000388 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000389 });
390});
Akronc1457bf2015-06-11 19:24:00 +0200391
392// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200393function _getCurrentVC (vcClass, vcArray) {
394 var vc = vcClass.create(vcArray);
395 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
396 vc.fromJson(KorAP.koralQuery["collection"]);
397 };
Akronc1457bf2015-06-11 19:24:00 +0200398 return vc;
399};