Skip to content

Commit ad98d1b

Browse files
authored
resolves #250 - Documentation for max date Moment|String is wrong (#260)
* resolves #250 - Documentation for max date Moment|String is wrong * fixing weekdays * fixing build
1 parent 94af6c1 commit ad98d1b

19 files changed

+146
-93
lines changed
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import {locale, Moment} from 'moment';
1+
import {Moment} from 'moment';
22

33
export interface ICalendar {
4+
locale?: string;
5+
min?: Moment | string;
6+
max?: Moment | string;
7+
}
8+
9+
export interface ICalendarInternal {
410
locale?: string;
511
min?: Moment;
612
max?: Moment;
7-
}
13+
}

src/app/common/services/utils/utils.service.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,11 @@ describe('Service: ObUtilsService', () => {
9494
moment().add(1, 'd'))
9595
).toBeFalsy();
9696
}));
97+
98+
it('should convertPropsToMoment method', inject([UtilsService], (service: UtilsService) => {
99+
const obj = {min: '14-01-1987', max: '14-01-1987'};
100+
service.convertPropsToMoment(obj, 'DD-MM-YYYY', ['min', 'max']);
101+
expect(moment.isMoment(obj.min)).toBeTruthy();
102+
expect(moment.isMoment(obj.max)).toBeTruthy();
103+
}));
97104
});

src/app/common/services/utils/utils.service.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,14 @@ export class UtilsService {
3232
return new Array(size).fill(1);
3333
}
3434

