blob: c960ec21fdc42ea29e792e590896bca26580994a [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;
31 KorAP.log = function (type, msg) {
32
33 // Use alertify to log errors
34 alertifyClass.log(
35 (type === 0 ? '' : type + ': ') +
36 msg,
37 'error',
38 5000
39 );
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 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000046 * Replace Virtual Collection field
47 */
48 var vcname;
Akronc1457bf2015-06-11 19:24:00 +020049 var input = document.getElementById('collection');
Nils Diewald7148c6f2015-05-04 15:07:53 +000050 if (input) {
51 input.style.display = 'none';
52 vcname = document.createElement('span');
53 vcname.setAttribute('id', 'vc-choose');
Akron941551e2015-06-11 16:06:22 +020054
Nils Diewald7148c6f2015-05-04 15:07:53 +000055 vcname.appendChild(
56 document.createTextNode(
Akron941551e2015-06-11 16:06:22 +020057 document.getElementById('vc-name').value ||
58 (KorAP.currentVC !== undefined) ? loc.VC_oneCollection : loc.VC_allCorpora
Nils Diewald7148c6f2015-05-04 15:07:53 +000059 )
60 );
61 input.parentNode.insertBefore(vcname, input);
62 };
63
64
65 /**
Nils Diewalda297f062015-04-02 00:23:46 +000066 * Add actions to match entries
67 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000068 var inactiveLi = document.querySelectorAll(
69 '#search > ol > li:not(.active)'
70 );
Nils Diewalda297f062015-04-02 00:23:46 +000071 var i = 0;
72 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000073 inactiveLi[i].addEventListener('click', function (e) {
74 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000075 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +000076 else {
77 matchClass.create(this).open();
78 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000079 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000080 });
81 };
82
Nils Diewald7148c6f2015-05-04 15:07:53 +000083
Nils Diewalda297f062015-04-02 00:23:46 +000084 /**
85 * Toggle the alignment (left <=> right)
86 */
87 if (i > 0) {
Nils Diewaldd0711ca2015-05-19 22:25:12 +000088 var br = document.querySelector('div.button.right');
Nils Diewald5c5a7472015-04-02 22:13:38 +000089 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +000090 var toggle = document.createElement('a');
91 toggle.setAttribute('title', 'toggle Alignment');
92 // Todo: Reuse old alignment from cookie!
93 var cl = toggle.classList;
Nils Diewald7148c6f2015-05-04 15:07:53 +000094 cl.add('align', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +000095 toggle.addEventListener(
96 'click',
97 function (e) {
98 var ol = document.querySelector('#search > ol');
99 ol.toggleClass("align-left", "align-right");
100 this.toggleClass("left", "right");
101 });
102 toggle.appendChild(document.createElement('span'))
103 .appendChild(document.createTextNode('Alignment'));
104 br.appendChild(toggle);
105 };
106 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000107
Nils Diewald6283d692015-04-23 20:32:53 +0000108
Nils Diewald7148c6f2015-05-04 15:07:53 +0000109 /**
110 * Toggle the Virtual Collection builder
111 */
112 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000113 var vc;
Nils Diewald58141332015-04-07 16:18:45 +0000114 vcname.onclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000115 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000116
117 // The vc is visible
118 if (this.classList.contains('active')) {
119 view.removeChild(vc.element());
120 this.classList.remove('active');
121 }
122
123 // The vc is not visible
124 else {
Akronc1457bf2015-06-11 19:24:00 +0200125 if (vc === undefined)
126 vc = _getCurrentVC(vcClass);
Nils Diewald6283d692015-04-23 20:32:53 +0000127 view.appendChild(vc.element());
128 this.classList.add('active');
129 };
Nils Diewald58141332015-04-07 16:18:45 +0000130 };
131 };
132
Nils Diewald0e6992a2015-04-14 20:13:52 +0000133
Nils Diewald58141332015-04-07 16:18:45 +0000134 /**
135 * Init Tutorial view
136 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000137 if (document.getElementById('view-tutorial')) {
138 window.tutorial = tutClass.create(
139 document.getElementById('view-tutorial')
140 );
141 obj.tutorial = window.tutorial;
142 }
Nils Diewald58141332015-04-07 16:18:45 +0000143
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000144 // Tutorial is in parent
145 else if (window.parent) {
146 obj.tutorial = window.parent.tutorial;
147 };
148
149 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000150 if (obj.tutorial)
151 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000152
Nils Diewald61e6ff52015-05-07 17:26:50 +0000153 // Initialize documentation links
154 obj.tutorial.initDocLinks(document);
155
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000156/*
Nils Diewald845282c2015-05-14 07:53:03 +0000157 if (KorAP.currentQuery !== undefined) {
158 var sb = document.getElementById('searchbar');
159 var kq = document.createElement('div');
160 kq.setAttribute('id', 'koralquery');
161 sb.parentNode.insertBefore(kq, sb.nextSibling);
162 kq.innerHTML = JSON.stringify(KorAP.currentQuery, null, ' ');
163 hljs.highlightBlock(kq);
164 };
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000165*/
Nils Diewald845282c2015-05-14 07:53:03 +0000166
Nils Diewald58141332015-04-07 16:18:45 +0000167 /**
Akronc1457bf2015-06-11 19:24:00 +0200168 * Add VC creation on submission.
169 */
170 var form = document.getElementById('searchform');
171 if (form !== undefined) {
172 form.addEventListener('submit', function (e) {
173 if (vc === undefined)
174 vc = _getCurrentVC(vcClass);
175
176 if (vc !== undefined)
177 input.value = vc.toQuery();
178 });
179 };
180
181 /**
Nils Diewald58141332015-04-07 16:18:45 +0000182 * Init hint helper
183 * has to be final because of
184 * reposition
185 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000186 // Todo: Pass an element, so this works with
187 // tutorial pages as well!
188 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000189
Nils Diewald58141332015-04-07 16:18:45 +0000190 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000191 });
192});
Akronc1457bf2015-06-11 19:24:00 +0200193
194// Render Virtual collection
195function _getCurrentVC (vcClass) {
196 var vc = vcClass.create([
197 ['title', 'string'],
198 ['subTitle', 'string'],
199 ['pubDate', 'date'],
200 ['author', 'string']
201 ]);
202 if (KorAP.currentVC !== undefined)
203 vc.fromJson(KorAP.currentVC);
204
205 return vc;
206};
207