Skip to content

Commit

Permalink
fix: Make options input public
Browse files Browse the repository at this point in the history
  • Loading branch information
k3nsei committed Oct 3, 2020
1 parent 2593f74 commit 42d4600
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions projects/ng-in-viewport/src/lib/in-viewport.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
Output,
PLATFORM_ID
} from '@angular/core';
import { Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { filter, takeUntil } from 'rxjs/operators';

import { InViewportConfig, InViewportConfigOptions } from './in-viewport-config';
import { InViewportService } from './in-viewport.service';
Expand All @@ -32,36 +32,44 @@ export const InViewportMetadata = Symbol('InViewportMetadata');
})
export class InViewportDirective implements AfterViewInit, OnDestroy {
private config: InViewportConfig = new InViewportConfig();
private readonly subscription: Subscription = new Subscription();
private readonly destroyed$: Subject<void> = new Subject();

@Input('inViewportOptions')
private set options(value: InViewportConfigOptions) {
public set options(value: InViewportConfigOptions) {
this.config = new InViewportConfig(value);
}

@Output() public readonly inViewportAction: EventEmitter<any> = new EventEmitter<any>();

constructor(
@Inject(PLATFORM_ID) private platformId: Object, // tslint:disable-line
private elementRef: ElementRef,
private inViewport: InViewportService
) {}
@Inject(PLATFORM_ID) private readonly platformId: Object, // tslint:disable-line
private readonly elementRef: ElementRef,
private readonly inViewport: InViewportService
) {
}

public ngAfterViewInit(): void {
if (isPlatformBrowser(this.platformId)) {
this.inViewport.register(this.elementRef.nativeElement, this.config);
this.subscription.add(
this.inViewport.trigger$
.pipe(filter((entry: IntersectionObserverEntry) => entry && entry.target === this.elementRef.nativeElement))
.subscribe((entry: IntersectionObserverEntry) => this.emitAction(entry, false))
);
this.inViewport.trigger$
.pipe(
filter((entry: IntersectionObserverEntry): boolean => {
return entry && entry.target === this.elementRef.nativeElement;
}),
takeUntil(this.destroyed$)
)
.subscribe((entry: IntersectionObserverEntry): void => {
this.emitAction(entry, false);
});
} else {
this.emitAction(undefined, true);
}
}

public ngOnDestroy(): void {
this.subscription.unsubscribe();
this.destroyed$.next();
this.destroyed$.complete();

if (isPlatformBrowser(this.platformId)) {
this.inViewport.unregister(this.elementRef.nativeElement, this.config);
}
Expand Down

0 comments on commit 42d4600

Please sign in to comment.