UNPKG

ng2-encrm-components

Version:

angular 2 components

56 lines (48 loc) 1.39 kB
import { Component, HostListener, ElementRef } from '@angular/core'; @Component({ selector: 'button-dropdown', template: require('./button-dropdown.component.html'), styles: [require('./button-dropdown.component.scss')] }) export class ButtonDropdownComponent { private isOpen: boolean = false; constructor(private _elementRef: ElementRef) { } /** * hides dropdown on outside click * @param target */ @HostListener('document:click', ['$event.target']) documentClickHandler = (target) => { if (!this._elementRef.nativeElement.contains(target)) { this.hide(); } }; toggleShow() { this.isOpen = !this.isOpen; this.checkPosition(); } hide() { this.isOpen = false; this.checkPosition(); } /** * check position of menu and move if it is placed out of view window */ checkPosition() { setTimeout(() => { let el = this._elementRef.nativeElement.querySelector('[list]'); if(!el){ return; } let rect = el.getBoundingClientRect(); if (rect.right > window.innerWidth) { let left: number = +el.style.left.slice(0, -2); el.style.left = left - (rect.right - window.innerWidth) - 20 + 'px'; } if(rect.left < 0){ let left: number = +el.style.left.slice(0, -2); el.style.left = left - rect.left + 5 + 'px'; } }, 1); } }