class CalendarViewController extends ViewController { viewDidLoad() { this.year = new Date().getFullYear(); this.month = new Date().getMonth(); } viewWillAppear() { this.refreshDates(); } onYearUpButtonPressed() { this.year--; this.refreshDates(); } onYearDownButtonPressed() { this.year++; this.refreshDates(); } onMonthUpButtonPressed() { this.month--; if (this.month < 0) { this.month = 11; this.year--; } this.refreshDates(); } onMonthDownButtonPressed() { this.month++; if (this.month > 11) { this.month = 0; this.year++; } this.refreshDates(); } refreshDates() { if (!ReservationManager.reservations) { ReservationManager.loadReservations(this.refreshDates.bind(this)); return; } this.yearLabel.innerText = this.year; this.monthLabel.innerText = new Date(this.year, this.month, 1).toLocaleString("default", { month: "long" }); //Clear the existing elements. while (this.dateTable.children.length > 0) { this.dateTable.removeChild(this.dateTable.children[0]); } //Get the first day of the month. var firstDay = new Date(0); firstDay.setFullYear(this.year); firstDay.setMonth(this.month); firstDay.setDate(1); //Get the weekday of the first day. var firstDayWeekday = firstDay.getDay() - 1; if (firstDayWeekday < 0) { firstDayWeekday += 7; } //Create a row with the weekday letters. var weekdayRow = document.createElement("TR"); var weekdays = [ "M", "D", "M", "D", "F", "S", "S" ]; for (var i = 0; i < weekdays.length; i++) { var weekdayCell = document.createElement("TD"); weekdayCell.innerText = weekdays[i]; weekdayCell.style.color = "#AAAAAA"; weekdayRow.appendChild(weekdayCell); } this.dateTable.appendChild(weekdayRow); //Create dates until the month (six lines) is full. var fullLinesCount = 0; var dayIndex = 0; var currentRow = document.createElement("TR"); while (fullLinesCount < 6) { var dayOffset = firstDayWeekday - dayIndex; var thisDay = new Date(firstDay.getTime()); thisDay.setDate(thisDay.getDate() - dayOffset); thisDay.setHours(0); thisDay.setMinutes(0); thisDay.setSeconds(0); var dayCell = document.createElement("TD"); dayCell.innerText = thisDay.getDate(); if (thisDay.getMonth() != firstDay.getMonth()) { dayCell.classList.add("other-month"); } var reservationStatus = ReservationManager.getReservationStatusForDate(new Date(thisDay.getTime() - (thisDay.getTimezoneOffset() * 60 * 1000))); if (reservationStatus == 1) { dayCell.classList.add("pending"); } else if (reservationStatus == 2) { dayCell.classList.add("reserved"); } currentRow.appendChild(dayCell); if (currentRow.children.length >= 7) { //Add the row to the table and start a new one until six rows are reached. this.dateTable.appendChild(currentRow); currentRow = document.createElement("TR"); fullLinesCount++; } dayIndex++; } } onBackButtonPressed(event) { this.dismissModally(); } } UIKit.registerViewControllerType(CalendarViewController);