-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
abstract-table.ts
127 lines (112 loc) · 4.7 KB
/
abstract-table.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
/**
* 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 { Directive, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChange, SimpleChanges, TemplateRef } from '@angular/core';
import { CandyDate } from 'ng-zorro-antd/core/time';
import { FunctionProp, NzSafeAny } from 'ng-zorro-antd/core/types';
import { isNonEmptyString, isTemplateRef } from 'ng-zorro-antd/core/util';
import { NzCalendarI18nInterface } from 'ng-zorro-antd/i18n';
import { DateBodyRow, DateCell } from './interface';
@Directive()
// eslint-disable-next-line @angular-eslint/directive-class-suffix
export abstract class AbstractTable implements OnInit, OnChanges {
isTemplateRef = isTemplateRef;
isNonEmptyString = isNonEmptyString;
headRow: DateCell[] = [];
bodyRows: DateBodyRow[] = [];
MAX_ROW = 6;
MAX_COL = 7;
@Input() prefixCls: string = 'ant-picker';
@Input() value!: CandyDate;
@Input() locale!: NzCalendarI18nInterface;
@Input() activeDate: CandyDate = new CandyDate();
@Input() showWeek: boolean = false;
@Input() selectedValue: CandyDate[] = []; // Range ONLY
@Input() hoverValue: CandyDate[] = []; // Range ONLY
@Input() disabledDate?: (d: Date) => boolean;
@Input() cellRender?: string | TemplateRef<Date> | FunctionProp<TemplateRef<Date> | string>;
@Input() fullCellRender?: string | TemplateRef<Date> | FunctionProp<TemplateRef<Date> | string>;
@Input() canSelectWeek: boolean = false;
@Output() readonly valueChange = new EventEmitter<CandyDate>();
@Output() readonly cellHover = new EventEmitter<CandyDate>(); // Emitted when hover on a day by mouse enter
protected render(): void {
if (this.activeDate) {
this.headRow = this.makeHeadRow();
this.bodyRows = this.makeBodyRows();
}
}
trackByBodyRow(_index: number, item: DateBodyRow): NzSafeAny {
return item.trackByIndex;
}
trackByBodyColumn(_index: number, item: DateCell): NzSafeAny {
return item.trackByIndex;
}
hasRangeValue(): boolean {
return this.selectedValue?.length > 0 || this.hoverValue?.length > 0;
}
getClassMap(cell: DateCell): { [key: string]: boolean } {
return {
[`ant-picker-cell`]: true,
[`ant-picker-cell-in-view`]: true,
[`ant-picker-cell-selected`]: cell.isSelected,
[`ant-picker-cell-disabled`]: cell.isDisabled,
[`ant-picker-cell-in-range`]: !!cell.isInSelectedRange,
[`ant-picker-cell-range-start`]: !!cell.isSelectedStart,
[`ant-picker-cell-range-end`]: !!cell.isSelectedEnd,
[`ant-picker-cell-range-start-single`]: !!cell.isStartSingle,
[`ant-picker-cell-range-end-single`]: !!cell.isEndSingle,
[`ant-picker-cell-range-hover`]: !!cell.isInHoverRange,
[`ant-picker-cell-range-hover-start`]: !!cell.isHoverStart,
[`ant-picker-cell-range-hover-end`]: !!cell.isHoverEnd,
[`ant-picker-cell-range-hover-edge-start`]: !!cell.isFirstCellInPanel,
[`ant-picker-cell-range-hover-edge-end`]: !!cell.isLastCellInPanel,
[`ant-picker-cell-range-start-near-hover`]: !!cell.isRangeStartNearHover,
[`ant-picker-cell-range-end-near-hover`]: !!cell.isRangeEndNearHover
};
}
abstract makeHeadRow(): DateCell[];
abstract makeBodyRows(): DateBodyRow[];
ngOnInit(): void {
this.render();
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.activeDate && !changes.activeDate.currentValue) {
this.activeDate = new CandyDate();
}
if (
changes.disabledDate ||
changes.locale ||
changes.showWeek ||
changes.selectWeek ||
this.isDateRealChange(changes.activeDate) ||
this.isDateRealChange(changes.value) ||
this.isDateRealChange(changes.selectedValue) ||
this.isDateRealChange(changes.hoverValue)
) {
this.render();
}
}
private isDateRealChange(change: SimpleChange): boolean {
if (change) {
const previousValue: CandyDate | CandyDate[] = change.previousValue;
const currentValue: CandyDate | CandyDate[] = change.currentValue;
if (Array.isArray(currentValue)) {
return (
!Array.isArray(previousValue) ||
currentValue.length !== previousValue.length ||
currentValue.some((value, index) => {
const previousCandyDate = previousValue[index];
return previousCandyDate instanceof CandyDate ? previousCandyDate.isSameDay(value) : previousCandyDate !== value;
})
);
} else {
return !this.isSameDate(previousValue as CandyDate, currentValue);
}
}
return false;
}
private isSameDate(left: CandyDate, right: CandyDate): boolean {
return (!left && !right) || (left && right && right.isSameDay(left));
}
}