forked from ng-bootstrap/ng-bootstrap
/
datepicker-service.ts
299 lines (250 loc) · 10.1 KB
/
datepicker-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import {NgbCalendar} from './ngb-calendar';
import {NgbDate} from './ngb-date';
import {NgbDateStruct} from './ngb-date-struct';
import {DatepickerViewModel, NgbDayTemplateData, NgbMarkDisabled} from './datepicker-view-model';
import {Injectable} from '@angular/core';
import {isInteger, toInteger} from '../util/util';
import {Observable, Subject} from 'rxjs';
import {
buildMonths,
checkDateInRange,
checkMinBeforeMax,
isChangedDate,
isChangedMonth,
isDateSelectable,
generateSelectBoxYears,
generateSelectBoxMonths,
prevMonthDisabled,
nextMonthDisabled
} from './datepicker-tools';
import {filter} from 'rxjs/operators';
import {NgbDatepickerI18n} from './datepicker-i18n';
@Injectable()
export class NgbDatepickerService {
private _model$ = new Subject<DatepickerViewModel>();
private _dateSelect$ = new Subject<NgbDate>();
private _state: DatepickerViewModel = {
disabled: false,
displayMonths: 1,
firstDayOfWeek: 1,
focusVisible: false,
months: [],
navigation: 'select',
outsideDays: 'visible',
prevDisabled: false,
nextDisabled: false,
selectBoxes: {years: [], months: []},
selectedDate: null
};
get model$(): Observable<DatepickerViewModel> { return this._model$.pipe(filter(model => model.months.length > 0)); }
get dateSelect$(): Observable<NgbDate> { return this._dateSelect$.pipe(filter(date => date !== null)); }
/** @deprecated please use `dateSelect$` */
get select$(): Observable<NgbDate> { return this.dateSelect$; }
set dayTemplateData(dayTemplateData: NgbDayTemplateData) {
if (this._state.dayTemplateData !== dayTemplateData) {
this._nextState({dayTemplateData});
}
}
set disabled(disabled: boolean) {
if (this._state.disabled !== disabled) {
this._nextState({disabled});
}
}
set displayMonths(displayMonths: number) {
displayMonths = toInteger(displayMonths);
if (isInteger(displayMonths) && displayMonths > 0 && this._state.displayMonths !== displayMonths) {
this._nextState({displayMonths});
}
}
set firstDayOfWeek(firstDayOfWeek: number) {
firstDayOfWeek = toInteger(firstDayOfWeek);
if (isInteger(firstDayOfWeek) && firstDayOfWeek >= 0 && this._state.firstDayOfWeek !== firstDayOfWeek) {
this._nextState({firstDayOfWeek});
}
}
set focusVisible(focusVisible: boolean) {
if (this._state.focusVisible !== focusVisible && !this._state.disabled) {
this._nextState({focusVisible});
}
}
set maxDate(date: NgbDate) {
const maxDate = this.toValidDate(date, null);
if (isChangedDate(this._state.maxDate, maxDate)) {
this._nextState({maxDate});
}
}
set markDisabled(markDisabled: NgbMarkDisabled) {
if (this._state.markDisabled !== markDisabled) {
this._nextState({markDisabled});
}
}
set minDate(date: NgbDate) {
const minDate = this.toValidDate(date, null);
if (isChangedDate(this._state.minDate, minDate)) {
this._nextState({minDate});
}
}
set navigation(navigation: 'select' | 'arrows' | 'none') {
if (this._state.navigation !== navigation) {
this._nextState({navigation});
}
}
set outsideDays(outsideDays: 'visible' | 'collapsed' | 'hidden') {
if (this._state.outsideDays !== outsideDays) {
this._nextState({outsideDays});
}
}
constructor(private _calendar: NgbCalendar, private _i18n: NgbDatepickerI18n) {}
focus(date: NgbDate) {
if (!this._state.disabled && this._calendar.isValid(date) && isChangedDate(this._state.focusDate, date)) {
this._nextState({focusDate: date});
}
}
focusSelect() {
if (isDateSelectable(this._state.focusDate, this._state)) {
this.select(this._state.focusDate, {emitEvent: true});
}
}
open(date: NgbDate) {
const firstDate = this.toValidDate(date, this._calendar.getToday());
if (!this._state.disabled && (!this._state.firstDate || isChangedMonth(this._state.firstDate, date))) {
this._nextState({firstDate});
}
}
select(date: NgbDate, options: {emitEvent?: boolean} = {}) {
const selectedDate = this.toValidDate(date, null);
if (!this._state.disabled) {
if (isChangedDate(this._state.selectedDate, selectedDate)) {
this._nextState({selectedDate});
}
if (options.emitEvent && isDateSelectable(selectedDate, this._state)) {
this._dateSelect$.next(selectedDate);
}
}
}
toValidDate(date: NgbDateStruct, defaultValue?: NgbDate): NgbDate {
const ngbDate = NgbDate.from(date);
if (defaultValue === undefined) {
defaultValue = this._calendar.getToday();
}
return this._calendar.isValid(ngbDate) ? ngbDate : defaultValue;
}
private _nextState(patch: Partial<DatepickerViewModel>) {
const newState = this._updateState(patch);
this._patchContexts(newState);
this._state = newState;
this._model$.next(this._state);
}
private _patchContexts(state: DatepickerViewModel) {
const {months, displayMonths, selectedDate, focusDate, focusVisible, disabled, outsideDays} = state;
state.months.forEach(month => {
month.weeks.forEach(week => {
week.days.forEach(day => {
// patch focus flag
if (focusDate) {
day.context.focused = focusDate.equals(day.date) && focusVisible;
}
// calculating tabindex
day.tabindex = !disabled && day.date.equals(focusDate) && focusDate.month === month.number ? 0 : -1;
// override context disabled
if (disabled === true) {
day.context.disabled = true;
}
// patch selection flag
if (selectedDate !== undefined) {
day.context.selected = selectedDate !== null && selectedDate.equals(day.date);
}
// visibility
if (month.number !== day.date.month) {
day.hidden = outsideDays === 'hidden' || outsideDays === 'collapsed' ||
(displayMonths > 1 && day.date.after(months[0].firstDate) &&
day.date.before(months[displayMonths - 1].lastDate));
}
});
});
});
}
private _updateState(patch: Partial<DatepickerViewModel>): DatepickerViewModel {
// patching fields
const state = Object.assign({}, this._state, patch);
let startDate = state.firstDate;
// min/max dates changed
if ('minDate' in patch || 'maxDate' in patch) {
checkMinBeforeMax(state.minDate, state.maxDate);
state.focusDate = checkDateInRange(state.focusDate, state.minDate, state.maxDate);
state.firstDate = checkDateInRange(state.firstDate, state.minDate, state.maxDate);
startDate = state.focusDate;
}
// disabled
if ('disabled' in patch) {
state.focusVisible = false;
}
// initial rebuild via 'select()'
if ('selectedDate' in patch && this._state.months.length === 0) {
startDate = state.selectedDate;
}
// terminate early if only focus visibility was changed
if ('focusVisible' in patch) {
return state;
}
// focus date changed
if ('focusDate' in patch) {
state.focusDate = checkDateInRange(state.focusDate, state.minDate, state.maxDate);
startDate = state.focusDate;
// nothing to rebuild if only focus changed and it is still visible
if (state.months.length !== 0 && !state.focusDate.before(state.firstDate) &&
!state.focusDate.after(state.lastDate)) {
return state;
}
}
// first date changed
if ('firstDate' in patch) {
state.firstDate = checkDateInRange(state.firstDate, state.minDate, state.maxDate);
startDate = state.firstDate;
}
// rebuilding months
if (startDate) {
const forceRebuild = 'dayTemplateData' in patch || 'firstDayOfWeek' in patch || 'markDisabled' in patch ||
'minDate' in patch || 'maxDate' in patch || 'disabled' in patch || 'outsideDays' in patch;
const months = buildMonths(this._calendar, startDate, state, this._i18n, forceRebuild);
// updating months and boundary dates
state.months = months;
state.firstDate = months.length > 0 ? months[0].firstDate : undefined;
state.lastDate = months.length > 0 ? months[months.length - 1].lastDate : undefined;
// reset selected date if 'markDisabled' returns true
if ('selectedDate' in patch && !isDateSelectable(state.selectedDate, state)) {
state.selectedDate = null;
}
// adjusting focus after months were built
if ('firstDate' in patch) {
if (state.focusDate === undefined || state.focusDate.before(state.firstDate) ||
state.focusDate.after(state.lastDate)) {
state.focusDate = startDate;
}
}
// adjusting months/years for the select box navigation
const yearChanged = !this._state.firstDate || this._state.firstDate.year !== state.firstDate.year;
const monthChanged = !this._state.firstDate || this._state.firstDate.month !== state.firstDate.month;
if (state.navigation === 'select') {
// years -> boundaries (min/max were changed)
if ('minDate' in patch || 'maxDate' in patch || state.selectBoxes.years.length === 0 || yearChanged) {
state.selectBoxes.years = generateSelectBoxYears(state.firstDate, state.minDate, state.maxDate);
}
// months -> when current year or boundaries change
if ('minDate' in patch || 'maxDate' in patch || state.selectBoxes.months.length === 0 || yearChanged) {
state.selectBoxes.months =
generateSelectBoxMonths(this._calendar, state.firstDate, state.minDate, state.maxDate);
}
} else {
state.selectBoxes = {years: [], months: []};
}
// updating navigation arrows -> boundaries change (min/max) or month/year changes
if ((state.navigation === 'arrows' || state.navigation === 'select') &&
(monthChanged || yearChanged || 'minDate' in patch || 'maxDate' in patch || 'disabled' in patch)) {
state.prevDisabled = state.disabled || prevMonthDisabled(this._calendar, state.firstDate, state.minDate);
state.nextDisabled = state.disabled || nextMonthDisabled(this._calendar, state.lastDate, state.maxDate);
}
}
return state;
}
}