-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
calendar.component.ts
211 lines (182 loc) · 6.67 KB
/
calendar.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
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
import { Direction, Directionality } from '@angular/cdk/bidi';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChild,
EventEmitter,
forwardRef,
Input,
OnChanges,
OnDestroy,
OnInit,
Optional,
Output,
SimpleChanges,
TemplateRef,
ViewEncapsulation
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { CandyDate } from 'ng-zorro-antd/core/time';
import { BooleanInput } from 'ng-zorro-antd/core/types';
import { InputBoolean } from 'ng-zorro-antd/core/util';
import {
NzDateCellDirective as DateCell,
NzDateFullCellDirective as DateFullCell,
NzMonthCellDirective as MonthCell,
NzMonthFullCellDirective as MonthFullCell
} from './calendar-cells';
export type NzCalendarMode = 'month' | 'year';
type NzCalendarDateTemplate = TemplateRef<{ $implicit: Date }>;
@Component({
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'nz-calendar',
exportAs: 'nzCalendar',
template: `
<nz-calendar-header
[fullscreen]="nzFullscreen"
[activeDate]="activeDate"
[(mode)]="nzMode"
(modeChange)="onModeChange($event)"
(yearChange)="onYearSelect($event)"
(monthChange)="onMonthSelect($event)"
></nz-calendar-header>
<div class="ant-picker-panel">
<div class="ant-picker-{{ nzMode === 'month' ? 'date' : 'month' }}-panel">
<div class="ant-picker-body">
<ng-container *ngIf="nzMode === 'month'; then monthModeTable; else yearModeTable"></ng-container>
</div>
</div>
</div>
<ng-template #monthModeTable>
<!-- TODO(@wenqi73) [cellRender] [fullCellRender] -->
<date-table
[prefixCls]="prefixCls"
[value]="activeDate"
[activeDate]="activeDate"
[cellRender]="$any(dateCell)"
[fullCellRender]="$any(dateFullCell)"
[disabledDate]="nzDisabledDate"
(valueChange)="onDateSelect($event)"
></date-table>
</ng-template>
<!-- TODO(@wenqi73) [cellRender] [fullCellRender] -->
<ng-template #yearModeTable>
<month-table
[prefixCls]="prefixCls"
[value]="activeDate"
[activeDate]="activeDate"
[cellRender]="$any(monthCell)"
[fullCellRender]="$any(monthFullCell)"
(valueChange)="onDateSelect($event)"
></month-table>
</ng-template>
`,
host: {
class: 'ant-picker-calendar',
'[class.ant-picker-calendar-full]': 'nzFullscreen',
'[class.ant-picker-calendar-mini]': '!nzFullscreen',
'[class.ant-picker-calendar-rtl]': `dir === 'rtl'`
},
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzCalendarComponent), multi: true }]
})
export class NzCalendarComponent implements ControlValueAccessor, OnChanges, OnInit, OnDestroy {
static ngAcceptInputType_nzFullscreen: BooleanInput;
activeDate: CandyDate = new CandyDate();
prefixCls: string = 'ant-picker-calendar';
private destroy$ = new Subject<void>();
dir: Direction = 'ltr';
private onChangeFn: (date: Date) => void = () => {};
private onTouchFn: () => void = () => {};
@Input() nzMode: NzCalendarMode = 'month';
@Input() nzValue?: Date;
@Input() nzDisabledDate?: (date: Date) => boolean;
@Output() readonly nzModeChange: EventEmitter<NzCalendarMode> = new EventEmitter();
@Output() readonly nzPanelChange: EventEmitter<{ date: Date; mode: NzCalendarMode }> = new EventEmitter();
@Output() readonly nzSelectChange: EventEmitter<Date> = new EventEmitter();
@Output() readonly nzValueChange: EventEmitter<Date> = new EventEmitter();
/**
* Cannot use @Input and @ContentChild on one variable
* because { static: false } will make @Input property get delayed
**/
@Input() nzDateCell?: NzCalendarDateTemplate;
@ContentChild(DateCell, { static: false, read: TemplateRef }) nzDateCellChild?: NzCalendarDateTemplate;
get dateCell(): NzCalendarDateTemplate {
return (this.nzDateCell || this.nzDateCellChild)!;
}
@Input() nzDateFullCell?: NzCalendarDateTemplate;
@ContentChild(DateFullCell, { static: false, read: TemplateRef }) nzDateFullCellChild?: NzCalendarDateTemplate;
get dateFullCell(): NzCalendarDateTemplate {
return (this.nzDateFullCell || this.nzDateFullCellChild)!;
}
@Input() nzMonthCell?: NzCalendarDateTemplate;
@ContentChild(MonthCell, { static: false, read: TemplateRef }) nzMonthCellChild?: NzCalendarDateTemplate;
get monthCell(): NzCalendarDateTemplate {
return (this.nzMonthCell || this.nzMonthCellChild)!;
}
@Input() nzMonthFullCell?: NzCalendarDateTemplate;
@ContentChild(MonthFullCell, { static: false, read: TemplateRef }) nzMonthFullCellChild?: NzCalendarDateTemplate;
get monthFullCell(): NzCalendarDateTemplate {
return (this.nzMonthFullCell || this.nzMonthFullCellChild)!;
}
@Input() @InputBoolean() nzFullscreen: boolean = true;
constructor(private cdr: ChangeDetectorRef, @Optional() private directionality: Directionality) {}
ngOnInit(): void {
this.dir = this.directionality.value;
this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.dir = this.directionality.value;
});
}
onModeChange(mode: NzCalendarMode): void {
this.nzModeChange.emit(mode);
this.nzPanelChange.emit({ date: this.activeDate.nativeDate, mode });
}
onYearSelect(year: number): void {
const date = this.activeDate.setYear(year);
this.updateDate(date);
}
onMonthSelect(month: number): void {
const date = this.activeDate.setMonth(month);
this.updateDate(date);
}
onDateSelect(date: CandyDate): void {
// Only activeDate is enough in calendar
// this.value = date;
this.updateDate(date);
}
writeValue(value: Date | null): void {
this.updateDate(new CandyDate(value as Date), false);
this.cdr.markForCheck();
}
registerOnChange(fn: (date: Date) => void): void {
this.onChangeFn = fn;
}
registerOnTouched(fn: () => void): void {
this.onTouchFn = fn;
}
private updateDate(date: CandyDate, touched: boolean = true): void {
this.activeDate = date;
if (touched) {
this.onChangeFn(date.nativeDate);
this.onTouchFn();
this.nzSelectChange.emit(date.nativeDate);
this.nzValueChange.emit(date.nativeDate);
}
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.nzValue) {
this.updateDate(new CandyDate(this.nzValue), false);
}
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}