Cloud-CV/EvalAI

View on GitHub
frontend_v2/src/app/Directives/image.lazyload.ts

Summary

Maintainability
A
35 mins
Test Coverage
import { AfterViewInit, Directive, ElementRef, HostBinding, Input } from '@angular/core';

@Directive({
  selector: 'img[appLazyLoadImage]'
})

export class LazyImageDirective implements AfterViewInit {

  @HostBinding('attr.src') srcAttribute = null;

  @Input() src: string;

  constructor(private el: ElementRef) {}

  ngAfterViewInit() {
    if(window && 'IntersectionObserver' in window) {
      const obs = new IntersectionObserver(entries => {
        entries.forEach(({ isIntersecting }) => {
          if (isIntersecting) {
            this.srcAttribute = this.src;
            obs.unobserve(this.el.nativeElement);
          }
        });
      });
      obs.observe(this.el.nativeElement);
    }
    else {
      this.srcAttribute = this.src;
    }
  }
}