Skip to content

Commit fc8a53e

Browse files
committed
2 parents 5ee80d6 + c0315c1 commit fc8a53e

File tree

7 files changed

+69
-220
lines changed

7 files changed

+69
-220
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,60 @@ export class UtilsService {
163163
const match = <HTMLElement>element.querySelector(selector);
164164
return match || this.closestParent(element.parentElement, selector);
165165
}
166+
167+
createValidator({minDate, maxDate}, format: string, granularity: unitOfTime.Base): (inputVal: CalendarValue) => {[key: string]: any} {
168+
let isValid: boolean;
169+
let value: Moment[];
170+
const validators = [];
171+
172+
if (minDate) {
173+
const md = this.convertToMoment(minDate, format);
174+
validators.push({
175+
key: 'minDate',
176+
isValid: () => {
177+
const _isValid = value.every(val => val.isSameOrAfter(md, granularity));
178+
isValid = isValid ? _isValid : false;
179+
return _isValid;
180+
}
181+
});
182+
}
183+
184+
if (maxDate) {
185+
const md = this.convertToMoment(maxDate, format);
186+
validators.push({
187+
key: 'maxDate',
188+
isValid: () => {
189+
const _isValid = value.every(val => val.isSameOrBefore(md, granularity));
190+
isValid = isValid ? _isValid : false;
191+
return _isValid;
192+
}
193+
});
194+
}
195+
196+
return (inputVal: CalendarValue) => {
197+
isValid = true;
198+
199+
value = this.convertToMomentArray(inputVal, format, true).filter(Boolean);
200+
201+
if (!value.every(val => val.isValid())) {
202+
return {
203+
format: {
204+
given: inputVal
205+
}
206+
};
207+
}
208+
209+
const errors = validators.reduce((map, err) => {
210+
if (!err.isValid()) {
211+
map[err.key] = {
212+
given: value
213+
};
214+
}
215+
216+
return map;
217+
}, {});
218+
219+
return !isValid ? errors : null;
220+
};
221+
}
166222
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class DatePickerComponent implements OnChanges,
9191
popupElem: HTMLElement;
9292
handleInnerElementClickUnlisteners: Function[] = [];
9393
globalListnersUnlisteners: Function[] = [];
94-
validateFn: (FormControl, string) => {[key: string]: any};
94+
validateFn: (inputVal: CalendarValue) => {[key: string]: any};
9595
api: IDpDayPickerApi = {
9696
open: this.showCalendars.bind(this),
9797
close: this.hideCalendar.bind(this)
@@ -194,7 +194,7 @@ export class DatePickerComponent implements OnChanges,
194194

195195
validate(formControl: FormControl): ValidationErrors | any {
196196
if (this.minDate || this.maxDate) {
197-
return this.validateFn(formControl, this.componentConfig.format);
197+
return this.validateFn(formControl.value);
198198
} else {
199199
return () => null;
200200
}
@@ -205,10 +205,8 @@ export class DatePickerComponent implements OnChanges,
205205
}
206206

207207
initValidators() {
208-
this.validateFn = this.dayPickerService.createValidator({
209-
minDate: this.utilsService.convertToMoment(this.minDate, this.componentConfig.format),
210-
maxDate: this.utilsService.convertToMoment(this.maxDate, this.componentConfig.format)
211-
}, this.componentConfig.format);
208+
this.validateFn = this.utilsService.createValidator(
209+
{minDate: this.minDate, maxDate: this.maxDate}, this.componentConfig.format, this.type);
212210

213211
this.onChangeCallback(this.processOnChangeCallback(this.selected));
214212
}

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

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -68,73 +68,6 @@ export class DatePickerService {
6868
};
6969
}
7070

