-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.html
More file actions
1728 lines (1441 loc) · 69.2 KB
/
Copy pathcalendar.html
File metadata and controls
1728 lines (1441 loc) · 69.2 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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flat Calendar</title>
<style>
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--danger-color: #e74c3c;
--background-color: #f5f5f5;
--card-color: #ffffff;
--text-color: #333333;
--border-color: #e1e1e1;
--event-colors: #3498db, #2ecc71, #e74c3c, #f39c12, #9b59b6, #1abc9c;
--time-slot-height: 15px;
--hour-label-width: 80px;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background-color: var(--background-color);
color: var(--text-color);
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.calendar-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.calendar-title {
font-size: 28px;
font-weight: 600;
}
.calendar-nav {
display: flex;
gap: 10px;
}
button {
background-color: var(--primary-color);
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.2s;
display: flex;
align-items: center;
gap: 5px;
}
button:hover {
background-color: #2980b9;
}
button.danger {
background-color: var(--danger-color);
}
button.danger:hover {
background-color: #c0392b;
}
.calendar-grid {
display: grid;
grid-template-columns: var(--hour-label-width) repeat(7, 1fr);
grid-auto-rows: var(--time-slot-height);
gap: 1px;
margin-bottom: 30px;
background-color: var(--border-color);
border: 1px solid var(--border-color);
}
.calendar-day-header {
text-align: center;
font-weight: 600;
padding: 10px;
background-color: var(--primary-color);
color: white;
border-radius: 0;
grid-column: auto / span 1;
}
.header-spacer {
background-color: var(--primary-color);
grid-column: 1 / 2;
color: white;
display: flex;
align-items: center;
justify-content: center;
font-weight: 600;
font-size: 14px;
}
.hour-label {
grid-column: 1 / 2;
display: flex;
align-items: center;
justify-content: flex-end;
padding-right: 10px;
background-color: var(--card-color);
font-size: 12px;
color: var(--text-color);
height: calc(var(--time-slot-height) * 4);
border-bottom: 1px solid var(--border-color);
position: relative;
}
.minute-marker {
position: absolute;
right: 2px;
font-size: 9px;
color: #999;
}
.minute-marker-15 {
top: 25%;
}
.minute-marker-30 {
top: 50%;
}
.minute-marker-45 {
top: 75%;
}
.time-slot {
background-color: var(--card-color);
height: var(--time-slot-height);
min-height: var(--time-slot-height);
cursor: pointer;
transition: background-color 0.2s;
position: relative;
border-bottom: 1px dashed var(--border-color);
border-right: 1px solid var(--border-color);
}
.time-slot.hour-start {
border-bottom: 1px solid var(--border-color);
}
.time-slot:hover {
background-color: rgba(52, 152, 219, 0.1);
}
.time-slot.today {
background-color: rgba(52, 152, 219, 0.05);
}
.day-label {
padding: 5px;
text-align: center;
font-weight: 600;
font-size: 14px;
background-color: rgba(52, 152, 219, 0.1);
margin-bottom: 5px;
border-radius: 4px;
}
.day-label.today {
background-color: var(--primary-color);
color: white;
}
.calendar-day {
min-height: 200px;
background-color: var(--card-color);
border-radius: 4px;
padding: 8px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
position: relative;
display: none; /* Hide this in the time grid view */
}
.calendar-day:hover {
transform: translateY(-2px);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.calendar-day.inactive {
opacity: 0.5;
}
.day-number {
font-weight: 600;
margin-bottom: 5px;
}
.today .day-number {
background-color: var(--primary-color);
color: white;
width: 25px;
height: 25px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
}
.event {
padding: 5px;
margin-bottom: 5px;
border-radius: 3px;
font-size: 12px;
color: white;
cursor: pointer;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
transition: all 0.2s;
position: absolute;
left: 2px;
right: 2px;
z-index: 10;
}
.event:hover {
transform: translateX(2px);
z-index: 20;
box-shadow: 0 2px 10px rgba(0,0,0,0.2);
}
.dragging {
opacity: 0.7;
pointer-events: none;
}
.resize-handle-top, .resize-handle-bottom {
position: absolute;
left: 0;
right: 0;
height: 8px;
cursor: ns-resize;
}
.resize-handle-top {
top: 0;
border-top: 2px solid rgba(255, 255, 255, 0.7);
}
.resize-handle-bottom {
bottom: 0;
border-bottom: 2px solid rgba(255, 255, 255, 0.7);
}
.resize-handle-top:hover, .resize-handle-bottom:hover {
background-color: rgba(255, 255, 255, 0.3);
}
.event.resizing {
z-index: 30;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.5);
}
.event .event-content {
height: 100%;
width: 100%;
cursor: move;
display: flex;
align-items: center;
padding: 0 5px;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
justify-content: center;
align-items: center;
z-index: 1000;
}
.modal-content {
background-color: var(--card-color);
padding: 20px;
border-radius: 8px;
width: 90%;
max-width: 500px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.modal-title {
font-size: 20px;
font-weight: 600;
}
.close-modal {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
color: var(--text-color);
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: 500;
}
input, select, textarea {
width: 100%;
padding: 10px;
border: 1px solid var(--border-color);
border-radius: 4px;
font-size: 14px;
}
.color-selector {
display: flex;
gap: 10px;
margin-top: 5px;
}
.color-option {
width: 25px;
height: 25px;
border-radius: 50%;
cursor: pointer;
transition: transform 0.2s;
}
.color-option:hover, .color-option.selected {
transform: scale(1.2);
box-shadow: 0 0 0 2px white, 0 0 0 4px var(--primary-color);
}
.modal-footer {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 20px;
}
.expanded-event {
margin-top: 10px;
padding: 8px;
border-radius: 4px;
background-color: rgba(0, 0, 0, 0.05);
}
.event-actions {
display: flex;
justify-content: flex-end;
gap: 5px;
margin-top: 8px;
}
.event-actions button {
padding: 4px 8px;
font-size: 12px;
}
@media (max-width: 768px) {
.calendar-grid {
gap: 5px;
}
.calendar-day {
min-height: 80px;
padding: 5px;
}
.calendar-day-header {
padding: 5px;
font-size: 12px;
}
.day-number {
font-size: 12px;
}
.event {
padding: 3px;
font-size: 10px;
}
}
@media (max-width: 480px) {
.calendar-grid {
display: flex;
flex-direction: column;
}
.calendar-day-header {
display: none;
}
.calendar-day {
display: flex;
flex-direction: column;
min-height: auto;
margin-bottom: 10px;
}
.day-number::before {
content: attr(data-day) " ";
font-weight: normal;
}
}
.time-slot.quarter {
border-bottom: 1px dotted var(--border-color);
}
.time-slot.half {
border-bottom: 1px dashed var(--border-color);
}
.time-slot.three-quarter {
border-bottom: 1px dotted var(--border-color);
}
.time-slot.hour-start {
border-bottom: 1px solid var(--border-color);
height: var(--time-slot-height);
}
</style>
</head>
<body>
<div class="container">
<div class="calendar-header">
<h1 class="calendar-title" id="calendar-title">Calendar</h1>
<div class="calendar-nav">
<button id="prev-month">Previous Week</button>
<button id="current-month">This Week</button>
<button id="next-month">Next Week</button>
<button id="reload-events" style="background-color: #2ecc71;">Reload Events</button>
</div>
</div>
<div class="calendar-grid" id="calendar-grid">
<!-- Calendar will be generated here by JavaScript -->
</div>
</div>
<!-- Event Modal -->
<div class="modal" id="event-modal">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title" id="modal-title">Add Event</h2>
<button class="close-modal" id="close-modal">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="event-title">Event Title</label>
<input type="text" id="event-title" placeholder="Enter event title">
</div>
<div class="form-group">
<label for="event-date">Date</label>
<input type="date" id="event-date">
</div>
<div class="form-group">
<label for="event-time">Time (optional)</label>
<input type="time" id="event-time">
</div>
<div class="form-group">
<label for="event-description">Description (optional)</label>
<textarea id="event-description" rows="3" placeholder="Enter event description"></textarea>
</div>
<div class="form-group">
<label>Color</label>
<div class="color-selector" id="color-selector">
<!-- Color options will be generated by JavaScript -->
</div>
</div>
</div>
<div class="modal-footer">
<button id="delete-event" class="danger" style="display: none;">Delete</button>
<button id="save-event">Save</button>
</div>
</div>
</div>
<!-- Event Details Modal -->
<div class="modal" id="event-details-modal">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title" id="details-modal-title">Event Details</h2>
<button class="close-modal" id="close-details-modal">×</button>
</div>
<div class="modal-body" id="event-details-content">
<!-- Event details will be displayed here -->
</div>
<div class="modal-footer">
<button id="edit-event" class="edit-button">Edit</button>
<button id="close-details-button">Close</button>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Store events in localStorage
let events = JSON.parse(localStorage.getItem('calendarEvents')) || [];
let currentDate = new Date();
let selectedDate = null;
let editingEventId = null;
const colorOptions = [
'#3498db', // Blue
'#2ecc71', // Green
'#e74c3c', // Red
'#f39c12', // Orange
'#9b59b6', // Purple
'#1abc9c' // Teal
];
let selectedColor = colorOptions[0];
// Working hours configuration
const workingHoursStart = 8; // 8:00 AM
const workingHoursEnd = 20; // 8:00 PM
// Drag and drop variables
let isDragging = false;
let isResizing = false;
let resizeDirection = null; // 'top' or 'bottom'
let draggedEvent = null;
let dragStartY = 0;
let dragStartX = 0;
let originalEventTime = null;
let originalEventElement = null;
let originalDay = null;
let originalEventRect = null;
// DOM elements
const calendarGrid = document.getElementById('calendar-grid');
const calendarTitle = document.getElementById('calendar-title');
const prevMonthBtn = document.getElementById('prev-month');
const nextMonthBtn = document.getElementById('next-month');
const currentMonthBtn = document.getElementById('current-month');
const reloadEventsBtn = document.getElementById('reload-events');
const eventModal = document.getElementById('event-modal');
const modalTitle = document.getElementById('modal-title');
const closeModalBtn = document.getElementById('close-modal');
const eventTitleInput = document.getElementById('event-title');
const eventDateInput = document.getElementById('event-date');
const eventTimeInput = document.getElementById('event-time');
const eventDescriptionInput = document.getElementById('event-description');
const saveEventBtn = document.getElementById('save-event');
const deleteEventBtn = document.getElementById('delete-event');
const colorSelector = document.getElementById('color-selector');
const eventDetailsModal = document.getElementById('event-details-modal');
const closeDetailsModalBtn = document.getElementById('close-details-modal');
const detailsModalTitle = document.getElementById('details-modal-title');
const eventDetailsContent = document.getElementById('event-details-content');
const editEventBtn = document.getElementById('edit-event');
const closeDetailsBtn = document.getElementById('close-details-button');
// Update button text for weekly navigation
prevMonthBtn.textContent = 'Previous Week';
nextMonthBtn.textContent = 'Next Week';
currentMonthBtn.textContent = 'This Week';
// Initialize the calendar
initCalendar();
// Generate color options
generateColorOptions();
// Event listeners
prevMonthBtn.addEventListener('click', () => {
// Navigate to previous week
currentDate.setDate(currentDate.getDate() - 7);
renderCalendar();
});
nextMonthBtn.addEventListener('click', () => {
// Navigate to next week
currentDate.setDate(currentDate.getDate() + 7);
renderCalendar();
});
currentMonthBtn.addEventListener('click', () => {
// Navigate to current week
currentDate = new Date();
renderCalendar();
});
reloadEventsBtn.addEventListener('click', forceReloadEvents);
closeModalBtn.addEventListener('click', closeEventModal);
closeDetailsModalBtn.addEventListener('click', closeEventDetailsModal);
closeDetailsBtn.addEventListener('click', closeEventDetailsModal);
saveEventBtn.addEventListener('click', saveEvent);
deleteEventBtn.addEventListener('click', deleteEvent);
editEventBtn.addEventListener('click', () => {
const eventId = editEventBtn.getAttribute('data-event-id');
openEditEventModal(eventId);
closeEventDetailsModal();
});
// Close modals when clicking outside
window.addEventListener('click', (e) => {
if (e.target === eventModal) {
closeEventModal();
}
if (e.target === eventDetailsModal) {
closeEventDetailsModal();
}
});
// Add a new listener for the end time input
const eventEndTimeInput = document.createElement('input');
eventEndTimeInput.type = 'time';
eventEndTimeInput.id = 'event-end-time';
const timeInputParent = eventTimeInput.parentNode;
// Clone the event time form group
const endTimeFormGroup = timeInputParent.cloneNode(true);
const endTimeLabel = endTimeFormGroup.querySelector('label');
const endTimeInput = endTimeFormGroup.querySelector('input');
endTimeLabel.textContent = 'End Time';
endTimeLabel.setAttribute('for', 'event-end-time');
endTimeInput.id = 'event-end-time';
// Insert the end time form group after the start time
timeInputParent.parentNode.insertBefore(endTimeFormGroup, timeInputParent.nextSibling);
// Correct the label for the start time (which was showing as optional)
const startTimeLabel = timeInputParent.querySelector('label');
startTimeLabel.textContent = 'Start Time';
// Get the end time input element
const eventEndTimeInput2 = document.getElementById('event-end-time');
// Initialize the calendar
function initCalendar() {
// Load events first
loadEventsFromStorage();
// Then render the calendar
renderCalendar();
}
// Load events from localStorage
function loadEventsFromStorage() {
try {
const storedEvents = localStorage.getItem('calendarEvents');
if (storedEvents) {
const parsedEvents = JSON.parse(storedEvents);
console.log(`Loaded ${parsedEvents.length} events from localStorage:`, parsedEvents);
events = parsedEvents;
return true;
} else {
console.log('No events found in localStorage');
events = [];
return false;
}
} catch (e) {
console.error('Error loading events from localStorage:', e);
localStorage.removeItem('calendarEvents');
events = [];
return false;
}
}
// Force reload events from localStorage and rerender
function forceReloadEvents() {
if (loadEventsFromStorage()) {
alert(`Successfully loaded ${events.length} events from localStorage`);
renderCalendar();
} else {
alert('No events found in localStorage or there was an error loading them');
}
}
// Get the start date of the week containing the current date
function getWeekStartDate(date) {
const d = new Date(date);
const day = d.getDay(); // Get the day of the week (0-6, where 0 is Sunday)
// Adjust to get the start of the week (Sunday)
d.setDate(d.getDate() - day);
return d;
}
// Render the calendar for the current week
function renderCalendar() {
calendarGrid.innerHTML = '';
// Get the start date of the current week
const weekStartDate = getWeekStartDate(currentDate);
// Update the calendar title to show the date range of the week
const weekEndDate = new Date(weekStartDate);
weekEndDate.setDate(weekEndDate.getDate() + 6);
// Format the title to show the week range
const formattedStartDate = weekStartDate.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric'
});
const formattedEndDate = weekEndDate.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric'
});
calendarTitle.textContent = `${formattedStartDate} - ${formattedEndDate}`;
// Add header spacer (top-left empty cell)
const headerSpacer = document.createElement('div');
headerSpacer.className = 'header-spacer';
headerSpacer.textContent = 'Time';
calendarGrid.appendChild(headerSpacer);
// Add day headers
const dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const today = new Date();
// Create day headers with dates
for (let i = 0; i < 7; i++) {
const date = new Date(weekStartDate);
date.setDate(date.getDate() + i);
const dayHeader = document.createElement('div');
dayHeader.className = 'calendar-day-header';
// Create formatted date string
const dayNumber = date.getDate();
const month = date.toLocaleDateString('en-US', { month: 'short' });
// Check if this day is today
const isToday =
today.getDate() === date.getDate() &&
today.getMonth() === date.getMonth() &&
today.getFullYear() === date.getFullYear();
dayHeader.innerHTML = `${dayNames[i]}<br>${month} ${dayNumber}`;
if (isToday) {
dayHeader.innerHTML += ' <span style="font-size: 10px;">(Today)</span>';
dayHeader.style.backgroundColor = '#2980b9';
}
calendarGrid.appendChild(dayHeader);
}
// Add time slots for each 15-minute interval
for (let hour = workingHoursStart; hour < workingHoursEnd; hour++) {
// Add hour label
const hourLabel = document.createElement('div');
hourLabel.className = 'hour-label';
hourLabel.style.gridRow = `span 4`; // Make the hour label span 4 rows (15-min intervals)
// Format the hour (AM/PM)
const hourFormatted = hour % 12 === 0 ? 12 : hour % 12;
const ampm = hour < 12 ? 'AM' : 'PM';
hourLabel.textContent = `${hourFormatted}:00 ${ampm}`;
// Add minute markers
const marker15 = document.createElement('span');
marker15.className = 'minute-marker minute-marker-15';
marker15.textContent = '15';
hourLabel.appendChild(marker15);
const marker30 = document.createElement('span');
marker30.className = 'minute-marker minute-marker-30';
marker30.textContent = '30';
hourLabel.appendChild(marker30);
const marker45 = document.createElement('span');
marker45.className = 'minute-marker minute-marker-45';
marker45.textContent = '45';
hourLabel.appendChild(marker45);
calendarGrid.appendChild(hourLabel);
// Add 4 time slots for each hour (00, 15, 30, 45 minutes)
for (let minute = 0; minute < 60; minute += 15) {
// For each day of the week
for (let day = 0; day < 7; day++) {
const date = new Date(weekStartDate);
date.setDate(date.getDate() + day);
const timeSlot = document.createElement('div');
timeSlot.className = 'time-slot';
// Add specific classes for better visualization
if (minute === 0) {
timeSlot.classList.add('hour-start');
} else if (minute === 15) {
timeSlot.classList.add('quarter');
} else if (minute === 30) {
timeSlot.classList.add('half');
} else if (minute === 45) {
timeSlot.classList.add('three-quarter');
}
// Add specific visual cues for each 15-minute increment
if (minute === 0) {
timeSlot.style.borderBottom = '1px solid var(--border-color)';
} else if (minute === 15) {
timeSlot.style.borderBottom = '1px dotted var(--border-color)';
} else if (minute === 30) {
timeSlot.style.borderBottom = '1px dashed var(--border-color)';
} else if (minute === 45) {
timeSlot.style.borderBottom = '1px dotted var(--border-color)';
}
// Check if this slot is for today
const isToday =
today.getDate() === date.getDate() &&
today.getMonth() === date.getMonth() &&
today.getFullYear() === date.getFullYear();
if (isToday) {
timeSlot.classList.add('today');
}
// Create data attributes for the date, hour, and minute
const year = date.getFullYear();
const month = date.getMonth();
const dayOfMonth = date.getDate();
const dateString = `${year}-${(month + 1).toString().padStart(2, '0')}-${dayOfMonth.toString().padStart(2, '0')}`;
const hourString = hour.toString().padStart(2, '0');
const minuteString = minute.toString().padStart(2, '0');
const timeString = `${hourString}:${minuteString}`;
timeSlot.setAttribute('data-date', dateString);
timeSlot.setAttribute('data-hour', hourString);
timeSlot.setAttribute('data-minute', minuteString);
timeSlot.setAttribute('data-time', timeString);
timeSlot.setAttribute('data-day', day);
// Add click event to add a new event
timeSlot.addEventListener('click', () => {
// Create a start time at this time interval
const startTime = timeString;
// Calculate end time (30 minutes later by default)
let endHour = hour;
let endMinute = minute + 30;
// Adjust if end minute exceeds 60
if (endMinute >= 60) {
endHour += 1;
endMinute -= 60;
}
const endTime = `${endHour.toString().padStart(2, '0')}:${endMinute.toString().padStart(2, '0')}`;
openAddEventModal(dateString, startTime, endTime);
});
calendarGrid.appendChild(timeSlot);
}
}
}
// Once the grid is created, add the events
renderEvents();
}
// Render events on the calendar
function renderEvents() {
// Clear any existing events from the calendar
document.querySelectorAll('.event').forEach(el => el.remove());
// Get the current week start date
const weekStartDate = getWeekStartDate(currentDate);
const weekEndDate = new Date(weekStartDate);
weekEndDate.setDate(weekEndDate.getDate() + 7);
console.log(`Filtering events for week: ${weekStartDate.toDateString()} to ${weekEndDate.toDateString()}`);
console.log(`Total events before filtering: ${events.length}`);
// Convert date strings to date objects for accurate comparison
weekStartDate.setHours(0, 0, 0, 0);
weekEndDate.setHours(0, 0, 0, 0);
// Filter events for the current week
const weekEvents = events.filter(event => {
const eventDate = new Date(event.date);
eventDate.setHours(0, 0, 0, 0); // Set time to midnight for date comparison only
const isInWeek = eventDate >= weekStartDate && eventDate < weekEndDate;
if (!isInWeek) {
console.log(`Event not in current week: ${event.title}, date: ${event.date}, parsed as: ${eventDate.toDateString()}`);
}
return isInWeek;
});
console.log(`Events for current week: ${weekEvents.length}`);
if (weekEvents.length === 0 && events.length > 0) {
console.warn('No events found for the current week. Possible date format issue?');
// Debug: Print all event dates
console.log('All event dates:');
events.forEach(event => {
const d = new Date(event.date);
console.log(`- ${event.title}: ${event.date} (parsed as: ${d.toDateString()})`);
});
}
// Render each event
weekEvents.forEach(event => {
renderEvent(event);
});
}
// Render a single event on the calendar
function renderEvent(event) {
console.log(`Rendering event: ${event.title}, date: ${event.date}, time: ${event.time}`);
// Parse the date
const eventDate = new Date(event.date);
const dateString = event.date;
// If there's no time specified, skip (this shouldn't happen in time grid view)
if (!event.time) {
console.log(`Skipping event without time: ${event.title}`);
return;
}
// Calculate the day column (0-6, Sunday to Saturday)
const dayIndex = eventDate.getDay();
// Parse the start and end times
const startTime = event.time;
const endTime = event.endTime || addMinutesToTime(startTime, 30); // Default to 30 minutes duration
// Split the time into hours and minutes
const [startHourStr, startMinuteStr] = startTime.split(':');
const [endHourStr, endMinuteStr] = endTime.split(':');
const startHour = parseInt(startHourStr);
const startMinute = parseInt(startMinuteStr);
const endHour = parseInt(endHourStr);
const endMinute = parseInt(endMinuteStr);
// Skip if outside working hours
if (startHour < workingHoursStart || endHour > workingHoursEnd) {
console.log(`Event outside working hours: ${event.title}, time: ${startTime}-${endTime}`);
return;
}
// Find the correct time slot for the starting time
const startHourString = startHour.toString().padStart(2, '0');
// Find the closest 15-minute interval time slot (round down)
const roundedStartMinute = Math.floor(startMinute / 15) * 15;
const startMinuteString = roundedStartMinute.toString().padStart(2, '0');
const timeSlot = document.querySelector(`.time-slot[data-date="${dateString}"][data-hour="${startHourString}"][data-minute="${startMinuteString}"]`);
if (!timeSlot) {
console.error(`Could not find time slot for event: ${event.title}, date: ${dateString}, time: ${startHourString}:${startMinuteString}`);
return;
}
// Debug message
console.log(`Found time slot for: ${event.title}, creating event element`);
// Create the event element
const eventElement = document.createElement('div');
eventElement.className = 'event';
eventElement.style.backgroundColor = event.color || colorOptions[0];
eventElement.setAttribute('data-event-id', event.id);
// Add inner content div for better handling of events