-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ad358c
commit 9a154da
Showing
8 changed files
with
119 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import { Controls } from './../src/components/controls'; | ||
import { Lazy } from './../src/components/lazy'; | ||
import { Controls } from './../src/components/controls/controls'; | ||
import { Lazy } from './../src/components/lazy/lazy'; | ||
import { LazyLoadImage } from './../src/components/lazyload-image/lazyload-image'; | ||
import { Virchual as VirchualCore, VirchualSettings } from './../src/virchual'; | ||
|
||
// @TODO settings with combined settings from components | ||
|
||
export default class Virchual extends VirchualCore { | ||
constructor(container: HTMLElement, settings: VirchualSettings = {}) { | ||
super(container, settings); | ||
|
||
this.register(Controls, { isEnabled: true }); | ||
this.register(Lazy, { threshold: 300 }); | ||
this.register(LazyLoadImage, { lazyload: false }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lazyload-image'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { ComponentDependencies } from './../component'; | ||
|
||
export type LazyLoadImageSettings = { | ||
/** | ||
* Enables lazyloading imagest that are currently not visible, thus saving bandwidth. | ||
*/ | ||
lazyload?: boolean; | ||
|
||
/** | ||
* The CSS selector for lazyloaded images. Default 'virchual__lazy-img' | ||
*/ | ||
lazyloadSelector?: string; | ||
|
||
/** | ||
* How many images should get eagerly loaded on both sides of current slide. | ||
*/ | ||
loadEager?: number; | ||
}; | ||
|
||
export class LazyLoadImage { | ||
private lazyload: boolean; | ||
private lazyloadSelector: string; | ||
private loadEager: number; | ||
|
||
constructor(private imports: ComponentDependencies, settings: LazyLoadImageSettings) { | ||
const { lazyload, lazyloadSelector, loadEager } = { | ||
lazyload: true, | ||
lazyloadSelector: '.virchual__lazy-img', | ||
loadEager: 1, | ||
...settings, | ||
}; | ||
|
||
this.lazyload = lazyload; | ||
this.lazyloadSelector = lazyloadSelector; | ||
this.loadEager = loadEager; | ||
|
||
imports.eventBus.on('mounted', () => { | ||
this.lazyLoad(); | ||
}); | ||
|
||
imports.eventBus.on('move', () => { | ||
this.lazyLoad(); | ||
}); | ||
} | ||
|
||
private lazyLoad(): void { | ||
const images = this.getImages(this.imports.virchual.currentIndex, this.imports.virchual.currentIndex + 1); | ||
|
||
images.forEach(image => { | ||
this.loadImage(image); | ||
}); | ||
} | ||
|
||
private loadImage(image: HTMLImageElement) { | ||
// @todo handle picture and img element slightly different | ||
|
||
[].forEach.call(image.getElementsByTagName('source'), (source: HTMLSourceElement) => { | ||
const dataSrc = source.dataset.srcset; | ||
|
||
if (dataSrc) { | ||
source.setAttribute('srcset', dataSrc); | ||
|
||
source.removeAttribute('data-srcset'); | ||
} | ||
}); | ||
|
||
image.classList.add('loading'); | ||
} | ||
|
||
private getImages(start: number, end: number) { | ||
if (start - this.loadEager >= 0) { | ||
start = start - this.loadEager; | ||
} | ||
|
||
if (this.imports.virchual.slides.length > end + this.loadEager) { | ||
end = end + this.loadEager; | ||
} | ||
|
||
const images = []; | ||
|
||
while (start <= end) { | ||
const slide = this.imports.virchual.slides[start]; | ||
|
||
start++; | ||
|
||
if (!slide.isMounted) { | ||
continue; | ||
} | ||
|
||
[].forEach.call(slide.ref.querySelectorAll(this.lazyloadSelector), (img: HTMLElement) => { | ||
images.push(img); | ||
}); | ||
} | ||
|
||
return images; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters