blob: 4e37a9b96f5121a585f89bed7144cc31992134e2 [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',
8 'lib/alertify',
9 'api',
Nils Diewaldc46003b2015-05-07 15:55:35 +000010 'mailToChiffre',
Nils Diewald845282c2015-05-14 07:53:03 +000011 'lib/highlight/highlight.pack',
Nils Diewald0e6992a2015-04-14 20:13:52 +000012 'util'
13], function (matchClass,
14 hintClass,
15 vcClass,
16 tutClass,
Nils Diewald7148c6f2015-05-04 15:07:53 +000017 domReady,
Nils Diewald4347ee92015-05-04 20:32:48 +000018 hintArray,
19 alertifyClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000020
21 // Set hint array for hint helper
22 KorAP.hintArray = hintArray;
23
Akron9cc3eaf2015-06-10 22:15:52 +020024 // Localization values
25 var loc = KorAP.Locale;
Akron941551e2015-06-11 16:06:22 +020026 loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora';
27 loc.VC_oneCollection = loc.VC_oneCollection || 'one Collection';
Akron9cc3eaf2015-06-10 22:15:52 +020028
Nils Diewalda0defc42015-05-07 23:54:17 +000029 // Override KorAP.log
30 window.alertify = alertifyClass;
Akronf55504a2015-06-18 16:42:55 +020031 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000032
33 // Use alertify to log errors
34 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020035 (code === 0 ? '' : code + ': ') +
Nils Diewalda0defc42015-05-07 23:54:17 +000036 msg,
37 'error',
Akronf55504a2015-06-18 16:42:55 +020038 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000039 );
40 };
41
Nils Diewald0e6992a2015-04-14 20:13:52 +000042 domReady(function (event) {
43 var obj = {};
Nils Diewalda297f062015-04-02 00:23:46 +000044
45 /**
Akronf55504a2015-06-18 16:42:55 +020046 * Release notifications
47 */
48 if (KorAP.Notifications !== undefined) {
49 var n = KorAP.Notifications;
50 for (var i = 0; i < n.length; i++) {
51 alertifyClass.log(n[i][1], n[i][0], 10000);
52 };
53 };
54
55 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000056 * Replace Virtual Collection field
57 */
58 var vcname;
Akronc1457bf2015-06-11 19:24:00 +020059 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000060 if (input) {
61 input.style.display = 'none';
62 vcname = document.createElement('span');
63 vcname.setAttribute('id', 'vc-choose');
Akron941551e2015-06-11 16:06:22 +020064
Nils Diewald7148c6f2015-05-04 15:07:53 +000065 vcname.appendChild(
66 document.createTextNode(
Akron941551e2015-06-11 16:06:22 +020067 document.getElementById('vc-name').value ||
68 (KorAP.currentVC !== undefined) ? loc.VC_oneCollection : loc.VC_allCorpora
Nils Diewald7148c6f2015-05-04 15:07:53 +000069 )
70 );
71 input.parentNode.insertBefore(vcname, input);
72 };
73
74
75 /**
Nils Diewalda297f062015-04-02 00:23:46 +000076 * Add actions to match entries
77 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000078 var inactiveLi = document.querySelectorAll(
79 '#search > ol > li:not(.active)'
80 );
Nils Diewalda297f062015-04-02 00:23:46 +000081 var i = 0;
82 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000083 inactiveLi[i].addEventListener('click', function (e) {
84 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000085 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +000086 else {
87 matchClass.create(this).open();
88 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000089 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000090 });
91 };
92
Nils Diewald7148c6f2015-05-04 15:07:53 +000093
Nils Diewalda297f062015-04-02 00:23:46 +000094 /**
95 * Toggle the alignment (left <=> right)
96 */
97 if (i > 0) {
Nils Diewaldd0711ca2015-05-19 22:25:12 +000098 var br = document.querySelector('div.button.right');
Nils Diewald5c5a7472015-04-02 22:13:38 +000099 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +0000100 var toggle = document.createElement('a');
101 toggle.setAttribute('title', 'toggle Alignment');
102 // Todo: Reuse old alignment from cookie!
103 var cl = toggle.classList;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000104 cl.add('align', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +0000105 toggle.addEventListener(
106 'click',
107 function (e) {
108 var ol = document.querySelector('#search > ol');
109 ol.toggleClass("align-left", "align-right");
110 this.toggleClass("left", "right");
111 });
112 toggle.appendChild(document.createElement('span'))
113 .appendChild(document.createTextNode('Alignment'));
114 br.appendChild(toggle);
115 };
116 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000117
Nils Diewald6283d692015-04-23 20:32:53 +0000118
Nils Diewald7148c6f2015-05-04 15:07:53 +0000119 /**
120 * Toggle the Virtual Collection builder
121 */
122 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000123 var vc;
Nils Diewald58141332015-04-07 16:18:45 +0000124 vcname.onclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000125 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000126
127 // The vc is visible
128 if (this.classList.contains('active')) {
129 view.removeChild(vc.element());
130 this.classList.remove('active');
131 }
132
133 // The vc is not visible
134 else {
Akronc1457bf2015-06-11 19:24:00 +0200135 if (vc === undefined)
136 vc = _getCurrentVC(vcClass);
Nils Diewald6283d692015-04-23 20:32:53 +0000137 view.appendChild(vc.element());
138 this.classList.add('active');
139 };
Nils Diewald58141332015-04-07 16:18:45 +0000140 };
141 };
142
Nils Diewald0e6992a2015-04-14 20:13:52 +0000143
Nils Diewald58141332015-04-07 16:18:45 +0000144 /**
145 * Init Tutorial view
146 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000147 if (document.getElementById('view-tutorial')) {
148 window.tutorial = tutClass.create(
149 document.getElementById('view-tutorial')
150 );
151 obj.tutorial = window.tutorial;
152 }
Nils Diewald58141332015-04-07 16:18:45 +0000153
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000154 // Tutorial is in parent
155 else if (window.parent) {
156 obj.tutorial = window.parent.tutorial;
157 };
158
159 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000160 if (obj.tutorial)
161 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000162
Nils Diewald61e6ff52015-05-07 17:26:50 +0000163 // Initialize documentation links
164 obj.tutorial.initDocLinks(document);
165
Akron48b1e4d2015-06-17 18:47:01 +0200166 // There is a currentQuery
Nils Diewald845282c2015-05-14 07:53:03 +0000167 if (KorAP.currentQuery !== undefined) {
Nils Diewald845282c2015-05-14 07:53:03 +0000168 var kq = document.createElement('div');
169 kq.setAttribute('id', 'koralquery');
Akron48b1e4d2015-06-17 18:47:01 +0200170
171 var kqInner = document.createElement('div');
172 kq.appendChild(kqInner);
173 kqInner.innerHTML = JSON.stringify(KorAP.currentQuery, null, ' ');
174 hljs.highlightBlock(kqInner);
175
176 var sb = document.getElementById('search');
177 sb.insertBefore(kq, sb.firstChild);
Nils Diewald845282c2015-05-14 07:53:03 +0000178 };
179
Nils Diewald58141332015-04-07 16:18:45 +0000180 /**
Akronc1457bf2015-06-11 19:24:00 +0200181 * Add VC creation on submission.
182 */
183 var form = document.getElementById('searchform');
184 if (form !== undefined) {
185 form.addEventListener('submit', function (e) {
186 if (vc === undefined)
187 vc = _getCurrentVC(vcClass);
188
189 if (vc !== undefined)
190 input.value = vc.toQuery();
191 });
192 };
193
194 /**
Nils Diewald58141332015-04-07 16:18:45 +0000195 * Init hint helper
196 * has to be final because of
197 * reposition
198 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000199 // Todo: Pass an element, so this works with
200 // tutorial pages as well!
201 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000202
Nils Diewald58141332015-04-07 16:18:45 +0000203 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000204 });
205});
Akronc1457bf2015-06-11 19:24:00 +0200206
207// Render Virtual collection
Akron48b1e4d2015-06-17 18:47:01 +0200208// TODO:: Use currentQuery!!!
Akronc1457bf2015-06-11 19:24:00 +0200209function _getCurrentVC (vcClass) {
210 var vc = vcClass.create([
211 ['title', 'string'],
212 ['subTitle', 'string'],
213 ['pubDate', 'date'],
Akron48b1e4d2015-06-17 18:47:01 +0200214 ['author', 'string'],
215 ['corpusID', 'string']
Akronc1457bf2015-06-11 19:24:00 +0200216 ]);
217 if (KorAP.currentVC !== undefined)
218 vc.fromJson(KorAP.currentVC);
219
220 return vc;
221};
222