Skip to content

Commit

Permalink
Extracted logic to service.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alban CHOREIN authored and vlio20 committed Apr 30, 2020
1 parent 22ed5eb commit b115f90
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 65 deletions.
65 changes: 28 additions & 37 deletions src/lib/day-calendar/day-calendar.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
});
});
30 changes: 2 additions & 28 deletions src/lib/day-calendar/day-calendar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/lib/day-calendar/day-calendar.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}));
});
38 changes: 38 additions & 0 deletions src/lib/day-calendar/day-calendar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const moment = momentNs;

@Injectable()
export class DayCalendarService {


readonly DEFAULT_CONFIG: IDayCalendarConfig = {
showNearMonthDays: true,
showWeekNumbers: false,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b115f90

Please sign in to comment.