forked from nadiafaya/alternativity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOMControl.js
239 lines (208 loc) · 7.56 KB
/
DOMControl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// DOM Events
document.addEventListener("DOMContentLoaded", function(){
// Switch page
var navButtons = document.querySelectorAll('.navbar .navbar-button');
Array.prototype.forEach.call(navButtons, function(button) {
button.addEventListener('click', function() {
// remove active nav button
document.querySelector('.navbar-button.active').classList.remove('active');
// add active nav button
button.classList.add('active');
// hide current page
document.querySelector('.page.active').classList.remove('active');
// show new page
document.querySelector(button.dataset.page).classList.add('active');
});
});
// openModal
var modalButtons = document.querySelectorAll('.triggerModal');
Array.prototype.forEach.call(modalButtons, function(button) {
button.addEventListener('click', function() {
var modalSelector = button.dataset.modal;
var modal = document.querySelector(modalSelector);
modal.style.display = 'block';
});
});
// closeModal
var closeModalButtons = document.querySelectorAll('.closeModal');
Array.prototype.forEach.call(closeModalButtons, function(button) {
button.addEventListener('click', function() {
var modalSelector = button.dataset.modal;
var modal = document.querySelector(modalSelector);
modal.style.display = 'none';
});
});
// escapeModal
document.body.addEventListener('keydown', function(event) {
if (event.keyCode == 27) {
Array.prototype.forEach.call(closeModalButtons, function(button) {
button.click();
});
};
});
// Add subject
var addSubjectButton = document.querySelector('#addSubject');
addSubjectButton.addEventListener("click", function() {
var subjectName = document.querySelector('#subjectName');
var subjectSchedules = document.querySelector('#subjectSchedules');
var errorMessages = document.querySelector('.errorMessages');
var subjectIsOptional = document.querySelector('#addSubjectForm #subjectOptional .on').classList.contains('active');
// Clear errors
subjectName.classList.remove('hasError');
subjectSchedules.classList.remove('hasError');
errorMessages.innerText = '';
// Validate empty fields
if (!subjectName.value) {
subjectName.classList.add('hasError');
return;
}
if (!subjectSchedules.value) {
subjectSchedules.classList.add('hasError');
return;
}
// Add subject
var newSubject = new Subject({
name: subjectName.value,
schedules: subjectSchedules.value,
isOptional: subjectIsOptional
});
// Check for errors
if (newSubject.errorLog) {
subjectSchedules.classList.add('hasError');
errorMessages.innerText = newSubject.errorLog;
return;
};
// Add to DOM
foundSubjectsController.addSubject(newSubject);
// Empty form
subjectName.value = "";
subjectSchedules.value = "";
// Start process
inscription.subjects.push(newSubject);
inscription.generateAlternatives();
foundAlternativesController.generateDOM();
inscription.persistSubjects();
// Close modal
var closeButton = document.querySelector('#addSubjectForm .closeModal');
closeButton.click();
});
// Attach on/off in add subject form
var optionalSubjectButtonContainer = document.querySelector('#addSubjectForm #subjectOptional');
onOff.attachOnOffSelectorEvents(optionalSubjectButtonContainer, function() {});
// Filter turns
var turnCheckboxes = document.querySelectorAll('.alternatives .filters.turns input');
Array.prototype.forEach.call(turnCheckboxes, function(turnCheckbox) {
turnCheckbox.addEventListener('change', function() {
var turns = {
m: document.getElementById('m').checked,
t: document.getElementById('t').checked,
n: document.getElementById('n').checked
};
inscription.availableTurns = turns;
inscription.generateAlternatives();
foundAlternativesController.generateDOM();
});
});
//Filter Days
var dayCheckboxes = document.querySelectorAll('.alternatives .filters.days input');
Array.prototype.forEach.call(dayCheckboxes, function(dayCheckbox) {
dayCheckbox.addEventListener('change', function() {
var days = {
Lu: document.getElementById('Lu').checked,
Ma: document.getElementById('Ma').checked,
Mi: document.getElementById('Mi').checked,
Ju: document.getElementById('Ju').checked,
Vi: document.getElementById('Vi').checked,
Sa: document.getElementById('Sa').checked
};
inscription.availableDays = days;
inscription.generateAlternatives();
foundAlternativesController.generateDOM();
});
});
// Filter days and turns
var switchGlobalAndAdvancedFiltersButton = document.getElementById('switchGlobalAndAdvancedFilters');
switchGlobalAndAdvancedFiltersButton.addEventListener('click', function() {
// Toggle global filters
toggleFilter('globalFilters');
// Toggle advanced filter
toggleFilter('advancedFilters');
// Switch button text
var button = switchGlobalAndAdvancedFiltersButton;
var oldText = button.innerText;
button.innerText = button.dataset.backuptext;
button.dataset.backuptext = oldText;
});
var toggleFilter = function(filterId) {
var filter = document.getElementById(filterId);
if (filter.classList.contains('hidden')) {
filter.classList.remove('hidden');
} else {
filter.classList.add('hidden');
}
};
//Advanced filter
var dayAndTurnCheckboxes = document.querySelectorAll('.alternatives .filters.daysAndTurns input');
Array.prototype.forEach.call(dayAndTurnCheckboxes, function(dayAndTurnCheckbox) {
dayAndTurnCheckbox.addEventListener('change', function() {
var day = dayAndTurnCheckbox.dataset.day;
var turn = dayAndTurnCheckbox.dataset.turn;
inscription.availableTurnsInDays[day][turn] = dayAndTurnCheckbox.checked;
inscription.generateAlternatives();
foundAlternativesController.generateDOM();
});
});
// Show picked alternatives
var showOnlyPickedButton = document.querySelector('.alternatives #showPickedAlternatives');
showOnlyPickedButton.addEventListener('click', function() {
var isActive = showOnlyPickedButton.classList.contains('active');
if (isActive) {
showOnlyPickedButton.classList.remove('active');
showOnlyPickedButton.innerText = "Mostrar solo las elegidas";
foundAlternativesController.showAllAlternatives();
} else {
showOnlyPickedButton.classList.add('active');
showOnlyPickedButton.innerText = "Mostrar todas";
foundAlternativesController.showOnlyPickedAlternatives();
}
});
// Load the subjects stored on local storage
if(inscription.isPersisted()) {
inscription.loadFromStorage();
inscription.subjects.forEach(function(subject) {
// remove persisted subject colors
subjectColors.getColor();
foundSubjectsController.addSubject(subject);
});
inscription.generateAlternatives();
foundAlternativesController.generateDOM();
}
});
var onOff = (function() {
onOff = {};
var onOffActive = function(button) {
return button.classList.contains('active');
};
var toggleOnOffButton = function(button) {
if (onOffActive(button)) {
button.classList.remove('active');
button.classList.add('inactive');
} else {
button.classList.remove('inactive');
button.classList.add('active');
}
}
onOff.attachOnOffSelectorEvents = function(onOffContainer, onOnOffChange) {
var onOffButtons = onOffContainer.querySelectorAll('.onOff span');
Array.prototype.forEach.call(onOffButtons, function(onOffButton) {
onOffButton.addEventListener('click', function() {
var sibling = this.classList.contains('on')? this.nextElementSibling: this.previousElementSibling;
var onButton = this.classList.contains('on')? this : sibling;
toggleOnOffButton(this);
toggleOnOffButton(sibling);
onOnOffChange(onOffActive(onButton));
});
});
};
return onOff;
})();