blob: feab93fb726350dd2567d661aaa933184644588e [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,
Nils Diewald4347ee92015-05-04 20:32:48 +000016 hintArray,
17 alertifyClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000018 domReady(function (event) {
19 var obj = {};
Nils Diewalda297f062015-04-02 00:23:46 +000020
21 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000022 * Replace Virtual Collection field
23 */
24 var vcname;
25 var input = document.getElementById('vc');
26 if (input) {
27 input.style.display = 'none';
28 vcname = document.createElement('span');
29 vcname.setAttribute('id', 'vc-choose');
30 vcname.appendChild(
31 document.createTextNode(
32 document.getElementById('vc-name').value
33 )
34 );
35 input.parentNode.insertBefore(vcname, input);
36 };
37
38
39 /**
Nils Diewalda297f062015-04-02 00:23:46 +000040 * Add actions to match entries
41 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000042 var inactiveLi = document.querySelectorAll(
43 '#search > ol > li:not(.active)'
44 );
Nils Diewalda297f062015-04-02 00:23:46 +000045 var i = 0;
46 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000047 inactiveLi[i].addEventListener('click', function (e) {
48 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000049 this._match.open();
Nils Diewald0e6992a2015-04-14 20:13:52 +000050 else {
51 matchClass.create(this).open();
52 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000053 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000054 });
55 };
56
Nils Diewald7148c6f2015-05-04 15:07:53 +000057
Nils Diewalda297f062015-04-02 00:23:46 +000058 /**
59 * Toggle the alignment (left <=> right)
60 */
61 if (i > 0) {
62 var br = document.getElementById('button-right');
Nils Diewald5c5a7472015-04-02 22:13:38 +000063 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +000064 var toggle = document.createElement('a');
65 toggle.setAttribute('title', 'toggle Alignment');
66 // Todo: Reuse old alignment from cookie!
67 var cl = toggle.classList;
Nils Diewald7148c6f2015-05-04 15:07:53 +000068 cl.add('align', 'right');
Nils Diewalda297f062015-04-02 00:23:46 +000069 toggle.addEventListener(
70 'click',
71 function (e) {
72 var ol = document.querySelector('#search > ol');
73 ol.toggleClass("align-left", "align-right");
74 this.toggleClass("left", "right");
75 });
76 toggle.appendChild(document.createElement('span'))
77 .appendChild(document.createTextNode('Alignment'));
78 br.appendChild(toggle);
79 };
80 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000081
Nils Diewald6283d692015-04-23 20:32:53 +000082
Nils Diewald7148c6f2015-05-04 15:07:53 +000083 /**
84 * Toggle the Virtual Collection builder
85 */
86 if (vcname) {
Nils Diewald6283d692015-04-23 20:32:53 +000087 var vc;
Nils Diewald58141332015-04-07 16:18:45 +000088 vcname.onclick = function () {
Nils Diewald58141332015-04-07 16:18:45 +000089 var view = document.getElementById('vc-view');
Nils Diewald6283d692015-04-23 20:32:53 +000090
91 // The vc is visible
92 if (this.classList.contains('active')) {
93 view.removeChild(vc.element());
94 this.classList.remove('active');
95 }
96
97 // The vc is not visible
98 else {
99 // The vc is not rendered yet
100 if (vc === undefined) {
101 vc = vcClass.create([
102 ['title', 'string'],
103 ['subTitle', 'string'],
104 ['pubDate', 'date'],
105 ['author', 'string']
106 ]);
107
108 if (KorAP.currentVC !== undefined)
109 vc.fromJson(KorAP.currentVC);
110 };
111 view.appendChild(vc.element());
112 this.classList.add('active');
113 };
Nils Diewald58141332015-04-07 16:18:45 +0000114 };
115 };
116
Nils Diewald0e6992a2015-04-14 20:13:52 +0000117
Nils Diewald58141332015-04-07 16:18:45 +0000118 /**
119 * Init Tutorial view
120 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000121 if (document.getElementById('view-tutorial')) {
122 window.tutorial = tutClass.create(
123 document.getElementById('view-tutorial')
124 );
125 obj.tutorial = window.tutorial;
126 }
Nils Diewald58141332015-04-07 16:18:45 +0000127
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000128 // Tutorial is in parent
129 else if (window.parent) {
130 obj.tutorial = window.parent.tutorial;
131 };
132
133 // Initialize queries for document
Nils Diewald4347ee92015-05-04 20:32:48 +0000134 if (obj.tutorial)
135 obj.tutorial.initQueries(document);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000136
Nils Diewald58141332015-04-07 16:18:45 +0000137 /**
138 * Init hint helper
139 * has to be final because of
140 * reposition
141 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000142 // Todo: Pass an element, so this works with
143 // tutorial pages as well!
144 obj.hint = hintClass.create();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000145
146 // Set hint array for hint helper
147 KorAP.hintArray = hintArray;
148
149 // Override KorAP.log
150 KorAP.log = function (type, msg) {
151
152 // Use alertify to log errors
Nils Diewald4347ee92015-05-04 20:32:48 +0000153 alertifyClass.log(
Nils Diewald7148c6f2015-05-04 15:07:53 +0000154 (type === 0 ? '' : type + ': ') +
155 msg,
Nils Diewald4347ee92015-05-04 20:32:48 +0000156 'error',
Nils Diewald7148c6f2015-05-04 15:07:53 +0000157 5000
158 );
159 };
160
Nils Diewald58141332015-04-07 16:18:45 +0000161 return obj;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000162 });
163});