/
tooltip.ts
162 lines (145 loc) · 5.25 KB
/
tooltip.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
/**
* 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 { Directionality } from '@angular/cdk/bidi';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ComponentFactoryResolver,
ComponentRef,
Directive,
ElementRef,
EventEmitter,
Host,
Input,
Optional,
Output,
Renderer2,
ViewContainerRef,
ViewEncapsulation
} from '@angular/core';
import { zoomBigMotion } from 'ng-zorro-antd/core/animation';
import { isPresetColor, NzPresetColor } from 'ng-zorro-antd/core/color';
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
import { BooleanInput, NgStyleInterface, NzTSType } from 'ng-zorro-antd/core/types';
import { InputBoolean } from 'ng-zorro-antd/core/util';
import {
isTooltipEmpty,
NzTooltipBaseComponent,
NzTooltipBaseDirective,
NzTooltipTrigger,
PropertyMapping
} from './base';
@Directive({
selector: '[nz-tooltip]',
exportAs: 'nzTooltip',
host: {
'[class.ant-tooltip-open]': 'visible'
}
})
export class NzTooltipDirective extends NzTooltipBaseDirective {
static ngAcceptInputType_nzTooltipArrowPointAtCenter: BooleanInput;
@Input('nzTooltipTitle') override title?: NzTSType | null;
@Input('nzTooltipTitleContext') titleContext?: Object | null = null;
@Input('nz-tooltip') override directiveTitle?: NzTSType | null;
@Input('nzTooltipTrigger') override trigger?: NzTooltipTrigger = 'hover';
@Input('nzTooltipPlacement') override placement?: string | string[] = 'top';
@Input('nzTooltipOrigin') override origin?: ElementRef<HTMLElement>;
@Input('nzTooltipVisible') override visible?: boolean;
@Input('nzTooltipMouseEnterDelay') override mouseEnterDelay?: number;
@Input('nzTooltipMouseLeaveDelay') override mouseLeaveDelay?: number;
@Input('nzTooltipOverlayClassName') override overlayClassName?: string;
@Input('nzTooltipOverlayStyle') override overlayStyle?: NgStyleInterface;
@Input('nzTooltipArrowPointAtCenter') @InputBoolean() override arrowPointAtCenter?: boolean;
@Input() nzTooltipColor?: string;
// eslint-disable-next-line @angular-eslint/no-output-rename
@Output('nzTooltipVisibleChange') override readonly visibleChange = new EventEmitter<boolean>();
override componentRef: ComponentRef<NzToolTipComponent> = this.hostView.createComponent(NzToolTipComponent);
constructor(
elementRef: ElementRef,
hostView: ViewContainerRef,
resolver: ComponentFactoryResolver,
renderer: Renderer2,
@Host() @Optional() noAnimation?: NzNoAnimationDirective
) {
super(elementRef, hostView, resolver, renderer, noAnimation);
}
protected override getProxyPropertyMap(): PropertyMapping {
return {
...super.getProxyPropertyMap(),
nzTooltipColor: ['nzColor', () => this.nzTooltipColor],
nzTooltipTitleContext: ['nzTitleContext', () => this.titleContext]
};
}
}
@Component({
selector: 'nz-tooltip',
exportAs: 'nzTooltipComponent',
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
animations: [zoomBigMotion],
template: `
<ng-template
#overlay="cdkConnectedOverlay"
cdkConnectedOverlay
nzConnectedOverlay
[cdkConnectedOverlayOrigin]="origin"
[cdkConnectedOverlayOpen]="_visible"
[cdkConnectedOverlayPositions]="_positions"
[cdkConnectedOverlayPush]="true"
[nzArrowPointAtCenter]="nzArrowPointAtCenter"
(overlayOutsideClick)="onClickOutside($event)"
(detach)="hide()"
(positionChange)="onPositionChange($event)"
>
<div
class="ant-tooltip"
[class.ant-tooltip-rtl]="dir === 'rtl'"
[ngClass]="_classMap"
[ngStyle]="nzOverlayStyle"
[@.disabled]="!!noAnimation?.nzNoAnimation"
[nzNoAnimation]="noAnimation?.nzNoAnimation"
[@zoomBigMotion]="'active'"
>
<div class="ant-tooltip-content">
<div class="ant-tooltip-arrow">
<span class="ant-tooltip-arrow-content" [ngStyle]="_contentStyleMap"></span>
</div>
<div class="ant-tooltip-inner" [ngStyle]="_contentStyleMap">
<ng-container *nzStringTemplateOutlet="nzTitle; context: nzTitleContext">{{ nzTitle }}</ng-container>
</div>
</div>
</div>
</ng-template>
`,
preserveWhitespaces: false
})
export class NzToolTipComponent extends NzTooltipBaseComponent {
override nzTitle: NzTSType | null = null;
nzTitleContext: Object | null = null;
nzColor?: string | NzPresetColor;
_contentStyleMap: NgStyleInterface = {};
constructor(
cdr: ChangeDetectorRef,
@Optional() directionality: Directionality,
@Host() @Optional() noAnimation?: NzNoAnimationDirective
) {
super(cdr, directionality, noAnimation);
}
protected isEmpty(): boolean {
return isTooltipEmpty(this.nzTitle);
}
protected override updateStyles(): void {
const isColorPreset = this.nzColor && isPresetColor(this.nzColor);
this._classMap = {
[this.nzOverlayClassName]: true,
[`${this._prefix}-placement-${this.preferredPlacement}`]: true,
[`${this._prefix}-${this.nzColor}`]: isColorPreset
};
this._contentStyleMap = {
backgroundColor: !!this.nzColor && !isColorPreset ? this.nzColor : null
};
}
}