blob: 61e783b628c74853d502c60df9190f87d30c9e60 [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',
Nils Diewald7148c6f2015-05-04 15:07:53 +000012 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000013 'mailToChiffre',
Nils Diewald845282c2015-05-14 07:53:03 +000014 'lib/highlight/highlight.pack',
Nils Diewald0e6992a2015-04-14 20:13:52 +000015 'util'
16], function (matchClass,
17 hintClass,
18 vcClass,
19 tutClass,
Nils Diewald7148c6f2015-05-04 15:07:53 +000020 domReady,
Nils Diewald4347ee92015-05-04 20:32:48 +000021 hintArray,
Akron27ae9ec2015-06-23 00:43:21 +020022 vcArray,
Akron7716f012015-07-01 20:38:32 +020023 alertifyClass,
Akronbe105b92016-01-04 14:13:20 +010024 sessionClass,
25 tagger) {
Nils Diewalda0defc42015-05-07 23:54:17 +000026
27 // Set hint array for hint helper
28 KorAP.hintArray = hintArray;
29
Akron9cc3eaf2015-06-10 22:15:52 +020030 // Localization values
31 var loc = KorAP.Locale;
Akron7716f012015-07-01 20:38:32 +020032 loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora';
33 loc.VC_oneCollection = loc.VC_oneCollection || 'one Collection';
34 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle Alignment';
35 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akron9cc3eaf2015-06-10 22:15:52 +020036
Nils Diewalda0defc42015-05-07 23:54:17 +000037 // Override KorAP.log
38 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020039 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000040
41 // Use alertify to log errors
42 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020043 (code === 0 ? '' : code + ': ') +
Nils Diewalda0defc42015-05-07 23:54:17 +000044 msg,
45 'error',
Akronf55504a2015-06-18 16:42:55 +020046 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000047 );
48 };
49
Nils Diewald0e6992a2015-04-14 20:13:52 +000050 domReady(function (event) {
51 var obj = {};
Akron7716f012015-07-01 20:38:32 +020052 var session = sessionClass.create('KalamarJS');
53
54 // What should be visible?
55 var show = session.get('show') || {};
Nils Diewalda297f062015-04-02 00:23:46 +000056
57 /**
Akronf55504a2015-06-18 16:42:55 +020058 * Release notifications
59 */
60 if (KorAP.Notifications !== undefined) {
61 var n = KorAP.Notifications;
62 for (var i = 0; i < n.length; i++) {
63 alertifyClass.log(n[i][1], n[i][0], 10000);
64 };
65 };
66
67 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000068 * Replace Virtual Collection field
69 */
70 var vcname;
Akronc1457bf2015-06-11 19:24:00 +020071 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000072 if (input) {
73 input.style.display = 'none';
74 vcname = document.createElement('span');
75 vcname.setAttribute('id', 'vc-choose');
Akron941551e2015-06-11 16:06:22 +020076
Akron27ae9ec2015-06-23 00:43:21 +020077 var currentVC = loc.VC_allCorpora;
78 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
79 currentVC = loc.VC_oneCollection;
80 };
81
Nils Diewald7148c6f2015-05-04 15:07:53 +000082 vcname.appendChild(
83 document.createTextNode(
Akron179c8ac2015-06-30 19:30:50 +020084 document.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +000085 )
86 );
Akron27ae9ec2015-06-23 00:43:21 +020087
Nils Diewald7148c6f2015-05-04 15:07:53 +000088 input.parentNode.insertBefore(vcname, input);
89 };
90
91
92 /**
Nils Diewalda297f062015-04-02 00:23:46 +000093 * Add actions to match entries
94 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000095 var inactiveLi = document.querySelectorAll(
96 '#search > ol > li:not(.active)'
97 );
Nils Diewalda297f062015-04-02 00:23:46 +000098 var i = 0;
Akron6a535d42015-08-26 20:16:58 +020099
Nils Diewalda297f062015-04-02 00:23:46 +0000100 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +0000101 inactiveLi[i].addEventListener('click', function (e) {
102 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +0000103 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000104 else {
105 matchClass.create(this).open();
106 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000107 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000108 });
Akron6a535d42015-08-26 20:16:58 +0200109 inactiveLi[i].addEventListener('keydown', function (e) {
110 var code = _codeFromEvent(e);
111
112 switch (code) {
113 case 32:
114 if (this._match !== undefined)
115 this._match.toggle();
116 else {
117 matchClass.create(this).open();
118 };
119 e.halt();
120 break;
121 };
122 });
Nils Diewalda297f062015-04-02 00:23:46 +0000123 };
124
Akron179c8ac2015-06-30 19:30:50 +0200125 var result = document.getElementById('resultinfo');
Akron6ed13992016-05-23 18:06:05 +0200126 var resultButton = null;
Akron179c8ac2015-06-30 19:30:50 +0200127 if (result != null) {
128 resultButton = result.appendChild(document.createElement('div'));
129 resultButton.classList.add('result', 'button');
130 };
131
132 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200133 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200134
Akron7716f012015-07-01 20:38:32 +0200135 if (resultButton !== null) {
136 var kq;
137 var toggle = document.createElement('a');
138 toggle.setAttribute('title', loc.SHOW_KQ)
139 toggle.classList.add('show-kq', 'button');
140 toggle.appendChild(document.createElement('span'))
141 .appendChild(document.createTextNode(loc.SHOW_KQ));
142 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200143
Akron7716f012015-07-01 20:38:32 +0200144 var showKQ = function () {
145 if (kq === undefined) {
146 kq = document.createElement('div');
147 kq.setAttribute('id', 'koralquery');
148 kq.style.display = 'none';
149 var kqInner = document.createElement('div');
150 kq.appendChild(kqInner);
151 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
152 hljs.highlightBlock(kqInner);
153 var sb = document.getElementById('search');
154 sb.insertBefore(kq, sb.firstChild);
155 };
156
157 if (kq.style.display === 'none') {
158 kq.style.display = 'block';
159 show['kq'] = true;
Akron179c8ac2015-06-30 19:30:50 +0200160 }
Akron7716f012015-07-01 20:38:32 +0200161 else {
162 kq.style.display = 'none';
163 delete show['kq'];
164 };
165 };
166
167 if (toggle !== undefined) {
168
169 // Show koralquery
170 toggle.addEventListener('click', showKQ);
171 };
Akron179c8ac2015-06-30 19:30:50 +0200172 };
Akron7716f012015-07-01 20:38:32 +0200173
174 // Session has KQ visibility stored
175 if (show["kq"])
176 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200177 };
178
Nils Diewald7148c6f2015-05-04 15:07:53 +0000179
Nils Diewalda297f062015-04-02 00:23:46 +0000180 /**
181 * Toggle the alignment (left <=> right)
182 */
Akron179c8ac2015-06-30 19:30:50 +0200183 // querySelector('div.button.right');
184 if (i > 0 && resultButton !== null) {
185 var toggle = document.createElement('a');
186 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
187 // Todo: Reuse old alignment from query
188 var cl = toggle.classList;
189 cl.add('align', 'right', 'button');
190 toggle.addEventListener(
191 'click',
192 function (e) {
193 var ol = document.querySelector('#search > ol');
194 ol.toggleClass("align-left", "align-right");
195 this.toggleClass("left", "right");
196 });
197 toggle.appendChild(document.createElement('span'))
198 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
199 resultButton.appendChild(toggle);
Nils Diewalda297f062015-04-02 00:23:46 +0000200 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000201
Nils Diewald6283d692015-04-23 20:32:53 +0000202
Nils Diewald7148c6f2015-05-04 15:07:53 +0000203 /**
204 * Toggle the Virtual Collection builder
205 */
206 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000207 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200208 var vcclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000209 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000210
211 // The vc is visible
Akron7716f012015-07-01 20:38:32 +0200212 if (vcname.classList.contains('active')) {
Nils Diewald6283d692015-04-23 20:32:53 +0000213 view.removeChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200214 vcname.classList.remove('active');
215 delete show['collection'];
Nils Diewald6283d692015-04-23 20:32:53 +0000216 }
217
218 // The vc is not visible
219 else {
Akronc1457bf2015-06-11 19:24:00 +0200220 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200221 vc = _getCurrentVC(vcClass, vcArray);
Nils Diewald6283d692015-04-23 20:32:53 +0000222 view.appendChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200223 vcname.classList.add('active');
224 show['collection'] = true;
Nils Diewald6283d692015-04-23 20:32:53 +0000225 };
Nils Diewald58141332015-04-07 16:18:45 +0000226 };
Akron179c8ac2015-06-30 19:30:50 +0200227 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200228 if (show['collection']) {
Akron179c8ac2015-06-30 19:30:50 +0200229 vcclick.apply();
230 };
Nils Diewald58141332015-04-07 16:18:45 +0000231 };
232
Nils Diewald0e6992a2015-04-14 20:13:52 +0000233
Nils Diewald58141332015-04-07 16:18:45 +0000234 /**
235 * Init Tutorial view
236 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000237 if (document.getElementById('view-tutorial')) {
238 window.tutorial = tutClass.create(
Akron7716f012015-07-01 20:38:32 +0200239 document.getElementById('view-tutorial'),
240 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000241 );
242 obj.tutorial = window.tutorial;
243 }
Nils Diewald58141332015-04-07 16:18:45 +0000244
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000245 // Tutorial is in parent
246 else if (window.parent) {
247 obj.tutorial = window.parent.tutorial;
248 };
249
250 // Initialize queries for document
Akron6ed13992016-05-23 18:06:05 +0200251 if (obj.tutorial) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000252 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000253
Akron6ed13992016-05-23 18:06:05 +0200254 // Initialize documentation links
255 obj.tutorial.initDocLinks(document);
256 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000257
Nils Diewald845282c2015-05-14 07:53:03 +0000258
Nils Diewald58141332015-04-07 16:18:45 +0000259 /**
Akronc1457bf2015-06-11 19:24:00 +0200260 * Add VC creation on submission.
261 */
262 var form = document.getElementById('searchform');
Akron792f58b2015-07-08 18:59:36 +0200263 if (form !== null) {
Akronc1457bf2015-06-11 19:24:00 +0200264 form.addEventListener('submit', function (e) {
Akron7716f012015-07-01 20:38:32 +0200265 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200266
Akron7716f012015-07-01 20:38:32 +0200267 // No query was defined
268 if (qf.value === undefined || qf.value === '') {
269 qf.focus();
270 e.halt();
271 KorAP.log(700, "No query given");
272 return;
273 };
274
275 // Store session information
276 session.set("show", show);
277
278 // Set Virtual collection
279 if (vc === undefined) {
280 vc = _getCurrentVC(vcClass, vcArray);
281 };
282
283 if (vc !== undefined) {
Akronc1457bf2015-06-11 19:24:00 +0200284 input.value = vc.toQuery();
Akron7716f012015-07-01 20:38:32 +0200285 }
286 else {
287 delete input['value'];
288 };
Akronc1457bf2015-06-11 19:24:00 +0200289 });
290 };
291
292 /**
Nils Diewald58141332015-04-07 16:18:45 +0000293 * Init hint helper
294 * has to be final because of
295 * reposition
296 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000297 // Todo: Pass an element, so this works with
298 // tutorial pages as well!
299 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000300
Nils Diewald58141332015-04-07 16:18:45 +0000301 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000302 });
303});
Akronc1457bf2015-06-11 19:24:00 +0200304
305// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200306function _getCurrentVC (vcClass, vcArray) {
307 var vc = vcClass.create(vcArray);
308 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
309 vc.fromJson(KorAP.koralQuery["collection"]);
310 };
Akronc1457bf2015-06-11 19:24:00 +0200311 return vc;
312};