blob: 0f6c1e8869ac83f3c7268d0334f9cb2aaccfc71c [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
Akron48b1e4d2015-06-17 18:47:01 +0200156 // There is a currentQuery
Nils Diewald845282c2015-05-14 07:53:03 +0000157 if (KorAP.currentQuery !== undefined) {
Nils Diewald845282c2015-05-14 07:53:03 +0000158 var kq = document.createElement('div');
159 kq.setAttribute('id', 'koralquery');
Akron48b1e4d2015-06-17 18:47:01 +0200160
161 var kqInner = document.createElement('div');
162 kq.appendChild(kqInner);
163 kqInner.innerHTML = JSON.stringify(KorAP.currentQuery, null, ' ');
164 hljs.highlightBlock(kqInner);
165
166 var sb = document.getElementById('search');
167 sb.insertBefore(kq, sb.firstChild);
Nils Diewald845282c2015-05-14 07:53:03 +0000168 };
169
Nils Diewald58141332015-04-07 16:18:45 +0000170 /**
Akronc1457bf2015-06-11 19:24:00 +0200171 * Add VC creation on submission.
172 */
173 var form = document.getElementById('searchform');
174 if (form !== undefined) {
175 form.addEventListener('submit', function (e) {
176 if (vc === undefined)
177 vc = _getCurrentVC(vcClass);
178
179 if (vc !== undefined)
180 input.value = vc.toQuery();
181 });
182 };
183
184 /**
Nils Diewald58141332015-04-07 16:18:45 +0000185 * Init hint helper
186 * has to be final because of
187 * reposition
188 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000189 // Todo: Pass an element, so this works with
190 // tutorial pages as well!
191 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000192
Nils Diewald58141332015-04-07 16:18:45 +0000193 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000194 });
195});
Akronc1457bf2015-06-11 19:24:00 +0200196
197// Render Virtual collection
Akron48b1e4d2015-06-17 18:47:01 +0200198// TODO:: Use currentQuery!!!
Akronc1457bf2015-06-11 19:24:00 +0200199function _getCurrentVC (vcClass) {
200 var vc = vcClass.create([
201 ['title', 'string'],
202 ['subTitle', 'string'],
203 ['pubDate', 'date'],
Akron48b1e4d2015-06-17 18:47:01 +0200204 ['author', 'string'],
205 ['corpusID', 'string']
Akronc1457bf2015-06-11 19:24:00 +0200206 ]);
207 if (KorAP.currentVC !== undefined)
208 vc.fromJson(KorAP.currentVC);
209
210 return vc;
211};
212