nts-ng2-components
Version:
Paquete de componentes para Angular2 desarrollado por NITSNETS.
101 lines (88 loc) • 3.22 kB
text/typescript
import {
AfterViewInit,
Component,
ContentChild,
Directive,
ElementRef,
EventEmitter,
Host,
HostListener,
Input,
OnDestroy,
Output,
} from '@angular/core';
import { NtsPopupContainerComponent } from './container/container.component';
export type NtsPopupPosition = 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right';
export class NtsPopupComponent implements AfterViewInit {
toggle = false;
keepOpen = true;
opened = false;
openedChange = new EventEmitter();
position: NtsPopupPosition = null;
calculatedPosition: NtsPopupPosition = null;
popupScope: NtsPopupContainerComponent;
constructor(private elementRef: ElementRef) { }
ngAfterViewInit() {
this.popupScope.close.subscribe(_ => this.close());
}
open(x = null) {
if (x) {
this.calculatedPosition = x > document.documentElement.clientWidth / 2 ? 'bottom-left' : 'bottom-right';
}
this.opened = true;
this.openedChange.emit(true);
}
close() {
this.opened = false;
this.openedChange.emit(false);
}
isInClosableZone(element: HTMLElement) {
if (!this.popupScope || this.keepOpen === false) { return false; }
return this.popupScope.elementRef.nativeElement.contains(element);
}
}
export class NtsPopupTriggerDirective implements OnDestroy {
private openedByFocus = false;
private closePopupOnOutsideClick = (event: MouseEvent) => this.close(event);
constructor( public popup: NtsPopupComponent, private elementRef: ElementRef) { }
openPopup(ev: MouseEvent) {
if (this.openedByFocus) {
this.openedByFocus = false;
return;
}
if (this.popup.opened && this.popup.toggle) {
this.popup.close();
document.removeEventListener('click', this.closePopupOnOutsideClick);
} else {
this.popup.open(ev.clientX);
document.addEventListener('click', this.closePopupOnOutsideClick, true);
}
}
onBlur(event: FocusEvent) {
if (event.relatedTarget &&
!this.popup.isInClosableZone(<HTMLElement>event.relatedTarget) &&
event.relatedTarget !== this.elementRef.nativeElement) {
this.popup.close();
document.removeEventListener('click', this.closePopupOnOutsideClick);
}
}
ngOnDestroy() {
document.removeEventListener('click', this.closePopupOnOutsideClick);
}
private close(event: Event) {
if (!this.popup.isInClosableZone(<HTMLElement>event.target)
&& event.target !== this.elementRef.nativeElement
&& !this.elementRef.nativeElement.contains(event.target)) {
this.popup.close();
document.removeEventListener('click', this.closePopupOnOutsideClick);
}
}
}