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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
function htmlEncode(value){
// Create a in-memory element, set its inner text (which is automatically encoded)
// Then grab the encoded contents back out. The element never exists on the DOM.
return $('<textarea/>').text(value).html();
}
function htmlDecode(value){
return $('<textarea/>').html(value).text();
}
const API_ENDPOINT = "https://gimb.tk/test.php";
// const API_ENDPOINT = "http://localhost:5000/test.php";
localforage.setItem('directory', {
"Anton Luka Šijanec": 6326,
"Rok Štular": 5313
}).then(function (value) {
// Do other things once the value has been saved.
console.log("fake directory set");
}).catch(function(err) {
// This code runs if there were any errors
M.toast({ html: "Unable to set fake directory."});
console.log(err);
});
function setLoading(state) {
if (state) {
$("#loading-bar").removeClass("hidden");
} else {
$("#loading-bar").addClass("hidden");
}
}
// Function, responsible for fetching and displaying data
async function loadMessages(force_refresh = true, katera = 0) {
setLoading(true);
// Load required data
let promises_to_run = [
localforage.getItem("username").then((value) => {
username = value;
}),
localforage.getItem("password").then((value) => {
password = value;
}),
localforage.getItem("messages").then((value) => {
messages = value;
})
];
Promise.all(promises_to_run).then(() => {
// If we don't have a list of teachers, query it
if (messages === null || force_refresh) {
$.ajax({
url: API_ENDPOINT,
crossDomain: true,
data: {
"u": username,
"p": password,
"m": "fetchsporocilaseznam",
"a": katera // prejeta
},
dataType: "json",
cache: false,
type: "GET",
success: (data) => {
// If data is null, the request failed
if (data === null) {
M.toast({ html: "Request failed!" });
setLoading(false);
} else {
// Save teachers & populate table
localforage.setItem("messages", data).then((value) => {
messages = value;
displayData();
setLoading(false);
});
}
},
error: () => {
M.toast({ html: "Error fetching messages!" });
setLoading(false);
}
})
} else {
displayData();
setLoading(false);
}
});
}
async function loadMsg(id) {
setLoading(true);
// Load required data
let promises_to_run = [
localforage.getItem("username").then((value) => {
username = value;
}),
localforage.getItem("password").then((value) => {
password = value;
}),
];
Promise.all(promises_to_run).then(() => {
$.ajax({
url: API_ENDPOINT,
crossDomain: true,
data: {
"u": username,
"p": password,
"m": "fetchsporocilo",
"a": id
},
dataType: "json",
cache: false,
type: "GET",
success: (data) => {
// If data is null, the request failed
if (data === null) {
M.toast({ html: "Unable to receive the message, Request failed!" });
setLoading(false);
} else {
displayMessage(id, data);
setLoading(false);
}
},
error: () => {
M.toast({ html: "Error fetching message, No Internet connnection?" });
setLoading(false);
}
})
});
}
async function deleteMsg(id) {
setLoading(true);
// Load required data
let promises_to_run = [
localforage.getItem("username").then((value) => {
username = value;
}),
localforage.getItem("password").then((value) => {
password = value;
}),
];
Promise.all(promises_to_run).then(() => {
$.ajax({
url: API_ENDPOINT,
crossDomain: true,
data: {
"u": username,
"p": password,
"m": "izbrisisporocilo",
"a": id
},
dataType: "json",
cache: false,
type: "GET",
success: (data) => {
// If data is null, the request failed
if (data === null) {
M.toast({ html: "Unable to delete the message, Request failed!" });
setLoading(false);
} else {
document.getElementById("msg_box-"+id).remove();
setLoading(false);
}
},
error: () => {
M.toast({ html: "Unable to delete the message, No Internet connnection?" });
setLoading(false);
}
})
});
}
function displayMessage(id, data) {
document.getElementById("msg_body-"+id).innerHTML = filterXSS(data["telo"]);
}
// Function for displaying data
function displayData() {
var msg_list = document.getElementById("msg_list");
msg_list.innerHTML = "";
messages.forEach(element => {
msg_list.innerHTML += '<div class="col s12 m6" id="msg_box-'+
filterXSS(element["id"])+
'"><div class="card blue-grey darken-1"><div class="card-content white-text"><span class="card-title">'+
filterXSS(element["zadeva"])+
'</span><p id="msg_body-'+
filterXSS(element["id"])+
'"><button class="btn waves-effect waves-light" onclick=loadMsg("'+
filterXSS(element["id"])+
'"); type="submit">Load message body<i class="material-icons right">system_update</i></button></p></div><div class="card-action"><a href=javascript:deleteMsg("'+
filterXSS(element["id"])+
'");><i class="material-icons">delete</i></a><a href=\'javascript:document.getElementById("full_name").value="'+
filterXSS(element["posiljatelj"])+
'";document.getElementById("msg_subject").value="Re: '+
filterXSS(element["zadeva"])+
'";M.updateTextFields();document.getElementById("navigation-main").scrollIntoView();\'><i class="material-icons">reply</i></a>'+
filterXSS(element["posiljatelj"])+" » "+filterXSS(element["datum"]["dan"])+". "+filterXSS(element["datum"]["mesec"])+". "+filterXSS(element["datum"]["leto"])+" at "+
filterXSS(element["cas"]["ura"])+":"+filterXSS(element["cas"]["minuta"])+
'</div></div></div>';
});
}
async function sendMessage(number, subject, bofdy) {
setLoading(true);
let promises_to_run = [
localforage.getItem("username").then((value) => {
username = value;
}),
localforage.getItem("password").then((value) => {
password = value;
}),
];
Promise.all(promises_to_run).then(() => {
$.ajax({
url: API_ENDPOINT,
crossDomain: true,
data: {
"u": username,
"p": password,
"m": "posljisporocilo",
"a": number,
"b": subject,
"c": bofdy
},
dataType: "json",
cache: false,
type: "POST", // big data not good, maybe u wanna many charzz
success: (data) => {
// we CAN't know wether the mesgg was delievered
M.toast({ html: "Message was probably sent, but check the Sent folder to be sure!" });
setLoading(false);
},
error: () => {
M.toast({ html: "Error sending message, No Internet connnection?" });
setLoading(false);
}
})
});
}
function validateName() {
localforage.getItem('directory').then(function(value) {
if(value == null) {
M.toast({ html: "Unable to read directory of people. Name could not be verified. Directory is empty."});
}
var evals = value;
for (var variableKey in evals){
if (evals.hasOwnProperty(variableKey)){
evals[variableKey] = null;
}
}
array =Object.getOwnPropertyNames(evals);
if(array.includes(document.getElementById("full_name").value)) {
document.getElementById("full_name").classList.add("valid");
document.getElementById("msg_send").disabled = false;
} else {
document.getElementById("full_name").classList.add("invalid");
document.getElementById("msg_send").disabled = true;
}
}).catch(function(err) {
M.toast({ html: "Unable to read directory of people. Name could not be verified."});
console.log(err);
});
}
var additionalstufftoaddtomessage;
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.autocomplete-fullname');
localforage.getItem('directory').then(function(value) {
// vse editam v nanotu
var evals = value;
for (var variableKey in evals){
if (evals.hasOwnProperty(variableKey)){
evals[variableKey] = null;
}
}
var instances = M.Autocomplete.init(elems, {
data: evals,
onAutocomplete: validateName,
minLength: 0
});
}).catch(function(err) {
M.toast({ html: "Unable to read directory of people. Unable to autocomplete the name of the person."});
console.log(err);
});
if(window.location.search.substring(1)) {
document.getElementById("full_name").value = decodeURIComponent(window.location.search.substring(1));
M.updateTextFields();
validateName();
}
document.getElementById("full_name").addEventListener("blur", validateName);
document.getElementById("msg_send").addEventListener("click", function() {
localforage.getItem('directory').then(function(value) {
sendMessage(value[document.getElementById("full_name").value], document.getElementById("msg_subject").value,
htmlEncode(document.getElementById("msg_body").value+additionalstufftoaddtomessage));
document.getElementById("msg_body").value = "";
document.getElementById("full_name").value = "";
document.getElementById("msg_subject").value = "";
additionalstufftoaddtomessage = "";
}).catch(function(err) {
M.toast({ html: "Unable to read directory of people. Message could not be sent."});
console.log(err);
});
});
// Setup side menu
const menus = document.querySelectorAll(".side-menu");
M.Sidenav.init(menus, { edge: "right", draggable: true });
var receivedmessages = null;
loadMessages(true, 0);
document.getElementById("msg_add_a_photo").addEventListener("click", function() {
var input = document.createElement('input');
input.type = 'file';
input.onchange = e => {
// getting a hold of the file reference
var file = e.target.files[0];
// setting up the reader
var reader = new FileReader();
reader.readAsDataURL(file); // this is reading as data url
// here we tell the reader what to do when it's done reading...
reader.onload = readerEvent => {
additionalstufftoaddtomessage += '<br><img src="' + readerEvent.target.result + '" />'; // this is the content!
M.toast({html:"Image added as an attachment."});
}
}
input.click();
});
});
|