forked from nadiafaya/alternativity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoundAlternativesController.js
222 lines (192 loc) · 6.79 KB
/
foundAlternativesController.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
var foundAlternativesController = (function() {
var foundAlternativesController = {};
var alternativeViewList = [];
var pickedAlternativesButtonOn = false;
foundAlternativesController.generateDOM = function() {
cleanAlternativeViews();
createAlternativeViews();
toggleEmptyText(!inscription.alternatives.length);
updateBadgeCount();
togglePickedAlternativesButton();
filterIfPickedAlternatives();
};
foundAlternativesController.unpickStarInAlternatives = function(starNumber) {
for (var i = 0; i < alternativeViewList.length; i++) {
alternativeViewList[i].unpickStar(starNumber);
};
};
foundAlternativesController.showOnlyPickedAlternatives = function() {
pickedAlternativesButtonOn = true;
var hiddenCount = 0;
for (var i = 0; i < alternativeViewList.length; i++) {
var hidden = alternativeViewList[i].hideIfNotPicked();
hidden? hiddenCount++ : hiddenCount;
};
toggleEmptyText(hiddenCount == alternativeViewList.length);
};
foundAlternativesController.showAllAlternatives = function() {
pickedAlternativesButtonOn = false;
for (var i = 0; i < alternativeViewList.length; i++) {
alternativeViewList[i].show();
};
toggleEmptyText(false);
};
var cleanAlternativeViews = function() {
var alternativesViews = document.querySelectorAll('.alternativesViews .alternative');
for (var i = 0; i < alternativesViews.length; i++) {
alternativesViews[i].parentNode.removeChild(alternativesViews[i]);
}
alternativeViewList = [];
};
var createAlternativeViews = function() {
for (var i = 0; i < inscription.alternatives.length; i++) {
var alternativeView = new AlternativeView(inscription.alternatives[i], i);
alternativeViewList.push(alternativeView);
}
};
var toggleEmptyText = function(value) {
var emptyText = document.querySelector('#alternatives #noAlternatives') ;
emptyText.innerText = getEmptyText();
emptyText.style.display = value? 'block' : 'none';
};
var getEmptyText = function() {
if (!inscription.subjects.length) {
return 'Agregá materias para ver las alternativas que se generan.';
}
if (!inscription.alternatives.length) {
if (anyFilterIsBeignUsed()) {
return 'No se encontraron alternativas para los filtros seleccionados.';
}
return 'No se encontraron alternativas para los horarios de las materias ingresadas. \nVerificá que los horarios sean correctos. \nProbá con deshabilitar los horarios de una materia si queres ver alternativas sin esa materia.';
}
if (pickedAlternativesButtonOn) {
return 'No se encontraron alternativas elegidas. Clickea el botón "Mostrar todas" y selecciona una estrella en alguna alternativa';
}
};
var anyFilterIsBeignUsed = function() {
var filterGroups = [inscription.availableDays, inscription.availableTurns, inscription.availableTurnsInDays];
return filterGroups.some(filterGroupIsBeignUsed);
};
var filterGroupIsBeignUsed = function(filterGroup) {
for(var prop in filterGroup){
var value = filterGroup[prop];
if (typeof value != "boolean") {
return filterGroupIsBeignUsed(value);
}
if (!value) {
return true;
}
}
return false;
};
var updateBadgeCount = function() {
var badge = document.getElementById('alternativesBadge');
badge.innerText = inscription.alternatives.length;
badge.className = inscription.alternatives.length? 'badge' : 'hidden';
};
var filterIfPickedAlternatives = function() {
if (pickedAlternativesButtonOn) {
foundAlternativesController.showOnlyPickedAlternatives();
}
};
var togglePickedAlternativesButton = function() {
var pickedAlternativesButton = document.querySelector('#showPickedAlternatives');
if (inscription.alternatives.length) {
pickedAlternativesButton.style.display = 'block';
} else {
pickedAlternativesButton.style.display = 'none';
if (pickedAlternativesButtonOn) {
pickedAlternativesButton.click();
}
}
pickedAlternativesButton.style.display = inscription.alternatives.length? 'block' : 'none';
};
return foundAlternativesController;
})();
var AlternativeView = function(alternative, index) {
var alternativeView = {};
var viewHtml = {};
var starsHtml = {};
var init = function() {
cloneDummyHtml();
addTitle();
paintAlternativeDays();
paintStars();
attachStarsEvent();
};
var cloneDummyHtml = function() {
var dummyHtml = document.querySelector('.alternativesViews .dummy');
viewHtml = dummyHtml.cloneNode(true); //deep
viewHtml.classList.remove('dummy');
viewHtml.classList.add('alternative');
document.querySelector('.alternativesViews').appendChild(viewHtml);
};
var addTitle = function() {
var alternativeTitle = viewHtml.querySelector('h4');
alternativeTitle.innerText = 'Alternativa ' + (index + 1);
};
var paintAlternativeDays = function() {
for (var i = 0; i < alternative.schedules.length; i++) {
var alternativeSubject = alternative.schedules[i];
for (var j = 0; j < alternativeSubject.schedule.days.length; j++) {
var day = alternativeSubject.schedule.days[j];
var dayField = viewHtml.querySelector('tr[rel="'+day.turn+'"] td.'+day.name);
dayField.style.backgroundColor = alternativeSubject.subject.color;
dayField.innerText = alternativeSubject.subject.shortName;
dayField.title = alternativeSubject.subject.name;
}
}
};
var paintStars = function() {
var starButtons = viewHtml.querySelectorAll('.pickAlternativeStars .star');
for (var i = 0; i < starButtons.length; i++) {
starsHtml[(i+1).toString()] = starButtons[i];
}
for (var i = 0; i < starButtons.length; i++) {
starButtons[i].addEventListener('click', starClicked);
if (alternative.pickedNumber) {
pickStar(alternative.pickedNumber);
}
}
};
var attachStarsEvent = function() {
};
var starClicked = function() {
var starNumber = this.dataset.starnumber;
if (alternative.pickedNumber === starNumber) {
alternativeView.unpickStar(starNumber);
} else {
foundAlternativesController.unpickStarInAlternatives(starNumber);
var numbers = ['1','2','3'];
numbers.splice(numbers.indexOf(starNumber),1);
for (var i = 0; i < numbers.length; i++) {
alternativeView.unpickStar(numbers[i]);
};
pickStar(starNumber);
}
};
var pickStar = function(starNumber) {
starsHtml[starNumber].classList.add('picked');
alternative.pickedNumber = starNumber;
inscription.persistPickedAlternatives();
};
alternativeView.unpickStar = function(starNumber) {
var star = starsHtml[starNumber];
star.classList.remove('picked');
if (alternative.pickedNumber === starNumber) {
alternative.pickedNumber = "";
inscription.persistPickedAlternatives();
}
};
alternativeView.hideIfNotPicked = function() {
if (!alternative.pickedNumber) {
viewHtml.style.display = 'none';
return true;
}
};
alternativeView.show = function() {
viewHtml.style.display = 'block';
};
init();
return alternativeView;
};