-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
264 lines (200 loc) · 8.63 KB
/
Copy pathscript.js
File metadata and controls
264 lines (200 loc) · 8.63 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
document.addEventListener('DOMContentLoaded', function () {
const calendarEl = document.getElementById('calendar');
const myModal = new bootstrap.Modal(document.getElementById('form'));
const dangerAlert = document.getElementById('danger-alert');
const close = document.querySelector('.btn-close');
const myEvents = JSON.parse(localStorage.getItem('events')) || [
{
id: uuidv4(),
title: `Edit Me`,
start: '2023-04-11',
backgroundColor: 'red',
allDay: false,
editable: false,
},
{
id: uuidv4(),
title: `Delete me`,
start: '2023-04-17',
end: '2023-04-21',
allDay: false,
editable: false,
},
];
const calendar = new FullCalendar.Calendar(calendarEl, {
customButtons: {
customButton: {
text: 'Add Event',
click: function () {
myModal.show();
const modalTitle = document.getElementById('modal-title');
const submitButton = document.getElementById('submit-button');
modalTitle.innerHTML = 'Add Event'
submitButton.innerHTML = 'Add Event'
submitButton.classList.remove('btn-primary');
submitButton.classList.add('btn-success');
close.addEventListener('click', () => {
myModal.hide()
})
}
}
},
header: {
center: 'customButton', // add your custom button here
right: 'today, prev,next '
},
plugins: ['dayGrid', 'interaction'],
allDay: false,
editable: true,
selectable: true,
unselectAuto: false,
displayEventTime: false,
events: myEvents,
eventRender: function(info) {
info.el.addEventListener('contextmenu', function(e) {
e.preventDefault();
let existingMenu = document.querySelector('.context-menu');
existingMenu && existingMenu.remove();
let menu = document.createElement('div');
menu.className = 'context-menu';
menu.innerHTML = `<ul>
<li><i class="fas fa-edit"></i>Edit</li>
<li><i class="fas fa-trash-alt"></i>Delete</li>
</ul>`;
const eventIndex = myEvents.findIndex(event => event.id === info.event.id);
document.body.appendChild(menu);
menu.style.top = e.pageY + 'px';
menu.style.left = e.pageX + 'px';
// Edit context menu
menu.querySelector('li:first-child').addEventListener('click', function() {
menu.remove();
const editModal = new bootstrap.Modal(document.getElementById('form'));
const modalTitle = document.getElementById('modal-title');
const titleInput = document.getElementById('event-title');
const startDateInput = document.getElementById('start-date');
const endDateInput = document.getElementById('end-date');
const colorInput = document.getElementById('event-color');
const submitButton = document.getElementById('submit-button');
const cancelButton = document.getElementById('cancel-button');
modalTitle.innerHTML = 'Edit Event';
titleInput.value = info.event.title;
startDateInput.value = moment(info.event.start).format('YYYY-MM-DD');
endDateInput.value = moment(info.event.end, 'YYYY-MM-DD').subtract(1, 'day').format('YYYY-MM-DD');
colorInput.value = info.event.backgroundColor;
submitButton.innerHTML = 'Save Changes';
editModal.show();
submitButton.classList.remove('btn-success')
submitButton.classList.add('btn-primary')
// Edit button
submitButton.addEventListener('click', function() {
const updatedEvents = {
id: info.event.id,
title: titleInput.value,
start: startDateInput.value,
end: moment(endDateInput.value, 'YYYY-MM-DD').add(1, 'day').format('YYYY-MM-DD'),
backgroundColor: colorInput.value
}
if ( updatedEvents.end <= updatedEvents.start) { // add if statement to check end date
dangerAlert.style.display = 'block';
return;
}
const eventIndex = myEvents.findIndex(event => event.id === updatedEvents.id);
myEvents.splice(eventIndex, 1, updatedEvents);
localStorage.setItem('events', JSON.stringify(myEvents));
// Update the event in the calendar
const calendarEvent = calendar.getEventById(info.event.id);
calendarEvent.setProp('title', updatedEvents.title);
calendarEvent.setStart(updatedEvents.start);
calendarEvent.setEnd(updatedEvents.end);
calendarEvent.setProp('backgroundColor', updatedEvents.backgroundColor);
editModal.hide();
})
});
// Delete menu
menu.querySelector('li:last-child').addEventListener('click', function() {
const deleteModal = new bootstrap.Modal(document.getElementById('delete-modal'));
const modalBody = document.getElementById('delete-modal-body');
const cancelModal = document.getElementById('cancel-button');
modalBody.innerHTML = `Are you sure you want to delete <b>"${info.event.title}"</b>`
deleteModal.show();
const deleteButton = document.getElementById('delete-button');
deleteButton.addEventListener('click', function () {
myEvents.splice(eventIndex, 1);
localStorage.setItem('events', JSON.stringify(myEvents));
calendar.getEventById(info.event.id).remove();
deleteModal.hide();
menu.remove();
});
cancelModal.addEventListener('click', function () {
deleteModal.hide();
})
});
document.addEventListener('click', function() {
menu.remove();
});
});
},
eventDrop: function(info) {
let myEvents = JSON.parse(localStorage.getItem('events')) || [];
const eventIndex = myEvents.findIndex(event => event.id === info.event.id);
const updatedEvent = {
...myEvents[eventIndex],
id: info.event.id,
title: info.event.title,
start: moment(info.event.start).format('YYYY-MM-DD'),
end: moment(info.event.end).format('YYYY-MM-DD'),
backgroundColor: info.event.backgroundColor
};
myEvents.splice(eventIndex, 1, updatedEvent); // Replace old event data with updated event data
localStorage.setItem('events', JSON.stringify(myEvents));
console.log(updatedEvent);
}
});
calendar.on('select', function(info) {
const startDateInput = document.getElementById('start-date');
const endDateInput = document.getElementById('end-date');
startDateInput.value = info.startStr;
const endDate = moment(info.endStr, 'YYYY-MM-DD').subtract(1, 'day').format('YYYY-MM-DD');
endDateInput.value = endDate;
if(startDateInput.value === endDate) {
endDateInput.value = '';
}
});
calendar.render();
const form = document.querySelector('form');
form.addEventListener('submit', function(event) {
event.preventDefault(); // prevent default form submission
// retrieve the form input values
const title = document.querySelector('#event-title').value;
const startDate = document.querySelector('#start-date').value;
const endDate = document.querySelector('#end-date').value;
const color = document.querySelector('#event-color').value;
const endDateFormatted = moment(endDate, 'YYYY-MM-DD').add(1, 'day').format('YYYY-MM-DD');
const eventId = uuidv4();
console.log(eventId);
if (endDateFormatted <= startDate) { // add if statement to check end date
dangerAlert.style.display = 'block';
return;
}
const newEvent = {
id: eventId,
title: title,
start: startDate,
end: endDateFormatted,
allDay: false,
backgroundColor: color
};
// add the new event to the myEvents array
myEvents.push(newEvent);
// render the new event on the calendar
calendar.addEvent(newEvent);
// save events to local storage
localStorage.setItem('events', JSON.stringify(myEvents));
myModal.hide();
form.reset();
});
myModal._element.addEventListener('hide.bs.modal', function () {
dangerAlert.style.display = 'none';
form.reset();
});
});