import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
OnDestroy,
Renderer2
} from '@angular/core';
import { DropdownButtonState } from "./dropdown-button.state";
import { dropdownButtonAnimation } from './dropdown-button-animations';
import { AnimationBuilder, AnimationFactory } from '@angular/animations';
@Component({
selector: 'p-dropdown-button-container',
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
style: 'display:block;position: absolute;z-index: 1040'
},
template: `
`
})
export class DropdownButtonContainerComponent implements OnDestroy {
isOpen = false;
private _factorydropdownButtonAnimation: AnimationFactory;
get direction(): 'down' | 'up' {
return this._state.direction;
}
// tslint:disable-next-line:no-any
private _subscription: any;
constructor(
private _state: DropdownButtonState,
private cd: ChangeDetectorRef,
private _renderer: Renderer2,
private _element: ElementRef,
_builder: AnimationBuilder
) {
this._factorydropdownButtonAnimation = _builder.build(dropdownButtonAnimation);
this._subscription = _state.isOpenChange.subscribe((value: boolean) => {
this.isOpen = value;
const dropdown = this._element.nativeElement.querySelector('.dropdown-menu');
this._renderer.addClass(this._element.nativeElement.querySelector('div'), 'open');
if (dropdown) {
this._renderer.addClass(dropdown, 'show');
if (dropdown.classList.contains('dropdown-menu-right')) {
this._renderer.setStyle(dropdown, 'left', 'auto');
this._renderer.setStyle(dropdown, 'right', '0');
}
if (this.direction === 'up') {
this._renderer.setStyle(dropdown, 'top', 'auto');
this._renderer.setStyle(
dropdown,
'transform',
'translateY(-101%)'
);
}
}
if (dropdown && this._state.isAnimated) {
this._factorydropdownButtonAnimation.create(dropdown)
.play();
}
this.cd.markForCheck();
this.cd.detectChanges();
});
}
/** @internal */
_contains(el: Element): boolean {
return this._element.nativeElement.contains(el);
}
ngOnDestroy(): void {
this._subscription.unsubscribe();
}
}