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
|
const API_ENDPOINT = "https://gimb.tk/test.php";
// const API_ENDPOINT = "http://localhost:5000/test.php";
async function checkLogin() {
localforage.getItem("logged_in").then(function (value) {
// This code runs once the value has been loaded
// from the offline store.
if (value !== true) {
window.location.replace("/index.html");
}
}).catch(function (err) {
// This code runs if there were any errors
console.log(err);
});
}
// Set loading bar visibility
function setLoading(state) {
if (state) {
$("#loading-bar").removeClass("hidden");
} else {
$("#loading-bar").addClass("hidden");
}
}
async function loadAbsences(force_refresh = false) {
setLoading(true);
// Load required data
let promises_to_run = [
localforage.getItem("username").then(function (value) {
username = value;
}),
localforage.getItem("password").then(function (value) {
password = value;
}),
localforage.getItem("absences").then(function (value) {
absences = value;
})
];
await Promise.all(promises_to_run);
// If we don't have a list of absences, query it
if (absences === null || force_refresh) {
$.ajax({
url: API_ENDPOINT,
crossDomain: true,
data: {
"u": username,
"p": password,
"m": "fetchizostanki",
"a": $("#datepicker-from").val(),
"b": $("#datepicker-to").val()
},
dataType: "json",
cache: false,
type: "GET",
success: function (data) {
// If data is null, the credentials were incorrect
if (data === null) {
M.toast({ html: "Request failed!" });
setLoading(false);
} else {
// Save absences & populate UI
localforage.setItem("absences", data).then(function (value) {
absences = value;
displayData();
setLoading(false);
});
}
},
error: function () {
M.toast({ html: "No internet connection!" });
setLoading(false);
}
})
} else {
displayData();
setLoading(false);
}
}
function displayData() {
absences.forEach(element => {
let li = document.createElement("li");
let date_string = element["datum"]["dan"] + ". " + element["datum"]["mesec"] + ". " + element["datum"]["leto"];
let header = document.createElement("div");
header.className = "collapsible-header";
header.innerText = date_string;
let body = document.createElement("div");
body.className = "collapsible-body";
let body_table = document.createElement("table");
body_table.className = "highlight";
let body_table_tbody = document.createElement("tbody");
element["predmeti"].forEach(lesson => {
let subject_row = document.createElement("tr");
let subject_lesson_icon = document.createElement("td");
let subject_lesson_text = document.createElement("td");
subject_lesson_text.innerText = "Lesson " + lesson["ura"];
let subject_lesson_icon_i = document.createElement("i");
subject_lesson_icon_i.className = "material-icons";
switch (lesson["opraviceno"]["status"]) {
case 0:
subject_lesson_icon_i.innerText = "schedule";
break;
case 1:
subject_lesson_icon_i.innerText = "check_circle_outline";
break;
case 2:
subject_lesson_icon_i.innerText = "error_outline";
break;
case 3:
subject_lesson_icon_i.innerText = "not_interested";
break;
}
subject_lesson_icon.appendChild(subject_lesson_icon_i);
let subject_name = document.createElement("td");
subject_name.innerText = lesson["ime"];
subject_row.appendChild(subject_lesson_icon);
subject_row.appendChild(subject_lesson_text);
subject_row.appendChild(subject_name);
body_table_tbody.appendChild(subject_row);
});
body_table.appendChild(body_table_tbody);
body.appendChild(body_table);
li.appendChild(header);
li.appendChild(body);
$("#absences-col").append(li);
});
}
function clearAbsences() {
const table = document.getElementById("absences-col");
while (table.firstChild) {
table.removeChild(table.firstChild);
}
}
function refreshAbsences() {
clearAbsences();
loadAbsences(true);
}
function setupPickers() {
// Setup pickers
var date_object = new Date();
let elems = document.querySelectorAll('#datepicker-to');
let options = {
autoClose: true,
format: "dd.mm.yyyy",
defaultDate: date_object,
setDefaultDate: true,
firstDay: 1,
onSelect: refreshAbsences
}
let instances = M.Datepicker.init(elems, options);
date_object.setDate(date_object.getDate() - 14);
elems = document.querySelectorAll('#datepicker-from');
options = {
autoClose: true,
format: "dd.mm.yyyy",
defaultDate: date_object,
setDefaultDate: true,
firstDay: 1,
onSelect: refreshAbsences
}
instances = M.Datepicker.init(elems, options);
}
document.addEventListener("DOMContentLoaded", () => {
checkLogin();
loadAbsences(true);
// Setup refresh handler
$("#refresh-icon").click(function () {
refreshAbsences();
});
setupPickers();
let coll_elem = document.querySelectorAll('.collapsible');
let coll_instance = M.Collapsible.init(coll_elem, {});
// Setup side menu
const menus = document.querySelectorAll('.side-menu');
M.Sidenav.init(menus, { edge: 'right', draggable: true });
});
|