forked from nadiafaya/alternativity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinscription.js
220 lines (192 loc) · 7.75 KB
/
inscription.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
var inscription = (function() {
var inscription = {};
inscription.subjects = [];
inscription.alternatives = [];
inscription.pickedAlternatives = [];
inscription.availableTurns = {
m: true,
t: true,
n: true
};
inscription.availableDays = {
Lu: true,
Ma: true,
Mi: true,
Ju: true,
Vi: true,
Sa: true
};
inscription.availableTurnsInDays = {
Lu: { m: true, t: true, n: true },
Ma: { m: true, t: true, n: true },
Mi: { m: true, t: true, n: true },
Ju: { m: true, t: true, n: true },
Vi: { m: true, t: true, n: true },
Sa: { m: true, t: true, n: true }
}
inscription.generateAlternatives = function() {
inscription.alternatives = [];
var subjectsToProcess = inscription.subjects;
var currentAlternative = [];
function processNextSubject () {
var currentSubject = subjectsToProcess.pop();
var optionalRound = currentSubject.isOptional || false;
for (var i = 0; i < currentSubject.schedules.length + optionalRound; i++) {
var schedule = {
active: true,
days: []
};
if (i != currentSubject.schedules.length) {
schedule = currentSubject.schedules[i];
}
if(scheduleMeetsFilters(schedule)){
currentAlternative.push({
subject: currentSubject,
schedule: schedule
});
if(subjectsToProcess.length){
processNextSubject();
} else {
var finalAlternative = {};
finalAlternative.schedules = currentAlternative.slice();
addPickedStatusFromStorage(finalAlternative);
inscription.alternatives.push(finalAlternative);
}
currentAlternative.pop();
}
}
subjectsToProcess.push(currentSubject);
}
function scheduleMeetsFilters (schedule) {
return schedule.active &&
scheduleIsUniqueInCurrentAlternative(schedule) &&
scheduleIsInAvailableTurns(schedule) &&
scheduleIsInAvailableDays(schedule) &&
scheduleIsInAvailableTurnsInDays(schedule);
}
function scheduleIsUniqueInCurrentAlternative (schedule) {
if (currentAlternative.length) {
var currentDays = getCurrentAlternativeDays();
for(var i = 0; i < currentDays.length; i++){
var day = currentDays[i];
for(var j = 0; j < schedule.days.length; j++){
var scheduleDay = schedule.days[j];
if(day.name === scheduleDay.name && day.turn === scheduleDay.turn ){
return false;
}
}
}
}
return true;
}
function getCurrentAlternativeDays () {
var currentDays = [];
for (var i = 0; i < currentAlternative.length; i++) {
currentDays = currentDays.concat(currentAlternative[i].schedule.days);
}
return currentDays;
}
function scheduleIsInAvailableTurns (schedule) {
for (var i = 0; i < schedule.days.length; i++) {
if(!inscription.availableTurns[schedule.days[i].turn]) {
return false;
}
}
return true;
}
function scheduleIsInAvailableDays (schedule) {
for (var i = 0; i < schedule.days.length; i++) {
if(!inscription.availableDays[schedule.days[i].name]) {
return false;
}
}
return true;
}
function scheduleIsInAvailableTurnsInDays (schedule) {
for (var i = 0; i < schedule.days.length; i++) {
var day = schedule.days[i].name;
var turn = schedule.days[i].turn;
if(!inscription.availableTurnsInDays[day][turn]) {
return false;
}
};
return true;
}
function addPickedStatusFromStorage (alternative) {
if (inscription.pickedAlternatives) {
for (var i = 0; i < inscription.pickedAlternatives.length; i++) {
var picked = inscription.pickedAlternatives[i];
var pickedCopy = { schedules: picked.schedules };
if (alternativesAreEqual(alternative, pickedCopy)) {
alternative.pickedNumber = picked.pickedNumber;
return;
}
}
}
}
function alternativesAreEqual (alt1, alt2) {
return JSON.stringify(alt1) == JSON.stringify(alt2);
}
function removeSubjectsWithNoSchedules () {
subjectsToProcess = subjectsToProcess.filter(function(subject) {
return subject.schedules.some(function(schedule) {
return schedule.active;
});
});
}
removeSubjectsWithNoSchedules();
if (subjectsToProcess.length) {
processNextSubject();
}
};
function findStoragePickedAlternative (alternative) {
if (inscription.pickedAlternatives) {
for (var i = 0; i < inscription.pickedAlternatives.length; i++) {
var picked = inscription.pickedAlternatives[i];
var pickedCopy = { schedules: picked.schedules };
var alternativeCopy = { schedules: alternative.schedules };
if (JSON.stringify(pickedCopy) == JSON.stringify(alternativeCopy)) {
return picked;
}
}
}
return;
}
inscription.subjectsStorageKey = "_alternativity_subjects";
inscription.pickedAlternativesStorageKey = "_alternativity_picked_alternatives";
inscription.persistSubjects = function() {
localStorage.setItem(this.subjectsStorageKey, JSON.stringify(this.subjects));
};
inscription.persistPickedAlternatives = function() {
for (var i = 0; i < this.alternatives.length; i++) {
var alternative = this.alternatives[i];
var pickedAlternative = findStoragePickedAlternative(alternative);
if (alternative.pickedNumber){
if (pickedAlternative) {
pickedAlternative.pickedNumber = alternative.pickedNumber;
} else {
this.pickedAlternatives.push(this.alternatives[i]);
}
}
if (!alternative.pickedNumber && pickedAlternative) {
var index = this.pickedAlternatives.indexOf(pickedAlternative);
this.pickedAlternatives.splice(index, 1);
}
};
localStorage.setItem(this.pickedAlternativesStorageKey, JSON.stringify(this.pickedAlternatives));
};
inscription.loadFromStorage = function() {
this.subjects = this.subjectsFromStorage() || [];
this.pickedAlternatives = this.alternativesFromStorage() || [];
};
inscription.isPersisted = function() {
return !!this.subjectsFromStorage();
};
inscription.subjectsFromStorage = function() {
return JSON.parse(localStorage.getItem(this.subjectsStorageKey));
};
inscription.alternativesFromStorage = function() {
return JSON.parse(localStorage.getItem(this.pickedAlternativesStorageKey));
};
return inscription;
})();