/
bs-datepicker-container.component.ts
239 lines (204 loc) · 6.38 KB
/
bs-datepicker-container.component.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
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
HostBinding,
OnDestroy,
OnInit,
Renderer2,
ViewChild
} from '@angular/core';
import { take } from 'rxjs/operators';
import { Subscription } from 'rxjs';
import { getFullYear, getMonth } from 'ngx-bootstrap/chronos';
import { PositioningService } from 'ngx-bootstrap/positioning';
import { TimepickerComponent } from 'ngx-bootstrap/timepicker';
import { datepickerAnimation } from '../../datepicker-animations';
import { BsDatepickerAbstractComponent } from '../../base/bs-datepicker-container';
import { BsDatepickerConfig } from '../../bs-datepicker.config';
import { CalendarCellViewModel, DayViewModel } from '../../models';
import { BsDatepickerActions } from '../../reducer/bs-datepicker.actions';
import { BsDatepickerEffects } from '../../reducer/bs-datepicker.effects';
import { BsDatepickerStore } from '../../reducer/bs-datepicker.store';
@Component({
selector: 'bs-datepicker-container',
providers: [BsDatepickerStore, BsDatepickerEffects],
templateUrl: './bs-datepicker-view.html',
host: {
class: 'bottom',
'(click)': '_stopPropagation($event)',
role: 'dialog',
'aria-label': 'calendar'
},
animations: [datepickerAnimation]
})
export class BsDatepickerContainerComponent
extends BsDatepickerAbstractComponent
implements OnInit, AfterViewInit, OnDestroy
{
valueChange: EventEmitter<Date> = new EventEmitter<Date>();
animationState = 'void';
override isRangePicker = false;
_subs: Subscription[] = [];
@ViewChild('startTP') startTimepicker?: TimepickerComponent;
set value(value: Date | undefined) {
this._effects?.setValue(value);
}
get isDatePickerDisabled(): boolean {
return !!this._config.isDisabled;
}
@HostBinding('attr.disabled') get isDatepickerDisabled() {
return this.isDatePickerDisabled ? '' : null;
}
@HostBinding('attr.readonly') get isDatepickerReadonly() {
return this.isDatePickerDisabled ? '' : null;
}
constructor(
_renderer: Renderer2,
private _config: BsDatepickerConfig,
private _store: BsDatepickerStore,
private _element: ElementRef,
private _actions: BsDatepickerActions,
_effects: BsDatepickerEffects,
private _positionService: PositioningService
) {
super();
this._effects = _effects;
_renderer.setStyle(_element.nativeElement, 'display', 'block');
_renderer.setStyle(_element.nativeElement, 'position', 'absolute');
}
ngOnInit(): void {
this._positionService.setOptions({
modifiers: {
flip: {
enabled: this._config.adaptivePosition
},
preventOverflow: {
enabled: this._config.adaptivePosition
}
},
allowedPositions: this._config.allowedPositions
});
this._positionService.event$?.pipe(take(1)).subscribe(() => {
this._positionService.disable();
if (this._config.isAnimated) {
this.animationState = this.isTopPosition ? 'animated-up' : 'animated-down';
return;
}
this.animationState = 'unanimated';
});
this.isOtherMonthsActive = this._config.selectFromOtherMonth;
this.containerClass = this._config.containerClass;
this.showTodayBtn = this._config.showTodayButton;
this.todayBtnLbl = this._config.todayButtonLabel;
this.todayPos = this._config.todayPosition;
this.showClearBtn = this._config.showClearButton;
this.clearBtnLbl = this._config.clearButtonLabel;
this.clearPos = this._config.clearPosition;
this.customRangeBtnLbl = this._config.customRangeButtonLabel;
this.withTimepicker = this._config.withTimepicker;
this._effects
?.init(this._store)
// intial state options
.setOptions(this._config)
// data binding view --> model
.setBindings(this)
// set event handlers
.setEventHandlers(this)
.registerDatepickerSideEffects();
let currentDate: Date | undefined;
// todo: move it somewhere else
// on selected date change
this._subs.push(
this._store
.select((state) => state.selectedDate)
.subscribe((date) => {
currentDate = date;
this.valueChange.emit(date);
})
);
this._subs.push(
this._store
.select((state) => state.selectedTime)
.subscribe((time) => {
if (!time || !time[0] || !(time[0] instanceof Date) || time[0] === currentDate) {
return;
}
this.valueChange.emit(time[0]);
})
);
this._store.dispatch(this._actions.changeViewMode(this._config.startView));
}
ngAfterViewInit(): void {
this.selectedTimeSub.add(
this.selectedTime?.subscribe((val) => {
if (Array.isArray(val) && val.length >= 1) {
this.startTimepicker?.writeValue(val[0]);
}
})
);
this.startTimepicker?.registerOnChange((val) => {
this.timeSelectHandler(val, 0);
});
}
get isTopPosition(): boolean {
return this._element.nativeElement.classList.contains('top');
}
positionServiceEnable(): void {
this._positionService.enable();
}
override timeSelectHandler(date: Date, index: number) {
this._store.dispatch(this._actions.selectTime(date, index));
}
override daySelectHandler(day: DayViewModel): void {
if (!day) {
return;
}
const isDisabled = this.isOtherMonthsActive ? day.isDisabled : day.isOtherMonth || day.isDisabled;
if (isDisabled) {
return;
}
this._store.dispatch(this._actions.select(day.date));
}
override monthSelectHandler(day: CalendarCellViewModel): void {
if (!day || day.isDisabled) {
return;
}
this._store.dispatch(
this._actions.navigateTo({
unit: {
month: getMonth(day.date),
year: getFullYear(day.date)
},
viewMode: 'day'
})
);
}
override yearSelectHandler(day: CalendarCellViewModel): void {
if (!day || day.isDisabled) {
return;
}
this._store.dispatch(
this._actions.navigateTo({
unit: {
year: getFullYear(day.date)
},
viewMode: 'month'
})
);
}
override setToday(): void {
this._store.dispatch(this._actions.select(new Date()));
}
override clearDate(): void {
this._store.dispatch(this._actions.select(undefined));
}
ngOnDestroy(): void {
for (const sub of this._subs) {
sub.unsubscribe();
}
this.selectedTimeSub.unsubscribe();
this._effects?.destroy();
}
}