blob: 7938a9e3173de4d3bd48ba445fd7afd84277f92a [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 Diewald0e6992a2015-04-14 20:13:52 +000011 'util'
12], function (matchClass,
13 hintClass,
14 vcClass,
15 tutClass,
Nils Diewald7148c6f2015-05-04 15:07:53 +000016 domReady,
Nils Diewald4347ee92015-05-04 20:32:48 +000017 hintArray,
18 alertifyClass) {
Nils Diewalda0defc42015-05-07 23:54:17 +000019
20 // Set hint array for hint helper
21 KorAP.hintArray = hintArray;
22
23 // Override KorAP.log
24 window.alertify = alertifyClass;
25 KorAP.log = function (type, msg) {
26
27 // Use alertify to log errors
28 alertifyClass.log(
29 (type === 0 ? '' : type + ': ') +
30 msg,
31 'error',
32 5000
33 );
34 };
35
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 domReady(function (event) {
37 var obj = {};
Nils Diewalda297f062015-04-02 00:23:46 +000038
39 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000040 * Replace Virtual Collection field
41 */
42 var vcname;
43 var input = document.getElementById('vc');
44 if (input) {
45 input.style.display = 'none';
46 vcname = document.createElement('span');
47 vcname.setAttribute('id', 'vc-choose');
48 vcname.appendChild(
49 document.createTextNode(
50 document.getElementById('vc-name').value
51 )
52 );
53 input.parentNode.insertBefore(vcname, input);
54 };
55
56
57 /**
Nils Diewalda297f062015-04-02 00:23:46 +000058 * Add actions to match entries
59 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000060 var inactiveLi = document.querySelectorAll(
61 '#search > ol > li:not(.active)'
62 );
Nils Diewalda297f062015-04-02 00:23:46 +000063 var i = 0;
64 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000065 inactiveLi[i].addEventListener('click', function (e) {
66 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000067 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +000068 else {
69 matchClass.create(this).open();
70 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000071 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000072 });
73 };
74
Nils Diewald7148c6f2015-05-04 15:07:53 +000075
Nils Diewalda297f062015-04-02 00:23:46 +000076 /**
77 * Toggle the alignment (left <=> right)
78 */
79 if (i > 0) {
80 var br = document.getElementById('button-right');
Nils Diewald5c5a7472015-04-02 22:13:38 +000081 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +000082 var toggle = document.createElement('a');
83 toggle.setAttribute('title', 'toggle Alignment');
84 // Todo: Reuse old alignment from cookie!
85 var cl = toggle.classList;
Nils Diewald7148c6f2015-05-04 15:07:53 +000086 cl.add('align', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +000087 toggle.addEventListener(
88 'click',
89 function (e) {
90 var ol = document.querySelector('#search > ol');
91 ol.toggleClass("align-left", "align-right");
92 this.toggleClass("left", "right");
93 });
94 toggle.appendChild(document.createElement('span'))
95 .appendChild(document.createTextNode('Alignment'));
96 br.appendChild(toggle);
97 };
98 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000099
Nils Diewald6283d692015-04-23 20:32:53 +0000100
Nils Diewald7148c6f2015-05-04 15:07:53 +0000101 /**
102 * Toggle the Virtual Collection builder
103 */
104 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +0000105 var vc;
Nils Diewald58141332015-04-07 16:18:45 +0000106 vcname.onclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +0000107 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +0000108
109 // The vc is visible
110 if (this.classList.contains('active')) {
111 view.removeChild(vc.element());
112 this.classList.remove('active');
113 }
114
115 // The vc is not visible
116 else {
117 // The vc is not rendered yet
118 if (vc === undefined) {
119 vc = vcClass.create([
120 ['title', 'string'],
121 ['subTitle', 'string'],
122 ['pubDate', 'date'],
123 ['author', 'string']
124 ]);
125
126 if (KorAP.currentVC !== undefined)
127 vc.fromJson(KorAP.currentVC);
128 };
129 view.appendChild(vc.element());
130 this.classList.add('active');
131 };
Nils Diewald58141332015-04-07 16:18:45 +0000132 };
133 };
134
Nils Diewald0e6992a2015-04-14 20:13:52 +0000135
Nils Diewald58141332015-04-07 16:18:45 +0000136 /**
137 * Init Tutorial view
138 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000139 if (document.getElementById('view-tutorial')) {
140 window.tutorial = tutClass.create(
141 document.getElementById('view-tutorial')
142 );
143 obj.tutorial = window.tutorial;
144 }
Nils Diewald58141332015-04-07 16:18:45 +0000145
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000146 // Tutorial is in parent
147 else if (window.parent) {
148 obj.tutorial = window.parent.tutorial;
149 };
150
151 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000152 if (obj.tutorial)
153 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000154
Nils Diewald61e6ff52015-05-07 17:26:50 +0000155 // Initialize documentation links
156 obj.tutorial.initDocLinks(document);
157
Nils Diewald58141332015-04-07 16:18:45 +0000158 /**
159 * Init hint helper
160 * has to be final because of
161 * reposition
162 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000163 // Todo: Pass an element, so this works with
164 // tutorial pages as well!
165 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000166
Nils Diewald58141332015-04-07 16:18:45 +0000167 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000168 });
169});