blob: 0e49091db67dbc0a7774afd593801bdc0366876e [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';
Akron9cc3eaf2015-06-10 22:15:52 +020030
Nils Diewalda0defc42015-05-07 23:54:17 +000031 // Override KorAP.log
32 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020033 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000034
35 // Use alertify to log errors
36 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020037 (code === 0 ? '' : code + ': ') +
Nils Diewalda0defc42015-05-07 23:54:17 +000038 msg,
39 'error',
Akronf55504a2015-06-18 16:42:55 +020040 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000041 );
42 };
43
Nils Diewald0e6992a2015-04-14 20:13:52 +000044 domReady(function (event) {
45 var obj = {};
Nils Diewalda297f062015-04-02 00:23:46 +000046
47 /**
Akronf55504a2015-06-18 16:42:55 +020048 * Release notifications
49 */
50 if (KorAP.Notifications !== undefined) {
51 var n = KorAP.Notifications;
52 for (var i = 0; i < n.length; i++) {
53 alertifyClass.log(n[i][1], n[i][0], 10000);
54 };
55 };
56
57 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000058 * Replace Virtual Collection field
59 */
60 var vcname;
Akronc1457bf2015-06-11 19:24:00 +020061 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000062 if (input) {
63 input.style.display = 'none';
64 vcname = document.createElement('span');
65 vcname.setAttribute('id', 'vc-choose');
Akron941551e2015-06-11 16:06:22 +020066
Akron27ae9ec2015-06-23 00:43:21 +020067 var currentVC = loc.VC_allCorpora;
68 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
69 currentVC = loc.VC_oneCollection;
70 };
71
Nils Diewald7148c6f2015-05-04 15:07:53 +000072 vcname.appendChild(
73 document.createTextNode(
Akron27ae9ec2015-06-23 00:43:21 +020074 document.getElementById('vc-name').value || currentVC
Nils Diewald7148c6f2015-05-04 15:07:53 +000075 )
76 );
Akron27ae9ec2015-06-23 00:43:21 +020077
Nils Diewald7148c6f2015-05-04 15:07:53 +000078 input.parentNode.insertBefore(vcname, input);
79 };
80
81
82 /**
Nils Diewalda297f062015-04-02 00:23:46 +000083 * Add actions to match entries
84 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000085 var inactiveLi = document.querySelectorAll(
86 '#search > ol > li:not(.active)'
87 );
Nils Diewalda297f062015-04-02 00:23:46 +000088 var i = 0;
89 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000090 inactiveLi[i].addEventListener('click', function (e) {
91 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000092 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +000093 else {
94 matchClass.create(this).open();
95 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000096 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000097 });
98 };
99
Nils Diewald7148c6f2015-05-04 15:07:53 +0000100
Nils Diewalda297f062015-04-02 00:23:46 +0000101 /**
102 * Toggle the alignment (left <=> right)
103 */
104 if (i > 0) {
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000105 var br = document.querySelector('div.button.right');
Nils Diewald5c5a7472015-04-02 22:13:38 +0000106 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +0000107 var toggle = document.createElement('a');
108 toggle.setAttribute('title', 'toggle Alignment');
109 // Todo: Reuse old alignment from cookie!
110 var cl = toggle.classList;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000111 cl.add('align', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +0000112 toggle.addEventListener(
113 'click',
114 function (e) {
115 var ol = document.querySelector('#search > ol');
116 ol.toggleClass("align-left", "align-right");
117 this.toggleClass("left", "right");
118 });
119 toggle.appendChild(document.createElement('span'))
120 .appendChild(document.createTextNode('Alignment'));
121 br.appendChild(toggle);
122 };
123 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000124
Nils Diewald6283d692015-04-23 20:32:53 +0000125
Nils Diewald7148c6f2015-05-04 15:07:53 +0000126 /**
127 * Toggle the Virtual Collection builder
128 */
129 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000130 var vc;
Nils Diewald58141332015-04-07 16:18:45 +0000131 vcname.onclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000132 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000133
134 // The vc is visible
135 if (this.classList.contains('active')) {
136 view.removeChild(vc.element());
137 this.classList.remove('active');
138 }
139
140 // The vc is not visible
141 else {
Akronc1457bf2015-06-11 19:24:00 +0200142 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200143 vc = _getCurrentVC(vcClass, vcArray);
Nils Diewald6283d692015-04-23 20:32:53 +0000144 view.appendChild(vc.element());
145 this.classList.add('active');
146 };
Nils Diewald58141332015-04-07 16:18:45 +0000147 };
148 };
149
Nils Diewald0e6992a2015-04-14 20:13:52 +0000150
Nils Diewald58141332015-04-07 16:18:45 +0000151 /**
152 * Init Tutorial view
153 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000154 if (document.getElementById('view-tutorial')) {
155 window.tutorial = tutClass.create(
156 document.getElementById('view-tutorial')
157 );
158 obj.tutorial = window.tutorial;
159 }
Nils Diewald58141332015-04-07 16:18:45 +0000160
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000161 // Tutorial is in parent
162 else if (window.parent) {
163 obj.tutorial = window.parent.tutorial;
164 };
165
166 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000167 if (obj.tutorial)
168 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000169
Nils Diewald61e6ff52015-05-07 17:26:50 +0000170 // Initialize documentation links
171 obj.tutorial.initDocLinks(document);
172
Akron48b1e4d2015-06-17 18:47:01 +0200173 // There is a currentQuery
Akron27ae9ec2015-06-23 00:43:21 +0200174 /*
175 if (KorAP.koralQuery !== undefined) {
Nils Diewald845282c2015-05-14 07:53:03 +0000176 var kq = document.createElement('div');
177 kq.setAttribute('id', 'koralquery');
Akron48b1e4d2015-06-17 18:47:01 +0200178
179 var kqInner = document.createElement('div');
180 kq.appendChild(kqInner);
Akron27ae9ec2015-06-23 00:43:21 +0200181 kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' ');
Akron48b1e4d2015-06-17 18:47:01 +0200182 hljs.highlightBlock(kqInner);
183
184 var sb = document.getElementById('search');
185 sb.insertBefore(kq, sb.firstChild);
Nils Diewald845282c2015-05-14 07:53:03 +0000186 };
Akron27ae9ec2015-06-23 00:43:21 +0200187 */
Nils Diewald845282c2015-05-14 07:53:03 +0000188
Nils Diewald58141332015-04-07 16:18:45 +0000189 /**
Akronc1457bf2015-06-11 19:24:00 +0200190 * Add VC creation on submission.
191 */
192 var form = document.getElementById('searchform');
193 if (form !== undefined) {
194 form.addEventListener('submit', function (e) {
195 if (vc === undefined)
Akron27ae9ec2015-06-23 00:43:21 +0200196 vc = _getCurrentVC(vcClass, vcArray);
Akronc1457bf2015-06-11 19:24:00 +0200197
198 if (vc !== undefined)
199 input.value = vc.toQuery();
200 });
201 };
202
203 /**
Nils Diewald58141332015-04-07 16:18:45 +0000204 * Init hint helper
205 * has to be final because of
206 * reposition
207 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000208 // Todo: Pass an element, so this works with
209 // tutorial pages as well!
210 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000211
Nils Diewald58141332015-04-07 16:18:45 +0000212 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000213 });
214});
Akronc1457bf2015-06-11 19:24:00 +0200215
216// Render Virtual collection
Akron27ae9ec2015-06-23 00:43:21 +0200217function _getCurrentVC (vcClass, vcArray) {
218 var vc = vcClass.create(vcArray);
219 if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
220 vc.fromJson(KorAP.koralQuery["collection"]);
221 };
Akronc1457bf2015-06-11 19:24:00 +0200222 return vc;
223};
224