blob: a4140209c700f93630cb721f589d82b13d5e5062 [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;
Akronf55504a2015-06-18 16:42:55 +020026 KorAP.log = function (code, msg) {
Nils Diewalda0defc42015-05-07 23:54:17 +000027
28 // Use alertify to log errors
29 alertifyClass.log(
Akronf55504a2015-06-18 16:42:55 +020030 (code === 0 ? '' : code + ': ') +
Nils Diewalda0defc42015-05-07 23:54:17 +000031 msg,
32 'error',
Akronf55504a2015-06-18 16:42:55 +020033 10000
Nils Diewalda0defc42015-05-07 23:54:17 +000034 );
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 /**
Akronf55504a2015-06-18 16:42:55 +020041 * Release notifications
42 */
43 if (KorAP.Notifications !== undefined) {
44 var n = KorAP.Notifications;
45 for (var i = 0; i < n.length; i++) {
46 alertifyClass.log(n[i][1], n[i][0], 10000);
47 };
48 };
49
50 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000051 * Replace Virtual Collection field
52 */
53 var vcname;
54 var input = document.getElementById('vc');
55 if (input) {
56 input.style.display = 'none';
57 vcname = document.createElement('span');
58 vcname.setAttribute('id', 'vc-choose');
59 vcname.appendChild(
60 document.createTextNode(
61 document.getElementById('vc-name').value
62 )
63 );
64 input.parentNode.insertBefore(vcname, input);
65 };
66
67
68 /**
Nils Diewalda297f062015-04-02 00:23:46 +000069 * Add actions to match entries
70 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000071 var inactiveLi = document.querySelectorAll(
72 '#search > ol > li:not(.active)'
73 );
Nils Diewalda297f062015-04-02 00:23:46 +000074 var i = 0;
75 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000076 inactiveLi[i].addEventListener('click', function (e) {
77 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000078 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +000079 else {
80 matchClass.create(this).open();
81 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000082 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000083 });
84 };
85
Nils Diewald7148c6f2015-05-04 15:07:53 +000086
Nils Diewalda297f062015-04-02 00:23:46 +000087 /**
88 * Toggle the alignment (left <=> right)
89 */
90 if (i > 0) {
Nils Diewaldd0711ca2015-05-19 22:25:12 +000091 var br = document.querySelector('div.button.right');
Nils Diewald5c5a7472015-04-02 22:13:38 +000092 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +000093 var toggle = document.createElement('a');
94 toggle.setAttribute('title', 'toggle Alignment');
95 // Todo: Reuse old alignment from cookie!
96 var cl = toggle.classList;
Nils Diewald7148c6f2015-05-04 15:07:53 +000097 cl.add('align', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +000098 toggle.addEventListener(
99 'click',
100 function (e) {
101 var ol = document.querySelector('#search > ol');
102 ol.toggleClass("align-left", "align-right");
103 this.toggleClass("left", "right");
104 });
105 toggle.appendChild(document.createElement('span'))
106 .appendChild(document.createTextNode('Alignment'));
107 br.appendChild(toggle);
108 };
109 };
Nils Diewald5c5a7472015-04-02 22:13:38 +0000110
Nils Diewald6283d692015-04-23 20:32:53 +0000111
Nils Diewald7148c6f2015-05-04 15:07:53 +0000112 /**
113 * Toggle the Virtual Collection builder
114 */
115 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000116 var vc;
Nils Diewald58141332015-04-07 16:18:45 +0000117 vcname.onclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000118 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000119
120 // The vc is visible
121 if (this.classList.contains('active')) {
122 view.removeChild(vc.element());
123 this.classList.remove('active');
124 }
125
126 // The vc is not visible
127 else {
128 // The vc is not rendered yet
129 if (vc === undefined) {
130 vc = vcClass.create([
131 ['title', 'string'],
132 ['subTitle', 'string'],
133 ['pubDate', 'date'],
134 ['author', 'string']
135 ]);
136
137 if (KorAP.currentVC !== undefined)
138 vc.fromJson(KorAP.currentVC);
139 };
140 view.appendChild(vc.element());
141 this.classList.add('active');
142 };
Nils Diewald58141332015-04-07 16:18:45 +0000143 };
144 };
145
Nils Diewald0e6992a2015-04-14 20:13:52 +0000146
Nils Diewald58141332015-04-07 16:18:45 +0000147 /**
148 * Init Tutorial view
149 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000150 if (document.getElementById('view-tutorial')) {
151 window.tutorial = tutClass.create(
152 document.getElementById('view-tutorial')
153 );
154 obj.tutorial = window.tutorial;
155 }
Nils Diewald58141332015-04-07 16:18:45 +0000156
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000157 // Tutorial is in parent
158 else if (window.parent) {
159 obj.tutorial = window.parent.tutorial;
160 };
161
162 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000163 if (obj.tutorial)
164 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000165
Nils Diewald61e6ff52015-05-07 17:26:50 +0000166 // Initialize documentation links
167 obj.tutorial.initDocLinks(document);
168
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000169/*
Nils Diewald845282c2015-05-14 07:53:03 +0000170 if (KorAP.currentQuery !== undefined) {
171 var sb = document.getElementById('searchbar');
172 var kq = document.createElement('div');
173 kq.setAttribute('id', 'koralquery');
174 sb.parentNode.insertBefore(kq, sb.nextSibling);
175 kq.innerHTML = JSON.stringify(KorAP.currentQuery, null, ' ');
176 hljs.highlightBlock(kq);
177 };
Nils Diewaldd0711ca2015-05-19 22:25:12 +0000178*/
Nils Diewald845282c2015-05-14 07:53:03 +0000179
Nils Diewald58141332015-04-07 16:18:45 +0000180 /**
181 * Init hint helper
182 * has to be final because of
183 * reposition
184 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000185 // Todo: Pass an element, so this works with
186 // tutorial pages as well!
187 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000188
Nils Diewald58141332015-04-07 16:18:45 +0000189 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000190 });
191});