pm-controls
Version:
ProModel Controls
107 lines (91 loc) • 3.29 kB
text/typescript
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
OnDestroy,
OnInit,
ElementRef,
HostBinding,
Input,
ViewContainerRef,
ViewChild
} from '@angular/core';
import { CompatibilityService } from '../../../services/compatibility/compatibility-service';
import { ElementExtensions } from '../../../../objects/extensions/element-extensions';
export class DropDownComponent implements OnInit, OnDestroy {
constructor(
public changeDetectorRef: ChangeDetectorRef,
public viewContainerRef: ViewContainerRef,
public CompatibilityService: CompatibilityService) {
this.changeDetectorRef.detach();
this.el = viewContainerRef.element.nativeElement;
this.scrollEvent = this.HandleScroll.bind(this);
this.clickEvent = this.HandleClick.bind(this);
this.resizeEvent = this.HandleResize.bind(this);
}
ngOnInit() {
let body = document.querySelector('body');
body.addEventListener('scroll', this.scrollEvent, true);
this.el.addEventListener('click', this.clickEvent, false);
window.addEventListener('resize', this.resizeEvent);
}
ngDoCheck() {
this.HandleResize();
}
ngOnDestroy() {
let body = document.querySelector('body');
body.removeEventListener('scroll', this.scrollEvent, true);
this.el.removeEventListener('click', this.clickEvent, false);
window.removeEventListener('resize', this.resizeEvent);
}
private el: any;
private scrollEvent;
private clickEvent;
private resizeEvent;
Width: string;
MaxWidth: string;
MinWidth: string;
ContainerWidth: number;
ContainerTop: number;
HorizontalAlignment: string;
Padding: string = '1px 0px 0px 0px';
DropDownPane: ElementRef;
private isDropDownOpen: boolean;
get IsDropDownOpen() {
return this.isDropDownOpen;
}
set IsDropDownOpen(value: boolean) {
if (value && !this.CompatibilityService.IsLegacyBrowser)
{
this.ContainerWidth = this.el.offsetWidth;
var parentOffsetTop = ElementExtensions.getParentScrollTop(this.el);
this.ContainerTop = this.el.offsetTop - parentOffsetTop;
this.changeDetectorRef.detectChanges();
}
this.isDropDownOpen = value;
this.changeDetectorRef.detectChanges();
}
HandleScroll(e) {
if (!this.IsDropDownOpen || !e.target) { return; };
var parentOffsetTop = ElementExtensions.getParentScrollTop(this.el);
this.ContainerTop = this.el.offsetTop - parentOffsetTop;
this.changeDetectorRef.detectChanges();
}
HandleClick(e) {
e.stopPropagation();
}
public HandleResize() {
if (!this.IsDropDownOpen) { return; };
this.ContainerWidth = this.el.offsetWidth;
var parentOffsetTop = ElementExtensions.getParentScrollTop(this.el);
this.ContainerTop = this.el.offsetTop - parentOffsetTop;
this.changeDetectorRef.detectChanges();
}
}