-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
abstract-picker.component.ts
313 lines (272 loc) · 10.1 KB
/
abstract-picker.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
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
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* 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 {
ChangeDetectorRef,
EventEmitter,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
SimpleChanges,
TemplateRef,
ViewChild
} from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
import {
CandyDate,
cloneDate,
CompatibleValue,
FunctionProp,
InputBoolean,
NzNoAnimationDirective,
valueFunctionProp,
warnDeprecation
} from 'ng-zorro-antd/core';
import { DateHelperService, NzDatePickerI18nInterface, NzI18nService } from 'ng-zorro-antd/i18n';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { DatePickerService } from './date-picker.service';
import { NzPickerComponent } from './picker.component';
import { CompatibleDate, DisabledTimeFn, PanelMode, PresetRanges, SupportTimeOptions } from './standard-types';
const POPUP_STYLE_PATCH = { position: 'relative' }; // Aim to override antd's style to support overlay's position strategy (position:absolute will cause it not working beacuse the overlay can't get the height/width of it's content)
/**
* The base picker for all common APIs
*/
export abstract class AbstractPickerComponent implements OnInit, OnChanges, OnDestroy, ControlValueAccessor {
isRange: boolean = false; // Indicate whether the value is a range value
showWeek: boolean = false; // Should show as week picker
focused: boolean = false;
extraFooter: TemplateRef<void> | string;
hostClassMap = {};
protected destroyed$: Subject<void> = new Subject();
protected isCustomPlaceHolder: boolean = false;
// --- Common API
@Input() @InputBoolean() nzAllowClear: boolean = true;
@Input() @InputBoolean() nzAutoFocus: boolean = false;
@Input() @InputBoolean() nzDisabled: boolean = false;
@Input() @InputBoolean() nzOpen: boolean;
/**
* @deprecated 10.0.0. This is deprecated and going to be removed in 10.0.0.
*/
@Input() nzClassName: string;
@Input() nzDisabledDate: (d: Date) => boolean;
@Input() nzLocale: NzDatePickerI18nInterface;
@Input() nzPlaceHolder: string | string[];
@Input() nzPopupStyle: object = POPUP_STYLE_PATCH;
@Input() nzDropdownClassName: string;
@Input() nzSize: 'large' | 'small';
/**
* @deprecated 10.0.0. This is deprecated and going to be removed in 10.0.0.
*/
@Input() nzStyle: object;
@Input() nzFormat: string;
@Input() nzDateRender: FunctionProp<TemplateRef<Date> | string>;
@Input() nzDisabledTime: DisabledTimeFn;
@Input() nzRenderExtraFooter: FunctionProp<TemplateRef<void> | string>;
@Input() @InputBoolean() nzShowToday: boolean = true;
@Input() nzMode: PanelMode | PanelMode[] = 'date';
@Input() nzRanges: PresetRanges;
@Input() nzDefaultPickerValue: CompatibleDate | null = null;
@Input() nzShowTime: SupportTimeOptions | boolean = false;
@Input() nzSeparator: string = '~';
@Output() readonly nzOnPanelChange = new EventEmitter<PanelMode | PanelMode[]>();
@Output() readonly nzOnCalendarChange = new EventEmitter<Array<Date | null>>();
@Output() readonly nzOnOk = new EventEmitter<CompatibleDate | null>();
@Output() readonly nzOnOpenChange = new EventEmitter<boolean>();
@ViewChild(NzPickerComponent, { static: true }) protected picker: NzPickerComponent;
get realOpenState(): boolean {
return this.picker.animationOpenState;
} // Use picker's real open state to let re-render the picker's content when shown up
updateHostClass(): void {
this.hostClassMap = {
[`ant-picker`]: true,
[`ant-picker-range`]: this.isRange,
[`ant-picker-large`]: this.nzSize === 'large',
[`ant-picker-small`]: this.nzSize === 'small',
[`ant-picker-focused`]: this.focused,
[`ant-picker-disabled`]: this.nzDisabled
};
}
constructor(
public datePickerService: DatePickerService,
protected i18n: NzI18nService,
protected cdr: ChangeDetectorRef,
protected dateHelper: DateHelperService,
public noAnimation?: NzNoAnimationDirective
) {}
ngOnInit(): void {
// Subscribe the every locale change if the nzLocale is not handled by user
if (!this.nzLocale) {
this.i18n.localeChange.pipe(takeUntil(this.destroyed$)).subscribe(() => this.setLocale());
}
// Default value
this.datePickerService.isRange = this.isRange;
this.datePickerService.initValue();
this.datePickerService.emitValue$.pipe(takeUntil(this.destroyed$)).subscribe(_ => {
const value = this.datePickerService.value;
this.datePickerService.initialValue = cloneDate(value);
// this.datePickerService.activeDate = cloneDate(value);
if (this.isRange) {
const vAsRange = value as CandyDate[];
if (vAsRange.length) {
this.onChangeFn([vAsRange[0].nativeDate, vAsRange[1].nativeDate]);
} else {
this.onChangeFn([]);
}
} else {
if (value) {
this.onChangeFn((value as CandyDate).nativeDate);
} else {
this.onChangeFn(null);
}
}
this.onTouchedFn();
// When value emitted, overlay will be closed
this.picker.hideOverlay();
});
this.updateHostClass();
this.updatePickerStyle();
// Default format when it's empty
if (!this.nzFormat) {
if (this.showWeek) {
this.nzFormat = 'yyyy-ww'; // Format for week
} else {
this.nzFormat = this.nzShowTime ? 'yyyy-MM-dd HH:mm:ss' : 'yyyy-MM-dd';
}
}
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.nzSize || changes.nzDisabled) {
this.updateHostClass();
}
if (changes.nzPopupStyle) {
// Always assign the popup style patch
this.nzPopupStyle = this.nzPopupStyle ? { ...this.nzPopupStyle, ...POPUP_STYLE_PATCH } : POPUP_STYLE_PATCH;
}
// Mark as customized placeholder by user once nzPlaceHolder assigned at the first time
if (changes.nzPlaceHolder && changes.nzPlaceHolder.firstChange && typeof this.nzPlaceHolder !== 'undefined') {
this.isCustomPlaceHolder = true;
}
if (changes.nzLocale) {
// The nzLocale is currently handled by user
this.setDefaultPlaceHolder();
}
if (changes.nzRenderExtraFooter) {
this.extraFooter = valueFunctionProp(this.nzRenderExtraFooter);
}
if (changes.nzStyle) {
warnDeprecation(
`'nzStyle' in DatePicker is going to be removed in 10.0.0. Please use CSS style attribute like <nz-date-picker style="..."></nz-date-picker> instead.`
);
this.updatePickerStyle();
}
if (changes.nzClassName) {
warnDeprecation(
`'nzClassName' in DatePicker is going to be removed in 10.0.0. Please use CSS class attribute like <nz-date-picker class="..."></nz-date-picker> instead.`
);
}
if (changes.nzMode) {
this.setPanelMode();
}
}
ngOnDestroy(): void {
this.destroyed$.next();
this.destroyed$.complete();
}
setPanelMode(): void {
if (!this.nzMode) {
this.nzMode = this.isRange ? ['date', 'date'] : 'date';
}
}
updatePickerStyle(): void {
this.nzStyle = { display: 'inherit', width: '100%', ...this.nzStyle };
}
/**
* Triggered when overlayOpen changes (different with realOpenState)
* @param open The overlayOpen in picker component
*/
onOpenChange(open: boolean): void {
this.nzOnOpenChange.emit(open);
}
// ------------------------------------------------------------------------
// | Control value accessor implements
// ------------------------------------------------------------------------
// NOTE: onChangeFn/onTouchedFn will not be assigned if user not use as ngModel
onChangeFn: (val: CompatibleDate | null) => void = () => void 0;
onTouchedFn: () => void = () => void 0;
writeValue(value: CompatibleDate): void {
this.setValue(value);
this.cdr.markForCheck();
}
// tslint:disable-next-line:no-any
registerOnChange(fn: any): void {
this.onChangeFn = fn;
}
// tslint:disable-next-line:no-any
registerOnTouched(fn: any): void {
this.onTouchedFn = fn;
}
// ------------------------------------------------------------------------
// | Internal methods
// ------------------------------------------------------------------------
// Reload locale from i18n with side effects
private setLocale(): void {
this.nzLocale = this.i18n.getLocaleData('DatePicker', {});
this.setDefaultPlaceHolder();
this.cdr.markForCheck();
}
private setDefaultPlaceHolder(): void {
if (!this.isCustomPlaceHolder && this.nzLocale) {
this.nzPlaceHolder = this.isRange ? this.nzLocale.lang.rangePlaceholder : this.nzLocale.lang.placeholder;
}
}
// Safe way of setting value with default
private setValue(value: CompatibleDate): void {
const newValue: CompatibleValue = this.datePickerService.makeValue(value);
this.datePickerService.setValue(newValue);
this.datePickerService.initialValue = newValue;
}
get realShowToday(): boolean {
// Range not support nzShowToday currently
return !this.isRange && this.nzShowToday;
}
onFocusChange(value: boolean): void {
this.focused = value;
this.updateHostClass();
}
onPanelModeChange(panelMode: PanelMode | PanelMode[]): void {
// this.nzMode = panelMode;
this.nzOnPanelChange.emit(panelMode);
}
// Emit nzOnCalendarChange when select date by nz-range-picker
onCalendarChange(value: CandyDate[]): void {
if (this.isRange) {
const rangeValue = value.filter(x => x instanceof CandyDate).map(x => x.nativeDate);
this.nzOnCalendarChange.emit(rangeValue);
}
}
// Emitted when done with date selecting
onResultOk(): void {
if (this.isRange) {
const value = this.datePickerService.value as CandyDate[];
if (value.length) {
this.nzOnOk.emit([value[0].nativeDate, value[1].nativeDate]);
} else {
this.nzOnOk.emit([]);
}
} else {
if (this.datePickerService.value) {
this.nzOnOk.emit((this.datePickerService.value as CandyDate).nativeDate);
} else {
this.nzOnOk.emit(null);
}
}
this.datePickerService.emitValue$.next();
}
}