blob: c003a1db1f54a09ef34f192e0381d62405d297dc [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
181 // Session has KQ visibility stored
182 if (show["kq"])
183 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200184 };
185
Nils Diewald7148c6f2015-05-04 15:07:53 +0000186
Nils Diewalda297f062015-04-02 00:23:46 +0000187 /**
188 * Toggle the alignment (left <=> right)
189 */
Akron179c8ac2015-06-30 19:30:50 +0200190 // querySelector('div.button.right');
191 if (i > 0 && resultButton !== null) {
192 var toggle = document.createElement('a');
193 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
194 // Todo: Reuse old alignment from query
195 var cl = toggle.classList;
196 cl.add('align', 'right', 'button');
197 toggle.addEventListener(
198 'click',
199 function (e) {
200 var ol = document.querySelector('#search > ol');
201 ol.toggleClass("align-left", "align-right");
202 this.toggleClass("left", "right");
203 });
204 toggle.appendChild(document.createElement('span'))
205 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
206 resultButton.appendChild(toggle);
Nils Diewalda297f062015-04-02 00:23:46 +0000207 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000208
Nils Diewald6283d692015-04-23 20:32:53 +0000209
Nils Diewald7148c6f2015-05-04 15:07:53 +0000210 /**
211 * Toggle the Virtual Collection builder
212 */
213 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000214 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200215 var vcclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000216 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000217
218 // The vc is visible
Akron7716f012015-07-01 20:38:32 +0200219 if (vcname.classList.contains('active')) {
Nils Diewald6283d692015-04-23 20:32:53 +0000220 view.removeChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200221 vcname.classList.remove('active');
222 delete show['collection'];
Nils Diewald6283d692015-04-23 20:32:53 +0000223 }
224
225 // The vc is not visible
226 else {
Akronc1457bf2015-06-11 19:24:00 +0200227 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200228 vc = _getCurrentVC(vcClass, vcArray);
Nils Diewald6283d692015-04-23 20:32:53 +0000229 view.appendChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200230 vcname.classList.add('active');
231 show['collection'] = true;
Nils Diewald6283d692015-04-23 20:32:53 +0000232 };
Nils Diewald58141332015-04-07 16:18:45 +0000233 };
Akron179c8ac2015-06-30 19:30:50 +0200234 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200235 if (show['collection']) {
Akron179c8ac2015-06-30 19:30:50 +0200236 vcclick.apply();
237 };
Nils Diewald58141332015-04-07 16:18:45 +0000238 };
239
Nils Diewald0e6992a2015-04-14 20:13:52 +0000240
Nils Diewald58141332015-04-07 16:18:45 +0000241 /**
242 * Init Tutorial view
243 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000244 if (document.getElementById('view-tutorial')) {
245 window.tutorial = tutClass.create(
Akron7716f012015-07-01 20:38:32 +0200246 document.getElementById('view-tutorial'),
247 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000248 );
249 obj.tutorial = window.tutorial;
250 }
Nils Diewald58141332015-04-07 16:18:45 +0000251
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000252 // Tutorial is in parent
253 else if (window.parent) {
254 obj.tutorial = window.parent.tutorial;
255 };
256
257 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200258 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000259 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000260
Akron6ed13992016-05-23 18:06:05 +0200261 // Initialize documentation links
262 obj.tutorial.initDocLinks(document);
263 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000264
Nils Diewald845282c2015-05-14 07:53:03 +0000265
Nils Diewald58141332015-04-07 16:18:45 +0000266 /**
Akronc1457bf2015-06-11 19:24:00 +0200267 * Add VC creation on submission.
268 */
269 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200270 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200271 form.addEventListener('submit', function (e) {
Akron7716f012015-07-01 20:38:32 +0200272 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200273
Akron7716f012015-07-01 20:38:32 +0200274 // No query was defined
275 if (qf.value === undefined || qf.value === '') {
276 qf.focus();
277 e.halt();
278 KorAP.log(700, "No query given");
279 return;
280 };
281
282 // Store session information
283 session.set("show", show);
284
285 // Set Virtual collection
286 if (vc === undefined) {
287 vc = _getCurrentVC(vcClass, vcArray);
288 };
289
290 if (vc !== undefined) {
Akronc1457bf2015-06-11 19:24:00 +0200291 input.value = vc.toQuery();
Akron7716f012015-07-01 20:38:32 +0200292 }
293 else {
294 delete input['value'];
295 };
Akronc1457bf2015-06-11 19:24:00 +0200296 });
297 };
298
299 /**
Nils Diewald58141332015-04-07 16:18:45 +0000300 * Init hint helper
301 * has to be final because of
302 * reposition
303 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000304 // Todo: Pass an element, so this works with
305 // tutorial pages as well!
306 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000307
Nils Diewald58141332015-04-07 16:18:45 +0000308 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000309 });
310});
Akronc1457bf2015-06-11 19:24:00 +0200311
312// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200313function _getCurrentVC (vcClass, vcArray) {
314 var vc = vcClass.create(vcArray);
315 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
316 vc.fromJson(KorAP.koralQuery["collection"]);
317 };
Akronc1457bf2015-06-11 19:24:00 +0200318 return vc;
319};