-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
bs-datepicker-container.component.ts
123 lines (103 loc) · 3.54 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
import { Component, ElementRef, EventEmitter, OnDestroy, OnInit, Renderer2 } from '@angular/core';
import { BsDatepickerAbstractComponent } from '../../base/bs-datepicker-container';
import { BsDatepickerConfig } from '../../bs-datepicker.config';
import { DayViewModel } from '../../models';
import { BsDatepickerActions } from '../../reducer/bs-datepicker.actions';
import { BsDatepickerEffects } from '../../reducer/bs-datepicker.effects';
import { BsDatepickerStore } from '../../reducer/bs-datepicker.store';
import { PositioningService } from 'ngx-bootstrap/positioning';
import { Subscription } from 'rxjs';
import { datepickerAnimation } from '../../datepicker-animations';
import { take } from 'rxjs/operators';
@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, OnDestroy {
set value(value: Date) {
this._effects.setValue(value);
}
valueChange: EventEmitter<Date> = new EventEmitter<Date>();
animationState = 'void';
_subs: Subscription[] = [];
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 } },
allowedPositions: ['top', 'bottom']
});
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._effects
.init(this._store)
// intial state options
.setOptions(this._config)
// data binding view --> model
.setBindings(this)
// set event handlers
.setEventHandlers(this)
.registerDatepickerSideEffects();
// todo: move it somewhere else
// on selected date change
this._subs.push(
this._store
/* tslint:disable-next-line: no-any */
.select((state: any) => state.selectedDate)
/* tslint:disable-next-line: no-any */
.subscribe((date: any) => this.valueChange.emit(date))
);
}
get isTopPosition(): boolean {
return this._element.nativeElement.classList.contains('top');
}
positionServiceEnable(): void {
this._positionService.enable();
}
daySelectHandler(day: DayViewModel): void {
const isDisabled = this.isOtherMonthsActive ? day.isDisabled : (day.isOtherMonth || day.isDisabled);
if (isDisabled) {
return;
}
this._store.dispatch(this._actions.select(day.date));
}
ngOnDestroy(): void {
for (const sub of this._subs) {
sub.unsubscribe();
}
this._effects.destroy();
}
}