-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
213 lines (182 loc) · 6.5 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
const isLeapYear = (year) => {
return (
(year % 4 === 0 && year % 100 !== 0 && year % 400 !== 0) ||
(year % 100 === 0 && year % 400 === 0)
);
};
const getFebDays = (year) => {
return isLeapYear(year) ? 29 : 28;
};
let calendar = document.querySelector('.calendar');
const month_names = [
'Janeiro',
'Feveiro',
'Março',
'Abril',
'Maio',
'Junho',
'Julho',
'Agosto',
'Setembro',
'Outubro',
'Novembro',
'Dezembro',
];
let month_picker = document.querySelector('#month-picker');
const dayTextFormate = document.querySelector('.day-text-formate');
const timeFormate = document.querySelector('.time-formate');
const dateFormate = document.querySelector('.date-formate');
month_picker.onclick = () => {
month_list.classList.remove('hideonce');
month_list.classList.remove('hide');
month_list.classList.add('show');
dayTextFormate.classList.remove('showtime');
dayTextFormate.classList.add('hidetime');
timeFormate.classList.remove('showtime');
timeFormate.classList.add('hideTime');
dateFormate.classList.remove('showtime');
dateFormate.classList.add('hideTime');
};
const generateCalendar = (month, year) => {
let calendar_days = document.querySelector('.calendar-days');
calendar_days.innerHTML = '';
let calendar_header_year = document.querySelector('#year');
let days_of_month = [
31,
getFebDays(year),
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
];
let currentDate = new Date();
month_picker.innerHTML = month_names[month];
calendar_header_year.innerHTML = year;
let first_day = new Date(year, month);
for (let i = 0; i <= days_of_month[month] + first_day.getDay() - 1; i++) {
let day = document.createElement('div');
if (i >= first_day.getDay()) {
day.innerHTML = i - first_day.getDay() + 1;
if (i - first_day.getDay() + 1 === currentDate.getDate() &&
year === currentDate.getFullYear() &&
month === currentDate.getMonth()
) {
day.classList.add('current-date');
}
}
calendar_days.appendChild(day);
}
};
let month_list = calendar.querySelector('.month-list');
month_names.forEach((e, index) => {
let month = document.createElement('div');
month.innerHTML = `<div>${e}</div>`;
month_list.append(month);
month.onclick = () => {
currentMonth.value = index;
generateCalendar(currentMonth.value, currentYear.value);
month_list.classList.replace('show', 'hide');
dayTextFormate.classList.remove('hideTime');
dayTextFormate.classList.add('showtime');
timeFormate.classList.remove('hideTime');
timeFormate.classList.add('showtime');
dateFormate.classList.remove('hideTime');
dateFormate.classList.add('showtime');
};
});
(function() {
month_list.classList.add('hideonce');
})();
document.querySelector('#pre-year').onclick = () => {
--currentYear.value;
const url = 'https://brasilapi.com.br/api/feriados/v1/' + currentYear.value;
// Using fetch to make the request
fetch(url)
.then(response => {
// Checking if the request was successful
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json(); // Converting the response to JSON
})
.then(data => {
// Assuming 'data' is an array of objects with 'date', 'name', and 'type'
const eventsList = document.getElementById('dynamic-events-list');
data.forEach(event => {
const listItem = document.createElement('li');
listItem.textContent = `${event.name} - ${event.date}`; // Adjusted to match the API response structure
eventsList.appendChild(listItem);
});
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
generateCalendar(currentMonth.value, currentYear.value);
};
document.querySelector('#next-year').onclick = () => {
++currentYear.value;
const url = 'https://brasilapi.com.br/api/feriados/v1/' + currentYear.value;
// Using fetch to make the request
fetch(url)
.then(response => {
// Checking if the request was successful
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json(); // Converting the response to JSON
})
.then(data => {
// Assuming 'data' is an array of objects with 'date', 'name', and 'type'
const eventsList = document.getElementById('dynamic-events-list');
data.forEach(event => {
const listItem = document.createElement('li');
listItem.textContent = `${event.name} - ${event.date}`; // Adjusted to match the API response structure
eventsList.appendChild(listItem);
});
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
generateCalendar(currentMonth.value, currentYear.value);
};
let currentDate = new Date();
let currentMonth = { value: currentDate.getMonth() };
let currentYear = { value: currentDate.getFullYear() };
generateCalendar(currentMonth.value, currentYear.value);
const todayShowTime = document.querySelector('.time-formate');
const todayShowDate = document.querySelector('.date-formate');
const currshowDate = new Date();
const showCurrentDateOption = {
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long',
};
const currentDateFormate = new Intl.DateTimeFormat(
'pt-BR',
showCurrentDateOption
).format(currshowDate);
todayShowDate.textContent = currentDateFormate;
setInterval(() => {
const timer = new Date();
const option = {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
};
const formateTimer = new Intl.DateTimeFormat('pt-BR', option).format(timer);
let time = `${`${timer.getHours()}`.padStart(
2,
'0'
)}:${`${timer.getMinutes()}`.padStart(
2,
'0'
)}: ${`${timer.getSeconds()}`.padStart(2, '0')}`;
todayShowTime.textContent = formateTimer;
}, 1000);