-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
498 lines (457 loc) · 14 KB
/
script.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//INICIALIZAR SINGLETON
document.addEventListener("load", checkLogin());
//
const calendar = document.querySelector(".calendar"),
date = document.querySelector(".date"),
daysContainer = document.querySelector(".days"),
prev = document.querySelector(".prev"),
next = document.querySelector(".next"),
todayBtn = document.querySelector(".today-btn"),
gotoBtn = document.querySelector(".goto-btn"),
dateInput = document.querySelector(".date-input"),
eventDay = document.querySelector(".event-day"),
eventDate = document.querySelector(".event-date"),
eventsContainer = document.querySelector(".events"),
addEventBtn = document.querySelector(".add-event"),
addEventWrapper = document.querySelector(".add-event-wrapper "),
addEventCloseBtn = document.querySelector(".close "),
addEventTitle = document.querySelector(".event-name "),
addEventFrom = document.querySelector(".event-time-from "),
addEventTo = document.querySelector(".event-time-to "),
addEventSubmit = document.querySelector(".add-event-btn ");
let today = new Date();
let activeDay;
let month = today.getMonth();
let year = today.getFullYear();
const months = [
"Janeiro",
"Fevereiro",
"Março",
"Abril",
"Maio",
"Junho",
"Julho",
"Agosto",
"Setembro",
"Outubro",
"Novembro",
"Dezembro",
];
const eventsArr = [];
getEvents();
console.log(eventsArr);
//function to add days in days with class day and prev-date next-date on previous month and next month days and active on today
function initCalendar() {
const firstDay = new Date(year, month, 1);
const lastDay = new Date(year, month + 1, 0);
const prevLastDay = new Date(year, month, 0);
const prevDays = prevLastDay.getDate();
const lastDate = lastDay.getDate();
const day = firstDay.getDay();
const nextDays = 7 - lastDay.getDay() - 1;
date.innerHTML = months[month] + " " + year;
let days = "";
for (let x = day; x > 0; x--) {
days += `<div class="day prev-date">${prevDays - x + 1}</div>`;
}
for (let i = 1; i <= lastDate; i++) {
//check if event is present on that day
let event = false;
eventsArr.forEach((eventObj) => {
if (
eventObj.day === i &&
eventObj.month === month + 1 &&
eventObj.year === year
) {
event = true;
}
});
if (
i === new Date().getDate() &&
year === new Date().getFullYear() &&
month === new Date().getMonth()
) {
activeDay = i;
getActiveDay(i);
updateEvents(i);
if (event) {
days += `<div class="day today active event">${i}</div>`;
} else {
days += `<div class="day today active">${i}</div>`;
}
} else {
if (event) {
days += `<div class="day event">${i}</div>`;
} else {
days += `<div class="day ">${i}</div>`;
}
}
}
for (let j = 1; j <= nextDays; j++) {
days += `<div class="day next-date">${j}</div>`;
}
daysContainer.innerHTML = days;
addListner();
}
//function to add month and year on prev and next button
function prevMonth() {
month--;
if (month < 0) {
month = 11;
year--;
}
initCalendar();
}
function nextMonth() {
month++;
if (month > 11) {
month = 0;
year++;
}
initCalendar();
}
prev.addEventListener("click", prevMonth);
next.addEventListener("click", nextMonth);
initCalendar();
//function to add active on day
function addListner() {
const days = document.querySelectorAll(".day");
days.forEach((day) => {
day.addEventListener("click", (e) => {
getActiveDay(e.target.innerHTML);
updateEvents(Number(e.target.innerHTML));
activeDay = Number(e.target.innerHTML);
//remove active
days.forEach((day) => {
day.classList.remove("active");
});
//if clicked prev-date or next-date switch to that month
if (e.target.classList.contains("prev-date")) {
prevMonth();
//add active to clicked day afte month is change
setTimeout(() => {
//add active where no prev-date or next-date
const days = document.querySelectorAll(".day");
days.forEach((day) => {
if (
!day.classList.contains("prev-date") &&
day.innerHTML === e.target.innerHTML
) {
day.classList.add("active");
}
});
}, 100);
} else if (e.target.classList.contains("next-date")) {
nextMonth();
//add active to clicked day afte month is changed
setTimeout(() => {
const days = document.querySelectorAll(".day");
days.forEach((day) => {
if (
!day.classList.contains("next-date") &&
day.innerHTML === e.target.innerHTML
) {
day.classList.add("active");
}
});
}, 100);
} else {
e.target.classList.add("active");
}
});
});
}
todayBtn.addEventListener("click", () => {
today = new Date();
month = today.getMonth();
year = today.getFullYear();
initCalendar();
});
dateInput.addEventListener("input", (e) => {
dateInput.value = dateInput.value.replace(/[^0-9/]/g, "");
if (dateInput.value.length === 2) {
dateInput.value += "/";
}
if (dateInput.value.length > 7) {
dateInput.value = dateInput.value.slice(0, 7);
}
if (e.inputType === "deleteContentBackward") {
if (dateInput.value.length === 3) {
dateInput.value = dateInput.value.slice(0, 2);
}
}
});
gotoBtn.addEventListener("click", gotoDate);
function gotoDate() {
console.log("here");
const dateArr = dateInput.value.split("/");
if (dateArr.length === 2) {
if (dateArr[0] > 0 && dateArr[0] < 13 && dateArr[1].length === 4) {
month = dateArr[0] - 1;
year = dateArr[1];
initCalendar();
return;
}
}
alert("Invalid Date");
}
//function get active day day name and date and update eventday eventdate
function getActiveDay(date) {
const day = new Date(year, month, date);
let dayName = day.toString().split(" ")[0];
if (dayName === "Sun")
dayName= "Domingo";
else if (dayName === "Mon")
dayName= "Segunda-Feira";
else if (dayName === "Tue")
dayName= "Terça-Feira";
else if (dayName === "Wed")
dayName= "Quarta-Feira";
else if (dayName === "Thu")
dayName= "Quinta-Feira";
else if (dayName === "Fri")
dayName= "Sexta-Feira";
else if (dayName === "Sat")
dayName= "Sábado";
eventDay.innerHTML = dayName;
eventDate.innerHTML = date + " " + months[month] + " " + year;
}
//function update events when a day is active
function updateEvents(date) {
let events = "";
eventsArr.forEach((event) => {
if (
date === event.day &&
month + 1 === event.month &&
year === event.year
) {
event.events.forEach((event) => {
events += `<div class="event">
<div class="title">
<i class="fas fa-circle"></i>
<h3 class="event-title">${event.title}</h3>
</div>
<div class="event-time">
<span class="event-time">${event.time}</span>
</div>
</div>`;
});
}
});
if (events === "") {
events = `<div class="no-event">
<h3>Não há compromissos</h3>
</div>`;
}
eventsContainer.innerHTML = events;
saveEvents();
}
//function to add event
addEventBtn.addEventListener("click", () => {
addEventWrapper.classList.toggle("active");
});
addEventCloseBtn.addEventListener("click", () => {
addEventWrapper.classList.remove("active");
});
document.addEventListener("click", (e) => {
if (e.target !== addEventBtn && !addEventWrapper.contains(e.target)) {
addEventWrapper.classList.remove("active");
}
});
//allow 50 chars in eventtitle
addEventTitle.addEventListener("input", (e) => {
addEventTitle.value = addEventTitle.value.slice(0, 60);
});
//allow only time in eventtime from and to
addEventFrom.addEventListener("input", (e) => {
addEventFrom.value = addEventFrom.value.replace(/[^0-9:]/g, "");
if (addEventFrom.value.length === 2) {
addEventFrom.value += ":";
}
if (addEventFrom.value.length > 5) {
addEventFrom.value = addEventFrom.value.slice(0, 5);
}
});
addEventTo.addEventListener("input", (e) => {
addEventTo.value = addEventTo.value.replace(/[^0-9:]/g, "");
if (addEventTo.value.length === 2) {
addEventTo.value += ":";
}
if (addEventTo.value.length > 5) {
addEventTo.value = addEventTo.value.slice(0, 5);
}
});
//function to add event to eventsArr
addEventSubmit.addEventListener("click", () => {
const eventTitle = addEventTitle.value;
const eventTimeFrom = addEventFrom.value;
const eventTimeTo = addEventTo.value;
if (eventTitle === "" || eventTimeFrom === "" || eventTimeTo === "") {
alert("Por favor, preencha todos os campos");
return;
}
//check correct time format 24 hour
const timeFromArr = eventTimeFrom.split(":");
const timeToArr = eventTimeTo.split(":");
if (
timeFromArr.length !== 2 ||
timeToArr.length !== 2 ||
timeFromArr[0] > 23 ||
timeFromArr[1] > 59 ||
timeToArr[0] > 23 ||
timeToArr[1] > 59
) {
alert("Formato de tempo inválido");
return;
}
const timeFrom = convertTime(eventTimeFrom);
const timeTo = convertTime(eventTimeTo);
//check if event is already added
let eventExist = false;
eventsArr.forEach((event) => {
if (
event.day === activeDay &&
event.month === month + 1 &&
event.year === year
) {
event.events.forEach((event) => {
if (event.title === eventTitle) {
eventExist = true;
}
});
}
});
if (eventExist) {
alert("Event already added");
return;
}
const newEvent = {
title: eventTitle,
time: timeFrom + " - " + timeTo,
};
console.log(newEvent);
console.log(activeDay);
let eventAdded = false;
if (eventsArr.length > 0) {
eventsArr.forEach((item) => {
if (
item.day === activeDay &&
item.month === month + 1 &&
item.year === year
) {
item.events.push(newEvent);
eventAdded = true;
}
});
}
if (!eventAdded) {
eventsArr.push({
day: activeDay,
month: month + 1,
year: year,
events: [newEvent],
});
}
console.log(eventsArr);
addEventWrapper.classList.remove("active");
addEventTitle.value = "";
addEventFrom.value = "";
addEventTo.value = "";
updateEvents(activeDay);
//select active day and add event class if not added
const activeDayEl = document.querySelector(".day.active");
if (!activeDayEl.classList.contains("event")) {
activeDayEl.classList.add("event");
}
});
//function to delete event when clicked on event
eventsContainer.addEventListener("click", (e) => {
if (e.target.classList.contains("event")) {
if (confirm("Concluir este evento irá apagá-lo. Tem certeza que deseja fazer isto?")) {
const eventTitle = e.target.children[0].children[1].innerHTML;
eventsArr.forEach((event) => {
if (
event.day === activeDay &&
event.month === month + 1 &&
event.year === year
) {
event.events.forEach((item, index) => {
if (item.title === eventTitle) {
event.events.splice(index, 1);
}
});
//if no events left in a day then remove that day from eventsArr
if (event.events.length === 0) {
eventsArr.splice(eventsArr.indexOf(event), 1);
//remove event class from day
const activeDayEl = document.querySelector(".day.active");
if (activeDayEl.classList.contains("event")) {
activeDayEl.classList.remove("event");
}
}
}
});
updateEvents(activeDay);
}
}
});
//function to save events in local storage
function saveEvents() {
localStorage.setItem(usuario.nome+"-events", JSON.stringify(eventsArr));
}
//function to get events from local storage
function getEvents() {
//check if events are already saved in local storage then return event else nothing
if (localStorage.getItem(usuario.nome+"-events") === null) {
return;
}
eventsArr.push(...JSON.parse(localStorage.getItem(usuario.nome+"-events")));
}
function convertTime(time) {
//convert time to 24 hour format
let timeArr = time.split(":");
let timeHour = timeArr[0];
let timeMin = timeArr[1];
let timeFormat = timeHour >= 12 ? "PM" : "AM";
timeHour = timeHour % 12 || 12;
time = timeHour + ":" + timeMin + " " + timeFormat;
return time;
}
if(usuario.tipo=="admin"){
document.getElementById('content').setAttribute('style','margin-right:3em');
document.getElementById('admOnly').setAttribute('class','container d-block');
let div=document.getElementById('painel');
let usuarios=localStorage.getItem('userlist').replace(/((null,)|(,null,))/g,'').split(',')
usuarios.forEach((usuario)=>{
let section = document.createElement('button');
section.setAttribute('class',"accordion");
section.setAttribute('onclick',"togglePanel('panel-"+usuario+"')");
section.innerHTML='<h1>'+usuario+'</h1>';
let details = document.createElement('div');
details.setAttribute('class',"panel");
details.setAttribute('id',"panel-"+usuario);
JSON.parse(localStorage.getItem(usuario+"-events")).forEach((dia)=>{
let hr = document.createElement('hr');
hr.setAttribute('style','margin: 1em 0; padding:0')
details.appendChild(hr);
let h3=document.createElement('h3');
h3.innerHTML=dia.day+'/'+dia.month+'/'+dia.year;
details.appendChild(h3);
dia.events.forEach((evento)=>{
let h2=document.createElement('h2');
h2.innerHTML = evento.title;
let h4 =document.createElement('h4');
var duracao = evento.time;
h4.innerHTML='Das '+evento.time.replaceAll(/ AM|PM/g,'').replace('-','até as');
details.appendChild(h2);
details.appendChild(h4);
})
})
section.appendChild(details);
div.appendChild(section);
});
}
function togglePanel(nome){
var panel = document.getElementById(nome);
(panel.style.display === "block")? panel.style.display = "none": panel.style.display = "block";
}