35-
convertToMoment(date: SingleCalendarValue, format: string): Moment | null {
36-
let retVal: Moment;
35+
convertToMoment(date: SingleCalendarValue, format: string): Moment {
3736
if (!date) {
3837
return null;
3938
} else if (typeof date === 'string') {
40-
retVal = moment(date, format);
39+
return moment(date, format);
4140
} else {
42-
retVal = date;
41+
return date;
4342
}
44-
45-
return retVal;
4643
}
4744

4845
isDateValid(date: string, format: string): boolean {
@@ -263,7 +260,10 @@ export class UtilsService {
263260
.map(d => moment(d, format));
264261
}
265262

266-
shouldShowCurrent(showGoToCurrent: boolean, mode: CalendarMode, min: Moment, max: Moment): boolean {
263+
shouldShowCurrent(showGoToCurrent: boolean,
264+
mode: CalendarMode,
265+
min: Moment,
266+
max: Moment): boolean {
267267
return showGoToCurrent &&
268268
mode !== 'time' &&
269269
this.isDateInRange(moment(), min, max);
@@ -272,4 +272,12 @@ export class UtilsService {
272272
isDateInRange(date: Moment, from: Moment, to: Moment): boolean {
273273
return date.isBetween(from, to, 'day', '[]');
274274
}
275+
276+
convertPropsToMoment(obj: {[key: string]: any}, format: string, props: string[]) {
277+
props.forEach((prop) => {
278+
if (obj.hasOwnProperty(prop)) {
279+
obj[prop] = this.convertToMoment(obj[prop], format);
280+
}
281+
});
282+
}
275283
}
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import {TDrops, TOpens} from '../common/types/poistions.type';
2-
import {IDayCalendarConfig} from '../day-calendar/day-calendar-config.model';
3-
import {IMonthCalendarConfig} from '../month-calendar/month-calendar-config';
4-
import {ITimeSelectConfig} from '../time-select/time-select-config.model';
2+
import {IDayCalendarConfig, IDayCalendarConfigInternal} from '../day-calendar/day-calendar-config.model';
3+
import {IMonthCalendarConfig, IMonthCalendarConfigInternal} from '../month-calendar/month-calendar-config';
4+
import {ITimeSelectConfig, ITimeSelectConfigInternal} from '../time-select/time-select-config.model';
55

6-
export interface IDatePickerConfig extends IDayCalendarConfig,
7-
IMonthCalendarConfig,
8-
ITimeSelectConfig {
6+
export interface IConfig {
97
closeOnSelect?: boolean;
108
closeOnSelectDelay?: number;
119
openOnFocus?: boolean;
@@ -18,3 +16,16 @@ export interface IDatePickerConfig extends IDayCalendarConfig,
1816
opens?: TOpens;
1917
hideInputContainer?: boolean;
2018
}
19+
20+
export interface IDatePickerConfig extends IConfig,
21+
IDayCalendarConfig,
22+
IMonthCalendarConfig,
23+
ITimeSelectConfig {
24+
25+
}
26+
27+
export interface IDatePickerConfigInternal extends IConfig,
28+
IDayCalendarConfigInternal,
29+
IMonthCalendarConfigInternal,
30+
ITimeSelectConfigInternal {
31+
}

src/app/date-picker/date-picker.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {DayTimeCalendarService} from '../day-time-calendar/day-time-calendar.ser
1414
import {ITimeSelectConfig} from '../time-select/time-select-config.model';
1515
import {TimeSelectComponent} from '../time-select/time-select.component';
1616
import {TimeSelectService} from '../time-select/time-select.service';
17-
import {IDatePickerConfig} from './date-picker-config.model';
17+
import {IDatePickerConfig, IDatePickerConfigInternal} from './date-picker-config.model';
1818
import {IDpDayPickerApi} from './date-picker.api';
1919
import {DatePickerService} from './date-picker.service';
2020
import {
@@ -93,10 +93,9 @@ export class DatePickerComponent implements OnChanges,
9393

9494
@ViewChild('container') calendarContainer: ElementRef;
9595
@ViewChild('dayCalendar') dayCalendarRef: DayCalendarComponent;
96-
@ViewChild('monthCalendar') monthCalendarRef: DayCalendarComponent;
9796
@ViewChild('timeSelect') timeSelectRef: TimeSelectComponent;
9897

99-
componentConfig: IDatePickerConfig;
98+
componentConfig: IDatePickerConfigInternal;
10099
dayCalendarConfig: IDayCalendarConfig;
101100
dayTimeCalendarConfig: IDayTimeCalendarConfig;
102101
timeSelectConfig: ITimeSelectConfig;

src/app/date-picker/date-picker.directive.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('Directive: DpDayPicker', () => {
1717
});
1818
});
1919

20-
const directive = new DatePickerDirective(null, null, null, null);
20+
const directive = new DatePickerDirective(null, null, null, null, null);
2121

2222
it('should create an instance', () => {
2323
expect(directive).toBeTruthy();

src/app/date-picker/date-picker.service.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {EventEmitter, Injectable} from '@angular/core';
2-
import {IDatePickerConfig} from './date-picker-config.model';
2+
import {IDatePickerConfig, IDatePickerConfigInternal} from './date-picker-config.model';
33
import * as moment from 'moment';
44
import {Moment} from 'moment';
55
import {UtilsService} from '../common/services/utils/utils.service';
@@ -12,7 +12,7 @@ import {CalendarMode} from '../common/types/calendar-mode';
1212
@Injectable()
1313
export class DatePickerService {
1414
readonly onPickerClosed: EventEmitter<null> = new EventEmitter();
15-
private defaultConfig: IDatePickerConfig = {
15+
private defaultConfig: IDatePickerConfigInternal = {
1616
closeOnSelect: true,
1717
closeOnSelectDelay: 100,
1818
format: 'DD-MM-YYYY',
@@ -33,21 +33,14 @@ export class DatePickerService {
3333
}
3434

3535
// todo:: add unit tests
36-
getConfig(config: IDatePickerConfig, mode: CalendarMode = 'daytime'): IDatePickerConfig {
37-
const _config: IDatePickerConfig = {
36+
getConfig(config: IDatePickerConfig, mode: CalendarMode = 'daytime'): IDatePickerConfigInternal {
37+
const _config = <IDatePickerConfigInternal>{
3838
...this.defaultConfig,
3939
format: this.getDefaultFormatByMode(mode),
4040
...this.utilsService.clearUndefined(config)
4141
};
4242

43-
const {min, max, format} = _config;
44-
if (min) {
45-
_config.min = this.utilsService.convertToMoment(min, format);
46-
}
47-
48-
if (max) {
49-
_config.max = this.utilsService.convertToMoment(max, format);
50-
}
43+
this.utilsService.convertPropsToMoment(_config, _config.format, ['min', 'max']);
5144

5245
if (config && config.allowMultiSelect && config.closeOnSelect === undefined) {
5346
_config.closeOnSelect = false;

src/app/day-calendar/day-calendar-config.model.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {ICalendar} from '../common/models/calendar.model';
1+
import {ICalendar, ICalendarInternal} from '../common/models/calendar.model';
22
import {WeekDays} from '../common/types/week-days.type';
33
import {Moment} from 'moment';
44
import {ECalendarValue} from '../common/types/calendar-value-enum';
55

6-
export interface IDayCalendarConfig extends ICalendar {
6+
export interface IConfig {
77
isDayDisabledCallback?: (date: Moment) => boolean;
88
isMonthDisabledCallback?: (date: Moment) => boolean;
99
weekDayFormat?: string;
@@ -29,3 +29,11 @@ export interface IDayCalendarConfig extends ICalendar {
2929
returnedValueType?: ECalendarValue;
3030
showGoToCurrent?: boolean;
3131
}
32+
33+
export interface IDayCalendarConfig extends IConfig,
34+
ICalendar {
35+
}
36+
37+
export interface IDayCalendarConfigInternal extends IConfig,
38+
ICalendarInternal {
39+
}

src/app/day-calendar/day-calendar.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class="dp-calendar-day"
2727
*ngFor="let day of week"
2828
(click)="dayClicked(day)"
29-
[disabled]="isDisabledDay(day)"
29+
[disabled]="day.disabled"
3030
[ngClass]="getDayBtnCssClass(day)">
3131
{{getDayBtnText(day)}}
3232
</button>

src/app/day-calendar/day-calendar.component.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import {DayCalendarService} from './day-calendar.service';
1717
import * as moment from 'moment';
1818
import {Moment} from 'moment';
19-
import {IDayCalendarConfig} from './day-calendar-config.model';
19+
import {IDayCalendarConfig, IDayCalendarConfigInternal} from './day-calendar-config.model';
2020
import {IDay} from './day.model';
2121
import {
2222
ControlValueAccessor,
@@ -65,7 +65,7 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
6565

6666
CalendarMode = ECalendarMode;
6767
isInited: boolean = false;
68-
componentConfig: IDayCalendarConfig;
68+
componentConfig: IDayCalendarConfigInternal;
6969
_selected: Moment[];
7070
weeks: IDay[][];
7171
weekdays: Moment[];
@@ -193,10 +193,6 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
193193
this.onChangeCallback(this.processOnChangeCallback(this.selected));
194194
}
195195

196-
isDisabledDay(day: IDay) {
197-
return this.dayCalendarService.isDateDisabled(day, this.componentConfig);
198-
}
199-
200196
dayClicked(day: IDay) {
201197
this.selected = this.utilsService
202198
.updateSelected(this.componentConfig.allowMultiSelect, this.selected, day);

0 commit comments

Comments
 (0)