Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing #340 #341

Merged
merged 1 commit into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/day-time-calendar/day-time-calendar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
44 changes: 40 additions & 4 deletions src/app/day-time-calendar/day-time-calendar.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
}));
})
);
});
17 changes: 15 additions & 2 deletions src/app/day-time-calendar/day-time-calendar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Moment>config.min;
updated = min.isAfter(updated) ? min : updated;
}

if (config.max) {
const max = <Moment>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);
}
}