// Interval graph coloring algorithm, by Twal class IntervalColoration { constructor (intervals) { this.intervals = intervals; this.n = this.intervals.length; this.computeInterferenceGraph(); this.computePEO(); this.computeColoration(); } computeInterferenceGraph() { this.adj = new Array(this.n); for (let i = 0; i < this.n; ++i) { this.adj[i] = []; } for (let i = 0; i < this.n; ++i) { for (let j = 0; j < i; ++j) { let inti = this.intervals[i]; let intj = this.intervals[j]; if (inti[0] < intj[1] && intj[0] < inti[1]) { this.adj[i].push(j); this.adj[j].push(i); } } } } //Perfect elimination order using Maximum Cardinality Search //Runs in O(n^2), could be optimized in O(n log n) computePEO() { let marked = new Array(this.n); let nbMarkedNeighbor = new Array(this.n); this.perm = new Array(this.n); for (let i = 0; i < this.n; ++i) { marked[i] = false; nbMarkedNeighbor[i] = 0; } for (let k = this.n-1; k >= 0; --k) { let maxi = -1; for (let i = 0; i < this.n; ++i) { if (!marked[i] && (maxi == -1 || nbMarkedNeighbor[i] >= nbMarkedNeighbor[maxi])) { maxi = i; } } for (let i = 0; i < this.adj[maxi].length; ++i) { nbMarkedNeighbor[this.adj[maxi][i]] += 1; } this.perm[maxi] = k; marked[maxi] = true; } // console.log(this.perm); } computeColoration() { this.colors = new Array(this.n); let isColorUsed = new Array(this.n); for (let i = 0; i < this.n; ++i) { this.colors[i] = -1; isColorUsed[i] = false; } for (let i = 0; i < this.n; ++i) { let ind = this.perm[i]; for (let j = 0; j < this.adj[ind].length; ++j) { let neigh = this.adj[ind][j]; if (this.colors[neigh] >= 0) { isColorUsed[this.colors[neigh]] = true; } } for (let j = 0; j < this.n; ++j) { if (!isColorUsed[j]) { this.colors[ind] = j; break; } } for (let j = 0; j < this.adj[ind].length; ++j) { let neigh = this.adj[ind][j]; if (this.colors[neigh] >= 0) { isColorUsed[this.colors[neigh]] = false; } } } } } // Based on https://stackoverflow.com/a/15289883 function computeDateDifferenceInHours (date1, date2) { d1 = new Date(date1.getYear(), date1.getMonth(), date1.getDate(), date1.getHours()); d2 = new Date(date2.getYear(), date2.getMonth(), date2.getDate(), date2.getHours()); const msPerHour = 60 * 60 * 1000; return Math.abs(d2.getTime() - d1.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.onlyDisplaySubscribedEvents = calendarParameters.onlyDisplaySubscribedEvents !== undefined ? calendarParameters.onlyDisplaySubscribedEvents : false; this.groupEventsByLocation = calendarParameters.groupEventsByLocation !== undefined ? calendarParameters.groupEventsByLocation : true; this.eventDetailURLFormat = calendarParameters.eventDetailURLFormat !== undefined ? calendarParameters.eventDetailURLFormat : ""; 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.updateTimeSlotContainerGridStyle(); this.updateEventContainerGridStyle(); this.createTimeSlots(); this.createEvents(); this.createLocationStyles(); this.applyLocationStylesAsCSS(); this.updateEventLocationStyleID(); //this.sortEventNodesByEndTimeAndLocation(); this.sortEventNodesByIntervalGraphColoring(); this.updateCalendarNodeHeight(); this.updateEventVisibilities(); this.initEventOverflowTooltips(); } // Date change setStartDate (newStartDate) { this.startDate = newStartDate; this.updateHoursToDisplay(); this.updateEventContainerGridStyle(); this.updateTimeSlots(); this.updateEventVisibilities(); this.updateCalendarNodeHeight(); this.sortEventNodesByIntervalGraphColoring(); this.startShowingEventOverflowTooltips(); } setEndDate (newEndDate) { this.endDate = newEndDate; this.updateHoursToDisplay(); this.updateEventContainerGridStyle(); this.updateTimeSlots(); this.updateEventVisibilities(); this.updateCalendarNodeHeight(); this.sortEventNodesByIntervalGraphColoring(); this.startShowingEventOverflowTooltips(); } updateHoursToDisplay () { this.startHourToDisplay = this.startDate.getHours(); this.endHourToDisplay = this.endDate.getHours(); this.nbHoursToDisplay = Math.floor(computeDateDifferenceInHours(this.startDate, this.endDate)); } // Calendar container updateCalendarNodeHeight () { // Time slot hour row let timeSlotHourRowHeight = $(".cal-time-slot-hour").outerHeight(); // Event grid this.containerNode.css("height", "calc(100% )"); let eventContainerHeight = this.eventContainerNode .css("grid-template-rows") .split("px ") .reduce((heightAccumulator, currentRowHeight) => { return heightAccumulator + parseInt(currentRowHeight); }, 0); this.containerNode.css("height", timeSlotHourRowHeight + eventContainerHeight); } // Time slots createTimeSlotContainer () { this.timeSlotsContainerNode = $("