-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathbs-datepicker.component.ts
More file actions
383 lines (337 loc) · 10.7 KB
/
bs-datepicker.component.ts
File metadata and controls
383 lines (337 loc) · 10.7 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import {
AfterViewInit,
ComponentRef,
Directive,
ElementRef,
EventEmitter, HostBinding,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
Renderer2,
SimpleChanges,
ViewContainerRef
} from '@angular/core';
import { ComponentLoader, ComponentLoaderFactory } from 'ngx-bootstrap/component-loader';
import { PositioningService } from 'ngx-bootstrap/positioning';
import { BehaviorSubject, Observable, Subject, Subscription } from 'rxjs';
import { filter, takeUntil } from 'rxjs/operators';
import { BsDatepickerConfig } from './bs-datepicker.config';
import { BsDatepickerViewMode, DatepickerDateCustomClasses, DatepickerDateTooltipText } from './models';
import { BsDatepickerContainerComponent } from './themes/bs/bs-datepicker-container.component';
import { copyTime } from './utils/copy-time-utils';
import { checkBsValue, setCurrentTimeOnDateSelect } from './utils/bs-calendar-utils';
export let previousDate: Date | Date[] | undefined;
@Directive({
selector: '[bsDatepicker]',
exportAs: 'bsDatepicker',
providers: [ComponentLoaderFactory, PositioningService],
standalone: true
})
export class BsDatepickerDirective implements OnInit, OnDestroy, OnChanges, AfterViewInit {
/**
* Placement of a datepicker. Accepts: "top", "bottom", "left", "right"
*/
@Input() placement: 'top' | 'bottom' | 'left' | 'right' = 'bottom';
/**
* Specifies events that should trigger. Supports a space separated list of
* event names.
*/
@Input() triggers = 'click';
/**
* Close datepicker on outside click
*/
@Input() outsideClick = true;
/**
* A selector specifying the element the datepicker should be appended to.
*/
@Input() container = 'body';
@Input() outsideEsc = true;
/**
* Emits an event when the datepicker is shown
*/
@Output() onShown: EventEmitter<unknown>;
/**
* Emits an event when the datepicker is hidden
*/
@Output() onHidden: EventEmitter<unknown>;
isOpen$: BehaviorSubject<boolean>;
isDestroy$ = new Subject();
/**
* Indicates whether datepicker's content is enabled or not
*/
@Input() isDisabled = false;
/**
* Minimum date which is available for selection
*/
@Input() minDate?: Date;
/**
* Maximum date which is available for selection
*/
@Input() maxDate?: Date;
/**
* Ignore validation errors when you reset to minDate or maxDate
*/
@Input() ignoreMinMaxErrors?: boolean;
/**
* Minimum view mode : day, month, or year
*/
@Input() minMode?: BsDatepickerViewMode;
/**
* Disable Certain days in the week
*/
@Input() daysDisabled?: number[];
/**
* Disable specific dates
*/
@Input() datesDisabled?: Date[];
/**
* Enable specific dates
*/
@Input() datesEnabled?: Date[];
/**
* Date custom classes
*/
@Input() dateCustomClasses?: DatepickerDateCustomClasses[];
/**
* Date tooltip text
*/
@Input() dateTooltipTexts?: DatepickerDateTooltipText[];
/**
* Emits when datepicker value has been changed
*/
@Output() bsValueChange: EventEmitter<Date> = new EventEmitter();
@HostBinding ('attr.readonly') get readonlyValue () {
return this.isDisabled ? '' : null;
}
protected _subs: Subscription[] = [];
private _datepicker: ComponentLoader<BsDatepickerContainerComponent>;
private _datepickerRef?: ComponentRef<BsDatepickerContainerComponent>;
private readonly _dateInputFormat$ = new Subject<string | undefined>();
constructor(public _config: BsDatepickerConfig,
private _elementRef: ElementRef,
private _renderer: Renderer2,
_viewContainerRef: ViewContainerRef,
cis: ComponentLoaderFactory) {
// todo: assign only subset of fields
Object.assign(this, this._config);
this._datepicker = cis.createLoader<BsDatepickerContainerComponent>(
_elementRef,
_viewContainerRef,
_renderer
);
this.onShown = this._datepicker.onShown;
this.onHidden = this._datepicker.onHidden;
this.isOpen$ = new BehaviorSubject(this.isOpen);
}
/**
* Returns whether or not the datepicker is currently being shown
*/
@Input()
get isOpen(): boolean {
return this._datepicker.isShown;
}
set isOpen(value: boolean) {
this.isOpen$.next(value);
}
_bsValue?: Date;
/**
* Initial value of datepicker
*/
@Input()
set bsValue(value: Date | undefined) {
if (this._bsValue && value && this._bsValue.getTime() === value.getTime()) {
return;
}
if (!this._bsValue && value && !this._config.withTimepicker) {
if (this._config.initCurrentTime) {
const now = new Date();
copyTime(value, now);
}
}
if (value && this._config?.initCurrentTime) {
value = setCurrentTimeOnDateSelect(value);
}
this.initPreviousValue();
this._bsValue = value;
this.bsValueChange.emit(value);
}
get dateInputFormat$(): Observable<string | undefined> {
return this._dateInputFormat$;
}
/**
* Config object for datepicker
*/
@Input() bsConfig?: Partial<BsDatepickerConfig>;
ngOnInit(): void {
this._datepicker.listen({
outsideClick: this.outsideClick,
outsideEsc: this.outsideEsc,
triggers: this.triggers,
show: () => this.show()
});
this.setConfig();
this.initPreviousValue();
}
initPreviousValue() {
previousDate = this._bsValue;
}
ngOnChanges(changes: SimpleChanges): void {
if (changes["bsConfig"]) {
if (changes["bsConfig"].currentValue?.initCurrentTime && changes["bsConfig"].currentValue?.initCurrentTime !== changes["bsConfig"].previousValue?.initCurrentTime && this._bsValue) {
this.initPreviousValue();
this._bsValue = setCurrentTimeOnDateSelect(this._bsValue);
this.bsValueChange.emit(this._bsValue);
}
this.setConfig();
this._dateInputFormat$.next(this.bsConfig && this.bsConfig.dateInputFormat);
}
if (!this._datepickerRef || !this._datepickerRef.instance) {
return;
}
if (changes["minDate"]) {
this._datepickerRef.instance.minDate = this.minDate;
}
if (changes["maxDate"]) {
this._datepickerRef.instance.maxDate = this.maxDate;
}
if (changes["daysDisabled"]) {
this._datepickerRef.instance.daysDisabled = this.daysDisabled;
}
if (changes["datesDisabled"]) {
this._datepickerRef.instance.datesDisabled = this.datesDisabled;
}
if (changes["datesEnabled"]) {
this._datepickerRef.instance.datesEnabled = this.datesEnabled;
}
if (changes["isDisabled"]) {
this._datepickerRef.instance.isDisabled = this.isDisabled;
}
if (changes["dateCustomClasses"]) {
this._datepickerRef.instance.dateCustomClasses = this.dateCustomClasses;
}
if (changes["dateTooltipTexts"]) {
this._datepickerRef.instance.dateTooltipTexts = this.dateTooltipTexts;
}
}
initSubscribes() {
// if date changes from external source (model -> view)
this._subs.push(
this.bsValueChange.subscribe((value: Date) => {
if (this._datepickerRef) {
this._datepickerRef.instance.value = value;
}
})
);
// if date changes from picker (view -> model)
if (this._datepickerRef) {
this._subs.push(
this._datepickerRef.instance.valueChange.subscribe((value: Date) => {
this.initPreviousValue();
this.bsValue = value;
if (this.keepDatepickerModalOpened()) {
return;
}
this.hide();
})
);
}
}
keepDatepickerModalOpened(): boolean {
if (!previousDate || !this.bsConfig?.keepDatepickerOpened || !this._config.withTimepicker) {
return false;
}
return this.isDateSame();
}
isDateSame(): boolean {
return (previousDate instanceof Date
&& (this._bsValue?.getDate() === previousDate?.getDate())
&& (this._bsValue?.getMonth() === previousDate?.getMonth())
&& (this._bsValue?.getFullYear() === previousDate?.getFullYear()));
}
ngAfterViewInit(): void {
this.isOpen$.pipe(
filter(isOpen => isOpen !== this.isOpen),
takeUntil(this.isDestroy$)
)
.subscribe(() => this.toggle());
}
/**
* Opens an element’s datepicker. This is considered a “manual” triggering of
* the datepicker.
*/
show(): void {
if (this._datepicker.isShown) {
return;
}
this.setConfig();
this._datepickerRef = this._datepicker
.provide({ provide: BsDatepickerConfig, useValue: this._config })
.attach(BsDatepickerContainerComponent)
.to(this.container)
.position({ attachment: this.placement })
.show({ placement: this.placement });
this.initSubscribes();
}
/**
* Closes an element’s datepicker. This is considered a “manual” triggering of
* the datepicker.
*/
hide(): void {
if (this.isOpen) {
this._datepicker.hide();
}
for (const sub of this._subs) {
sub.unsubscribe();
}
if (this._config.returnFocusToInput) {
this._renderer.selectRootElement(this._elementRef.nativeElement).focus();
}
}
/**
* Toggles an element’s datepicker. This is considered a “manual” triggering
* of the datepicker.
*/
toggle(): void {
if (this.isOpen) {
return this.hide();
}
this.show();
}
/**
* Set config for datepicker
*/
setConfig(): void {
this._config = Object.assign({}, this._config, this.bsConfig, {
value: this._config.keepDatesOutOfRules ? this._bsValue : checkBsValue(this._bsValue, this.maxDate || this.bsConfig && this.bsConfig.maxDate),
isDisabled: this.isDisabled,
minDate: this.minDate || this.bsConfig && this.bsConfig.minDate,
maxDate: this.maxDate || this.bsConfig && this.bsConfig.maxDate,
daysDisabled: this.daysDisabled || this.bsConfig && this.bsConfig.daysDisabled,
dateCustomClasses: this.dateCustomClasses || this.bsConfig && this.bsConfig.dateCustomClasses,
dateTooltipTexts: this.dateTooltipTexts || this.bsConfig && this.bsConfig.dateTooltipTexts,
datesDisabled: this.datesDisabled || this.bsConfig && this.bsConfig.datesDisabled,
datesEnabled: this.datesEnabled || this.bsConfig && this.bsConfig.datesEnabled,
minMode: this.minMode || this.bsConfig && this.bsConfig.minMode,
initCurrentTime: this.bsConfig?.initCurrentTime,
keepDatepickerOpened: this.bsConfig?.keepDatepickerOpened,
keepDatesOutOfRules: this.bsConfig?.keepDatesOutOfRules
});
}
unsubscribeSubscriptions() {
if (this._subs?.length) {
this._subs.map(sub => sub.unsubscribe());
this._subs.length = 0;
}
}
ngOnDestroy(): void {
this._datepicker.dispose();
this.isOpen$.next(false);
if (this.isDestroy$) {
this.isDestroy$.next(null);
this.isDestroy$.complete();
}
this.unsubscribeSubscriptions();
}
}