forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline.component.ts
177 lines (153 loc) 路 5.44 KB
/
timeline.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
/**
* 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 {
AfterContentInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChildren,
Input,
OnChanges,
OnDestroy,
OnInit,
Optional,
QueryList,
SimpleChange,
SimpleChanges,
TemplateRef,
ViewEncapsulation
} from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { NzTimelineItemComponent } from './timeline-item.component';
import { TimelineService } from './timeline.service';
import { NzTimelineMode, NzTimelinePosition } from './typings';
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
selector: 'nz-timeline',
providers: [TimelineService],
exportAs: 'nzTimeline',
template: `
<ul
class="ant-timeline"
[class.ant-timeline-label]="hasLabelItem"
[class.ant-timeline-right]="!hasLabelItem && nzMode === 'right'"
[class.ant-timeline-alternate]="nzMode === 'alternate' || nzMode === 'custom'"
[class.ant-timeline-pending]="!!nzPending"
[class.ant-timeline-reverse]="nzReverse"
[class.ant-timeline-rtl]="dir === 'rtl'"
>
<!-- pending dot (reversed) -->
<ng-container *ngIf="nzReverse" [ngTemplateOutlet]="pendingTemplate"></ng-container>
<!-- timeline items -->
<ng-container *ngFor="let item of timelineItems">
<ng-template [ngTemplateOutlet]="item.template"></ng-template>
</ng-container>
<ng-container *ngIf="!nzReverse" [ngTemplateOutlet]="pendingTemplate"></ng-container>
<!-- pending dot -->
</ul>
<ng-template #pendingTemplate>
<li *ngIf="nzPending" class="ant-timeline-item ant-timeline-item-pending">
<div class="ant-timeline-item-tail"></div>
<div class="ant-timeline-item-head ant-timeline-item-head-custom ant-timeline-item-head-blue">
<ng-container *nzStringTemplateOutlet="nzPendingDot">
{{ nzPendingDot }}
<i *ngIf="!nzPendingDot" nz-icon nzType="loading"></i>
</ng-container>
</div>
<div class="ant-timeline-item-content">
<ng-container *nzStringTemplateOutlet="nzPending">
{{ isPendingBoolean ? '' : nzPending }}
</ng-container>
</div>
</li>
</ng-template>
<!-- Grasp items -->
<ng-content></ng-content>
`
})
export class NzTimelineComponent implements AfterContentInit, OnChanges, OnDestroy, OnInit {
@ContentChildren(NzTimelineItemComponent) listOfItems!: QueryList<NzTimelineItemComponent>;
@Input() nzMode: NzTimelineMode = 'left';
@Input() nzPending?: string | boolean | TemplateRef<void>;
@Input() nzPendingDot?: string | TemplateRef<void>;
@Input() nzReverse: boolean = false;
isPendingBoolean: boolean = false;
timelineItems: NzTimelineItemComponent[] = [];
dir: Direction = 'ltr';
hasLabelItem = false;
private destroy$ = new Subject<void>();
constructor(
private cdr: ChangeDetectorRef,
private timelineService: TimelineService,
@Optional() private directionality: Directionality
) {}
ngOnChanges(changes: SimpleChanges): void {
const { nzMode, nzReverse, nzPending } = changes;
if (simpleChangeActivated(nzMode) || simpleChangeActivated(nzReverse)) {
this.updateChildren();
}
if (nzPending) {
this.isPendingBoolean = nzPending.currentValue === true;
}
}
ngOnInit(): void {
this.timelineService.check$.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.cdr.markForCheck();
});
this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
this.dir = direction;
this.cdr.detectChanges();
});
this.dir = this.directionality.value;
}
ngAfterContentInit(): void {
this.updateChildren();
this.listOfItems.changes.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.updateChildren();
});
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
private updateChildren(): void {
if (this.listOfItems && this.listOfItems.length) {
const length = this.listOfItems.length;
let hasLabelItem = false;
this.listOfItems.forEach((item: NzTimelineItemComponent, index: number) => {
item.isLast = !this.nzReverse ? index === length - 1 : index === 0;
item.position = getInferredTimelineItemPosition(index, this.nzMode);
if (!hasLabelItem && item.nzLabel) {
hasLabelItem = true;
}
item.detectChanges();
});
this.timelineItems = this.nzReverse ? this.listOfItems.toArray().reverse() : this.listOfItems.toArray();
this.hasLabelItem = hasLabelItem;
} else {
this.timelineItems = [];
this.hasLabelItem = false;
}
this.cdr.markForCheck();
}
}
function simpleChangeActivated(simpleChange?: SimpleChange): boolean {
return !!(simpleChange && (simpleChange.previousValue !== simpleChange.currentValue || simpleChange.isFirstChange()));
}
function getInferredTimelineItemPosition(index: number, mode: NzTimelineMode): NzTimelinePosition | undefined {
return mode === 'custom'
? undefined
: mode === 'left'
? 'left'
: mode === 'right'
? 'right'
: mode === 'alternate' && index % 2 === 0
? 'left'
: 'right';
}