/
calendar-header.component.ts
169 lines (138 loc) · 4.73 KB
/
calendar-header.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
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
Input,
OnChanges,
OnInit,
Output,
SimpleChanges,
ViewEncapsulation
} from '@angular/core';
import { PanelMode } from '../../standard-types';
import { CandyDate } from '../../../core';
import { DateHelperService, DateHelperByDatePipe } from '../../date-helper.service';
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'calendar-header',
exportAs: 'calendarHeader',
templateUrl: 'calendar-header.component.html'
})
export class CalendarHeaderComponent implements OnInit, OnChanges {
@Input() enablePrev = true;
@Input() enableNext = true;
@Input() disabledMonth: (date: Date) => boolean;
@Input() disabledYear: (date: Date) => boolean;
@Input() value: CandyDate;
@Output() readonly valueChange = new EventEmitter<CandyDate>();
@Input() panelMode: PanelMode;
@Output() readonly panelModeChange = new EventEmitter<PanelMode>();
@Output() readonly chooseDecade = new EventEmitter<CandyDate>();
@Output() readonly chooseYear = new EventEmitter<CandyDate>();
@Output() readonly chooseMonth = new EventEmitter<CandyDate>();
prefixCls = 'thy-calendar';
yearMonthDaySelectors: YearMonthDaySelector[];
// tslint:disable-next-line: max-line-length
private yearToMonth = false; // Indicate whether should change to month panel when current is year panel (if referer=month, it should show month panel when choosed a year)
constructor(private dateHelper: DateHelperService) {}
ngOnInit(): void {
if (!this.value) {
this.value = new CandyDate();
}
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.value) {
this.render();
}
}
previousYear(): void {
this.gotoYear(-1);
}
nextYear(): void {
this.gotoYear(1);
}
previousMonth(): void {
this.gotoMonth(-1);
}
nextMonth(): void {
this.gotoMonth(1);
}
changePanel(mode: PanelMode, value?: CandyDate): void {
this.panelModeChange.emit(mode);
if (value) {
this.changeValueFromInside(value);
}
}
onChooseDecade(value: CandyDate): void {
this.changePanel('year', value);
this.chooseDecade.emit(value);
}
onChooseYear(value: CandyDate): void {
this.changePanel(this.yearToMonth ? 'month' : 'date', value);
this.yearToMonth = false; // Clear
this.chooseYear.emit(value);
}
onChooseMonth(value: CandyDate): void {
this.changePanel('date', value);
this.yearToMonth = false; // Clear
this.chooseMonth.emit(value);
}
changeToMonthPanel(): void {
this.changePanel('month');
this.yearToMonth = true;
}
private render(): void {
if (this.value) {
this.yearMonthDaySelectors = this.createYearMonthDaySelectors();
}
}
private gotoMonth(amount: number): void {
this.changeValueFromInside(this.value.addMonths(amount));
}
private gotoYear(amount: number): void {
this.changeValueFromInside(this.value.addYears(amount));
}
private changeValueFromInside(value: CandyDate): void {
if (this.value !== value) {
this.value = value;
this.valueChange.emit(this.value);
this.render();
}
}
private formatDateTime(format: string): string {
return this.dateHelper.format(this.value.nativeDate, format);
}
private createYearMonthDaySelectors(): YearMonthDaySelector[] {
let year: YearMonthDaySelector;
let month: YearMonthDaySelector;
// NOTE: Compat for DatePipe formatting rules
let yearFormat = 'yyyy年';
if (this.dateHelper.relyOnDatePipe) {
yearFormat = (this.dateHelper as DateHelperByDatePipe).transCompatFormat(yearFormat);
}
year = {
className: `${this.prefixCls}-year-select`,
onClick: () => this.changePanel('year'),
label: this.formatDateTime(yearFormat)
};
month = {
className: `${this.prefixCls}-month-select`,
onClick: () => this.changeToMonthPanel(),
label: this.formatDateTime('MMM')
};
// NOTE: Compat for DatePipe formatting rules
let dayFormat = 'd日';
if (this.dateHelper.relyOnDatePipe) {
dayFormat = (this.dateHelper as DateHelperByDatePipe).transCompatFormat(dayFormat);
}
let result: YearMonthDaySelector[];
result = [year, month];
return result.filter(selector => !!selector);
}
}
export interface YearMonthDaySelector {
className: string;
title?: string;
label: string;
onClick?(): void;
}