-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathbadge.component.ts
145 lines (135 loc) · 4.85 KB
/
badge.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
/**
* 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 {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Host,
Input,
OnChanges,
OnDestroy,
OnInit,
Optional,
Renderer2,
SimpleChanges,
TemplateRef,
ViewEncapsulation
} from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { zoomBadgeMotion } from 'ng-zorro-antd/core/animation';
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
import { BooleanInput, NzSafeAny, NzSizeDSType } from 'ng-zorro-antd/core/types';
import { InputBoolean } from 'ng-zorro-antd/core/util';
import { badgePresetColors } from './preset-colors';
import { NzBadgeStatusType } from './types';
const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'badge';
@Component({
selector: 'nz-badge',
exportAs: 'nzBadge',
preserveWhitespaces: false,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
animations: [zoomBadgeMotion],
template: `
<ng-container *ngIf="nzStatus || nzColor">
<span
class="ant-badge-status-dot ant-badge-status-{{ nzStatus || presetColor }}"
[style.background]="!presetColor && nzColor"
[ngStyle]="nzStyle"
></span>
<span class="ant-badge-status-text">
<ng-container *nzStringTemplateOutlet="nzText">{{ nzText }}</ng-container>
</span>
</ng-container>
<ng-content></ng-content>
<ng-container *nzStringTemplateOutlet="nzCount">
<nz-badge-sup
*ngIf="showSup"
[nzOffset]="nzOffset"
[nzSize]="nzSize"
[nzTitle]="nzTitle"
[nzStyle]="nzStyle"
[nzDot]="nzDot"
[nzOverflowCount]="nzOverflowCount"
[disableAnimation]="!!(nzStandalone || nzStatus || nzColor || noAnimation?.nzNoAnimation)"
[nzCount]="nzCount"
[noAnimation]="!!noAnimation?.nzNoAnimation"
></nz-badge-sup>
</ng-container>
`,
host: {
class: 'ant-badge',
'[class.ant-badge-status]': 'nzStatus',
'[class.ant-badge-not-a-wrapper]': '!!(nzStandalone || nzStatus || nzColor)'
}
})
export class NzBadgeComponent implements OnChanges, OnDestroy, OnInit {
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
static ngAcceptInputType_nzShowZero: BooleanInput;
static ngAcceptInputType_nzShowDot: BooleanInput;
static ngAcceptInputType_nzDot: BooleanInput;
static ngAcceptInputType_nzStandalone: BooleanInput;
showSup = false;
presetColor: string | null = null;
dir: Direction = 'ltr';
private destroy$ = new Subject<void>();
@Input() @InputBoolean() nzShowZero: boolean = false;
@Input() @InputBoolean() nzShowDot = true;
@Input() @InputBoolean() nzStandalone = false;
@Input() @InputBoolean() nzDot = false;
@Input() @WithConfig() nzOverflowCount: number = 99;
@Input() @WithConfig() nzColor?: string = undefined;
@Input() nzStyle: { [key: string]: string } | null = null;
@Input() nzText?: string | TemplateRef<void> | null = null;
@Input() nzTitle?: string | null | undefined;
@Input() nzStatus?: NzBadgeStatusType | string;
@Input() nzCount?: number | TemplateRef<NzSafeAny>;
@Input() nzOffset?: [number, number];
@Input() nzSize: NzSizeDSType = 'default';
constructor(
public nzConfigService: NzConfigService,
private renderer: Renderer2,
private cdr: ChangeDetectorRef,
private elementRef: ElementRef,
@Optional() private directionality: Directionality,
@Host() @Optional() public noAnimation?: NzNoAnimationDirective
) {}
ngOnInit(): void {
this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
this.dir = direction;
this.prepareBadgeForRtl();
this.cdr.detectChanges();
});
this.dir = this.directionality.value;
this.prepareBadgeForRtl();
}
ngOnChanges(changes: SimpleChanges): void {
const { nzColor, nzShowDot, nzDot, nzCount, nzShowZero } = changes;
if (nzColor) {
this.presetColor = this.nzColor && badgePresetColors.indexOf(this.nzColor) !== -1 ? this.nzColor : null;
}
if (nzShowDot || nzDot || nzCount || nzShowZero) {
this.showSup = (this.nzShowDot && this.nzDot) || this.nzCount! > 0 || (this.nzCount! <= 0 && this.nzShowZero);
}
}
private prepareBadgeForRtl(): void {
if (this.isRtlLayout) {
this.renderer.addClass(this.elementRef.nativeElement, 'ant-badge-rtl');
} else {
this.renderer.removeClass(this.elementRef.nativeElement, 'ant-badge-rtl');
}
}
get isRtlLayout(): boolean {
return this.dir === 'rtl';
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}