blob: 935491757dda2287b44bd2726a13e4ec5fa759dd [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;
49 var input = document.getElementById('vc');
50 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 {
125 // The vc is not rendered yet
126 if (vc === undefined) {
127 vc = vcClass.create([
128 ['title', 'string'],
129 ['subTitle', 'string'],
130 ['pubDate', 'date'],
131 ['author', 'string']
132 ]);
133
134 if (KorAP.currentVC !== undefined)
135 vc.fromJson(KorAP.currentVC);
136 };
137 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
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000166/*
Nils Diewald845282c2015-05-14 07:53:03 +0000167 if (KorAP.currentQuery !== undefined) {
168 var sb = document.getElementById('searchbar');
169 var kq = document.createElement('div');
170 kq.setAttribute('id', 'koralquery');
171 sb.parentNode.insertBefore(kq, sb.nextSibling);
172 kq.innerHTML = JSON.stringify(KorAP.currentQuery, null, ' ');
173 hljs.highlightBlock(kq);
174 };
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000175*/
Nils Diewald845282c2015-05-14 07:53:03 +0000176
Nils Diewald58141332015-04-07 16:18:45 +0000177 /**
178 * Init hint helper
179 * has to be final because of
180 * reposition
181 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000182 // Todo: Pass an element, so this works with
183 // tutorial pages as well!
184 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000185
Nils Diewald58141332015-04-07 16:18:45 +0000186 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000187 });
188});