forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropdown-menu.component.ts
120 lines (108 loc) · 3.61 KB
/
dropdown-menu.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
/**
* 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 { AnimationEvent } from '@angular/animations';
import { Direction, Directionality } from '@angular/cdk/bidi';
import {
AfterContentInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Host,
OnDestroy,
OnInit,
Optional,
Renderer2,
TemplateRef,
ViewChild,
ViewContainerRef,
ViewEncapsulation
} from '@angular/core';
import { BehaviorSubject, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { slideMotion } from 'ng-zorro-antd/core/animation';
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
import { IndexableObject, NzSafeAny } from 'ng-zorro-antd/core/types';
import { MenuService, NzIsMenuInsideDropDownToken } from 'ng-zorro-antd/menu';
export type NzPlacementType = 'bottomLeft' | 'bottomCenter' | 'bottomRight' | 'topLeft' | 'topCenter' | 'topRight';
@Component({
selector: `nz-dropdown-menu`,
exportAs: `nzDropdownMenu`,
animations: [slideMotion],
providers: [
MenuService,
/** menu is inside dropdown-menu component **/
{
provide: NzIsMenuInsideDropDownToken,
useValue: true
}
],
template: `
<ng-template>
<div
class="ant-dropdown"
[class.ant-dropdown-rtl]="dir === 'rtl'"
[ngClass]="nzOverlayClassName"
[ngStyle]="nzOverlayStyle"
@slideMotion
(@slideMotion.done)="onAnimationEvent($event)"
[@.disabled]="!!noAnimation?.nzNoAnimation"
[nzNoAnimation]="noAnimation?.nzNoAnimation"
(mouseenter)="setMouseState(true)"
(mouseleave)="setMouseState(false)"
>
<ng-content></ng-content>
</div>
</ng-template>
`,
preserveWhitespaces: false,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class NzDropdownMenuComponent implements AfterContentInit, OnDestroy, OnInit {
mouseState$ = new BehaviorSubject<boolean>(false);
isChildSubMenuOpen$ = this.nzMenuService.isChildSubMenuOpen$;
descendantMenuItemClick$ = this.nzMenuService.descendantMenuItemClick$;
animationStateChange$ = new EventEmitter<AnimationEvent>();
nzOverlayClassName: string = '';
nzOverlayStyle: IndexableObject = {};
@ViewChild(TemplateRef, { static: true }) templateRef!: TemplateRef<NzSafeAny>;
dir: Direction = 'ltr';
private destroy$ = new Subject<void>();
onAnimationEvent(event: AnimationEvent): void {
this.animationStateChange$.emit(event);
}
setMouseState(visible: boolean): void {
this.mouseState$.next(visible);
}
setValue<T extends keyof NzDropdownMenuComponent>(key: T, value: this[T]): void {
this[key] = value;
this.cdr.markForCheck();
}
constructor(
private cdr: ChangeDetectorRef,
private elementRef: ElementRef,
private renderer: Renderer2,
public viewContainerRef: ViewContainerRef,
public nzMenuService: MenuService,
@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.cdr.detectChanges();
});
this.dir = this.directionality.value;
}
ngAfterContentInit(): void {
this.renderer.removeChild(this.renderer.parentNode(this.elementRef.nativeElement), this.elementRef.nativeElement);
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}