blob: 7c403aa05f3533e785d337a72f1d5b812775e55c [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
24 // Override KorAP.log
25 window.alertify = alertifyClass;
26 KorAP.log = function (type, msg) {
27
28 // Use alertify to log errors
29 alertifyClass.log(
30 (type === 0 ? '' : type + ': ') +
31 msg,
32 'error',
33 5000
34 );
35 };
36
Nils Diewald0e6992a2015-04-14 20:13:52 +000037 domReady(function (event) {
38 var obj = {};
Nils Diewalda297f062015-04-02 00:23:46 +000039
40 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000041 * Replace Virtual Collection field
42 */
43 var vcname;
44 var input = document.getElementById('vc');
45 if (input) {
46 input.style.display = 'none';
47 vcname = document.createElement('span');
48 vcname.setAttribute('id', 'vc-choose');
49 vcname.appendChild(
50 document.createTextNode(
51 document.getElementById('vc-name').value
52 )
53 );
54 input.parentNode.insertBefore(vcname, input);
55 };
56
57
58 /**
Nils Diewalda297f062015-04-02 00:23:46 +000059 * Add actions to match entries
60 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000061 var inactiveLi = document.querySelectorAll(
62 '#search > ol > li:not(.active)'
63 );
Nils Diewalda297f062015-04-02 00:23:46 +000064 var i = 0;
65 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000066 inactiveLi[i].addEventListener('click', function (e) {
67 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000068 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 else {
70 matchClass.create(this).open();
71 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000072 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000073 });
74 };
75
Nils Diewald7148c6f2015-05-04 15:07:53 +000076
Nils Diewalda297f062015-04-02 00:23:46 +000077 /**
78 * Toggle the alignment (left <=> right)
79 */
80 if (i > 0) {
Nils Diewaldd0711ca2015-05-19 22:25:12 +000081 var br = document.querySelector('div.button.right');
Nils Diewald5c5a7472015-04-02 22:13:38 +000082 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +000083 var toggle = document.createElement('a');
84 toggle.setAttribute('title', 'toggle Alignment');
85 // Todo: Reuse old alignment from cookie!
86 var cl = toggle.classList;
Nils Diewald7148c6f2015-05-04 15:07:53 +000087 cl.add('align', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +000088 toggle.addEventListener(
89 'click',
90 function (e) {
91 var ol = document.querySelector('#search > ol');
92 ol.toggleClass("align-left", "align-right");
93 this.toggleClass("left", "right");
94 });
95 toggle.appendChild(document.createElement('span'))
96 .appendChild(document.createTextNode('Alignment'));
97 br.appendChild(toggle);
98 };
99 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000100
Nils Diewald6283d692015-04-23 20:32:53 +0000101
Nils Diewald7148c6f2015-05-04 15:07:53 +0000102 /**
103 * Toggle the Virtual Collection builder
104 */
105 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000106 var vc;
Nils Diewald58141332015-04-07 16:18:45 +0000107 vcname.onclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000108 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000109
110 // The vc is visible
111 if (this.classList.contains('active')) {
112 view.removeChild(vc.element());
113 this.classList.remove('active');
114 }
115
116 // The vc is not visible
117 else {
118 // The vc is not rendered yet
119 if (vc === undefined) {
120 vc = vcClass.create([
121 ['title', 'string'],
122 ['subTitle', 'string'],
123 ['pubDate', 'date'],
124 ['author', 'string']
125 ]);
126
127 if (KorAP.currentVC !== undefined)
128 vc.fromJson(KorAP.currentVC);
129 };
130 view.appendChild(vc.element());
131 this.classList.add('active');
132 };
Nils Diewald58141332015-04-07 16:18:45 +0000133 };
134 };
135
Nils Diewald0e6992a2015-04-14 20:13:52 +0000136
Nils Diewald58141332015-04-07 16:18:45 +0000137 /**
138 * Init Tutorial view
139 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000140 if (document.getElementById('view-tutorial')) {
141 window.tutorial = tutClass.create(
142 document.getElementById('view-tutorial')
143 );
144 obj.tutorial = window.tutorial;
145 }
Nils Diewald58141332015-04-07 16:18:45 +0000146
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000147 // Tutorial is in parent
148 else if (window.parent) {
149 obj.tutorial = window.parent.tutorial;
150 };
151
152 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000153 if (obj.tutorial)
154 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000155
Nils Diewald61e6ff52015-05-07 17:26:50 +0000156 // Initialize documentation links
157 obj.tutorial.initDocLinks(document);
158
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000159/*
Nils Diewald845282c2015-05-14 07:53:03 +0000160 if (KorAP.currentQuery !== undefined) {
161 var sb = document.getElementById('searchbar');
162 var kq = document.createElement('div');
163 kq.setAttribute('id', 'koralquery');
164 sb.parentNode.insertBefore(kq, sb.nextSibling);
165 kq.innerHTML = JSON.stringify(KorAP.currentQuery, null, ' ');
166 hljs.highlightBlock(kq);
167 };
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000168*/
Nils Diewald845282c2015-05-14 07:53:03 +0000169
Nils Diewald58141332015-04-07 16:18:45 +0000170 /**
171 * Init hint helper
172 * has to be final because of
173 * reposition
174 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000175 // Todo: Pass an element, so this works with
176 // tutorial pages as well!
177 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000178
Nils Diewald58141332015-04-07 16:18:45 +0000179 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000180 });
181});