text
stringlengths
1
80.5k
public addTarget( target: LazyTarget ) : void {
if ( this.observer ) {
this.targets.set( target.element, target );
this.observer.observe( target.element );
// If we don't actually have an observer (lacking browser support), then we're
// going to punt on the feature for now and just immediately tell the target
// that it is visible on the page.
} else {
target.updateVisibility( true, 1.0 );
}
}
// I setup the IntersectionObserver with the given element as the root.
public setup( element: Element = null, offset: number = 0 ) : void {
// While the IntersectionObserver is supported in the modern browsers, it will
// never be added to Internet Explorer (IE) and is not in my version of Safari
// (at the time of this post). As such, we'll only use it if it's available.
// And, if it's not, we'll fall-back to non-lazy behaviors.
if ( ! global[ "IntersectionObserver" ] ) {
return;
}
this.observer = new IntersectionObserver(
this.handleIntersectionUpdate,
{
root: element,
rootMargin: `${ offset }px`
}
);
}
// I remove the given LazyTarget implementation from the collection of objects being
// tracked by the IntersectionObserver.
public removeTarget( target: LazyTarget ) : void {
// If the IntersectionObserver isn't supported, we never started tracking the
// given target in the first place.
if ( this.observer ) {
this.targets.delete( target.element );
this.observer.unobserve( target.element );
}
}
// I teardown this service instance.
public teardown() : void {
if ( this.observer ) {
this.observer.disconnect();
this.observer = null;
}
this.targets.clear();
this.targets = null;
}
// ---
// PRIVATE METHODS.
// ---
// I handle changes in the visibility for elements being tracked by the intersection
// observer.
// --
// CAUTION: Using fat-arrow binding for method.
private handleIntersectionUpdate = ( entries: IntersectionObserverEntry[] ) : void => {
for ( var entry of entries ) {
var lazyTarget = this.targets.get( entry.target );
( lazyTarget ) && lazyTarget.updateVisibility(
entry.isIntersecting,
entry.intersectionRatio
);
}
}
}
As of this writing, the IntersectionObserver API is only supported in my Chrome and Firefox browsers. My Safari browser, which I think is a version behind, doesn't support it. And, IE will never support it. As such, my LazyViewport service has to be a bit defensive. There is an official IntersectionObserver polyfill, but I haven't tried it. That said, most of the code in this service just adds and removes targets - it doesn't actually handle any of the intersection logic - that's all done by the
IntersectionObserver API.
By default, the injected LazyViewport instance will be associated with the browser's viewport because we don't provide a root element when providing the LazyViewport service in the LazyModule:
// Import the core angular services.
import { NgModule } from "@angular/core";
// Import the application components and services.
import { LazySrcDirective } from "./lazy-src.directive.ts";
import { LazyViewport } from "./lazy-viewport.ts";
import { LazyViewportDirective } from "./lazy-viewport.directive.ts";
// ----------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------- //
@NgModule({
declarations: [
LazySrcDirective,
LazyViewportDirective
],
exports: [
LazySrcDirective,
LazyViewportDirective
],
providers: [
// Setup the default LazyViewport instance without an associated element. This
// will create a IntersectionObserver that uses the browser's viewport as the
// observer root. This way, an instance of LazyViewport is always available for
// injection into other directives and services.
// --
// NOTE: This service will be overridden at lower-levels in the component tree
// whenever a [lazyViewport] directive is applied.
{
provide: LazyViewport,
useFactory: function() {
var viewport = new LazyViewport();
viewport.setup( /* No root. */ );
return( viewport );
}
}