blob: 8fcc162bae2c957567448cd67b34b50407e38900 [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 = {};
Akron7716f012015-07-01 20:38:32 +020066 var session = sessionClass.create('KalamarJS');
67
68 // What should be visible?
69 var show = session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000070
71 /**
Akronf55504a2015-06-18 16:42:55 +020072 * Release notifications
73 */
74 if (KorAP.Notifications !== undefined) {
75 var n = KorAP.Notifications;
76 for (var i = 0; i < n.length; i++) {
77 alertifyClass.log(n[i][1], n[i][0], 10000);
78 };
79 };
80
81 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000082 * Replace Virtual Collection field
83 */
84 var vcname;
Akronc1457bf2015-06-11 19:24:00 +020085 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000086 if (input) {
87 input.style.display = 'none';
88 vcname = document.createElement('span');
89 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020090 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020091
Akron27ae9ec2015-06-23 00:43:21 +020092 var currentVC = loc.VC_allCorpora;
93 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
94 currentVC = loc.VC_oneCollection;
95 };
96
Nils Diewald7148c6f2015-05-04 15:07:53 +000097 vcname.appendChild(
98 document.createTextNode(
Akron179c8ac2015-06-30 19:30:50 +020099 document.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +0000100 )
101 );
Akron27ae9ec2015-06-23 00:43:21 +0200102
Nils Diewald7148c6f2015-05-04 15:07:53 +0000103 input.parentNode.insertBefore(vcname, input);
104 };
105
Nils Diewald7148c6f2015-05-04 15:07:53 +0000106 /**
Nils Diewalda297f062015-04-02 00:23:46 +0000107 * Add actions to match entries
108 */
Nils Diewald5c5a7472015-04-02 22:13:38 +0000109 var inactiveLi = document.querySelectorAll(
110 '#search > ol > li:not(.active)'
111 );
Nils Diewalda297f062015-04-02 00:23:46 +0000112 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200113
Nils Diewalda297f062015-04-02 00:23:46 +0000114 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000115 inactiveLi[i].addEventListener('click', function (e) {
116 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +0000117 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000118 else {
Akron7524be12016-06-01 17:31:33 +0200119 // lazyLoad
Nils Diewald0e6992a2015-04-14 20:13:52 +0000120 matchClass.create(this).open();
121 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000122 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000123 });
Akron6a535d42015-08-26 20:16:58 +0200124 inactiveLi[i].addEventListener('keydown', function (e) {
125 var code = _codeFromEvent(e);
126
127 switch (code) {
128 case 32:
129 if (this._match !== undefined)
130 this._match.toggle();
131 else {
Akron7524be12016-06-01 17:31:33 +0200132 // lazyLoad
Akron6a535d42015-08-26 20:16:58 +0200133 matchClass.create(this).open();
134 };
135 e.halt();
136 break;
137 };
138 });
Nils Diewalda297f062015-04-02 00:23:46 +0000139 };
140
Akron6bb71582016-06-10 20:41:08 +0200141 // Replace QL select menus with KorAP menus
142 selectMenuClass.create(
143 document.getElementById('ql-field').parentNode
144 ).limit(5);
145
Akron179c8ac2015-06-30 19:30:50 +0200146 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200147 var resultButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200148 if (result != null) {
149 resultButton = result.appendChild(document.createElement('div'));
150 resultButton.classList.add('result', 'button');
151 };
152
153 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200154 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200155
Akron7716f012015-07-01 20:38:32 +0200156 if (resultButton !== null) {
157 var kq;
158 var toggle = document.createElement('a');
159 toggle.setAttribute('title', loc.SHOW_KQ)
160 toggle.classList.add('show-kq', 'button');
161 toggle.appendChild(document.createElement('span'))
162 .appendChild(document.createTextNode(loc.SHOW_KQ));
163 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200164
Akron7716f012015-07-01 20:38:32 +0200165 var showKQ = function () {
166 if (kq === undefined) {
167 kq = document.createElement('div');
168 kq.setAttribute('id', 'koralquery');
169 kq.style.display = 'none';
170 var kqInner = document.createElement('div');
171 kq.appendChild(kqInner);
172 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
173 hljs.highlightBlock(kqInner);
174 var sb = document.getElementById('search');
175 sb.insertBefore(kq, sb.firstChild);
176 };
177
178 if (kq.style.display === 'none') {
179 kq.style.display = 'block';
180 show['kq'] = true;
Akron179c8ac2015-06-30 19:30:50 +0200181 }
Akron7716f012015-07-01 20:38:32 +0200182 else {
183 kq.style.display = 'none';
184 delete show['kq'];
185 };
186 };
187
188 if (toggle !== undefined) {
189
190 // Show koralquery
191 toggle.addEventListener('click', showKQ);
192 };
Akron179c8ac2015-06-30 19:30:50 +0200193 };
Akron7716f012015-07-01 20:38:32 +0200194
Akron00cd4d12016-05-31 21:01:11 +0200195 if (KorAP.koralQuery["errors"]) {
196 var errors = KorAP.koralQuery["errors"];
197 for (var i in errors) {
198 if (errors[i][0] === 302) {
199 obj.hint = hintClass.create();
200 obj.hint.alert(errors[i][2], errors[i][1]);
201 break;
202 }
203 }
204 };
205
Akron7716f012015-07-01 20:38:32 +0200206 // Session has KQ visibility stored
207 if (show["kq"])
208 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200209 };
210
Nils Diewald7148c6f2015-05-04 15:07:53 +0000211
Nils Diewalda297f062015-04-02 00:23:46 +0000212 /**
213 * Toggle the alignment (left <=> right)
214 */
Akron179c8ac2015-06-30 19:30:50 +0200215 // querySelector('div.button.right');
216 if (i > 0 && resultButton !== null) {
217 var toggle = document.createElement('a');
218 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
219 // Todo: Reuse old alignment from query
220 var cl = toggle.classList;
221 cl.add('align', 'right', 'button');
222 toggle.addEventListener(
223 'click',
224 function (e) {
225 var ol = document.querySelector('#search > ol');
226 ol.toggleClass("align-left", "align-right");
227 this.toggleClass("left", "right");
228 });
229 toggle.appendChild(document.createElement('span'))
230 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
231 resultButton.appendChild(toggle);
Nils Diewalda297f062015-04-02 00:23:46 +0000232 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000233
Nils Diewald6283d692015-04-23 20:32:53 +0000234
Nils Diewald7148c6f2015-05-04 15:07:53 +0000235 /**
236 * Toggle the Virtual Collection builder
237 */
238 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000239 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200240 var vcclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000241 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000242
243 // The vc is visible
Akron7716f012015-07-01 20:38:32 +0200244 if (vcname.classList.contains('active')) {
Nils Diewald6283d692015-04-23 20:32:53 +0000245 view.removeChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200246 vcname.classList.remove('active');
247 delete show['collection'];
Nils Diewald6283d692015-04-23 20:32:53 +0000248 }
249
250 // The vc is not visible
251 else {
Akronc1457bf2015-06-11 19:24:00 +0200252 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200253 vc = _getCurrentVC(vcClass, vcArray);
Nils Diewald6283d692015-04-23 20:32:53 +0000254 view.appendChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200255 vcname.classList.add('active');
256 show['collection'] = true;
Nils Diewald6283d692015-04-23 20:32:53 +0000257 };
Nils Diewald58141332015-04-07 16:18:45 +0000258 };
Akron179c8ac2015-06-30 19:30:50 +0200259 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200260 if (show['collection']) {
Akron179c8ac2015-06-30 19:30:50 +0200261 vcclick.apply();
262 };
Nils Diewald58141332015-04-07 16:18:45 +0000263 };
264
Nils Diewald0e6992a2015-04-14 20:13:52 +0000265
Nils Diewald58141332015-04-07 16:18:45 +0000266 /**
267 * Init Tutorial view
268 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000269 if (document.getElementById('view-tutorial')) {
270 window.tutorial = tutClass.create(
Akron7716f012015-07-01 20:38:32 +0200271 document.getElementById('view-tutorial'),
272 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000273 );
274 obj.tutorial = window.tutorial;
275 }
Nils Diewald58141332015-04-07 16:18:45 +0000276
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000277 // Tutorial is in parent
278 else if (window.parent) {
279 obj.tutorial = window.parent.tutorial;
280 };
281
282 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200283 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000284 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000285
Akron6ed13992016-05-23 18:06:05 +0200286 // Initialize documentation links
287 obj.tutorial.initDocLinks(document);
288 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000289
Nils Diewald845282c2015-05-14 07:53:03 +0000290
Nils Diewald58141332015-04-07 16:18:45 +0000291 /**
Akronc1457bf2015-06-11 19:24:00 +0200292 * Add VC creation on submission.
293 */
294 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200295 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200296 form.addEventListener('submit', function (e) {
Akron7716f012015-07-01 20:38:32 +0200297 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200298
Akron7716f012015-07-01 20:38:32 +0200299 // No query was defined
300 if (qf.value === undefined || qf.value === '') {
301 qf.focus();
302 e.halt();
303 KorAP.log(700, "No query given");
304 return;
305 };
306
307 // Store session information
308 session.set("show", show);
309
310 // Set Virtual collection
311 if (vc === undefined) {
312 vc = _getCurrentVC(vcClass, vcArray);
313 };
314
315 if (vc !== undefined) {
Akronc1457bf2015-06-11 19:24:00 +0200316 input.value = vc.toQuery();
Akron7716f012015-07-01 20:38:32 +0200317 }
318 else {
319 delete input['value'];
320 };
Akronc1457bf2015-06-11 19:24:00 +0200321 });
322 };
323
324 /**
Nils Diewald58141332015-04-07 16:18:45 +0000325 * Init hint helper
326 * has to be final because of
327 * reposition
328 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000329 // Todo: Pass an element, so this works with
330 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200331 if (obj.hint === undefined)
332 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000333
Nils Diewald58141332015-04-07 16:18:45 +0000334 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000335 });
336});
Akronc1457bf2015-06-11 19:24:00 +0200337
338// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200339function _getCurrentVC (vcClass, vcArray) {
340 var vc = vcClass.create(vcArray);
341 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
342 vc.fromJson(KorAP.koralQuery["collection"]);
343 };
Akronc1457bf2015-06-11 19:24:00 +0200344 return vc;
345};