blob: 294a3c97ac48c212751ed7840d0fd5f129a17bf1 [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
Akron6bb71582016-06-10 20:41:08 +0200144 // Replace QL select menus with KorAP menus
Akronaba7a5a2016-08-15 21:58:33 +0200145 var qlField = document.getElementById('ql-field');
146 if (qlField !== null) {
147 selectMenuClass.create(
148 document.getElementById('ql-field').parentNode
149 ).limit(5);
150 };
Akron6bb71582016-06-10 20:41:08 +0200151
Akron179c8ac2015-06-30 19:30:50 +0200152 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200153 var resultButton = null;
Akron9a5b1e12016-12-06 18:18:23 +0100154 var leftButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200155 if (result != null) {
Akron9a5b1e12016-12-06 18:18:23 +0100156
157 // Add right buttons
Akron179c8ac2015-06-30 19:30:50 +0200158 resultButton = result.appendChild(document.createElement('div'));
Akron9a5b1e12016-12-06 18:18:23 +0100159 resultButton.classList.add('result', 'button');
160
161 leftButton = result.appendChild(document.createElement('div'));
162 leftButton.classList.add('result', 'button', 'left');
Akron179c8ac2015-06-30 19:30:50 +0200163 };
164
165 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200166 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200167
Akron7716f012015-07-01 20:38:32 +0200168 if (resultButton !== null) {
Akron19d97fe2016-09-06 20:47:05 +0200169 var kq;
170 var toggle = document.createElement('a');
171 toggle.setAttribute('title', loc.SHOW_KQ)
172 toggle.classList.add('show-kq', 'button');
173 toggle.appendChild(document.createElement('span'))
174 .appendChild(document.createTextNode(loc.SHOW_KQ));
175 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200176
Akron19d97fe2016-09-06 20:47:05 +0200177 var showKQ = function () {
178 if (kq === undefined) {
179 kq = document.createElement('div');
180 kq.setAttribute('id', 'koralquery');
181 kq.style.display = 'none';
182 var kqInner = document.createElement('div');
183 kq.appendChild(kqInner);
184 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
185 hljs.highlightBlock(kqInner);
186 var sb = document.getElementById('search');
187 sb.insertBefore(kq, sb.firstChild);
188 };
Akron7716f012015-07-01 20:38:32 +0200189
Akron19d97fe2016-09-06 20:47:05 +0200190 if (kq.style.display === 'none') {
191 kq.style.display = 'block';
192 show['kq'] = true;
193 }
194 else {
195 kq.style.display = 'none';
196 delete show['kq'];
197 };
198 };
Akron7716f012015-07-01 20:38:32 +0200199
Akron19d97fe2016-09-06 20:47:05 +0200200 if (toggle !== undefined) {
201
202 // Show koralquery
203 toggle.addEventListener('click', showKQ);
204 };
Akron179c8ac2015-06-30 19:30:50 +0200205 };
Akron7716f012015-07-01 20:38:32 +0200206
Akron00cd4d12016-05-31 21:01:11 +0200207 if (KorAP.koralQuery["errors"]) {
Akron19d97fe2016-09-06 20:47:05 +0200208 var errors = KorAP.koralQuery["errors"];
209 for (var i in errors) {
Akronf0c31ed2016-06-11 11:27:01 +0200210
Akron19d97fe2016-09-06 20:47:05 +0200211 // Malformed query
212 if (errors[i][0] === 302 && errors[i][2]) {
213 obj.hint = hintClass.create();
214 obj.hint.alert(errors[i][2], errors[i][1]);
215 break;
216 }
Akronf0c31ed2016-06-11 11:27:01 +0200217
Akron19d97fe2016-09-06 20:47:05 +0200218 // no query
219 else if (errors[i][0] === 301) {
220 obj.hint = hintClass.create();
221 obj.hint.alert(0, errors[i][1]);
222 }
223 }
Akron00cd4d12016-05-31 21:01:11 +0200224 };
225
Akron7716f012015-07-01 20:38:32 +0200226 // Session has KQ visibility stored
227 if (show["kq"])
Akron19d97fe2016-09-06 20:47:05 +0200228 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200229 };
230
Akron9a5b1e12016-12-06 18:18:23 +0100231 // There is more than 0 matches and there is a resultButton
232 if (i > 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000233
Akron9a5b1e12016-12-06 18:18:23 +0100234 if (resultButton !== null) {
235
236 /**
237 * Toggle the alignment (left <=> right)
238 */
239 // querySelector('div.button.right');
240
241 var toggle = document.createElement('a');
242 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
243 // Todo: Reuse old alignment from query
244 var cl = toggle.classList;
245 cl.add('align', 'right', 'button');
246 toggle.addEventListener(
247 'click',
248 function (e) {
249 var ol = document.querySelector('#search > ol');
250 ol.toggleClass("align-left", "align-right");
251 this.toggleClass("left", "right");
252 });
253 toggle.appendChild(document.createElement('span'))
254 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
255 resultButton.appendChild(toggle);
256 };
257
258 /*
259 // Not ready yet
260 if (leftButton !== null) {
261 var metaInfo = document.createElement('a');
262 metaInfo.setAttribute('title', loc.SHOW_META)
263
264 var cl = metaInfo.classList;
265 cl.add('show-meta', 'button');
266 metaInfo.appendChild(document.createElement('span'))
267 .appendChild(document.createTextNode(loc.SHOW_META));
268 leftButton.appendChild(metaInfo);
269 };
270 */
Nils Diewalda297f062015-04-02 00:23:46 +0000271 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000272
Nils Diewald7148c6f2015-05-04 15:07:53 +0000273 /**
274 * Toggle the Virtual Collection builder
275 */
276 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000277 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200278 var vcclick = function () {
Akron19d97fe2016-09-06 20:47:05 +0200279 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000280
Akron19d97fe2016-09-06 20:47:05 +0200281 // The vc is visible
282 if (vcname.classList.contains('active')) {
283 view.removeChild(vc.element());
284 vcname.classList.remove('active');
285 delete show['collection'];
286 }
Nils Diewald6283d692015-04-23 20:32:53 +0000287
Akron19d97fe2016-09-06 20:47:05 +0200288 // The vc is not visible
289 else {
290 if (vc === undefined)
291 vc = _getCurrentVC(vcClass, vcArray);
292 view.appendChild(vc.element());
293 vcname.classList.add('active');
294 show['collection'] = true;
295 };
Nils Diewald58141332015-04-07 16:18:45 +0000296 };
Akron179c8ac2015-06-30 19:30:50 +0200297 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200298 if (show['collection']) {
Akron19d97fe2016-09-06 20:47:05 +0200299 vcclick.apply();
Akron179c8ac2015-06-30 19:30:50 +0200300 };
Nils Diewald58141332015-04-07 16:18:45 +0000301 };
302
Akron19d97fe2016-09-06 20:47:05 +0200303
Nils Diewald58141332015-04-07 16:18:45 +0000304 /**
305 * Init Tutorial view
306 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000307 if (document.getElementById('view-tutorial')) {
308 window.tutorial = tutClass.create(
Akron19d97fe2016-09-06 20:47:05 +0200309 document.getElementById('view-tutorial'),
310 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000311 );
312 obj.tutorial = window.tutorial;
313 }
Nils Diewald58141332015-04-07 16:18:45 +0000314
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000315 // Tutorial is in parent
316 else if (window.parent) {
317 obj.tutorial = window.parent.tutorial;
318 };
319
320 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200321 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000322 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000323
Akron6ed13992016-05-23 18:06:05 +0200324 // Initialize documentation links
325 obj.tutorial.initDocLinks(document);
326 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000327
Nils Diewald845282c2015-05-14 07:53:03 +0000328
Nils Diewald58141332015-04-07 16:18:45 +0000329 /**
Akronc1457bf2015-06-11 19:24:00 +0200330 * Add VC creation on submission.
331 */
332 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200333 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200334 form.addEventListener('submit', function (e) {
Akron19d97fe2016-09-06 20:47:05 +0200335 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200336
Akron19d97fe2016-09-06 20:47:05 +0200337 // No query was defined
338 if (qf.value === undefined || qf.value === '') {
339 qf.focus();
340 e.halt();
341 KorAP.log(700, "No query given");
342 return;
343 };
Akron7716f012015-07-01 20:38:32 +0200344
Akron19d97fe2016-09-06 20:47:05 +0200345 // Store session information
346 session.set("show", show);
Akron7716f012015-07-01 20:38:32 +0200347
Akron19d97fe2016-09-06 20:47:05 +0200348 // Set Virtual collection
349 if (vc === undefined) {
350 vc = _getCurrentVC(vcClass, vcArray);
351 };
Akron7716f012015-07-01 20:38:32 +0200352
Akron19d97fe2016-09-06 20:47:05 +0200353 if (vc !== undefined) {
354 input.value = vc.toQuery();
355 }
356 else {
357 delete input['value'];
358 };
Akronc1457bf2015-06-11 19:24:00 +0200359 });
360 };
361
362 /**
Nils Diewald58141332015-04-07 16:18:45 +0000363 * Init hint helper
364 * has to be final because of
365 * reposition
366 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000367 // Todo: Pass an element, so this works with
368 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200369 if (obj.hint === undefined)
370 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000371
Nils Diewald58141332015-04-07 16:18:45 +0000372 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000373 });
374});
Akronc1457bf2015-06-11 19:24:00 +0200375
376// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200377function _getCurrentVC (vcClass, vcArray) {
378 var vc = vcClass.create(vcArray);
379 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
380 vc.fromJson(KorAP.koralQuery["collection"]);
381 };
Akronc1457bf2015-06-11 19:24:00 +0200382 return vc;
383};