blob: 09804ca7541ac74f0d07a7847e2afb65a447a861 [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;
26 loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora';
27
Nils Diewalda0defc42015-05-07 23:54:17 +000028 // Override KorAP.log
29 window.alertify = alertifyClass;
30 KorAP.log = function (type, msg) {
31
32 // Use alertify to log errors
33 alertifyClass.log(
34 (type === 0 ? '' : type + ': ') +
35 msg,
36 'error',
37 5000
38 );
39 };
40
Nils Diewald0e6992a2015-04-14 20:13:52 +000041 domReady(function (event) {
42 var obj = {};
Nils Diewalda297f062015-04-02 00:23:46 +000043
44 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000045 * Replace Virtual Collection field
46 */
47 var vcname;
48 var input = document.getElementById('vc');
49 if (input) {
50 input.style.display = 'none';
51 vcname = document.createElement('span');
52 vcname.setAttribute('id', 'vc-choose');
53 vcname.appendChild(
54 document.createTextNode(
Akron9cc3eaf2015-06-10 22:15:52 +020055 document.getElementById('vc-name').value || loc.VC_allCorpora
Nils Diewald7148c6f2015-05-04 15:07:53 +000056 )
57 );
58 input.parentNode.insertBefore(vcname, input);
59 };
60
61
62 /**
Nils Diewalda297f062015-04-02 00:23:46 +000063 * Add actions to match entries
64 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000065 var inactiveLi = document.querySelectorAll(
66 '#search > ol > li:not(.active)'
67 );
Nils Diewalda297f062015-04-02 00:23:46 +000068 var i = 0;
69 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000070 inactiveLi[i].addEventListener('click', function (e) {
71 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000072 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +000073 else {
74 matchClass.create(this).open();
75 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000076 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000077 });
78 };
79
Nils Diewald7148c6f2015-05-04 15:07:53 +000080
Nils Diewalda297f062015-04-02 00:23:46 +000081 /**
82 * Toggle the alignment (left <=> right)
83 */
84 if (i > 0) {
Nils Diewaldd0711ca2015-05-19 22:25:12 +000085 var br = document.querySelector('div.button.right');
Nils Diewald5c5a7472015-04-02 22:13:38 +000086 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +000087 var toggle = document.createElement('a');
88 toggle.setAttribute('title', 'toggle Alignment');
89 // Todo: Reuse old alignment from cookie!
90 var cl = toggle.classList;
Nils Diewald7148c6f2015-05-04 15:07:53 +000091 cl.add('align', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +000092 toggle.addEventListener(
93 'click',
94 function (e) {
95 var ol = document.querySelector('#search > ol');
96 ol.toggleClass("align-left", "align-right");
97 this.toggleClass("left", "right");
98 });
99 toggle.appendChild(document.createElement('span'))
100 .appendChild(document.createTextNode('Alignment'));
101 br.appendChild(toggle);
102 };
103 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000104
Nils Diewald6283d692015-04-23 20:32:53 +0000105
Nils Diewald7148c6f2015-05-04 15:07:53 +0000106 /**
107 * Toggle the Virtual Collection builder
108 */
109 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000110 var vc;
Nils Diewald58141332015-04-07 16:18:45 +0000111 vcname.onclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000112 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000113
114 // The vc is visible
115 if (this.classList.contains('active')) {
116 view.removeChild(vc.element());
117 this.classList.remove('active');
118 }
119
120 // The vc is not visible
121 else {
122 // The vc is not rendered yet
123 if (vc === undefined) {
124 vc = vcClass.create([
125 ['title', 'string'],
126 ['subTitle', 'string'],
127 ['pubDate', 'date'],
128 ['author', 'string']
129 ]);
130
131 if (KorAP.currentVC !== undefined)
132 vc.fromJson(KorAP.currentVC);
133 };
134 view.appendChild(vc.element());
135 this.classList.add('active');
136 };
Nils Diewald58141332015-04-07 16:18:45 +0000137 };
138 };
139
Nils Diewald0e6992a2015-04-14 20:13:52 +0000140
Nils Diewald58141332015-04-07 16:18:45 +0000141 /**
142 * Init Tutorial view
143 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000144 if (document.getElementById('view-tutorial')) {
145 window.tutorial = tutClass.create(
146 document.getElementById('view-tutorial')
147 );
148 obj.tutorial = window.tutorial;
149 }
Nils Diewald58141332015-04-07 16:18:45 +0000150
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000151 // Tutorial is in parent
152 else if (window.parent) {
153 obj.tutorial = window.parent.tutorial;
154 };
155
156 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000157 if (obj.tutorial)
158 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000159
Nils Diewald61e6ff52015-05-07 17:26:50 +0000160 // Initialize documentation links
161 obj.tutorial.initDocLinks(document);
162
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000163/*
Nils Diewald845282c2015-05-14 07:53:03 +0000164 if (KorAP.currentQuery !== undefined) {
165 var sb = document.getElementById('searchbar');
166 var kq = document.createElement('div');
167 kq.setAttribute('id', 'koralquery');
168 sb.parentNode.insertBefore(kq, sb.nextSibling);
169 kq.innerHTML = JSON.stringify(KorAP.currentQuery, null, ' ');
170 hljs.highlightBlock(kq);
171 };
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000172*/
Nils Diewald845282c2015-05-14 07:53:03 +0000173
Nils Diewald58141332015-04-07 16:18:45 +0000174 /**
175 * Init hint helper
176 * has to be final because of
177 * reposition
178 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000179 // Todo: Pass an element, so this works with
180 // tutorial pages as well!
181 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000182
Nils Diewald58141332015-04-07 16:18:45 +0000183 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000184 });
185});