// Based on https://stackoverflow.com/a/15289883 function computeDateDifferenceInHours (date1, date2) { const msPerHour = 60 * 60 * 1000; return Math.floor(Math.abs(date2.getTime() - date1.getTime()) / msPerHour); } class Calendar { constructor (calendarParameters = {}) { this.containerNode = calendarParameters.containerNode !== undefined ? calendarParameters.containerNode : $("#cal-container"); this.eventContainerNode = null; this.timeSlotsContainerNode = null; this.eventDetailsContainerNode = null; this.startDate = calendarParameters.startDate !== undefined ? calendarParameters.startDate : new Date(); this.endDate = calendarParameters.endDate !== undefined ? calendarParameters.endDate : new Date(Date.now() + (24 * 60 * 60 * 1000)); this.nbHoursToDisplay = 0; this.firstHourToDisplay = 0; this.endHourToDisplay = 0; this.events = []; this.subscriptionURLFormat = calendarParameters.subscriptionURLFormat !== undefined ? calendarParameters.subscriptionURLFormat : ""; this.csrfToken = calendarParameters.csrfToken !== undefined ? calendarParameters.csrfToken : ""; // Map from locations to their CSS styles this.locationStyles = new Map(); this.init(); } init () { this.updateHoursToDisplay(); this.createTimeSlotContainer(); this.createEventContainer(); this.createEventDetailsContainer(); this.updateEventContainerGridStyle(); this.createTimeSlots(); this.createEvents(); this.createLocationStyles(); this.applyLocationStylesAsCSS(); this.updateEventLocationStyleID(); this.sortEventNodesByEndTimeAndLocation(); } // Date change setStartDate (newStartDate) { this.startDate = newStartDate; this.updateHoursToDisplay(); this.updateEventContainerGridStyle(); this.updateTimeSlots(); this.updateEventVisibilities(); } setEndDate (newEndDate) { this.endDate = newEndDate; this.updateHoursToDisplay(); this.updateEventContainerGridStyle(); this.updateTimeSlots(); this.updateEventVisibilities(); } updateHoursToDisplay () { this.startHourToDisplay = this.startDate.getHours(); this.endHourToDisplay = this.endDate.getHours(); this.nbHoursToDisplay = computeDateDifferenceInHours(this.startDate, this.endDate); } // Time slots createTimeSlotContainer () { this.timeSlotsContainerNode = $("