blob: 90592ec127204fab22627d35b45991f37c1701f2 [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,
30 hintClass,
31 vcClass,
32 tutClass,
Nils Diewald7148c6f2015-05-04 15:07:53 +000033 domReady,
Nils Diewald4347ee92015-05-04 20:32:48 +000034 hintArray,
Akron27ae9ec2015-06-23 00:43:21 +020035 vcArray,
Akron7716f012015-07-01 20:38:32 +020036 alertifyClass,
Akronbe105b92016-01-04 14:13:20 +010037 sessionClass,
Akron6bb71582016-06-10 20:41:08 +020038 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';
Akron9cc3eaf2015-06-10 22:15:52 +020050
Nils Diewalda0defc42015-05-07 23:54:17 +000051 // Override KorAP.log
52 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020053 KorAP.log = function (code, msg) {
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 + ': ') +
Nils Diewalda0defc42015-05-07 23:54:17 +000058 msg,
59 '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
Akron7716f012015-07-01 20:38:32 +020067 var session = sessionClass.create('KalamarJS');
68
69 // What should be visible?
70 var show = session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000071
72 /**
Akronf55504a2015-06-18 16:42:55 +020073 * Release notifications
74 */
75 if (KorAP.Notifications !== undefined) {
76 var n = KorAP.Notifications;
77 for (var i = 0; i < n.length; i++) {
78 alertifyClass.log(n[i][1], n[i][0], 10000);
79 };
80 };
81
82 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000083 * Replace Virtual Collection field
84 */
85 var vcname;
Akronc1457bf2015-06-11 19:24:00 +020086 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000087 if (input) {
88 input.style.display = 'none';
89 vcname = document.createElement('span');
90 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020091 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020092
Akron27ae9ec2015-06-23 00:43:21 +020093 var currentVC = loc.VC_allCorpora;
94 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
95 currentVC = loc.VC_oneCollection;
96 };
97
Nils Diewald7148c6f2015-05-04 15:07:53 +000098 vcname.appendChild(
99 document.createTextNode(
Akron179c8ac2015-06-30 19:30:50 +0200100 document.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +0000101 )
102 );
Akron27ae9ec2015-06-23 00:43:21 +0200103
Nils Diewald7148c6f2015-05-04 15:07:53 +0000104 input.parentNode.insertBefore(vcname, input);
105 };
106
Nils Diewald7148c6f2015-05-04 15:07:53 +0000107 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000108 * Add actions to match entries
109 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000110 var inactiveLi = document.querySelectorAll(
111 '#search > ol > li:not(.active)'
112 );
Nils Diewalda297f062015-04-02 00:23:46 +0000113 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200114
Nils Diewalda297f062015-04-02 00:23:46 +0000115 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000116 inactiveLi[i].addEventListener('click', function (e) {
117 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +0000118 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000119 else {
Akron7524be12016-06-01 17:31:33 +0200120 // lazyLoad
Nils Diewald0e6992a2015-04-14 20:13:52 +0000121 matchClass.create(this).open();
122 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000123 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000124 });
Akron6a535d42015-08-26 20:16:58 +0200125 inactiveLi[i].addEventListener('keydown', function (e) {
126 var code = _codeFromEvent(e);
127
128 switch (code) {
129 case 32:
130 if (this._match !== undefined)
131 this._match.toggle();
132 else {
Akron7524be12016-06-01 17:31:33 +0200133 // lazyLoad
Akron6a535d42015-08-26 20:16:58 +0200134 matchClass.create(this).open();
135 };
136 e.halt();
137 break;
138 };
139 });
Nils Diewalda297f062015-04-02 00:23:46 +0000140 };
141
Akron6bb71582016-06-10 20:41:08 +0200142 // Replace QL select menus with KorAP menus
Akronaba7a5a2016-08-15 21:58:33 +0200143 var qlField = document.getElementById('ql-field');
144 if (qlField !== null) {
145 selectMenuClass.create(
146 document.getElementById('ql-field').parentNode
147 ).limit(5);
148 };
Akron6bb71582016-06-10 20:41:08 +0200149
Akron179c8ac2015-06-30 19:30:50 +0200150 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200151 var resultButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200152 if (result != null) {
153 resultButton = result.appendChild(document.createElement('div'));
154 resultButton.classList.add('result', 'button');
155 };
156
157 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200158 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200159
Akron7716f012015-07-01 20:38:32 +0200160 if (resultButton !== null) {
161 var kq;
162 var toggle = document.createElement('a');
163 toggle.setAttribute('title', loc.SHOW_KQ)
164 toggle.classList.add('show-kq', 'button');
165 toggle.appendChild(document.createElement('span'))
166 .appendChild(document.createTextNode(loc.SHOW_KQ));
167 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200168
Akron7716f012015-07-01 20:38:32 +0200169 var showKQ = function () {
170 if (kq === undefined) {
171 kq = document.createElement('div');
172 kq.setAttribute('id', 'koralquery');
173 kq.style.display = 'none';
174 var kqInner = document.createElement('div');
175 kq.appendChild(kqInner);
176 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
177 hljs.highlightBlock(kqInner);
178 var sb = document.getElementById('search');
179 sb.insertBefore(kq, sb.firstChild);
180 };
181
182 if (kq.style.display === 'none') {
183 kq.style.display = 'block';
184 show['kq'] = true;
Akron179c8ac2015-06-30 19:30:50 +0200185 }
Akron7716f012015-07-01 20:38:32 +0200186 else {
187 kq.style.display = 'none';
188 delete show['kq'];
189 };
190 };
191
192 if (toggle !== undefined) {
193
194 // Show koralquery
195 toggle.addEventListener('click', showKQ);
196 };
Akron179c8ac2015-06-30 19:30:50 +0200197 };
Akron7716f012015-07-01 20:38:32 +0200198
Akron00cd4d12016-05-31 21:01:11 +0200199 if (KorAP.koralQuery["errors"]) {
200 var errors = KorAP.koralQuery["errors"];
201 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200202
203 // Malformed query
204 if (errors[i][0] === 302 && errors[i][2]) {
Akron00cd4d12016-05-31 21:01:11 +0200205 obj.hint = hintClass.create();
206 obj.hint.alert(errors[i][2], errors[i][1]);
207 break;
208 }
Akronf0c31ed2016-06-11 11:27:01 +0200209
210 // no query
211 else if (errors[i][0] === 301) {
212 obj.hint = hintClass.create();
213 obj.hint.alert(0, errors[i][1]);
214 }
Akron00cd4d12016-05-31 21:01:11 +0200215 }
216 };
217
Akron7716f012015-07-01 20:38:32 +0200218 // Session has KQ visibility stored
219 if (show["kq"])
220 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200221 };
222
Nils Diewald7148c6f2015-05-04 15:07:53 +0000223
Nils Diewalda297f062015-04-02 00:23:46 +0000224 /**
225 * Toggle the alignment (left <=> right)
226 */
Akron179c8ac2015-06-30 19:30:50 +0200227 // querySelector('div.button.right');
228 if (i > 0 && resultButton !== null) {
229 var toggle = document.createElement('a');
230 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
231 // Todo: Reuse old alignment from query
232 var cl = toggle.classList;
233 cl.add('align', 'right', 'button');
234 toggle.addEventListener(
235 'click',
236 function (e) {
237 var ol = document.querySelector('#search > ol');
238 ol.toggleClass("align-left", "align-right");
239 this.toggleClass("left", "right");
240 });
241 toggle.appendChild(document.createElement('span'))
242 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
243 resultButton.appendChild(toggle);
Nils Diewalda297f062015-04-02 00:23:46 +0000244 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000245
Nils Diewald6283d692015-04-23 20:32:53 +0000246
Nils Diewald7148c6f2015-05-04 15:07:53 +0000247 /**
248 * Toggle the Virtual Collection builder
249 */
250 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000251 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200252 var vcclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000253 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000254
255 // The vc is visible
Akron7716f012015-07-01 20:38:32 +0200256 if (vcname.classList.contains('active')) {
Nils Diewald6283d692015-04-23 20:32:53 +0000257 view.removeChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200258 vcname.classList.remove('active');
259 delete show['collection'];
Nils Diewald6283d692015-04-23 20:32:53 +0000260 }
261
262 // The vc is not visible
263 else {
Akronc1457bf2015-06-11 19:24:00 +0200264 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200265 vc = _getCurrentVC(vcClass, vcArray);
Nils Diewald6283d692015-04-23 20:32:53 +0000266 view.appendChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200267 vcname.classList.add('active');
268 show['collection'] = true;
Nils Diewald6283d692015-04-23 20:32:53 +0000269 };
Nils Diewald58141332015-04-07 16:18:45 +0000270 };
Akron179c8ac2015-06-30 19:30:50 +0200271 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200272 if (show['collection']) {
Akron179c8ac2015-06-30 19:30:50 +0200273 vcclick.apply();
274 };
Nils Diewald58141332015-04-07 16:18:45 +0000275 };
276
Nils Diewald0e6992a2015-04-14 20:13:52 +0000277
Nils Diewald58141332015-04-07 16:18:45 +0000278 /**
279 * Init Tutorial view
280 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000281 if (document.getElementById('view-tutorial')) {
282 window.tutorial = tutClass.create(
Akron7716f012015-07-01 20:38:32 +0200283 document.getElementById('view-tutorial'),
284 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000285 );
286 obj.tutorial = window.tutorial;
287 }
Nils Diewald58141332015-04-07 16:18:45 +0000288
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000289 // Tutorial is in parent
290 else if (window.parent) {
291 obj.tutorial = window.parent.tutorial;
292 };
293
294 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200295 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000296 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000297
Akron6ed13992016-05-23 18:06:05 +0200298 // Initialize documentation links
299 obj.tutorial.initDocLinks(document);
300 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000301
Nils Diewald845282c2015-05-14 07:53:03 +0000302
Nils Diewald58141332015-04-07 16:18:45 +0000303 /**
Akronc1457bf2015-06-11 19:24:00 +0200304 * Add VC creation on submission.
305 */
306 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200307 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200308 form.addEventListener('submit', function (e) {
Akron7716f012015-07-01 20:38:32 +0200309 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200310
Akron7716f012015-07-01 20:38:32 +0200311 // No query was defined
312 if (qf.value === undefined || qf.value === '') {
313 qf.focus();
314 e.halt();
315 KorAP.log(700, "No query given");
316 return;
317 };
318
319 // Store session information
320 session.set("show", show);
321
322 // Set Virtual collection
323 if (vc === undefined) {
324 vc = _getCurrentVC(vcClass, vcArray);
325 };
326
327 if (vc !== undefined) {
Akronc1457bf2015-06-11 19:24:00 +0200328 input.value = vc.toQuery();
Akron7716f012015-07-01 20:38:32 +0200329 }
330 else {
331 delete input['value'];
332 };
Akronc1457bf2015-06-11 19:24:00 +0200333 });
334 };
335
336 /**
Nils Diewald58141332015-04-07 16:18:45 +0000337 * Init hint helper
338 * has to be final because of
339 * reposition
340 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000341 // Todo: Pass an element, so this works with
342 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200343 if (obj.hint === undefined)
344 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000345
Nils Diewald58141332015-04-07 16:18:45 +0000346 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000347 });
348});
Akronc1457bf2015-06-11 19:24:00 +0200349
350// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200351function _getCurrentVC (vcClass, vcArray) {
352 var vc = vcClass.create(vcArray);
353 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
354 vc.fromJson(KorAP.koralQuery["collection"]);
355 };
Akronc1457bf2015-06-11 19:24:00 +0200356 return vc;
357};