-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
292 lines (282 loc) · 8.24 KB
/
script.js
File metadata and controls
292 lines (282 loc) · 8.24 KB
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
// Elements
const calendar = document.querySelector('.calendar');
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
const btns = document.querySelectorAll('.title span');
const year = document.querySelector('.title p');
// Initialize events object
let currentYear = new Date().getFullYear();
let events;
function updateEventsList() {
year.innerHTML = currentYear;
if (localStorage.getItem(`${currentYear}events`)) {
events = JSON.parse(localStorage.getItem(`${currentYear}events`));
} else {
events = {
[currentYear]: {
January: {},
February: {},
March: {},
April: {},
May: {},
June: {},
July: {},
August: {},
September: {},
October: {},
November: {},
December: {}
}
};
}
}
let day;
let month;
let color = '#ff3419';
// Listeners
window.addEventListener('load', displayMonths);
btns.forEach(btn => btn.addEventListener('click', changeYear));
document.body.addEventListener('click', e => {
if (!e.target.className.match('number active') && !e.target.className.match('btn')) {
const numbers = document.querySelectorAll('.number');
numbers.forEach(n => n.classList.remove('active'));
}
});
// Functions //
// Make calendar
function displayMonths() {
calendar.innerHTML = '';
months.forEach((month, i) => {
calendar.innerHTML += `
<div class="month">
<h1>${month}</h1>
<ul class="days">
<li class="day">sun</li>
<li class="day">mon</li>
<li class="day">tue</li>
<li class="day">wed</li>
<li class="day">thu</li>
<li class="day">fri</li>
<li class="day">sat</li>
</ul>
</div>
`;
const days = document.querySelectorAll('.days');
let day = 1;
let date = 0;
while (new Date(`${month} ${day} ${currentYear}`).getDay() > date) {
const li = document.createElement('li');
li.innerHTML = '';
days[i].appendChild(li);
date++;
}
while (new Date(`${month} ${day} ${currentYear}`).getMonth() === new Date(`${month} 1 ${currentYear}`).getMonth()) {
const li = document.createElement('li');
if (
new Date(`${month} ${day} ${currentYear}`).getMonth() === new Date().getMonth() &&
new Date(`${month} ${day} ${currentYear}`).getDate() === new Date().getDate() &&
new Date(`${month} ${day} ${currentYear}`).getFullYear() === new Date().getFullYear()
) {
li.classList.add('today');
}
li.classList.add('number');
li.innerHTML = day;
days[i].appendChild(li);
day++;
}
});
const numbers = document.querySelectorAll('.number');
numbers.forEach(n => n.addEventListener('click', () => selectDay(event, numbers)));
updateEventsList();
displayColoredEvents();
}
// Check for Events
function selectDay(e, numbers) {
numbers.forEach(n => n.classList.remove('active'));
e.target.classList.add('active');
hasEvent(e.target);
}
let lastOne = document.createElement('div');
function hasEvent(number) {
day = number.innerHTML;
month = number.parentElement.parentElement.querySelector('h1').innerHTML;
displayEvent();
}
function addModal() {
const modal = document.createElement('div');
modal.classList.add('modal');
modal.innerHTML = `
<form>
<h2>Add Event</h2>
<input class="name" type="text" placeholder="Event name..." required>
<div class="hour">
<input type="number" min="1" max="12" required>
<span>:</span>
<input type="number" min="0" max="59" required>
<select>
<option value="am">am</option>
<option value="pm">pm</option>
</select>
</div>
<div class="label-colors">
<div data-color="#ff3419"></div>
<div data-color="#ff9f1a"></div>
<div data-color="#fff200"></div>
<div data-color="#32ff7e"></div>
<div data-color="#18dcff"></div>
<div data-color="#c56cf0"></div>
</div>
<input class="submit" type="submit" value="Add">
</form>
`;
document.body.appendChild(modal);
modal.addEventListener('click', e => {
if (e.target.className === 'modal') {
modal.remove();
}
});
modal.querySelector('form')[0].focus();
modal.querySelector('form').addEventListener('submit', addEvent);
modal.querySelectorAll('.label-colors div').forEach(color => color.addEventListener('click', selectColor));
}
function addEvent(e) {
e.preventDefault();
const form = document.querySelector('form');
const name = form[0].value;
const hour = form[1].value;
const mins = form[2].value;
let min;
if (mins < 10 && mins.length < 2) {
min = `0${mins}`;
} else if (mins.length > 2) {
min = '00';
} else {
min = mins;
}
const meridiem = form[3].value;
const id = new Date()
.getTime()
.toString()
.substring(0, 11);
if (!events[currentYear][month][day]) {
events[currentYear][month][day] = [];
events[currentYear][month][day].push({
name,
time: `${hour}:${min}${meridiem}`,
color,
id
});
localStorage.setItem(`${currentYear}events`, JSON.stringify(events));
} else {
events[currentYear][month][day].push({
name,
time: `${hour}:${min}${meridiem}`,
color,
id
});
localStorage.setItem(`${currentYear}events`, JSON.stringify(events));
}
form.parentElement.remove();
displayEvent();
setEventColor();
displayColoredEvents();
}
// DELETE EVENT
function deleteEvent(e) {
if (e.target.className === 'delete') {
const { id } = e.target.parentElement.dataset;
const test = events[currentYear][month][day].filter(ev => ev.id != id);
events[currentYear][month][day] = test;
localStorage.setItem(`${currentYear}events`, JSON.stringify(events));
const numbers = document.querySelectorAll('.number');
numbers.forEach(number => {
if (number.className.match('active')) {
if (events[currentYear][month][+number.innerHTML].length == 0) {
number.style.borderColor = 'transparent';
}
}
});
displayEvent();
}
}
function selectColor(e) {
const label = document.querySelector('.label-colors');
color = e.target.dataset.color;
label.style.backgroundColor = color;
}
function setEventColor(e) {
const numbers = document.querySelectorAll('.number');
numbers.forEach(number => {
if (number.className.match('active')) {
number.style.borderColor = `${color}`;
}
});
}
function changeYear(e) {
if (e.target.innerHTML === '◂' && currentYear > 1975) {
currentYear--;
displayMonths();
displayColoredEvents();
} else {
currentYear++;
displayMonths();
displayColoredEvents();
}
}
// DISPLAY EVENTS
function displayEvent() {
if (events[currentYear][month][day] == null || events[currentYear][month][day].length == 0) {
const ui = document.createElement('div');
ui.classList.add('ui');
lastOne.remove();
ui.innerHTML = `<h2>No events</h2>`;
ui.innerHTML += `<button class="btn">+</button>`;
lastOne = ui;
document.body.appendChild(ui);
document.querySelector('.btn').addEventListener('click', addModal);
} else {
const ui = document.createElement('div');
ui.classList.add('ui');
lastOne.remove();
const dayEvents = events[currentYear][month][day];
dayEvents.forEach(e => {
ui.innerHTML += `
<ul>
<li style="border-color: ${e.color};" data-id="${e.id}">
<p>${e.name}</p>
<span>${e.time}</span>
<button class="delete">X</button></li>
</ul>
`;
});
ui.innerHTML += `<button class="btn">+</button>`;
lastOne = ui;
document.body.appendChild(ui);
document.querySelector('.btn').addEventListener('click', addModal);
ui.addEventListener('click', deleteEvent);
}
}
function displayColoredEvents() {
const numbers = document.querySelectorAll('.number');
numbers.forEach(number => {
const eventMonth = number.parentElement.parentElement.querySelector('h1').innerHTML;
const eventDay = number.innerHTML;
if (events[currentYear][eventMonth][+eventDay]) {
if (events[currentYear][eventMonth][+eventDay][0]) {
number.style.borderColor = `${events[currentYear][eventMonth][+eventDay][0].color}`;
}
}
});
}