blob: 9aa410d47cf558469bf8eb1c2415f843d0909342 [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',
Nils Diewald7148c6f2015-05-04 15:07:53 +000011 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000012 'mailToChiffre',
Nils Diewald845282c2015-05-14 07:53:03 +000013 'lib/highlight/highlight.pack',
Nils Diewald0e6992a2015-04-14 20:13:52 +000014 'util'
15], function (matchClass,
16 hintClass,
17 vcClass,
18 tutClass,
Nils Diewald7148c6f2015-05-04 15:07:53 +000019 domReady,
Nils Diewald4347ee92015-05-04 20:32:48 +000020 hintArray,
Akron27ae9ec2015-06-23 00:43:21 +020021 vcArray,
Akron7716f012015-07-01 20:38:32 +020022 alertifyClass,
23 sessionClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000024
25 // Set hint array for hint helper
26 KorAP.hintArray = hintArray;
27
Akron9cc3eaf2015-06-10 22:15:52 +020028 // Localization values
29 var loc = KorAP.Locale;
Akron7716f012015-07-01 20:38:32 +020030 loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora';
31 loc.VC_oneCollection = loc.VC_oneCollection || 'one Collection';
32 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle Alignment';
33 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akron9cc3eaf2015-06-10 22:15:52 +020034
Nils Diewalda0defc42015-05-07 23:54:17 +000035 // Override KorAP.log
36 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020037 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000038
39 // Use alertify to log errors
40 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020041 (code === 0 ? '' : code + ': ') +
Nils Diewalda0defc42015-05-07 23:54:17 +000042 msg,
43 'error',
Akronf55504a2015-06-18 16:42:55 +020044 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000045 );
46 };
47
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 domReady(function (event) {
49 var obj = {};
Akron7716f012015-07-01 20:38:32 +020050 var session = sessionClass.create('KalamarJS');
51
52 // What should be visible?
53 var show = session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000054
55 /**
Akronf55504a2015-06-18 16:42:55 +020056 * Release notifications
57 */
58 if (KorAP.Notifications !== undefined) {
59 var n = KorAP.Notifications;
60 for (var i = 0; i < n.length; i++) {
61 alertifyClass.log(n[i][1], n[i][0], 10000);
62 };
63 };
64
65 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000066 * Replace Virtual Collection field
67 */
68 var vcname;
Akronc1457bf2015-06-11 19:24:00 +020069 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000070 if (input) {
71 input.style.display = 'none';
72 vcname = document.createElement('span');
73 vcname.setAttribute('id', 'vc-choose');
Akron941551e2015-06-11 16:06:22 +020074
Akron27ae9ec2015-06-23 00:43:21 +020075 var currentVC = loc.VC_allCorpora;
76 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
77 currentVC = loc.VC_oneCollection;
78 };
79
Nils Diewald7148c6f2015-05-04 15:07:53 +000080 vcname.appendChild(
81 document.createTextNode(
Akron179c8ac2015-06-30 19:30:50 +020082 document.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +000083 )
84 );
Akron27ae9ec2015-06-23 00:43:21 +020085
Nils Diewald7148c6f2015-05-04 15:07:53 +000086 input.parentNode.insertBefore(vcname, input);
87 };
88
89
90 /**
Nils Diewalda297f062015-04-02 00:23:46 +000091 * Add actions to match entries
92 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000093 var inactiveLi = document.querySelectorAll(
94 '#search > ol > li:not(.active)'
95 );
Nils Diewalda297f062015-04-02 00:23:46 +000096 var i = 0;
Akron6a535d42015-08-26 20:16:58 +020097
Nils Diewalda297f062015-04-02 00:23:46 +000098 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000099 inactiveLi[i].addEventListener('click', function (e) {
100 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +0000101 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000102 else {
103 matchClass.create(this).open();
104 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000105 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000106 });
Akron6a535d42015-08-26 20:16:58 +0200107 inactiveLi[i].addEventListener('keydown', function (e) {
108 var code = _codeFromEvent(e);
109
110 switch (code) {
111 case 32:
112 if (this._match !== undefined)
113 this._match.toggle();
114 else {
115 matchClass.create(this).open();
116 };
117 e.halt();
118 break;
119 };
120 });
Nils Diewalda297f062015-04-02 00:23:46 +0000121 };
122
Akron179c8ac2015-06-30 19:30:50 +0200123 var result = document.getElementById('resultinfo');
124 var resultButton;
125 if (result != null) {
126 resultButton = result.appendChild(document.createElement('div'));
127 resultButton.classList.add('result', 'button');
128 };
129
130 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200131 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200132
Akron7716f012015-07-01 20:38:32 +0200133 if (resultButton !== null) {
134 var kq;
135 var toggle = document.createElement('a');
136 toggle.setAttribute('title', loc.SHOW_KQ)
137 toggle.classList.add('show-kq', 'button');
138 toggle.appendChild(document.createElement('span'))
139 .appendChild(document.createTextNode(loc.SHOW_KQ));
140 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200141
Akron7716f012015-07-01 20:38:32 +0200142 var showKQ = function () {
143 if (kq === undefined) {
144 kq = document.createElement('div');
145 kq.setAttribute('id', 'koralquery');
146 kq.style.display = 'none';
147 var kqInner = document.createElement('div');
148 kq.appendChild(kqInner);
149 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
150 hljs.highlightBlock(kqInner);
151 var sb = document.getElementById('search');
152 sb.insertBefore(kq, sb.firstChild);
153 };
154
155 if (kq.style.display === 'none') {
156 kq.style.display = 'block';
157 show['kq'] = true;
Akron179c8ac2015-06-30 19:30:50 +0200158 }
Akron7716f012015-07-01 20:38:32 +0200159 else {
160 kq.style.display = 'none';
161 delete show['kq'];
162 };
163 };
164
165 if (toggle !== undefined) {
166
167 // Show koralquery
168 toggle.addEventListener('click', showKQ);
169 };
Akron179c8ac2015-06-30 19:30:50 +0200170 };
Akron7716f012015-07-01 20:38:32 +0200171
172 // Session has KQ visibility stored
173 if (show["kq"])
174 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200175 };
176
Nils Diewald7148c6f2015-05-04 15:07:53 +0000177
Nils Diewalda297f062015-04-02 00:23:46 +0000178 /**
179 * Toggle the alignment (left <=> right)
180 */
Akron179c8ac2015-06-30 19:30:50 +0200181 // querySelector('div.button.right');
182 if (i > 0 && resultButton !== null) {
183 var toggle = document.createElement('a');
184 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
185 // Todo: Reuse old alignment from query
186 var cl = toggle.classList;
187 cl.add('align', 'right', 'button');
188 toggle.addEventListener(
189 'click',
190 function (e) {
191 var ol = document.querySelector('#search > ol');
192 ol.toggleClass("align-left", "align-right");
193 this.toggleClass("left", "right");
194 });
195 toggle.appendChild(document.createElement('span'))
196 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
197 resultButton.appendChild(toggle);
Nils Diewalda297f062015-04-02 00:23:46 +0000198 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000199
Nils Diewald6283d692015-04-23 20:32:53 +0000200
Nils Diewald7148c6f2015-05-04 15:07:53 +0000201 /**
202 * Toggle the Virtual Collection builder
203 */
204 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000205 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200206 var vcclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000207 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000208
209 // The vc is visible
Akron7716f012015-07-01 20:38:32 +0200210 if (vcname.classList.contains('active')) {
Nils Diewald6283d692015-04-23 20:32:53 +0000211 view.removeChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200212 vcname.classList.remove('active');
213 delete show['collection'];
Nils Diewald6283d692015-04-23 20:32:53 +0000214 }
215
216 // The vc is not visible
217 else {
Akronc1457bf2015-06-11 19:24:00 +0200218 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200219 vc = _getCurrentVC(vcClass, vcArray);
Nils Diewald6283d692015-04-23 20:32:53 +0000220 view.appendChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200221 vcname.classList.add('active');
222 show['collection'] = true;
Nils Diewald6283d692015-04-23 20:32:53 +0000223 };
Nils Diewald58141332015-04-07 16:18:45 +0000224 };
Akron179c8ac2015-06-30 19:30:50 +0200225 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200226 if (show['collection']) {
Akron179c8ac2015-06-30 19:30:50 +0200227 vcclick.apply();
228 };
Nils Diewald58141332015-04-07 16:18:45 +0000229 };
230
Nils Diewald0e6992a2015-04-14 20:13:52 +0000231
Nils Diewald58141332015-04-07 16:18:45 +0000232 /**
233 * Init Tutorial view
234 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000235 if (document.getElementById('view-tutorial')) {
236 window.tutorial = tutClass.create(
Akron7716f012015-07-01 20:38:32 +0200237 document.getElementById('view-tutorial'),
238 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000239 );
240 obj.tutorial = window.tutorial;
241 }
Nils Diewald58141332015-04-07 16:18:45 +0000242
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000243 // Tutorial is in parent
244 else if (window.parent) {
245 obj.tutorial = window.parent.tutorial;
246 };
247
248 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000249 if (obj.tutorial)
250 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000251
Nils Diewald61e6ff52015-05-07 17:26:50 +0000252 // Initialize documentation links
253 obj.tutorial.initDocLinks(document);
254
Nils Diewald845282c2015-05-14 07:53:03 +0000255
Nils Diewald58141332015-04-07 16:18:45 +0000256 /**
Akronc1457bf2015-06-11 19:24:00 +0200257 * Add VC creation on submission.
258 */
259 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200260 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200261 form.addEventListener('submit', function (e) {
Akron7716f012015-07-01 20:38:32 +0200262 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200263
Akron7716f012015-07-01 20:38:32 +0200264 // No query was defined
265 if (qf.value === undefined || qf.value === '') {
266 qf.focus();
267 e.halt();
268 KorAP.log(700, "No query given");
269 return;
270 };
271
272 // Store session information
273 session.set("show", show);
274
275 // Set Virtual collection
276 if (vc === undefined) {
277 vc = _getCurrentVC(vcClass, vcArray);
278 };
279
280 if (vc !== undefined) {
Akronc1457bf2015-06-11 19:24:00 +0200281 input.value = vc.toQuery();
Akron7716f012015-07-01 20:38:32 +0200282 }
283 else {
284 delete input['value'];
285 };
Akronc1457bf2015-06-11 19:24:00 +0200286 });
287 };
288
289 /**
Nils Diewald58141332015-04-07 16:18:45 +0000290 * Init hint helper
291 * has to be final because of
292 * reposition
293 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000294 // Todo: Pass an element, so this works with
295 // tutorial pages as well!
296 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000297
Nils Diewald58141332015-04-07 16:18:45 +0000298 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000299 });
300});
Akronc1457bf2015-06-11 19:24:00 +0200301
302// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200303function _getCurrentVC (vcClass, vcArray) {
304 var vc = vcClass.create(vcArray);
305 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
306 vc.fromJson(KorAP.koralQuery["collection"]);
307 };
Akronc1457bf2015-06-11 19:24:00 +0200308 return vc;
309};