71-
createValidator({minDate, maxDate}, dateFormat: string): (FormControl, string) => {[key: string]: any} {
72-
let isValid: boolean;
73-
let value: Moment[];
74-
const validators = [];
75-
76-
if (minDate) {
77-
validators.push({
78-
key: 'minDate',
79-
isValid: () => {
80-
const _isValid = value.every(val => val.isSameOrAfter(minDate, 'day'));
81-
isValid = isValid ? _isValid : false;
82-
return _isValid;
83-
}
84-
});
85-
}
86-
87-
if (maxDate) {
88-
validators.push({
89-
key: 'maxDate',
90-
isValid: () => {
91-
const _isValid = value.every(val => val.isSameOrBefore(maxDate, 'day'));
92-
isValid = isValid ? _isValid : false;
93-
return _isValid;
94-
}
95-
});
96-
}
97-
98-
return function validateInput(formControl: FormControl, format: string) {
99-
isValid = true;
100-
101-
if (formControl.value) {
102-
if (typeof formControl.value === 'string') {
103-
const dateStrings = formControl.value.split(',').map(date => date.trim());
104-
const validDateStrings = dateStrings
105-
.filter(date => this.utilsService.isDateValid(date, format));
106-
value = validDateStrings.map(dateString => moment(dateString, dateFormat));
107-
} else if (!Array.isArray(formControl.value)) {
108-
value = [formControl.value];
109-
} else {
110-
value = formControl.value.map(val => this.utilsService.convertToMoment(val, dateFormat));
111-
}
112-
} else {
113-
return null;
114-
}
115-
116-
if (!value.every(val => val.isValid())) {
117-
return {
118-
format: {
119-
given: formControl.value
120-
}
121-
};
122-
}
123-
124-
const errors = validators.reduce((map, err) => {
125-
if (!err.isValid()) {
126-
map[err.key] = {
127-
given: value
128-
};
129-
}
130-
131-
return map;
132-
}, {});
133-
134-
return !isValid ? errors : null;
135-
};
136-
}
137-
13871
pickerClosed() {
13972
this.onPickerClosed.emit();
14073
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
6868
currentDateView: Moment;
6969
inputValue: CalendarValue;
7070
inputValueType: ECalendarValue;
71-
validateFn: (FormControl, string) => {[key: string]: any};
71+
validateFn: (inputVal: CalendarValue) => {[key: string]: any};
7272
currentCalendarType: ECalendarType = ECalendarType.Day;
7373
monthCalendarConfig: IMonthCalendarConfig;
7474

@@ -147,7 +147,7 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
147147

148148
validate(formControl: FormControl): ValidationErrors | any {
149149
if (this.minDate || this.maxDate) {
150-
return this.validateFn(formControl, this.componentConfig.format);
150+
return this.validateFn(formControl.value);
151151
} else {
152152
return () => null;
153153
}
@@ -158,10 +158,8 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
158158
}
159159

160160
initValidators() {
161-
this.validateFn = this.dayCalendarService.createValidator({
162-
minDate: this.utilsService.convertToMoment(this.minDate, this.componentConfig.format),
163-
maxDate: this.utilsService.convertToMoment(this.maxDate, this.componentConfig.format)
164-
}, this.componentConfig.format);
161+
this.validateFn = this.utilsService.createValidator(
162+
{minDate: this.minDate, maxDate: this.maxDate}, this.componentConfig.format, 'day');
165163

166164
this.onChangeCallback(this.processOnChangeCallback(this.selected));
167165
}

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

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -120,73 +120,6 @@ export class DayCalendarService {
120120
return !!(config.max && day.date.isAfter(config.max, 'day'));
121121
}
122122

123-
createValidator({minDate, maxDate}, dateFormat: string): (FormControl, string) => {[key: string]: any} {
124-
let isValid: boolean;
125-
let value: Moment[];
126-
const validators = [];
127-
128-
if (minDate) {
129-
validators.push({
130-
key: 'minDate',
131-
isValid: () => {
132-
const _isValid = value.every(val => val.isSameOrAfter(minDate, 'day'));
133-
isValid = isValid ? _isValid : false;
134-
return _isValid;
135-
}
136-
});
137-
}
138-
139-
if (maxDate) {
140-
validators.push({
141-
key: 'maxDate',
142-
isValid: () => {
143-
const _isValid = value.every(val => val.isSameOrBefore(maxDate, 'day'));
144-
isValid = isValid ? _isValid : false;
145-
return _isValid;
146-
}
147-
});
148-
}
149-
150-
return function validateInput(formControl: FormControl, format: string) {
151-
isValid = true;
152-
153-
if (formControl.value) {
154-
if (typeof formControl.value === 'string') {
155-
const dateStrings = formControl.value.split(',').map(date => date.trim());
156-
const validDateStrings = dateStrings
157-
.filter(date => this.utilsService.isDateValid(date, format));
158-
value = validDateStrings.map(dateString => moment(dateString, dateFormat));
159-
} else if (!Array.isArray(formControl.value)) {
160-
value = [formControl.value];
161-
} else {
162-
value = formControl.value.map(val => this.utilsService.convertToMoment(val, dateFormat));
163-
}
164-
} else {
165-
return null;
166-
}
167-
168-
if (!value.every(val => val.isValid())) {
169-
return {
170-
format: {
171-
given: formControl.value
172-
}
173-
};
174-
}
175-
176-
const errors = validators.reduce((map, err) => {
177-
if (!err.isValid()) {
178-
map[err.key] = {
179-
given: value
180-
};
181-
}
182-
183-
return map;
184-
}, {});
185-
186-
return !isValid ? errors : null;
187-
};
188-
}
189-
190123
// todo:: add unit tests
191124
getHeaderLabel(config: IDayCalendarConfig, month: Moment): string {
192125
if (config.monthFormatter) {
@@ -237,4 +170,4 @@ export class DayCalendarService {
237170

238171
return day.format(config.dayBtnFormat);
239172
}
240-
}
173+
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class MonthCalendarComponent implements OnInit, OnChanges, ControlValueAc
5959
currentDateView: Moment;
6060
inputValue: CalendarValue;
6161
inputValueType: ECalendarValue;
62-
validateFn: (FormControl, string) => {[key: string]: any};
62+
validateFn: (inputVal: CalendarValue) => {[key: string]: any};
6363

6464
set selected(selected: Moment[]) {
6565
this._selected = selected;
@@ -125,7 +125,7 @@ export class MonthCalendarComponent implements OnInit, OnChanges, ControlValueAc
125125

126126
validate(formControl: FormControl): ValidationErrors | any {
127127
if (this.minDate || this.maxDate) {
128-
return this.validateFn(formControl, this.componentConfig.format);
128+
return this.validateFn(formControl.value);
129129
} else {
130130
return () => null;
131131
}
@@ -136,10 +136,8 @@ export class MonthCalendarComponent implements OnInit, OnChanges, ControlValueAc
136136
}
137137

138138
initValidators() {
139-
this.validateFn = this.monthCalendarService.createValidator({
140-
minDate: this.utilsService.convertToMoment(this.minDate, this.componentConfig.format),
141-
maxDate: this.utilsService.convertToMoment(this.maxDate, this.componentConfig.format)
142-
}, this.componentConfig.format);
139+
this.validateFn = this.validateFn = this.utilsService.createValidator(
140+
{minDate: this.minDate, maxDate: this.maxDate}, this.componentConfig.format, 'month');
143141

144142
this.onChangeCallback(this.processOnChangeCallback(this.selected));
145143
}

src/app/month-calendar/month-calendar.service.ts

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -48,73 +48,6 @@ export class MonthCalendarService {
4848
return !!(config.max && month.date.isAfter(config.max, 'month'));
4949
}
5050

51-
createValidator({minDate, maxDate}, dateFormat: string): (FormControl, string) => {[key: string]: any} {
52-
let isValid: boolean;
53-
let value: Moment[];
54-
const validators = [];
55-
56-
if (minDate) {
57-
validators.push({
58-
key: 'minDate',
59-
isValid: () => {
60-
const _isValid = value.every(val => val.isSameOrAfter(minDate, 'month'));
61-
isValid = isValid ? _isValid : false;
62-
return _isValid;
63-
}
64-
});
65-
}
66-
67-
if (maxDate) {
68-
validators.push({
69-
key: 'maxDate',
70-
isValid: () => {
71-
const _isValid = value.every(val => val.isSameOrBefore(maxDate, 'month'));
72-
isValid = isValid ? _isValid : false;
73-
return _isValid;
74-
}
75-
});
76-
}
77-
78-
return function validateInput(formControl: FormControl, format: string) {
79-
isValid = true;
80-
81-
if (formControl.value) {
82-
if (typeof formControl.value === 'string') {
83-
const dateStrings = formControl.value.split(',').map(date => date.trim());
84-
const validDateStrings = dateStrings
85-
.filter(date => this.utilsService.isDateValid(date, format));
86-
value = validDateStrings.map(dateString => moment(dateString, dateFormat));
87-
} else if (!Array.isArray(formControl.value)) {
88-
value = [formControl.value];
89-
} else {
90-
value = formControl.value.map(val => this.utilsService.convertToMoment(val, dateFormat));
91-
}
92-
} else {
93-
return null;
94-
}
95-
96-
if (!value.every(val => val.isValid())) {
97-
return {
98-
format: {
99-
given: formControl.value
100-
}
101-
};
102-
}
103-
104-
const errors = validators.reduce((map, err) => {
105-
if (!err.isValid()) {
106-
map[err.key] = {
107-
given: value
108-
};
109-
}
110-
111-
return map;
112-
}, {});
113-
114-
return !isValid ? errors : null;
115-
};
116-
}
117-
11851
shouldShowLeft(min: Moment, currentMonthView: Moment): boolean {
11952
return min ? min.isBefore(currentMonthView, 'year') : true;
12053
}

0 commit comments

Comments
 (0)