From da3c72df49ca8a7c8538f7b9faaf257b40d6a309 Mon Sep 17 00:00:00 2001 From: Vlad Ioffe Date: Thu, 18 Jan 2018 21:15:47 +0200 Subject: [PATCH] fixing #340 --- .../day-time-calendar.component.ts | 2 +- .../day-time-calendar.service.spec.ts | 44 +++++++++++++++++-- .../day-time-calendar.service.ts | 17 ++++++- 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/app/day-time-calendar/day-time-calendar.component.ts b/src/app/day-time-calendar/day-time-calendar.component.ts index cd8ab461..e10436e5 100644 --- a/src/app/day-time-calendar/day-time-calendar.component.ts +++ b/src/app/day-time-calendar/day-time-calendar.component.ts @@ -171,7 +171,7 @@ export class DayTimeCalendarComponent implements OnInit, OnChanges, ControlValue } dateSelected(day: IDate) { - this.selected = this.dayTimeCalendarService.updateDay(this.selected, day.date); + this.selected = this.dayTimeCalendarService.updateDay(this.selected, day.date, this.config); this.emitChange(); } diff --git a/src/app/day-time-calendar/day-time-calendar.service.spec.ts b/src/app/day-time-calendar/day-time-calendar.service.spec.ts index 9964f669..0abed602 100644 --- a/src/app/day-time-calendar/day-time-calendar.service.spec.ts +++ b/src/app/day-time-calendar/day-time-calendar.service.spec.ts @@ -4,6 +4,7 @@ import * as moment from 'moment'; import {UtilsService} from '../common/services/utils/utils.service'; import {DayCalendarService} from '../day-calendar/day-calendar.service'; import {TimeSelectService} from '../time-select/time-select.service'; +import {IDayCalendarConfigInternal} from '../day-calendar/day-calendar-config.model'; const DAY_FORMAT = 'YYYYMMDD'; const TIME_FORMAT = 'HH:mm:ss'; @@ -20,13 +21,48 @@ describe('Service: DayTimeCalendarService', () => { (service: DayTimeCalendarService) => { const daytime = moment('2011091313:12:11', COMBINED_FORMAT); const day = moment('10110203', DAY_FORMAT); - expect(service.updateDay(daytime, day).format(COMBINED_FORMAT)).toEqual('1011020313:12:11'); - })); + expect(service.updateDay(daytime, day, {}).format(COMBINED_FORMAT)).toEqual('1011020313:12:11'); + }) + ); - it('should check the updateTime method', inject([DayTimeCalendarService], + it('should check the updateTime method when time is before min', inject([DayTimeCalendarService], (service: DayTimeCalendarService) => { + const daytime = moment('2011091313:12:11', COMBINED_FORMAT); + const config: IDayCalendarConfigInternal = { + min: daytime.clone().add(10, 'm'), + max: daytime.clone().add(50, 'm') + }; + + const time = daytime.clone(); + expect(service.updateDay(daytime, time, config).format(COMBINED_FORMAT)) + .toEqual(daytime.clone().add(10, 'm').format(COMBINED_FORMAT)); + + expect(service.updateDay(daytime, time, {}).format(COMBINED_FORMAT)) + .toEqual(daytime.format(COMBINED_FORMAT)); + }) + ); + + it('should check the updateTime method when time is before max', inject([DayTimeCalendarService], + (service: DayTimeCalendarService) => { + const daytime = moment('2011091313:12:11', COMBINED_FORMAT); + const config: IDayCalendarConfigInternal = { + min: daytime.clone().subtract(50, 'm'), + max: daytime.clone().subtract(10, 'm') + }; + + const time = daytime.clone(); + expect(service.updateDay(daytime, time, config).format(COMBINED_FORMAT)) + .toEqual(daytime.clone().subtract(10, 'm').format(COMBINED_FORMAT)); + + expect(service.updateDay(daytime, time, {}).format(COMBINED_FORMAT)) + .toEqual(daytime.format(COMBINED_FORMAT)); + }) + ); + + it('should check the updateTime method', inject([DayTimeCalendarService], (service: DayTimeCalendarService) => { const daytime = moment('2011091313:12:11', COMBINED_FORMAT); const time = moment('03:11:10', TIME_FORMAT); expect(service.updateTime(daytime, time).format(COMBINED_FORMAT)).toEqual('2011091303:11:10'); - })); + }) + ); }); diff --git a/src/app/day-time-calendar/day-time-calendar.service.ts b/src/app/day-time-calendar/day-time-calendar.service.ts index 292be5b7..834e357f 100644 --- a/src/app/day-time-calendar/day-time-calendar.service.ts +++ b/src/app/day-time-calendar/day-time-calendar.service.ts @@ -34,13 +34,26 @@ export class DayTimeCalendarService { return _config; } - updateDay(current: Moment, day: Moment): Moment { + updateDay(current: Moment, day: Moment, config: IDayTimeCalendarConfig): Moment { const time = current ? current : moment(); - return moment(day.format(DAY_FORMAT) + time.format(TIME_FORMAT), COMBINED_FORMAT); + let updated = moment(day.format(DAY_FORMAT) + time.format(TIME_FORMAT), COMBINED_FORMAT) + + if (config.min) { + const min = config.min; + updated = min.isAfter(updated) ? min : updated; + } + + if (config.max) { + const max = config.max; + updated = max.isBefore(updated) ? max : updated; + } + + return updated; } updateTime(current: Moment, time: Moment): Moment { const day = current ? current : moment(); + return moment(day.format(DAY_FORMAT) + time.format(TIME_FORMAT), COMBINED_FORMAT); } }