blob: 3b911b6ea2dc5ae106c78528ae3de9db36c2f34b [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001define([
2 'match',
3 'hint',
4 'vc',
5 'tutorial',
6 'lib/domReady',
Nils Diewald7148c6f2015-05-04 15:07:53 +00007 'hint/array',
Akron27ae9ec2015-06-23 00:43:21 +02008 'vc/array',
Nils Diewald7148c6f2015-05-04 15:07:53 +00009 'lib/alertify',
Akron7716f012015-07-01 20:38:32 +020010 'session',
Akronbe105b92016-01-04 14:13:20 +010011 'tagger',
Akron6bb71582016-06-10 20:41:08 +020012 'selectMenu',
Nils Diewald7148c6f2015-05-04 15:07:53 +000013 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000014 'mailToChiffre',
Nils Diewald845282c2015-05-14 07:53:03 +000015 'lib/highlight/highlight.pack',
Nils Diewald0e6992a2015-04-14 20:13:52 +000016 'util'
17], function (matchClass,
18 hintClass,
19 vcClass,
20 tutClass,
Nils Diewald7148c6f2015-05-04 15:07:53 +000021 domReady,
Nils Diewald4347ee92015-05-04 20:32:48 +000022 hintArray,
Akron27ae9ec2015-06-23 00:43:21 +020023 vcArray,
Akron7716f012015-07-01 20:38:32 +020024 alertifyClass,
Akronbe105b92016-01-04 14:13:20 +010025 sessionClass,
Akron6bb71582016-06-10 20:41:08 +020026 tagger,
27 selectMenuClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000028
29 // Set hint array for hint helper
30 KorAP.hintArray = hintArray;
31
Akron9cc3eaf2015-06-10 22:15:52 +020032 // Localization values
33 var loc = KorAP.Locale;
Akron7716f012015-07-01 20:38:32 +020034 loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora';
35 loc.VC_oneCollection = loc.VC_oneCollection || 'one Collection';
36 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle Alignment';
37 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akron9cc3eaf2015-06-10 22:15:52 +020038
Nils Diewalda0defc42015-05-07 23:54:17 +000039 // Override KorAP.log
40 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020041 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000042
43 // Use alertify to log errors
44 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020045 (code === 0 ? '' : code + ': ') +
Nils Diewalda0defc42015-05-07 23:54:17 +000046 msg,
47 'error',
Akronf55504a2015-06-18 16:42:55 +020048 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000049 );
50 };
51
Nils Diewald0e6992a2015-04-14 20:13:52 +000052 domReady(function (event) {
53 var obj = {};
Akron7716f012015-07-01 20:38:32 +020054 var session = sessionClass.create('KalamarJS');
55
56 // What should be visible?
57 var show = session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000058
59 /**
Akronf55504a2015-06-18 16:42:55 +020060 * Release notifications
61 */
62 if (KorAP.Notifications !== undefined) {
63 var n = KorAP.Notifications;
64 for (var i = 0; i < n.length; i++) {
65 alertifyClass.log(n[i][1], n[i][0], 10000);
66 };
67 };
68
69 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000070 * Replace Virtual Collection field
71 */
72 var vcname;
Akronc1457bf2015-06-11 19:24:00 +020073 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000074 if (input) {
75 input.style.display = 'none';
76 vcname = document.createElement('span');
77 vcname.setAttribute('id', 'vc-choose');
Akron6bb71582016-06-10 20:41:08 +020078 vcname.classList.add('select');
Akron941551e2015-06-11 16:06:22 +020079
Akron27ae9ec2015-06-23 00:43:21 +020080 var currentVC = loc.VC_allCorpora;
81 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
82 currentVC = loc.VC_oneCollection;
83 };
84
Nils Diewald7148c6f2015-05-04 15:07:53 +000085 vcname.appendChild(
86 document.createTextNode(
Akron179c8ac2015-06-30 19:30:50 +020087 document.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +000088 )
89 );
Akron27ae9ec2015-06-23 00:43:21 +020090
Nils Diewald7148c6f2015-05-04 15:07:53 +000091 input.parentNode.insertBefore(vcname, input);
92 };
93
Nils Diewald7148c6f2015-05-04 15:07:53 +000094 /**
Nils Diewalda297f062015-04-02 00:23:46 +000095 * Add actions to match entries
96 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000097 var inactiveLi = document.querySelectorAll(
98 '#search > ol > li:not(.active)'
99 );
Nils Diewalda297f062015-04-02 00:23:46 +0000100 var i = 0;
Akron6a535d42015-08-26 20:16:58 +0200101
Nils Diewalda297f062015-04-02 00:23:46 +0000102 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000103 inactiveLi[i].addEventListener('click', function (e) {
104 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +0000105 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000106 else {
107 matchClass.create(this).open();
108 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000109 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000110 });
Akron6a535d42015-08-26 20:16:58 +0200111 inactiveLi[i].addEventListener('keydown', function (e) {
112 var code = _codeFromEvent(e);
113
114 switch (code) {
115 case 32:
116 if (this._match !== undefined)
117 this._match.toggle();
118 else {
119 matchClass.create(this).open();
120 };
121 e.halt();
122 break;
123 };
124 });
Nils Diewalda297f062015-04-02 00:23:46 +0000125 };
126
Akron6bb71582016-06-10 20:41:08 +0200127 // Replace QL select menus with KorAP menus
128 selectMenuClass.create(
129 document.getElementById('ql-field').parentNode
130 ).limit(5);
131
Akron179c8ac2015-06-30 19:30:50 +0200132 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200133 var resultButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200134 if (result != null) {
135 resultButton = result.appendChild(document.createElement('div'));
136 resultButton.classList.add('result', 'button');
137 };
138
139 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200140 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200141
Akron7716f012015-07-01 20:38:32 +0200142 if (resultButton !== null) {
143 var kq;
144 var toggle = document.createElement('a');
145 toggle.setAttribute('title', loc.SHOW_KQ)
146 toggle.classList.add('show-kq', 'button');
147 toggle.appendChild(document.createElement('span'))
148 .appendChild(document.createTextNode(loc.SHOW_KQ));
149 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200150
Akron7716f012015-07-01 20:38:32 +0200151 var showKQ = function () {
152 if (kq === undefined) {
153 kq = document.createElement('div');
154 kq.setAttribute('id', 'koralquery');
155 kq.style.display = 'none';
156 var kqInner = document.createElement('div');
157 kq.appendChild(kqInner);
158 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
159 hljs.highlightBlock(kqInner);
160 var sb = document.getElementById('search');
161 sb.insertBefore(kq, sb.firstChild);
162 };
163
164 if (kq.style.display === 'none') {
165 kq.style.display = 'block';
166 show['kq'] = true;
Akron179c8ac2015-06-30 19:30:50 +0200167 }
Akron7716f012015-07-01 20:38:32 +0200168 else {
169 kq.style.display = 'none';
170 delete show['kq'];
171 };
172 };
173
174 if (toggle !== undefined) {
175
176 // Show koralquery
177 toggle.addEventListener('click', showKQ);
178 };
Akron179c8ac2015-06-30 19:30:50 +0200179 };
Akron7716f012015-07-01 20:38:32 +0200180
Akron00cd4d12016-05-31 21:01:11 +0200181 if (KorAP.koralQuery["errors"]) {
182 var errors = KorAP.koralQuery["errors"];
183 for (var i in errors) {
184 if (errors[i][0] === 302) {
185 obj.hint = hintClass.create();
186 obj.hint.alert(errors[i][2], errors[i][1]);
187 break;
188 }
189 }
190 };
191
Akron7716f012015-07-01 20:38:32 +0200192 // Session has KQ visibility stored
193 if (show["kq"])
194 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200195 };
196
Nils Diewald7148c6f2015-05-04 15:07:53 +0000197
Nils Diewalda297f062015-04-02 00:23:46 +0000198 /**
199 * Toggle the alignment (left <=> right)
200 */
Akron179c8ac2015-06-30 19:30:50 +0200201 // querySelector('div.button.right');
202 if (i > 0 && resultButton !== null) {
203 var toggle = document.createElement('a');
204 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
205 // Todo: Reuse old alignment from query
206 var cl = toggle.classList;
207 cl.add('align', 'right', 'button');
208 toggle.addEventListener(
209 'click',
210 function (e) {
211 var ol = document.querySelector('#search > ol');
212 ol.toggleClass("align-left", "align-right");
213 this.toggleClass("left", "right");
214 });
215 toggle.appendChild(document.createElement('span'))
216 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
217 resultButton.appendChild(toggle);
Nils Diewalda297f062015-04-02 00:23:46 +0000218 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000219
Nils Diewald6283d692015-04-23 20:32:53 +0000220
Nils Diewald7148c6f2015-05-04 15:07:53 +0000221 /**
222 * Toggle the Virtual Collection builder
223 */
224 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000225 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200226 var vcclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000227 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000228
229 // The vc is visible
Akron7716f012015-07-01 20:38:32 +0200230 if (vcname.classList.contains('active')) {
Nils Diewald6283d692015-04-23 20:32:53 +0000231 view.removeChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200232 vcname.classList.remove('active');
233 delete show['collection'];
Nils Diewald6283d692015-04-23 20:32:53 +0000234 }
235
236 // The vc is not visible
237 else {
Akronc1457bf2015-06-11 19:24:00 +0200238 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200239 vc = _getCurrentVC(vcClass, vcArray);
Nils Diewald6283d692015-04-23 20:32:53 +0000240 view.appendChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200241 vcname.classList.add('active');
242 show['collection'] = true;
Nils Diewald6283d692015-04-23 20:32:53 +0000243 };
Nils Diewald58141332015-04-07 16:18:45 +0000244 };
Akron179c8ac2015-06-30 19:30:50 +0200245 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200246 if (show['collection']) {
Akron179c8ac2015-06-30 19:30:50 +0200247 vcclick.apply();
248 };
Nils Diewald58141332015-04-07 16:18:45 +0000249 };
250
Nils Diewald0e6992a2015-04-14 20:13:52 +0000251
Nils Diewald58141332015-04-07 16:18:45 +0000252 /**
253 * Init Tutorial view
254 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000255 if (document.getElementById('view-tutorial')) {
256 window.tutorial = tutClass.create(
Akron7716f012015-07-01 20:38:32 +0200257 document.getElementById('view-tutorial'),
258 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000259 );
260 obj.tutorial = window.tutorial;
261 }
Nils Diewald58141332015-04-07 16:18:45 +0000262
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000263 // Tutorial is in parent
264 else if (window.parent) {
265 obj.tutorial = window.parent.tutorial;
266 };
267
268 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200269 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000270 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000271
Akron6ed13992016-05-23 18:06:05 +0200272 // Initialize documentation links
273 obj.tutorial.initDocLinks(document);
274 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000275
Nils Diewald845282c2015-05-14 07:53:03 +0000276
Nils Diewald58141332015-04-07 16:18:45 +0000277 /**
Akronc1457bf2015-06-11 19:24:00 +0200278 * Add VC creation on submission.
279 */
280 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200281 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200282 form.addEventListener('submit', function (e) {
Akron7716f012015-07-01 20:38:32 +0200283 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200284
Akron7716f012015-07-01 20:38:32 +0200285 // No query was defined
286 if (qf.value === undefined || qf.value === '') {
287 qf.focus();
288 e.halt();
289 KorAP.log(700, "No query given");
290 return;
291 };
292
293 // Store session information
294 session.set("show", show);
295
296 // Set Virtual collection
297 if (vc === undefined) {
298 vc = _getCurrentVC(vcClass, vcArray);
299 };
300
301 if (vc !== undefined) {
Akronc1457bf2015-06-11 19:24:00 +0200302 input.value = vc.toQuery();
Akron7716f012015-07-01 20:38:32 +0200303 }
304 else {
305 delete input['value'];
306 };
Akronc1457bf2015-06-11 19:24:00 +0200307 });
308 };
309
310 /**
Nils Diewald58141332015-04-07 16:18:45 +0000311 * Init hint helper
312 * has to be final because of
313 * reposition
314 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000315 // Todo: Pass an element, so this works with
316 // tutorial pages as well!
Akron00cd4d12016-05-31 21:01:11 +0200317 if (obj.hint === undefined)
318 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000319
Nils Diewald58141332015-04-07 16:18:45 +0000320 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000321 });
322});
Akronc1457bf2015-06-11 19:24:00 +0200323
324// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200325function _getCurrentVC (vcClass, vcArray) {
326 var vc = vcClass.create(vcArray);
327 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
328 vc.fromJson(KorAP.koralQuery["collection"]);
329 };
Akronc1457bf2015-06-11 19:24:00 +0200330 return vc;
331};