class CustomCalendar extends HTMLElement { connectedCallback() { this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = `
`; // Check if dependencies are already loaded if (window.tui && window.tui.Calendar) { this.initCalendar(); } else { // Wait for dependencies to be loaded from index.html const checkDependencies = setInterval(() => { if (window.tui && window.tui.Calendar) { clearInterval(checkDependencies); this.initCalendar(); } }, 100); } } initCalendar() { const Calendar = window.tui.Calendar; const container = this.shadowRoot.getElementById('calendar'); this.calendar = new Calendar(container, { defaultView: 'month', taskView: false, scheduleView: ['time'], useCreationPopup: false, useDetailPopup: true, template: { monthDayname: function(dayname) { return '' + dayname.label + ''; } } }); // Add courses as schedules const schedules = window.coursesData.map(course => { const start = new Date(course.start_timestamp.$date); const end = new Date(course.end_timestamp.$date); return { id: course['Registration Code'] || String(Math.random()), calendarId: '1', title: course.Course, category: 'time', dueDateClass: '', start: start, end: end, location: course['Time Zone'], instructor: course.Instructor, raw: { time: course['Time Slot'] } }; }); this.calendar.createSchedules(schedules); // Set today as default date and adjust view if needed const today = new Date(); this.calendar.setDate(today); // If no events today, show the month with the next upcoming event const upcomingEvents = schedules.filter(s => new Date(s.start) >= today); if (upcomingEvents.length > 0) { const nextEventDate = new Date(upcomingEvents[0].start); if (nextEventDate.getMonth() !== today.getMonth()) { this.calendar.setDate(nextEventDate); } } } } customElements.define('custom-calendar', CustomCalendar);