blob: 80257e62d8d8ca893465b0d59339ad20f9562185 [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;
97 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000098 inactiveLi[i].addEventListener('click', function (e) {
99 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +0000100 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000101 else {
102 matchClass.create(this).open();
103 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000104 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +0000105 });
106 };
107
Akron179c8ac2015-06-30 19:30:50 +0200108 var result = document.getElementById('resultinfo');
109 var resultButton;
110 if (result != null) {
111 resultButton = result.appendChild(document.createElement('div'));
112 resultButton.classList.add('result', 'button');
113 };
114
115 // There is a koralQuery
Akron7716f012015-07-01 20:38:32 +0200116 if (KorAP.koralQuery !== undefined) {
Akron179c8ac2015-06-30 19:30:50 +0200117
Akron7716f012015-07-01 20:38:32 +0200118 if (resultButton !== null) {
119 var kq;
120 var toggle = document.createElement('a');
121 toggle.setAttribute('title', loc.SHOW_KQ)
122 toggle.classList.add('show-kq', 'button');
123 toggle.appendChild(document.createElement('span'))
124 .appendChild(document.createTextNode(loc.SHOW_KQ));
125 resultButton.appendChild(toggle);
Akron179c8ac2015-06-30 19:30:50 +0200126
Akron7716f012015-07-01 20:38:32 +0200127 var showKQ = function () {
128 if (kq === undefined) {
129 kq = document.createElement('div');
130 kq.setAttribute('id', 'koralquery');
131 kq.style.display = 'none';
132 var kqInner = document.createElement('div');
133 kq.appendChild(kqInner);
134 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
135 hljs.highlightBlock(kqInner);
136 var sb = document.getElementById('search');
137 sb.insertBefore(kq, sb.firstChild);
138 };
139
140 if (kq.style.display === 'none') {
141 kq.style.display = 'block';
142 show['kq'] = true;
Akron179c8ac2015-06-30 19:30:50 +0200143 }
Akron7716f012015-07-01 20:38:32 +0200144 else {
145 kq.style.display = 'none';
146 delete show['kq'];
147 };
148 };
149
150 if (toggle !== undefined) {
151
152 // Show koralquery
153 toggle.addEventListener('click', showKQ);
154 };
Akron179c8ac2015-06-30 19:30:50 +0200155 };
Akron7716f012015-07-01 20:38:32 +0200156
157 // Session has KQ visibility stored
158 if (show["kq"])
159 showKQ.apply();
Akron179c8ac2015-06-30 19:30:50 +0200160 };
161
Nils Diewald7148c6f2015-05-04 15:07:53 +0000162
Nils Diewalda297f062015-04-02 00:23:46 +0000163 /**
164 * Toggle the alignment (left <=> right)
165 */
Akron179c8ac2015-06-30 19:30:50 +0200166 // querySelector('div.button.right');
167 if (i > 0 && resultButton !== null) {
168 var toggle = document.createElement('a');
169 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
170 // Todo: Reuse old alignment from query
171 var cl = toggle.classList;
172 cl.add('align', 'right', 'button');
173 toggle.addEventListener(
174 'click',
175 function (e) {
176 var ol = document.querySelector('#search > ol');
177 ol.toggleClass("align-left", "align-right");
178 this.toggleClass("left", "right");
179 });
180 toggle.appendChild(document.createElement('span'))
181 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
182 resultButton.appendChild(toggle);
Nils Diewalda297f062015-04-02 00:23:46 +0000183 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000184
Nils Diewald6283d692015-04-23 20:32:53 +0000185
Nils Diewald7148c6f2015-05-04 15:07:53 +0000186 /**
187 * Toggle the Virtual Collection builder
188 */
189 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000190 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200191 var vcclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000192 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000193
194 // The vc is visible
Akron7716f012015-07-01 20:38:32 +0200195 if (vcname.classList.contains('active')) {
Nils Diewald6283d692015-04-23 20:32:53 +0000196 view.removeChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200197 vcname.classList.remove('active');
198 delete show['collection'];
Nils Diewald6283d692015-04-23 20:32:53 +0000199 }
200
201 // The vc is not visible
202 else {
Akronc1457bf2015-06-11 19:24:00 +0200203 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200204 vc = _getCurrentVC(vcClass, vcArray);
Nils Diewald6283d692015-04-23 20:32:53 +0000205 view.appendChild(vc.element());
Akron7716f012015-07-01 20:38:32 +0200206 vcname.classList.add('active');
207 show['collection'] = true;
Nils Diewald6283d692015-04-23 20:32:53 +0000208 };
Nils Diewald58141332015-04-07 16:18:45 +0000209 };
Akron179c8ac2015-06-30 19:30:50 +0200210 vcname.onclick = vcclick;
Akron7716f012015-07-01 20:38:32 +0200211 if (show['collection']) {
Akron179c8ac2015-06-30 19:30:50 +0200212 vcclick.apply();
213 };
Nils Diewald58141332015-04-07 16:18:45 +0000214 };
215
Nils Diewald0e6992a2015-04-14 20:13:52 +0000216
Nils Diewald58141332015-04-07 16:18:45 +0000217 /**
218 * Init Tutorial view
219 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000220 if (document.getElementById('view-tutorial')) {
221 window.tutorial = tutClass.create(
Akron7716f012015-07-01 20:38:32 +0200222 document.getElementById('view-tutorial'),
223 session
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000224 );
225 obj.tutorial = window.tutorial;
226 }
Nils Diewald58141332015-04-07 16:18:45 +0000227
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000228 // Tutorial is in parent
229 else if (window.parent) {
230 obj.tutorial = window.parent.tutorial;
231 };
232
233 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000234 if (obj.tutorial)
235 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000236
Nils Diewald61e6ff52015-05-07 17:26:50 +0000237 // Initialize documentation links
238 obj.tutorial.initDocLinks(document);
239
Nils Diewald845282c2015-05-14 07:53:03 +0000240
Nils Diewald58141332015-04-07 16:18:45 +0000241 /**
Akronc1457bf2015-06-11 19:24:00 +0200242 * Add VC creation on submission.
243 */
244 var form = document.getElementById('searchform');
245 if (form !== undefined) {
246 form.addEventListener('submit', function (e) {
Akron7716f012015-07-01 20:38:32 +0200247 var qf = document.getElementById('q-field');
Akronc1457bf2015-06-11 19:24:00 +0200248
Akron7716f012015-07-01 20:38:32 +0200249 // No query was defined
250 if (qf.value === undefined || qf.value === '') {
251 qf.focus();
252 e.halt();
253 KorAP.log(700, "No query given");
254 return;
255 };
256
257 // Store session information
258 session.set("show", show);
259
260 // Set Virtual collection
261 if (vc === undefined) {
262 vc = _getCurrentVC(vcClass, vcArray);
263 };
264
265 if (vc !== undefined) {
Akronc1457bf2015-06-11 19:24:00 +0200266 input.value = vc.toQuery();
Akron7716f012015-07-01 20:38:32 +0200267 }
268 else {
269 delete input['value'];
270 };
Akronc1457bf2015-06-11 19:24:00 +0200271 });
272 };
273
274 /**
Nils Diewald58141332015-04-07 16:18:45 +0000275 * Init hint helper
276 * has to be final because of
277 * reposition
278 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000279 // Todo: Pass an element, so this works with
280 // tutorial pages as well!
281 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000282
Nils Diewald58141332015-04-07 16:18:45 +0000283 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000284 });
285});
Akronc1457bf2015-06-11 19:24:00 +0200286
287// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200288function _getCurrentVC (vcClass, vcArray) {
289 var vc = vcClass.create(vcArray);
290 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
291 vc.fromJson(KorAP.koralQuery["collection"]);
292 };
Akronc1457bf2015-06-11 19:24:00 +0200293 return vc;
294};
295