pm-controls
Version:
ProModel Controls
56 lines (48 loc) • 1.6 kB
text/typescript
import {
Component,
EventEmitter,
Input,
OnInit,
Output,
OnDestroy,
ViewContainerRef
} from '@angular/core';
export class MenuButtonComponent implements OnInit, OnDestroy {
IsDropDownOpen: boolean;
IsDisabled: boolean;
HorizontalAlignment: string;
ButtonClass: string = "menu-button-button";
DropDownClass: string;
IsDropDownOpenChange: EventEmitter<boolean> = new EventEmitter();
private el: Element;
private clickEvent;
constructor(viewContainerRef: ViewContainerRef) {
this.el = viewContainerRef.element.nativeElement;
this.clickEvent = this.HandleClick.bind(this);
}
ngOnInit() {
this.IsDropDownOpen = this.IsDropDownOpen || false;
let body = document.querySelector('body');
body.addEventListener('click', this.clickEvent, false);
}
ngOnDestroy() {
let body = document.querySelector('body');
body.removeEventListener('click', this.clickEvent, false);
}
HandleClick(e) {
if (!this.IsDropDownOpen || !e.target) { return; };
if (this.el !== e.target && !this.el.contains((<any>e.target))) {
this.IsDropDownOpen = false;
this.IsDropDownOpenChange.emit(this.IsDropDownOpen);
}
}
click() {
this.IsDropDownOpen = !this.IsDropDownOpen;
this.IsDropDownOpenChange.emit(this.IsDropDownOpen);
}
}