Skip to content

Commit

Permalink
feat(core): add resize event
Browse files Browse the repository at this point in the history
  • Loading branch information
Enlcxx committed Dec 1, 2018
1 parent 2bbcd05 commit 78d9c9f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/lib/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './src/dom/overlay-container';
export * from './src/dom/overlay';
export * from './src/dom/overlay.module';
export * from './src/dom/mutation-observer-factory';
export * from './src/dom/resize';
export * from './src/common/index';
export * from './src/ripple/index';
export * from './src/position/position';
28 changes: 15 additions & 13 deletions src/lib/src/dom/overlay.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TemplateRef, EmbeddedViewRef, Injectable, ApplicationRef, ComponentFactoryResolver, Injector, ComponentRef, Type } from '@angular/core';
import { LyOverlayContainer, LyOverlayBackdrop, WindowScrollService } from './overlay-container';
import { Subscription } from 'rxjs';
import { Subscription, merge } from 'rxjs';
import { ResizeService } from './resize';

interface OverlayConfig {
styles: Object;
Expand Down Expand Up @@ -28,7 +29,7 @@ class CreateFromTemplateRef implements OverlayFromTemplateRef {
private _el: HTMLDivElement;
private _compRef: ComponentRef<any>;
private _compRefOverlayBackdrop: ComponentRef<any>;
windowScrollSub: Subscription = Subscription.EMPTY;
windowSRSub: Subscription = Subscription.EMPTY;
get containerElement() {
return this._el;
}
Expand All @@ -40,6 +41,7 @@ class CreateFromTemplateRef implements OverlayFromTemplateRef {
_context: any,
private _injector: Injector,
windowScroll: WindowScrollService,
resizeService: ResizeService,
config?: OverlayConfig
) {
// this._viewRef = _templateRef.createEmbeddedView(_context);
Expand Down Expand Up @@ -71,15 +73,13 @@ class CreateFromTemplateRef implements OverlayFromTemplateRef {

this.updateStyles(__styles);
if (config.host) {
this.windowScrollSub = windowScroll.scroll$.subscribe(() => {
this.windowSRSub = merge(windowScroll.scroll$, resizeService.resize$).subscribe(() => {
const rect = config.host.getBoundingClientRect();
if (rect.top !== __styles.top || rect.left !== __styles.left) {
const newStyles = {
top: rect.top,
left: rect.left
};
this.updateStyles(newStyles);
}
const newStyles = {
top: rect.top,
left: rect.left
};
this.updateStyles(newStyles);
});
}

Expand Down Expand Up @@ -161,7 +161,7 @@ class CreateFromTemplateRef implements OverlayFromTemplateRef {
const backdropEl = this._compRefOverlayBackdrop.location.nativeElement;
this._overlayContainer._remove(backdropEl);
}
this.windowScrollSub.unsubscribe();
this.windowSRSub.unsubscribe();
}

destroy() {
Expand All @@ -180,10 +180,12 @@ export class LyOverlay {
private _componentFactoryResolver: ComponentFactoryResolver,
private _appRef: ApplicationRef,
private _injector: Injector,
private _windowScroll: WindowScrollService
private _windowScroll: WindowScrollService,
private _resizeService: ResizeService
) { }

create(template: TemplateRef<any> | string, context?: any, config?: OverlayConfig): OverlayFromTemplateRef {
return new CreateFromTemplateRef(this._componentFactoryResolver, this._appRef, template, this._overlayContainer, context, this._injector, this._windowScroll, config);
return new CreateFromTemplateRef(
this._componentFactoryResolver, this._appRef, template, this._overlayContainer, context, this._injector, this._windowScroll, this._resizeService, config);
}
}
33 changes: 33 additions & 0 deletions src/lib/src/dom/resize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable, Inject, NgZone } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { fromEvent , Observable, empty } from 'rxjs';
import { map, share, auditTime } from 'rxjs/operators';

import { Platform } from '../platform/index';

@Injectable({
providedIn: 'root'
})
export class ResizeService {

public resize$: Observable<number>;

constructor(
@Inject(DOCUMENT) private document: any,
ngZone: NgZone
) {
if (Platform.isBrowser) {
ngZone.runOutsideAngular(() => {
this.resize$ = fromEvent(window, 'resize').pipe(
auditTime(20),
map(() => {
return window.innerHeight || this.document.documentElement.clientHeight;
}),
share()
);
});
} else {
this.resize$ = empty();
}
}
}

0 comments on commit 78d9c9f

Please sign in to comment.