diff --git a/src/addon/calendar/components/calendar/addon-calendar-calendar.html b/src/addon/calendar/components/calendar/addon-calendar-calendar.html index 679b0996e..5872e86d0 100644 --- a/src/addon/calendar/components/calendar/addon-calendar-calendar.html +++ b/src/addon/calendar/components/calendar/addon-calendar-calendar.html @@ -1,3 +1,11 @@ + + + + + + diff --git a/src/addon/calendar/components/calendar/calendar.ts b/src/addon/calendar/components/calendar/calendar.ts index cbeeb6379..dcdb412b1 100644 --- a/src/addon/calendar/components/calendar/calendar.ts +++ b/src/addon/calendar/components/calendar/calendar.ts @@ -37,6 +37,7 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest @Input() courseId: number | string; @Input() categoryId: number | string; // Category ID the course belongs to. @Input() canNavigate?: string | boolean; // Whether to include arrows to change the month. Defaults to true. + @Input() displayNavButtons?: string | boolean; // Whether to display nav buttons created by this component. Defaults to true. @Output() onEventClicked = new EventEmitter(); @Output() onDayClicked = new EventEmitter<{day: number, month: number, year: number}>(); @@ -45,6 +46,7 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest weeks: any[]; loaded = false; timeFormat: string; + isCurrentMonth: boolean; protected year: number; protected month: number; @@ -106,7 +108,8 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest this.year = this.initialYear ? Number(this.initialYear) : now.getFullYear(); this.month = this.initialMonth ? Number(this.initialMonth) : now.getMonth() + 1; - this.canNavigate = typeof this.canNavigate == 'undefined' ? true : this.utils.isTrueOrOne(this.canNavigate); + + this.calculateIsCurrentMonth(); this.fetchData(); } @@ -115,6 +118,9 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest * Detect changes on input properties. */ ngOnChanges(changes: {[name: string]: SimpleChange}): void { + this.canNavigate = typeof this.canNavigate == 'undefined' ? true : this.utils.isTrueOrOne(this.canNavigate); + this.displayNavButtons = typeof this.displayNavButtons == 'undefined' ? true : + this.utils.isTrueOrOne(this.displayNavButtons); if ((changes.courseId || changes.categoryId) && this.weeks) { this.filterEvents(); @@ -274,6 +280,7 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest this.domUtils.showErrorModalDefault(error, 'addon.calendar.errorloadevents', true); this.decreaseMonth(); }).finally(() => { + this.calculateIsCurrentMonth(); this.loaded = true; }); } @@ -290,6 +297,7 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest this.domUtils.showErrorModalDefault(error, 'addon.calendar.errorloadevents', true); this.increaseMonth(); }).finally(() => { + this.calculateIsCurrentMonth(); this.loaded = true; }); } @@ -312,6 +320,39 @@ export class AddonCalendarCalendarComponent implements OnInit, OnChanges, OnDest this.onDayClicked.emit({day: day, month: this.month, year: this.year}); } + /** + * Check if user is viewing the current month. + */ + calculateIsCurrentMonth(): void { + const now = new Date(); + + this.isCurrentMonth = this.year == now.getFullYear() && this.month == now.getMonth() + 1; + } + + /** + * Go to current month. + */ + goToCurrentMonth(): void { + const now = new Date(), + initialMonth = this.month, + initialYear = this.year; + + this.month = now.getMonth() + 1; + this.year = now.getFullYear(); + + this.loaded = false; + + this.fetchEvents().then(() => { + this.isCurrentMonth = true; + }).catch((error) => { + this.domUtils.showErrorModalDefault(error, 'addon.calendar.errorloadevents', true); + this.year = initialYear; + this.month = initialMonth; + }).finally(() => { + this.loaded = true; + }); + } + /** * Decrease the current month. */ diff --git a/src/addon/calendar/pages/day/day.html b/src/addon/calendar/pages/day/day.html index e6db57d36..0ef39ef72 100644 --- a/src/addon/calendar/pages/day/day.html +++ b/src/addon/calendar/pages/day/day.html @@ -5,6 +5,9 @@ + diff --git a/src/addon/calendar/pages/day/day.ts b/src/addon/calendar/pages/day/day.ts index d18e25d75..a8c1e90a1 100644 --- a/src/addon/calendar/pages/day/day.ts +++ b/src/addon/calendar/pages/day/day.ts @@ -73,6 +73,7 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy { hasOffline = false; isOnline = false; syncIcon: string; + isCurrentDay: boolean; constructor(localNotificationsProvider: CoreLocalNotificationsProvider, navParams: NavParams, @@ -196,7 +197,8 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy { * View loaded. */ ngOnInit(): void { - this.currentMoment = moment().year(this.year).month(this.month - 1).date(this.day); + this.calculateCurrentMoment(); + this.calculateIsCurrentDay(); this.fetchData(true, false); } @@ -533,6 +535,52 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy { this.navCtrl.push('AddonCalendarEditEventPage', params); } + /** + * Calculate current moment. + */ + calculateCurrentMoment(): void { + this.currentMoment = moment().year(this.year).month(this.month - 1).date(this.day); + } + + /** + * Check if user is viewing the current day. + */ + calculateIsCurrentDay(): void { + const now = new Date(); + + this.isCurrentDay = this.year == now.getFullYear() && this.month == now.getMonth() + 1 && this.day == now.getDate(); + } + + /** + * Go to current day. + */ + goToCurrentDay(): void { + const now = new Date(), + initialDay = this.day, + initialMonth = this.month, + initialYear = this.year; + + this.day = now.getDate(); + this.month = now.getMonth() + 1; + this.year = now.getFullYear(); + this.calculateCurrentMoment(); + + this.loaded = false; + + this.fetchEvents().then(() => { + this.isCurrentDay = true; + }).catch((error) => { + this.domUtils.showErrorModalDefault(error, 'addon.calendar.errorloadevents', true); + + this.year = initialYear; + this.month = initialMonth; + this.day = initialDay; + this.calculateCurrentMoment(); + }).finally(() => { + this.loaded = true; + }); + } + /** * Load next month. */ @@ -545,6 +593,7 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy { this.domUtils.showErrorModalDefault(error, 'addon.calendar.errorloadevents', true); this.decreaseDay(); }).finally(() => { + this.calculateIsCurrentDay(); this.loaded = true; }); } @@ -561,6 +610,7 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy { this.domUtils.showErrorModalDefault(error, 'addon.calendar.errorloadevents', true); this.increaseDay(); }).finally(() => { + this.calculateIsCurrentDay(); this.loaded = true; }); } diff --git a/src/addon/calendar/pages/index/index.html b/src/addon/calendar/pages/index/index.html index ca8e832f3..adfbe89db 100644 --- a/src/addon/calendar/pages/index/index.html +++ b/src/addon/calendar/pages/index/index.html @@ -6,7 +6,7 @@