From 1ee9bc8faec4e18c318f90518c5b1d8835155f57 Mon Sep 17 00:00:00 2001 From: Gautam Jajoo Date: Thu, 16 Sep 2021 21:15:25 +0530 Subject: [PATCH] Frontend_V2: Add lazy loading for images(#3571) * add lazy load for images * add new line Co-authored-by: Rishabh Jain --- .../src/app/Directives/image.lazyload.ts | 31 +++++++++++++++++++ .../challengecard.component.html | 2 +- frontend_v2/src/app/shared/shared.module.ts | 3 ++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 frontend_v2/src/app/Directives/image.lazyload.ts diff --git a/frontend_v2/src/app/Directives/image.lazyload.ts b/frontend_v2/src/app/Directives/image.lazyload.ts new file mode 100644 index 0000000000..89f9516c11 --- /dev/null +++ b/frontend_v2/src/app/Directives/image.lazyload.ts @@ -0,0 +1,31 @@ +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; + } + } +} diff --git a/frontend_v2/src/app/components/publiclists/challengelist/challengecard/challengecard.component.html b/frontend_v2/src/app/components/publiclists/challengelist/challengecard/challengecard.component.html index 489b284970..4e3743d5b4 100644 --- a/frontend_v2/src/app/components/publiclists/challengelist/challengecard/challengecard.component.html +++ b/frontend_v2/src/app/components/publiclists/challengelist/challengecard/challengecard.component.html @@ -1,6 +1,6 @@
- +

{{ challenge['title'] }}

diff --git a/frontend_v2/src/app/shared/shared.module.ts b/frontend_v2/src/app/shared/shared.module.ts index 654e30d00e..62e0739ff6 100644 --- a/frontend_v2/src/app/shared/shared.module.ts +++ b/frontend_v2/src/app/shared/shared.module.ts @@ -6,6 +6,7 @@ import { FormsModule } from '@angular/forms'; // import directive import { PasswordMismatchValidatorDirective } from '../Directives/password.validator'; import { EmailValidatorDirective } from '../Directives/email.validator'; +import { LazyImageDirective } from '../Directives/image.lazyload'; // import component import { HeaderStaticComponent } from '../components/nav/header-static/header-static.component'; @@ -19,6 +20,7 @@ import { UtilityModule } from '../components/utility/utility.module'; declarations: [ PasswordMismatchValidatorDirective, EmailValidatorDirective, + LazyImageDirective, HeaderStaticComponent, FooterComponent, NotFoundComponent, @@ -27,6 +29,7 @@ import { UtilityModule } from '../components/utility/utility.module'; exports: [ PasswordMismatchValidatorDirective, EmailValidatorDirective, + LazyImageDirective, CommonModule, RouterModule, FormsModule,