From b115f901a8910521ad4d83e648556ee63ffcb7b2 Mon Sep 17 00:00:00 2001 From: Alban CHOREIN Date: Thu, 30 Apr 2020 21:36:54 +0200 Subject: [PATCH] Extracted logic to service. --- .../day-calendar.component.spec.ts | 65 ++++++++----------- .../day-calendar/day-calendar.component.ts | 30 +-------- .../day-calendar/day-calendar.service.spec.ts | 17 +++++ src/lib/day-calendar/day-calendar.service.ts | 38 +++++++++++ 4 files changed, 85 insertions(+), 65 deletions(-) diff --git a/src/lib/day-calendar/day-calendar.component.spec.ts b/src/lib/day-calendar/day-calendar.component.spec.ts index 5a8d6885..99ab1eff 100644 --- a/src/lib/day-calendar/day-calendar.component.spec.ts +++ b/src/lib/day-calendar/day-calendar.component.spec.ts @@ -139,41 +139,32 @@ describe('Component: DayCalendarComponent', () => { expect(component.onGoToCurrent.emit).toHaveBeenCalled(); }); - it('should not display secondary navigation button when not asked to', () => { - const currentDate = moment(); - component.currentDateView = currentDate; // updates showLeftNav && showLeftSecondaryNav - expect(component.showSecondaryLeftNav).toBeFalsy(); - }) - - it('should display secondary navigation button when true in config', () => { - const currentDate = moment(); - component.componentConfig.showSecondaryNavigationDayView = true; - - component.currentDateView = currentDate; // updates showLeftNav && showLeftSecondaryNav - expect(component.showSecondaryLeftNav).toBeTruthy(); - }) - - it('should not display secondary navigation button when primary button is not displayed', () => { - const currentDate = moment(); - component.componentConfig.min = currentDate.clone(); - component.componentConfig.showSecondaryNavigationDayView = true; - - component.currentDateView = currentDate; // updates showLeftNav && showLeftSecondaryNav - - expect(component.showSecondaryLeftNav).toBeFalsy(); - }) - - it('should navigate to min date if secondaryStep is too big', () => { - const currentDate = moment(); - const minDate = currentDate.clone().subtract('4', 'month'); - component.componentConfig.min = minDate; - component.componentConfig.showSecondaryNavigationDayView = true; - component.componentConfig.secondaryNavigationStepDayView = 10; - - component.currentDateView = currentDate; // updates showLeftNav && showLeftSecondaryNav - - component.onLeftSecondaryNavClick(); - - expect(component.currentDateView.format('YYYY-MM-DD')).toEqual(minDate.format('YYYY-MM-DD')); - }) + describe('should display secondary nav buttons if necessary', () => { + it('should not display secondary navigation button when not asked to', () => { + const currentDate = moment(); + // updates showLeftNav && showLeftSecondaryNav + component.currentDateView = currentDate; + expect(component.showSecondaryLeftNav).toBeFalsy(); + }) + + it('should display secondary navigation button when true in config', () => { + const currentDate = moment(); + component.componentConfig.showSecondaryNavigationDayView = true; + + // updates showLeftNav && showLeftSecondaryNav + component.currentDateView = currentDate; + expect(component.showSecondaryLeftNav).toBeTruthy(); + }) + + it('should not display secondary navigation button when primary button is not displayed', () => { + const currentDate = moment(); + component.componentConfig.min = currentDate.clone(); + component.componentConfig.showSecondaryNavigationDayView = true; + + // updates showLeftNav && showLeftSecondaryNav + component.currentDateView = currentDate; + + expect(component.showSecondaryLeftNav).toBeFalsy(); + }) + }); }); diff --git a/src/lib/day-calendar/day-calendar.component.ts b/src/lib/day-calendar/day-calendar.component.ts index 7a5f0b19..ab19ad66 100644 --- a/src/lib/day-calendar/day-calendar.component.ts +++ b/src/lib/day-calendar/day-calendar.component.ts @@ -258,20 +258,7 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce } onLeftSecondaryNavClick(): void { - let navigateBy = this.componentConfig.secondaryNavigationStepDayView; - - let targetDate = this.currentDateView.clone().subtract(navigateBy, 'month'); - if (this.componentConfig.min) { - const yearsDiff = targetDate.year() - this.componentConfig.min.year(); - const monthsDiff = targetDate.month() - this.componentConfig.min.month(); - const isOutsideRange = (yearsDiff * 12) + monthsDiff < 0; - - if (isOutsideRange) { - navigateBy = (this.currentDateView.year() - this.componentConfig.min.year()) * 12 - + (this.currentDateView.month() - this.componentConfig.min.month()); - targetDate = this.currentDateView.clone().subtract(navigateBy, 'month'); - } - } + const targetDate = this.dayCalendarService.computeDateAfterLeftSecondaryNav(this.componentConfig, this.currentDateView); const from = this.currentDateView.clone(); this.currentDateView = targetDate; @@ -287,20 +274,7 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce } onRightSecondaryNavClick(): void { - let navigateBy = this.componentConfig.secondaryNavigationStepDayView; - let targetDate = this.currentDateView.clone().add(navigateBy, 'month'); - - if (this.componentConfig.max) { - const yearsDiff = targetDate.year() - this.componentConfig.max.year(); - const monthsDiff = targetDate.month() - this.componentConfig.max.month(); - const isOutsideRange = (yearsDiff * 12) + monthsDiff > 0; - - if (isOutsideRange) { - navigateBy = (this.componentConfig.max.year() - this.currentDateView.year()) * 12 - + (this.componentConfig.max.month() - this.currentDateView.month()); - targetDate = this.currentDateView.clone().add(navigateBy, 'month'); - } - } + const targetDate = this.dayCalendarService.computeDateAfterRightSecondaryNav(this.componentConfig, this.currentDateView); const from = this.currentDateView.clone(); this.currentDateView = targetDate; diff --git a/src/lib/day-calendar/day-calendar.service.spec.ts b/src/lib/day-calendar/day-calendar.service.spec.ts index 6f7b6544..93019b56 100644 --- a/src/lib/day-calendar/day-calendar.service.spec.ts +++ b/src/lib/day-calendar/day-calendar.service.spec.ts @@ -176,4 +176,21 @@ describe('Service: Calendar', () => { expect(service.getDayBtnCssClass({dayBtnCssClassCallback: (m => 'class1 class2')}, date)) .toEqual('class1 class2'); })); + + it('should navigate to min date if secondaryStep is too big', + inject([DayCalendarService], + (service: DayCalendarService) => { + const currentDate = moment(); + const minDate = currentDate.clone().subtract('4', 'month'); + + const config: IDayCalendarConfigInternal = { + min: minDate, + showSecondaryNavigationDayView: true, + secondaryNavigationStepDayView: 10 + }; + + const targetDate = service.computeDateAfterLeftSecondaryNav(config, currentDate); + + expect(targetDate.format('YYYY-MM-DD')).toEqual(minDate.format('YYYY-MM-DD')); + })); }); diff --git a/src/lib/day-calendar/day-calendar.service.ts b/src/lib/day-calendar/day-calendar.service.ts index d728cc89..9364d398 100644 --- a/src/lib/day-calendar/day-calendar.service.ts +++ b/src/lib/day-calendar/day-calendar.service.ts @@ -11,6 +11,8 @@ const moment = momentNs; @Injectable() export class DayCalendarService { + + readonly DEFAULT_CONFIG: IDayCalendarConfig = { showNearMonthDays: true, showWeekNumbers: false, @@ -201,6 +203,42 @@ export class DayCalendarService { return ''; } + computeDateAfterLeftSecondaryNav(componentConfig: IDayCalendarConfigInternal, currentDateView: momentNs.Moment) { + let navigateBy = componentConfig.secondaryNavigationStepDayView; + let targetDate = currentDateView.clone().subtract(navigateBy, 'month'); + + if (componentConfig.min) { + const yearsDiff = targetDate.year() - componentConfig.min.year(); + const monthsDiff = targetDate.month() - componentConfig.min.month(); + const isOutsideRange = (yearsDiff * 12) + monthsDiff < 0; + + if (isOutsideRange) { + navigateBy = (currentDateView.year() - componentConfig.min.year()) * 12 + + (currentDateView.month() - componentConfig.min.month()); + targetDate = currentDateView.clone().subtract(navigateBy, 'month'); + } + } + return targetDate; + } + + computeDateAfterRightSecondaryNav(componentConfig: IDayCalendarConfigInternal, currentDateView: momentNs.Moment) { + let navigateBy = componentConfig.secondaryNavigationStepDayView; + let targetDate = currentDateView.clone().add(navigateBy, 'month'); + + if (componentConfig.max) { + const yearsDiff = targetDate.year() - componentConfig.max.year(); + const monthsDiff = targetDate.month() - componentConfig.max.month(); + const isOutsideRange = (yearsDiff * 12) + monthsDiff > 0; + + if (isOutsideRange) { + navigateBy = (componentConfig.max.year() - currentDateView.year()) * 12 + + (componentConfig.max.month() - currentDateView.month()); + targetDate = currentDateView.clone().add(navigateBy, 'month'); + } + } + return targetDate; + } + private removeNearMonthWeeks(currentMonth: Moment, monthArray: IDay[][]): IDay[][] { if (monthArray[monthArray.length - 1].find((day) => day.date.isSame(currentMonth, 'month'))) { return monthArray;