-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathanchor-link.component.ts
115 lines (101 loc) · 2.79 KB
/
anchor-link.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
/**
* 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 { Platform } from '@angular/cdk/platform';
import { NgTemplateOutlet } from '@angular/common';
import {
booleanAttribute,
ChangeDetectionStrategy,
Component,
ContentChild,
ElementRef,
Input,
OnDestroy,
OnInit,
Renderer2,
TemplateRef,
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { NzDirectionVHType, NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzAnchorComponent } from './anchor.component';
@Component({
selector: 'nz-link',
exportAs: 'nzLink',
preserveWhitespaces: false,
standalone: true,
imports: [NgTemplateOutlet],
template: `
<a
#linkTitle
class="ant-anchor-link-title"
[href]="nzHref"
[attr.title]="titleStr"
[target]="nzTarget"
(click)="goToClick($event)"
>
@if (titleStr) {
<span>{{ titleStr }}</span>
} @else {
<ng-template [ngTemplateOutlet]="titleTpl || nzTemplate" />
}
</a>
@if (nzDirection === 'vertical') {
<ng-content></ng-content>
}
`,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class: 'ant-anchor-link'
}
})
export class NzAnchorLinkComponent implements OnInit, OnDestroy {
@Input() nzHref = '#';
@Input() nzTarget?: string;
@Input({ transform: booleanAttribute }) nzReplace: boolean = false;
titleStr: string | null = '';
titleTpl?: TemplateRef<NzSafeAny>;
nzDirection: NzDirectionVHType = 'vertical';
@Input()
set nzTitle(value: string | TemplateRef<void>) {
if (value instanceof TemplateRef) {
this.titleStr = null;
this.titleTpl = value;
} else {
this.titleStr = value;
}
}
@ContentChild('nzTemplate', { static: false }) nzTemplate!: TemplateRef<void>;
@ViewChild('linkTitle') linkTitle!: ElementRef<HTMLAnchorElement>;
constructor(
public elementRef: ElementRef,
private anchorComp: NzAnchorComponent,
private platform: Platform,
private renderer: Renderer2
) {}
ngOnInit(): void {
this.anchorComp.registerLink(this);
this.nzDirection = this.anchorComp.nzDirection;
}
getLinkTitleElement(): HTMLAnchorElement {
return this.linkTitle.nativeElement;
}
setActive(): void {
this.renderer.addClass(this.elementRef.nativeElement, 'ant-anchor-link-active');
}
unsetActive(): void {
this.renderer.removeClass(this.elementRef.nativeElement, 'ant-anchor-link-active');
}
goToClick(e: Event): void {
e.preventDefault();
e.stopPropagation();
if (this.platform.isBrowser) {
this.anchorComp.handleScrollTo(this);
}
}
ngOnDestroy(): void {
this.anchorComp.unregisterLink(this);
}
}