blob: 6b4ad67233d0fd7696c2087320c0cce79e9c3ae9 [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 };
125 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000126 });
Akron6a535d42015-08-26 20:16:58 +0200127 inactiveLi[i].addEventListener('keydown', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200128 var code = _codeFromEvent(e);
129
130 switch (code) {
131 case 32:
132 if (this._match !== undefined)
133 this._match.toggle();
134 else {
135 // lazyLoad
136 matchClass.create(this).open();
137 };
138 e.halt();
139 break;
140 };
Akron6a535d42015-08-26 20:16:58 +0200141 });
Nils Diewalda297f062015-04-02 00:23:46 +0000142 };
143
Akronb9cdb102017-04-25 00:52:31 +0200144 document.getElementsByTagName('aside')[0].addEventListener('focus', function() {
145 this.classList.add('active')
146 });
147
148
Akron6bb71582016-06-10 20:41:08 +0200149 // Replace QL select menus with KorAP menus
Akronaba7a5a2016-08-15 21:58:33 +0200150 var qlField = document.getElementById('ql-field');
151 if (qlField !== null) {
152 selectMenuClass.create(
153 document.getElementById('ql-field').parentNode
154 ).limit(5);
155 };
Akron6bb71582016-06-10 20:41:08 +0200156
Akron179c8ac2015-06-30 19:30:50 +0200157 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200158 var resultButton = null;
Akron9a5b1e12016-12-06 18:18:23 +0100159 var leftButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200160 if (result != null) {
Akron9a5b1e12016-12-06 18:18:23 +0100161
162 // Add right buttons
Akron179c8ac2015-06-30 19:30:50 +0200163 resultButton = result.appendChild(document.createElement('div'));
Akron9a5b1e12016-12-06 18:18:23 +0100164 resultButton.classList.add('result', 'button');
165
166 leftButton = result.appendChild(document.createElement('div'));
167 leftButton.classList.add('result', 'button', 'left');
Akron179c8ac2015-06-30 19:30:50 +0200168 };
169
170 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200171 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200172
Akron7716f012015-07-01 20:38:32 +0200173 if (resultButton !== null) {
Akron19d97fe2016-09-06 20:47:05 +0200174 var kq;
175 var toggle = document.createElement('a');
176 toggle.setAttribute('title', loc.SHOW_KQ)
177 toggle.classList.add('show-kq', 'button');
178 toggle.appendChild(document.createElement('span'))
179 .appendChild(document.createTextNode(loc.SHOW_KQ));
180 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200181
Akron19d97fe2016-09-06 20:47:05 +0200182 var showKQ = function () {
183 if (kq === undefined) {
184 kq = document.createElement('div');
185 kq.setAttribute('id', 'koralquery');
186 kq.style.display = 'none';
187 var kqInner = document.createElement('div');
188 kq.appendChild(kqInner);
189 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
190 hljs.highlightBlock(kqInner);
191 var sb = document.getElementById('search');
192 sb.insertBefore(kq, sb.firstChild);
193 };
Akron7716f012015-07-01 20:38:32 +0200194
Akron19d97fe2016-09-06 20:47:05 +0200195 if (kq.style.display === 'none') {
196 kq.style.display = 'block';
197 show['kq'] = true;
198 }
199 else {
200 kq.style.display = 'none';
201 delete show['kq'];
202 };
203 };
Akron7716f012015-07-01 20:38:32 +0200204
Akron19d97fe2016-09-06 20:47:05 +0200205 if (toggle !== undefined) {
206
207 // Show koralquery
208 toggle.addEventListener('click', showKQ);
209 };
Akron179c8ac2015-06-30 19:30:50 +0200210 };
Akron7716f012015-07-01 20:38:32 +0200211
Akron00cd4d12016-05-31 21:01:11 +0200212 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200213 var errors = KorAP.koralQuery["errors"];
214 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200215
Akron19d97fe2016-09-06 20:47:05 +0200216 // Malformed query
217 if (errors[i][0] === 302 && errors[i][2]) {
218 obj.hint = hintClass.create();
219 obj.hint.alert(errors[i][2], errors[i][1]);
220 break;
221 }
Akronf0c31ed2016-06-11 11:27:01 +0200222
Akron19d97fe2016-09-06 20:47:05 +0200223 // no query
224 else if (errors[i][0] === 301) {
225 obj.hint = hintClass.create();
226 obj.hint.alert(0, errors[i][1]);
227 }
228 }
Akron00cd4d12016-05-31 21:01:11 +0200229 };
230
Akron7716f012015-07-01 20:38:32 +0200231 // Session has KQ visibility stored
232 if (show["kq"])
Akron19d97fe2016-09-06 20:47:05 +0200233 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200234 };
235
Akron9a5b1e12016-12-06 18:18:23 +0100236 // There is more than 0 matches and there is a resultButton
237 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000238
Akron9a5b1e12016-12-06 18:18:23 +0100239 if (resultButton !== null) {
240
241 /**
242 * Toggle the alignment (left <=> right)
243 */
244 // querySelector('div.button.right');
245
246 var toggle = document.createElement('a');
247 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
248 // Todo: Reuse old alignment from query
249 var cl = toggle.classList;
250 cl.add('align', 'right', 'button');
251 toggle.addEventListener(
252 'click',
253 function (e) {
254 var ol = document.querySelector('#search > ol');
255 ol.toggleClass("align-left", "align-right");
256 this.toggleClass("left", "right");
257 });
258 toggle.appendChild(document.createElement('span'))
259 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
260 resultButton.appendChild(toggle);
261 };
262
263 /*
264 // Not ready yet
265 if (leftButton !== null) {
266 var metaInfo = document.createElement('a');
267 metaInfo.setAttribute('title', loc.SHOW_META)
268
269 var cl = metaInfo.classList;
270 cl.add('show-meta', 'button');
271 metaInfo.appendChild(document.createElement('span'))
272 .appendChild(document.createTextNode(loc.SHOW_META));
273 leftButton.appendChild(metaInfo);
274 };
275 */
Nils Diewalda297f062015-04-02 00:23:46 +0000276 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000277
Nils Diewald7148c6f2015-05-04 15:07:53 +0000278 /**
279 * Toggle the Virtual Collection builder
280 */
281 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000282 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200283 var vcclick = function () {
Akron19d97fe2016-09-06 20:47:05 +0200284 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000285
Akron19d97fe2016-09-06 20:47:05 +0200286 // The vc is visible
287 if (vcname.classList.contains('active')) {
288 view.removeChild(vc.element());
289 vcname.classList.remove('active');
290 delete show['collection'];
291 }
Nils Diewald6283d692015-04-23 20:32:53 +0000292
Akron19d97fe2016-09-06 20:47:05 +0200293 // The vc is not visible
294 else {
295 if (vc === undefined)
296 vc = _getCurrentVC(vcClass, vcArray);
297 view.appendChild(vc.element());
298 vcname.classList.add('active');
299 show['collection'] = true;
300 };
Nils Diewald58141332015-04-07 16:18:45 +0000301 };
Akron179c8ac2015-06-30 19:30:50 +0200302 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200303 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200304 vcclick.apply();
Akron179c8ac2015-06-30 19:30:50 +0200305 };
Nils Diewald58141332015-04-07 16:18:45 +0000306 };
307
Akron19d97fe2016-09-06 20:47:05 +0200308
Nils Diewald58141332015-04-07 16:18:45 +0000309 /**
310 * Init Tutorial view
311 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000312 if (document.getElementById('view-tutorial')) {
313 window.tutorial = tutClass.create(
Akron19d97fe2016-09-06 20:47:05 +0200314 document.getElementById('view-tutorial'),
315 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000316 );
317 obj.tutorial = window.tutorial;
318 }
Nils Diewald58141332015-04-07 16:18:45 +0000319
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000320 // Tutorial is in parent
321 else if (window.parent) {
322 obj.tutorial = window.parent.tutorial;
323 };
324
325 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200326 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000327 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000328
Akron6ed13992016-05-23 18:06:05 +0200329 // Initialize documentation links
330 obj.tutorial.initDocLinks(document);
331 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000332
Nils Diewald845282c2015-05-14 07:53:03 +0000333
Nils Diewald58141332015-04-07 16:18:45 +0000334 /**
Akronc1457bf2015-06-11 19:24:00 +0200335 * Add VC creation on submission.
336 */
337 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200338 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200339 form.addEventListener('submit', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200340 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200341
Akron19d97fe2016-09-06 20:47:05 +0200342 // No query was defined
343 if (qf.value === undefined || qf.value === '') {
344 qf.focus();
345 e.halt();
346 KorAP.log(700, "No query given");
347 return;
348 };
Akron7716f012015-07-01 20:38:32 +0200349
Akron19d97fe2016-09-06 20:47:05 +0200350 // Store session information
351 session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200352
Akron19d97fe2016-09-06 20:47:05 +0200353 // Set Virtual collection
354 if (vc === undefined) {
355 vc = _getCurrentVC(vcClass, vcArray);
356 };
Akron7716f012015-07-01 20:38:32 +0200357
Akron19d97fe2016-09-06 20:47:05 +0200358 if (vc !== undefined) {
359 input.value = vc.toQuery();
360 }
361 else {
362 delete input['value'];
363 };
Akronc1457bf2015-06-11 19:24:00 +0200364 });
365 };
366
367 /**
Nils Diewald58141332015-04-07 16:18:45 +0000368 * Init hint helper
369 * has to be final because of
370 * reposition
371 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000372 // Todo: Pass an element, so this works with
373 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200374 if (obj.hint === undefined)
375 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000376
Nils Diewald58141332015-04-07 16:18:45 +0000377 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000378 });
379});
Akronc1457bf2015-06-11 19:24:00 +0200380
381// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200382function _getCurrentVC (vcClass, vcArray) {
383 var vc = vcClass.create(vcArray);
384 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
385 vc.fromJson(KorAP.koralQuery["collection"]);
386 };
Akronc1457bf2015-06-11 19:24:00 +0200387 return vc;
388};