blob: 1749e6d4a08b6788b37d5909c831db0433148d67 [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',
10 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000011 'mailToChiffre',
Nils Diewald845282c2015-05-14 07:53:03 +000012 'lib/highlight/highlight.pack',
Nils Diewald0e6992a2015-04-14 20:13:52 +000013 'util'
14], function (matchClass,
15 hintClass,
16 vcClass,
17 tutClass,
Nils Diewald7148c6f2015-05-04 15:07:53 +000018 domReady,
Nils Diewald4347ee92015-05-04 20:32:48 +000019 hintArray,
Akron27ae9ec2015-06-23 00:43:21 +020020 vcArray,
Nils Diewald4347ee92015-05-04 20:32:48 +000021 alertifyClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000022
23 // Set hint array for hint helper
24 KorAP.hintArray = hintArray;
25
Akron9cc3eaf2015-06-10 22:15:52 +020026 // Localization values
27 var loc = KorAP.Locale;
Akron941551e2015-06-11 16:06:22 +020028 loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora';
29 loc.VC_oneCollection = loc.VC_oneCollection || 'one Collection';
Akron179c8ac2015-06-30 19:30:50 +020030 loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle Alignment';
31 loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery';
Akron9cc3eaf2015-06-10 22:15:52 +020032
Nils Diewalda0defc42015-05-07 23:54:17 +000033 // Override KorAP.log
34 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020035 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000036
37 // Use alertify to log errors
38 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020039 (code === 0 ? '' : code + ': ') +
Nils Diewalda0defc42015-05-07 23:54:17 +000040 msg,
41 'error',
Akronf55504a2015-06-18 16:42:55 +020042 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000043 );
44 };
45
Nils Diewald0e6992a2015-04-14 20:13:52 +000046 domReady(function (event) {
47 var obj = {};
Nils Diewalda297f062015-04-02 00:23:46 +000048
49 /**
Akronf55504a2015-06-18 16:42:55 +020050 * Release notifications
51 */
52 if (KorAP.Notifications !== undefined) {
53 var n = KorAP.Notifications;
54 for (var i = 0; i < n.length; i++) {
55 alertifyClass.log(n[i][1], n[i][0], 10000);
56 };
57 };
58
59 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000060 * Replace Virtual Collection field
61 */
62 var vcname;
Akronc1457bf2015-06-11 19:24:00 +020063 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000064 if (input) {
65 input.style.display = 'none';
66 vcname = document.createElement('span');
67 vcname.setAttribute('id', 'vc-choose');
Akron941551e2015-06-11 16:06:22 +020068
Akron27ae9ec2015-06-23 00:43:21 +020069 var currentVC = loc.VC_allCorpora;
70 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
71 currentVC = loc.VC_oneCollection;
72 };
73
Nils Diewald7148c6f2015-05-04 15:07:53 +000074 vcname.appendChild(
75 document.createTextNode(
Akron179c8ac2015-06-30 19:30:50 +020076 document.getElementById('collection-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +000077 )
78 );
Akron27ae9ec2015-06-23 00:43:21 +020079
Nils Diewald7148c6f2015-05-04 15:07:53 +000080 input.parentNode.insertBefore(vcname, input);
81 };
82
83
84 /**
Nils Diewalda297f062015-04-02 00:23:46 +000085 * Add actions to match entries
86 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000087 var inactiveLi = document.querySelectorAll(
88 '#search > ol > li:not(.active)'
89 );
Nils Diewalda297f062015-04-02 00:23:46 +000090 var i = 0;
91 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000092 inactiveLi[i].addEventListener('click', function (e) {
93 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000094 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +000095 else {
96 matchClass.create(this).open();
97 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000098 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000099 });
100 };
101
Akron179c8ac2015-06-30 19:30:50 +0200102 var result = document.getElementById('resultinfo');
103 var resultButton;
104 if (result != null) {
105 resultButton = result.appendChild(document.createElement('div'));
106 resultButton.classList.add('result', 'button');
107 };
108
109 // There is a koralQuery
110 if (KorAP.koralQuery !== undefined && resultButton !== null) {
111 var kq;
112
113 var toggle = document.createElement('a');
114 toggle.setAttribute('title', loc.SHOW_KQ)
115 toggle.classList.add('show-kq', 'button');
116 toggle.appendChild(document.createElement('span'))
117 .appendChild(document.createTextNode(loc.SHOW_KQ));
118 resultButton.appendChild(toggle);
119
120 if (toggle !== undefined) {
121
122 // Show koralquery
123 toggle.addEventListener(
124 'click', function () {
125 if (kq === undefined) {
126 kq = document.createElement('div');
127 kq.setAttribute('id', 'koralquery');
128 kq.style.display = 'none';
129 var kqInner = document.createElement('div');
130 kq.appendChild(kqInner);
131 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
132 hljs.highlightBlock(kqInner);
133 var sb = document.getElementById('search');
134 sb.insertBefore(kq, sb.firstChild);
135 };
136
137 kq.style.display = (kq.style.display === 'none') ? 'block' : 'none';
138 }
139 );
140 };
141 };
142
Nils Diewald7148c6f2015-05-04 15:07:53 +0000143
Nils Diewalda297f062015-04-02 00:23:46 +0000144 /**
145 * Toggle the alignment (left <=> right)
146 */
Akron179c8ac2015-06-30 19:30:50 +0200147 // querySelector('div.button.right');
148 if (i > 0 && resultButton !== null) {
149 var toggle = document.createElement('a');
150 toggle.setAttribute('title', loc.TOGGLE_ALIGN);
151 // Todo: Reuse old alignment from query
152 var cl = toggle.classList;
153 cl.add('align', 'right', 'button');
154 toggle.addEventListener(
155 'click',
156 function (e) {
157 var ol = document.querySelector('#search > ol');
158 ol.toggleClass("align-left", "align-right");
159 this.toggleClass("left", "right");
160 });
161 toggle.appendChild(document.createElement('span'))
162 .appendChild(document.createTextNode(loc.TOGGLE_ALIGN));
163 resultButton.appendChild(toggle);
Nils Diewalda297f062015-04-02 00:23:46 +0000164 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000165
Nils Diewald6283d692015-04-23 20:32:53 +0000166
Nils Diewald7148c6f2015-05-04 15:07:53 +0000167 /**
168 * Toggle the Virtual Collection builder
169 */
170 if (vcname) {
Akron179c8ac2015-06-30 19:30:50 +0200171 var collectionShow = document.getElementById('collection-show');
Nils Diewald6283d692015-04-23 20:32:53 +0000172 var vc;
Akron179c8ac2015-06-30 19:30:50 +0200173 var vcclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000174 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000175
176 // The vc is visible
177 if (this.classList.contains('active')) {
178 view.removeChild(vc.element());
179 this.classList.remove('active');
Akron179c8ac2015-06-30 19:30:50 +0200180 delete collectionShow['value'];
Nils Diewald6283d692015-04-23 20:32:53 +0000181 }
182
183 // The vc is not visible
184 else {
Akronc1457bf2015-06-11 19:24:00 +0200185 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200186 vc = _getCurrentVC(vcClass, vcArray);
Nils Diewald6283d692015-04-23 20:32:53 +0000187 view.appendChild(vc.element());
188 this.classList.add('active');
Akron179c8ac2015-06-30 19:30:50 +0200189 collectionShow.value = 'true';
Nils Diewald6283d692015-04-23 20:32:53 +0000190 };
Nils Diewald58141332015-04-07 16:18:45 +0000191 };
Akron179c8ac2015-06-30 19:30:50 +0200192 vcname.onclick = vcclick;
193 if (collectionShow.value === 'true') {
194 vcclick.apply();
195 };
Nils Diewald58141332015-04-07 16:18:45 +0000196 };
197
Nils Diewald0e6992a2015-04-14 20:13:52 +0000198
Nils Diewald58141332015-04-07 16:18:45 +0000199 /**
200 * Init Tutorial view
201 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000202 if (document.getElementById('view-tutorial')) {
203 window.tutorial = tutClass.create(
204 document.getElementById('view-tutorial')
205 );
206 obj.tutorial = window.tutorial;
207 }
Nils Diewald58141332015-04-07 16:18:45 +0000208
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000209 // Tutorial is in parent
210 else if (window.parent) {
211 obj.tutorial = window.parent.tutorial;
212 };
213
214 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000215 if (obj.tutorial)
216 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000217
Nils Diewald61e6ff52015-05-07 17:26:50 +0000218 // Initialize documentation links
219 obj.tutorial.initDocLinks(document);
220
Nils Diewald845282c2015-05-14 07:53:03 +0000221
Nils Diewald58141332015-04-07 16:18:45 +0000222 /**
Akronc1457bf2015-06-11 19:24:00 +0200223 * Add VC creation on submission.
224 */
225 var form = document.getElementById('searchform');
226 if (form !== undefined) {
227 form.addEventListener('submit', function (e) {
228 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200229 vc = _getCurrentVC(vcClass, vcArray);
Akronc1457bf2015-06-11 19:24:00 +0200230
231 if (vc !== undefined)
232 input.value = vc.toQuery();
233 });
234 };
235
236 /**
Nils Diewald58141332015-04-07 16:18:45 +0000237 * Init hint helper
238 * has to be final because of
239 * reposition
240 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000241 // Todo: Pass an element, so this works with
242 // tutorial pages as well!
243 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000244
Nils Diewald58141332015-04-07 16:18:45 +0000245 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000246 });
247});
Akronc1457bf2015-06-11 19:24:00 +0200248
249// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200250function _getCurrentVC (vcClass, vcArray) {
251 var vc = vcClass.create(vcArray);
252 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
253 vc.fromJson(KorAP.koralQuery["collection"]);
254 };
Akronc1457bf2015-06-11 19:24:00 +0200255 return vc;
256};
257