class CalendarViewController extends TimeCardsViewController { viewDidLoad() { TimeCards.dataManager.addDataListener("user", { deleted: 0 }, this.onUserUpdate.bind(this)); //Initiate the date update interval which will check if the time goes past 23:59:59 and select the next day automatically. setInterval(this.dateUpdate.bind(this), 60000); } onUserUpdate(userId, user, sortedBefore) { //Initialise the user dropdown if it has not been done before. if (!this.userDropdown) { this.userDropdown = new ContextMenu(); this.userDropdown.element.id = "work-time-user-dropdown"; this.userDropdown.setViewController(this); this.userDropdownEntries = { }; } //Get the user dropdown entry for the changed user. var userEntry = (userId in this.userDropdownEntries) ? this.userDropdownEntries[userId] : null; //We insert the session user's entry at the very top. All the other users are sorted alphabetically. var insertBeforeElement = user.is_session_user ? this.userDropdown.element.firstChild.firstChild : ((sortedBefore in this.userDropdownEntries) ? this.userDropdownEntries[sortedBefore] : null); if (!userEntry) { //Handle the added user. var userEntry = document.createElement("menu-option"); userEntry.innerText = user.full_name; if (user.is_session_user) { userEntry.innerText += " (me)"; } userEntry.setAttribute("value", userId); userEntry.setAttribute("action", "onUserSelected"); if (user.is_session_user) { this.selectUser(user); this.selectDay(Time.getCurrentLocalDayIndex()); } this.userDropdown.element.firstChild.insertBefore(userEntry, insertBeforeElement); this.userDropdownEntries[user.user_id] = userEntry; } else if (!user) { //Handle the deleted user. delete this.userDropdownEntries[user.user_id]; this.userDropdown.element.firstChild.removeChild(userEntry); } else { //Handle the changed user. userEntry.innerText = user.full_name; if (user.is_session_user) { userEntry.innerText += " (me)"; } //Update the position in case the name has changed. this.userDropdown.element.firstChild.insertBefore(userEntry, insertBeforeElement); //Also re-select the user to update the name in the navigation bar. if (userId == this.selectedUser) { this.selectUser(user); } } } onUserDropdownPressed(event) { //event.stopPropagation(); this.userDropdown.showForElement(event.currentTarget, "bottom"); } onUserSelected(event) { this.selectUser(TimeCards.dataManager.getEntity("user", event.currentTarget.getAttribute("value"))); this.dayViewController.displayDay(this.selectedDayIndex, this.selectedUser); } selectUser(user) { this.selectedUser = user.user_id; this.displayingWorkTimeUserNameLabel.innerText = user.full_name; } /** * Selects the given day in the date selection and displays the work time periods for that date. */ selectDay(dayIndex) { this.selectedDayIndex = dayIndex; var date = Time.getDateForLocalDayIndex(this.selectedDayIndex); //Enable or disable the today button. //We store a flag that indicates whether the selected date is "today" or not. Used in the dateUpdate() function below. this.isToday = this.selectedDayIndex == Time.getCurrentLocalDayIndex(); if (this.isToday) { if (!this.todayButton.hasAttribute("disabled")) { this.todayButton.setAttribute("disabled", ""); } } else { this.todayButton.removeAttribute("disabled"); } var weekdayString = date.toLocaleDateString(undefined, { weekday: "long" }); this.dayNameLabel.innerText = weekdayString; this.dateField.value = Time.getUnixDate(date); this.dayViewController.displayDay(this.selectedDayIndex, this.selectedUser); } /** * Move the day selection by the given amount of days (positive or negative amount). */ selectDayRelative(days) { this.selectDay(this.selectedDayIndex + days); } onNextDayButtonPressed(event) { this.selectDayRelative(1); } onPreviousDayButtonPressed(event) { this.selectDayRelative(-1); } onSelectTodayButtonPressed(event) { this.selectDay(Time.getCurrentLocalDayIndex()); } /** * Called every minute and checks whether a new day has started (i.e. the current date is not the same as the one from the last update). * This way, the date view will select the next day if the time passes 23:59:59. */ dateUpdate() { //Do nothing if not the current day is selected at all. if (!this.isToday) { return; } var currentDayIndex = Time.getCurrentLocalDayIndex(); //Only perform the check if this is not the first call. if (this.lastUpdateDayIndex) { //Select the new current day index if it is different from the last update. if (this.lastUpdateDayIndex != currentDayIndex) { this.selectDay(currentDayIndex); } } this.lastUpdateDayIndex = currentDayIndex; } /** * Called when the user changes the date in the date field. */ onDateFieldChanged(event) { if (!this.dateField.value || this.dateField.value == "") { this.dayNameLabel.innerText = "Invalid date"; this.dayView.style.display = "none"; return; } this.dayView.style.display = ""; this.selectDay(Time.getLocalDayIndex(new Date(this.dateField.value))); } } UIKit.registerViewControllerType(CalendarViewController);