-
Notifications
You must be signed in to change notification settings - Fork 667
/
range.directive.ts
196 lines (181 loc) · 5.71 KB
/
range.directive.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
import {
AfterViewInit,
ComponentRef,
Directive,
EventEmitter,
Host,
Input,
OnDestroy,
Optional,
Output,
TemplateRef,
ViewContainerRef
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { AlainConfigService, AlainDateRangePickerShortcut, AlainDateRangePickerShortcutItem } from '@delon/util/config';
import { fixEndTimeOfRange, getTimeDistance } from '@delon/util/date-time';
import { assert, deepMergeKey } from '@delon/util/other';
import type { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzDatePickerComponent, NzRangePickerComponent } from 'ng-zorro-antd/date-picker';
import { DatePickerService } from 'ng-zorro-antd/date-picker/date-picker.service';
import { RangePickerShortcutTplComponent } from './range-shortcut.component';
@Directive({
selector: 'nz-range-picker[extend]',
exportAs: 'extendRangePicker',
standalone: true
})
export class RangePickerDirective implements OnDestroy, AfterViewInit {
static ngAcceptInputType_shortcut: AlainDateRangePickerShortcut | string | null;
private defaultShortcuts: AlainDateRangePickerShortcut;
private _shortcut: AlainDateRangePickerShortcut | null = null;
private shortcutFactory: ComponentRef<RangePickerShortcutTplComponent> | null = null;
start: Date | null = null;
end: Date | null = null;
@Input()
set shortcut(val: AlainDateRangePickerShortcut | null) {
const item = deepMergeKey(
{ list: [] },
true,
this.defaultShortcuts,
val == null ? {} : val
) as AlainDateRangePickerShortcut;
if (typeof val !== 'object') {
item.enabled = val !== false;
}
(item.list || []).forEach(i => {
i._text = this.dom.bypassSecurityTrustHtml(i.text);
});
this._shortcut = item;
this.refreshShortcut();
}
get shortcut(): AlainDateRangePickerShortcut | null {
return this._shortcut;
}
@Input({ required: true }) ngModelEnd: NzSafeAny;
@Output() readonly ngModelEndChange = new EventEmitter<NzSafeAny>();
private get dp(): NzDatePickerComponent {
return this.nativeComp.datePicker;
}
private get srv(): DatePickerService {
return this.dp.datePickerService;
}
constructor(
private dom: DomSanitizer,
configSrv: AlainConfigService,
@Host() @Optional() private nativeComp: NzRangePickerComponent,
private vcr: ViewContainerRef
) {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
assert(
!!nativeComp,
`It should be attached to nz-range-picker component, for example: '<nz-range-picker [(ngModel)]="i.start" extend [(ngModelEnd)]="i.end" shortcut></nz-range-picker>'`
);
}
const cog = configSrv.merge('dataRange', {
nzFormat: 'yyyy-MM-dd',
nzAllowClear: true,
nzAutoFocus: false,
nzPopupStyle: { position: 'relative' },
nzShowToday: true,
shortcuts: {
enabled: false,
closed: true,
list: [
{
text: '今天',
fn: () => getTimeDistance('today')
},
{
text: '昨天',
fn: () => getTimeDistance('yesterday')
},
{
text: '近3天',
fn: () => getTimeDistance(-2)
},
{
text: '近7天',
fn: () => getTimeDistance(-6)
},
{
text: '本周',
fn: () => getTimeDistance('week')
},
{
text: '本月',
fn: () => getTimeDistance('month')
},
{
text: '全年',
fn: () => getTimeDistance('year')
}
]
}
})!;
this.defaultShortcuts = { ...cog.shortcuts } as AlainDateRangePickerShortcut;
Object.assign(this, cog);
}
private cd(): void {
(this.dp as NzSafeAny).cdr.markForCheck();
}
private overrideNative(): void {
const dp = this.dp;
dp.writeValue = (value: Date) => {
const dates = (value && this.ngModelEnd ? [value, this.ngModelEnd] : []).filter(w => !!w);
this.srv.setValue(this.srv.makeValue(dates));
this.start = dates.length > 0 ? dates[0] : null;
this.end = dates.length > 0 ? dates[1] : null;
this.cd();
};
const oldOnChangeFn = dp.onChangeFn;
dp.onChangeFn = (list: Array<Date | null>) => {
let start: Date | null = null;
let end: Date | null = null;
if (list.length > 0 && list.filter(w => w != null).length === 2) {
[start, end] = fixEndTimeOfRange([list[0]!, list[1]!]);
}
this.start = start;
this.end = end;
oldOnChangeFn(start);
this.ngModelEnd = end;
this.ngModelEndChange.emit(end);
};
}
private refreshShortcut(): void {
if (!this._shortcut) {
return;
}
const { enabled, list } = this._shortcut;
let extraFooter: TemplateRef<NzSafeAny> | undefined;
if (!this.nativeComp || !enabled) {
extraFooter = undefined;
} else {
if (!this.shortcutFactory) {
this.shortcutFactory = this.vcr.createComponent(RangePickerShortcutTplComponent);
}
const { instance } = this.shortcutFactory;
instance.list = list!;
instance.click = (item: AlainDateRangePickerShortcutItem) => {
const res = item.fn([this.start, this.end]);
this.srv.setValue(this.srv.makeValue(res as Date[]));
this.dp.onChangeFn(res);
this.dp.close();
};
extraFooter = instance.tpl;
}
this.nativeComp.datePicker.extraFooter = extraFooter;
Promise.resolve().then(() => this.cd());
}
ngAfterViewInit(): void {
this.overrideNative();
this.refreshShortcut();
}
private destoryShortcut(): void {
if (this.shortcutFactory != null) {
this.shortcutFactory.destroy();
}
}
ngOnDestroy(): void {
this.destoryShortcut();
}
}