-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
485 lines (413 loc) · 11.9 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
const calendar = document.querySelector('.calendar'),
data = 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"),
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'
]
//set a empty array
let eventsArr = []
//add events on local Storage
const saveEvents = () => {
localStorage.setItem('events', JSON.stringify(eventsArr))
}
const getEvents = () => {
if (localStorage.getItem('events' === null)) {
return
}
//eventsArr.push(...JSON.parse(localStorage.getItem('events')))
}
//active day events and date at top
const getActiveDay = (date) => {
const day = new Date(year, month, date)
const dayName = day.toString().split(" ")[0]
eventDay.innerHTML = dayName
eventDate.innerHTML = date + " " + months[month] + " " + year
}
//function to show events of that day
const updateEvents = (date) => {
let events = ''
eventsArr.forEach((event) => {
//get events of active day only
if (
date === event.day &&
month + 1 === event.month &&
year === event.year
) {
//show event on document
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>No Events</h3>
</div>
`
}
//Save events when new one added
saveEvents()
eventsContainer.innerHTML = events
}
//then call get
getEvents()
//add days after render
const addListener = () => {
const days = document.querySelectorAll('.day')
days.forEach((day) => {
day.addEventListener('click', (e) => {
//set current day as active day
activeDay = Number(e.target.innerHTML)
//call active day after click
getActiveDay(e.target.innerHTML)
updateEvents(Number(e.target.innerHTML))
//remove active from already active day
days.forEach((day) => {
day.classList.remove('active')
})
//if prev month day clicked goto prev month and add active
if(e.target.classList.contains("prev-date")) {
prevMonth()
setTimeout(() => {
//select all days of that month
const days = document.querySelectorAll('.day')
//after going to prev moth, add active to clicked
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()
setTimeout(() => {
//select all days of that month
const days = document.querySelectorAll('.day')
//after going to prev moth, add active to clicked
days.forEach((day) => {
if(!day.classList.contains('next-date') &&
day.innerHTML === e.target.innerHTML
) {
day.classList.add('active')
}
})
}, 100)
} else {
//remaing current month days
e.target.classList.add('active')
}
})
})
}
const 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 now
data.innerHTML = months[month] + "/" + year
//adding days
let days = ''
for(let x = day; x > 0; x--){
days += `<div class="day prev-date">${prevDays - x + 1}</div>`
}
//current month days
for (let i = 1; i <= lastDate; i++) {
//check if event is present on current day
let event = false
eventsArr.forEach((eventObj) => {
if (
eventObj.day === i &&
eventObj.month === month + 1 &&
eventObj.year === year
) {
//if event found
event = true
}
})
//if days is today, add class today
if(i === new Date().getDate()
&& year === new Date().getFullYear()
&& month === new Date().getMonth())
{
activeDay = i
getActiveDay(i)
updateEvents(i)
//if event found also add event class
//add active on today at startup
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>`
}
}
}
//next month days
for(let j = 1; j <= nextDays; j++) {
days += `<div class="day next-date">${j}</div>`
}
daysContainer.innerHTML = days
//add listener after calendar initialized
addListener()
}
initCalendar()
//prev month
const prevMonth = () => {
month--
if(month < 0) {
month = 11
year--
}
initCalendar()
}
const nextMonth = () => {
month++
if(month > 11) {
month = 0
year++
}
initCalendar()
}
//add eventlistener on prev and next
prev.addEventListener('click', prevMonth)
next.addEventListener('click', nextMonth)
todayBtn.addEventListener('click', () => {
today = new Date()
month = today.getMonth()
year = today.getFullYear()
initCalendar()
})
dateInput.addEventListener('keyup', (e) => {
//regex to remove slashes
dateInput.value = dateInput.value.replace(/[^0-9/]/g, '')
if(dateInput.value.length === 2) {
//add a slash after two numbers
dateInput.value += "/"
}
if(dateInput.value.length > 7) {
//block if exist more than 7 characters
dateInput.value = dateInput.value.slice(0, 7)
}
//if backspace is pressed
if(e.inputType === "deleteContentBackward") {
if(dateInput.value.length === 3) {
dateInput.value = dateInput.value.slice(0, 2)
}
}
})
const gotoDate = () => {
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
}
//if invalid
alert('invalid date')
}
}
const convertTime = (time) => {
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
}
const addEventBtn = document.querySelector('.add-event'),
addEventContainer = 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')
gotoBtn.addEventListener('click', gotoDate)
addEventBtn.addEventListener('click', () => {
addEventContainer.classList.toggle("active")
})
addEventCloseBtn.addEventListener('click', () => {
addEventContainer.classList.remove("active")
})
document.addEventListener('click', (e) => {
if(e.target !== addEventBtn && !addEventContainer.contains(e.target)) {
//if click outside (modal like)
addEventContainer.classList.remove("active")
}
})
//allow only 50 chars in title
addEventTitle.addEventListener('input', (e) => {
addEventTitle.value = addEventTitle.value.slice(0, 50)
})
//--- FROM TIME ---
//format time in from and to
addEventFrom.addEventListener('input', (e) => {
//remove anything else numbers
//addEventFrom.value = addEventFrom.value.replace(/[^0-9:]/g, "")
//if two numbers entered, auto add :
if (addEventFrom.value.length === 2) {
addEventFrom.value += ":"
}
//dont allow user enter mroe than 5 chars
if (addEventFrom.value.length > 5) {
addEventFrom.value = addEventFrom.value.slice(0, 5)
}
})
//--- TO TIME ---
//format time in from and to
addEventTo.addEventListener('input', (e) => {
//remove anything else numbers
//addEventTo.value = addEventTo.value.replace(/[^0-9:]/g, "")
//if two numbers entered, auto add :
if (addEventTo.value.length === 2) {
addEventTo.value += ":"
}
//dont allow user enter mroe than 5 chars
if (addEventTo.value.length > 5) {
addEventTo.value = addEventTo.value.slice(0, 5)
}
})
//function to add events
addEventSubmit.addEventListener('click', () => {
const eventTitle = addEventTitle.value
const eventTimeFrom = addEventFrom.value
const eventTimeTo = addEventTo.value
//validations
if(eventTitle === "" || eventTimeFrom === '' || eventTimeTo === '') {
alert('Please fill all the fields')
}
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('Invalid Time')
}
const timeFrom = convertTime(eventTimeFrom)
const timeTo = convertTime(eventTimeTo)
const newEvent = {
title : eventTitle,
time: timeFrom + " - " + timeTo,
}
let eventAdded = false
//check if eventsArr not empty
if (eventsArr.length > 0) {
//check if current day has already an event
eventsArr.forEach((item) => {
if(
item.day === activeDay &&
item.month === month + 1 &&
item.year === year
) {
item.events.push(newEvent)
eventAdded = true
}
})
}
//if event array empty or current day has no event create a new
if (!eventAdded) {
eventsArr.push({
day: activeDay,
month: month + 1,
year: year,
events: [newEvent]
})
}
//remove active from add event form
addEventContainer.classList.remove('active')
//clear the fields
addEventTitle.value = ""
addEventFrom.value = ""
addEventTo.value = ""
//show current added event
updateEvents(activeDay)
//add event class to newly event
const activeDayElem = document.querySelector('.day.active')
if (!activeDayElem.classList.contains('event')) {
activeDayElem.classList.add('event')
}
})
//remove events on click
eventsContainer.addEventListener('click', (e) => {
if (e.target.classList.contains('event')) {
const eventTitle = e.target.children[0].children[1].innerHTML
//get title of event and search in the array to delete
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 not event remaing on day, remove complete day
if (event.events.length === 0) {
eventsArr.splice(eventsArr.indexOf(event), 1)
//after remove complete day also remove active class of that day
const activeDayElem = document.querySelector('.day.active')
if (activeDayElem.classList.contains('event')) {
activeDayElem.classList.remove('event')
}
}
}
})
//after remove, update
updateEvents(activeDay)
}
})