blob: 5ef8dee85556f7a1a8413ecdcfbd85ed30e58c50 [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 Diewald0e6992a2015-04-14 20:13:52 +000010 'util'
11], function (matchClass,
12 hintClass,
13 vcClass,
14 tutClass,
Nils Diewald7148c6f2015-05-04 15:07:53 +000015 domReady,
16 hintArray) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000017 domReady(function (event) {
18 var obj = {};
Nils Diewalda297f062015-04-02 00:23:46 +000019
20 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000021 * Replace Virtual Collection field
22 */
23 var vcname;
24 var input = document.getElementById('vc');
25 if (input) {
26 input.style.display = 'none';
27 vcname = document.createElement('span');
28 vcname.setAttribute('id', 'vc-choose');
29 vcname.appendChild(
30 document.createTextNode(
31 document.getElementById('vc-name').value
32 )
33 );
34 input.parentNode.insertBefore(vcname, input);
35 };
36
37
38 /**
Nils Diewalda297f062015-04-02 00:23:46 +000039 * Add actions to match entries
40 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000041 var inactiveLi = document.querySelectorAll(
42 '#search > ol > li:not(.active)'
43 );
Nils Diewalda297f062015-04-02 00:23:46 +000044 var i = 0;
45 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000046 inactiveLi[i].addEventListener('click', function (e) {
47 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000048 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +000049 else {
50 matchClass.create(this).open();
51 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000052 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000053 });
54 };
55
Nils Diewald7148c6f2015-05-04 15:07:53 +000056
Nils Diewalda297f062015-04-02 00:23:46 +000057 /**
58 * Toggle the alignment (left <=> right)
59 */
60 if (i > 0) {
61 var br = document.getElementById('button-right');
Nils Diewald5c5a7472015-04-02 22:13:38 +000062 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +000063 var toggle = document.createElement('a');
64 toggle.setAttribute('title', 'toggle Alignment');
65 // Todo: Reuse old alignment from cookie!
66 var cl = toggle.classList;
Nils Diewald7148c6f2015-05-04 15:07:53 +000067 cl.add('align', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +000068 toggle.addEventListener(
69 'click',
70 function (e) {
71 var ol = document.querySelector('#search > ol');
72 ol.toggleClass("align-left", "align-right");
73 this.toggleClass("left", "right");
74 });
75 toggle.appendChild(document.createElement('span'))
76 .appendChild(document.createTextNode('Alignment'));
77 br.appendChild(toggle);
78 };
79 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000080
Nils Diewald6283d692015-04-23 20:32:53 +000081
Nils Diewald7148c6f2015-05-04 15:07:53 +000082 /**
83 * Toggle the Virtual Collection builder
84 */
85 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +000086 var vc;
Nils Diewald58141332015-04-07 16:18:45 +000087 vcname.onclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +000088 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +000089
90 // The vc is visible
91 if (this.classList.contains('active')) {
92 view.removeChild(vc.element());
93 this.classList.remove('active');
94 }
95
96 // The vc is not visible
97 else {
98 // The vc is not rendered yet
99 if (vc === undefined) {
100 vc = vcClass.create([
101 ['title', 'string'],
102 ['subTitle', 'string'],
103 ['pubDate', 'date'],
104 ['author', 'string']
105 ]);
106
107 if (KorAP.currentVC !== undefined)
108 vc.fromJson(KorAP.currentVC);
109 };
110 view.appendChild(vc.element());
111 this.classList.add('active');
112 };
Nils Diewald58141332015-04-07 16:18:45 +0000113 };
114 };
115
Nils Diewald0e6992a2015-04-14 20:13:52 +0000116
Nils Diewald58141332015-04-07 16:18:45 +0000117 /**
118 * Init Tutorial view
119 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000120 if (document.getElementById('view-tutorial')) {
121 window.tutorial = tutClass.create(
122 document.getElementById('view-tutorial')
123 );
124 obj.tutorial = window.tutorial;
125 }
Nils Diewald58141332015-04-07 16:18:45 +0000126
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000127 // Tutorial is in parent
128 else if (window.parent) {
129 obj.tutorial = window.parent.tutorial;
130 };
131
132 // Initialize queries for document
133 obj.tutorial.initQueries(document);
134
Nils Diewald58141332015-04-07 16:18:45 +0000135 /**
136 * Init hint helper
137 * has to be final because of
138 * reposition
139 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000140 // Todo: Pass an element, so this works with
141 // tutorial pages as well!
142 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000143
144 // Set hint array for hint helper
145 KorAP.hintArray = hintArray;
146
147 // Override KorAP.log
148 KorAP.log = function (type, msg) {
149
150 // Use alertify to log errors
151 alertify.log(
152 (type === 0 ? '' : type + ': ') +
153 msg,
154 'warn',
155 5000
156 );
157 };
158
Nils Diewald58141332015-04-07 16:18:45 +0000159 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000160 });
161});