2020-04-21 11:21:10 -04:00
|
|
|
import { debounce, throttle } from 'lodash';
|
2017-07-24 13:36:52 -04:00
|
|
|
|
2018-08-07 02:04:33 -04:00
|
|
|
export const placeholderImage =
|
|
|
|
'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
|
2018-09-06 10:36:05 -04:00
|
|
|
const SCROLL_THRESHOLD = 500;
|
2017-07-24 13:36:52 -04:00
|
|
|
|
|
|
|
export default class LazyLoader {
|
|
|
|
constructor(options = {}) {
|
2018-09-06 10:36:05 -04:00
|
|
|
this.intersectionObserver = null;
|
2017-07-24 13:36:52 -04:00
|
|
|
this.lazyImages = [];
|
|
|
|
this.observerNode = options.observerNode || '#content-body';
|
|
|
|
|
|
|
|
const scrollContainer = options.scrollContainer || window;
|
2018-09-06 10:36:05 -04:00
|
|
|
scrollContainer.addEventListener('load', () => this.register());
|
|
|
|
}
|
|
|
|
|
|
|
|
static supportsIntersectionObserver() {
|
|
|
|
return 'IntersectionObserver' in window;
|
2017-07-24 13:36:52 -04:00
|
|
|
}
|
2018-09-06 10:36:05 -04:00
|
|
|
|
2017-07-24 13:36:52 -04:00
|
|
|
searchLazyImages() {
|
2018-11-26 03:57:21 -05:00
|
|
|
window.requestIdleCallback(
|
2018-08-07 22:50:01 -04:00
|
|
|
() => {
|
2018-09-06 10:36:05 -04:00
|
|
|
const lazyImages = [].slice.call(document.querySelectorAll('.lazy'));
|
2017-10-31 13:18:09 -04:00
|
|
|
|
2018-09-06 10:36:05 -04:00
|
|
|
if (LazyLoader.supportsIntersectionObserver()) {
|
|
|
|
if (this.intersectionObserver) {
|
|
|
|
lazyImages.forEach(img => this.intersectionObserver.observe(img));
|
|
|
|
}
|
|
|
|
} else if (lazyImages.length) {
|
|
|
|
this.lazyImages = lazyImages;
|
|
|
|
this.checkElementsInView();
|
2018-08-07 22:50:01 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{ timeout: 500 },
|
|
|
|
);
|
2017-07-24 13:36:52 -04:00
|
|
|
}
|
2018-09-06 10:36:05 -04:00
|
|
|
|
2017-07-24 13:36:52 -04:00
|
|
|
startContentObserver() {
|
|
|
|
const contentNode = document.querySelector(this.observerNode) || document.querySelector('body');
|
|
|
|
if (contentNode) {
|
2018-09-06 10:36:05 -04:00
|
|
|
this.mutationObserver = new MutationObserver(() => this.searchLazyImages());
|
2017-07-24 13:36:52 -04:00
|
|
|
|
2018-09-06 10:36:05 -04:00
|
|
|
this.mutationObserver.observe(contentNode, {
|
2017-07-24 13:36:52 -04:00
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 10:36:05 -04:00
|
|
|
|
|
|
|
stopContentObserver() {
|
|
|
|
if (this.mutationObserver) {
|
|
|
|
this.mutationObserver.takeRecords();
|
|
|
|
this.mutationObserver.disconnect();
|
|
|
|
this.mutationObserver = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unregister() {
|
|
|
|
this.stopContentObserver();
|
|
|
|
if (this.intersectionObserver) {
|
|
|
|
this.intersectionObserver.takeRecords();
|
|
|
|
this.intersectionObserver.disconnect();
|
|
|
|
this.intersectionObserver = null;
|
|
|
|
}
|
|
|
|
if (this.throttledScrollCheck) {
|
|
|
|
window.removeEventListener('scroll', this.throttledScrollCheck);
|
|
|
|
}
|
|
|
|
if (this.debouncedElementsInView) {
|
|
|
|
window.removeEventListener('resize', this.debouncedElementsInView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
register() {
|
|
|
|
if (LazyLoader.supportsIntersectionObserver()) {
|
|
|
|
this.startIntersectionObserver();
|
|
|
|
} else {
|
|
|
|
this.startLegacyObserver();
|
|
|
|
}
|
2017-07-24 13:36:52 -04:00
|
|
|
this.startContentObserver();
|
2018-09-06 10:36:05 -04:00
|
|
|
this.searchLazyImages();
|
2017-07-24 13:36:52 -04:00
|
|
|
}
|
2018-09-06 10:36:05 -04:00
|
|
|
|
|
|
|
startIntersectionObserver = () => {
|
2020-04-21 11:21:10 -04:00
|
|
|
this.throttledElementsInView = throttle(() => this.checkElementsInView(), 300);
|
2018-09-06 10:36:05 -04:00
|
|
|
this.intersectionObserver = new IntersectionObserver(this.onIntersection, {
|
|
|
|
rootMargin: `${SCROLL_THRESHOLD}px 0px`,
|
|
|
|
thresholds: 0.1,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onIntersection = entries => {
|
|
|
|
entries.forEach(entry => {
|
2018-11-26 08:26:23 -05:00
|
|
|
// We are using `intersectionRatio > 0` over `isIntersecting`, as some browsers did not ship the latter
|
2019-09-18 10:02:45 -04:00
|
|
|
// See: https://gitlab.com/gitlab-org/gitlab-foss/issues/54407
|
2018-11-26 08:26:23 -05:00
|
|
|
if (entry.intersectionRatio > 0) {
|
2018-09-06 10:36:05 -04:00
|
|
|
this.intersectionObserver.unobserve(entry.target);
|
|
|
|
this.lazyImages.push(entry.target);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.throttledElementsInView();
|
|
|
|
};
|
|
|
|
|
|
|
|
startLegacyObserver() {
|
2020-04-21 11:21:10 -04:00
|
|
|
this.throttledScrollCheck = throttle(() => this.scrollCheck(), 300);
|
|
|
|
this.debouncedElementsInView = debounce(() => this.checkElementsInView(), 300);
|
2018-09-06 10:36:05 -04:00
|
|
|
window.addEventListener('scroll', this.throttledScrollCheck);
|
|
|
|
window.addEventListener('resize', this.debouncedElementsInView);
|
|
|
|
}
|
|
|
|
|
2017-07-24 13:36:52 -04:00
|
|
|
scrollCheck() {
|
2018-11-26 03:57:21 -05:00
|
|
|
window.requestAnimationFrame(() => this.checkElementsInView());
|
2017-07-24 13:36:52 -04:00
|
|
|
}
|
2018-09-06 10:36:05 -04:00
|
|
|
|
2017-07-24 13:36:52 -04:00
|
|
|
checkElementsInView() {
|
2018-06-15 11:58:27 -04:00
|
|
|
const scrollTop = window.pageYOffset;
|
|
|
|
const visHeight = scrollTop + window.innerHeight + SCROLL_THRESHOLD;
|
2017-07-24 13:36:52 -04:00
|
|
|
|
|
|
|
// Loading Images which are in the current viewport or close to them
|
2018-08-07 02:04:33 -04:00
|
|
|
this.lazyImages = this.lazyImages.filter(selectedImage => {
|
2017-07-24 13:36:52 -04:00
|
|
|
if (selectedImage.getAttribute('data-src')) {
|
2017-10-31 13:18:09 -04:00
|
|
|
const imgBoundRect = selectedImage.getBoundingClientRect();
|
|
|
|
const imgTop = scrollTop + imgBoundRect.top;
|
|
|
|
const imgBound = imgTop + imgBoundRect.height;
|
2017-07-24 13:36:52 -04:00
|
|
|
|
2018-09-06 10:36:05 -04:00
|
|
|
if (scrollTop <= imgBound && visHeight >= imgTop) {
|
2018-11-26 03:57:21 -05:00
|
|
|
window.requestAnimationFrame(() => {
|
2018-08-07 22:50:01 -04:00
|
|
|
LazyLoader.loadImage(selectedImage);
|
|
|
|
});
|
2017-07-24 13:36:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-06 10:36:05 -04:00
|
|
|
/*
|
|
|
|
If we are scrolling fast, the img we watched intersecting could have left the view port.
|
|
|
|
So we are going watch for new intersections.
|
|
|
|
*/
|
|
|
|
if (LazyLoader.supportsIntersectionObserver()) {
|
|
|
|
if (this.intersectionObserver) {
|
|
|
|
this.intersectionObserver.observe(selectedImage);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-24 13:36:52 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2018-09-06 10:36:05 -04:00
|
|
|
|
2017-07-24 13:36:52 -04:00
|
|
|
static loadImage(img) {
|
|
|
|
if (img.getAttribute('data-src')) {
|
2018-08-07 02:04:33 -04:00
|
|
|
let imgUrl = img.getAttribute('data-src');
|
|
|
|
// Only adding width + height for avatars for now
|
|
|
|
if (imgUrl.indexOf('/avatar/') > -1 && imgUrl.indexOf('?') === -1) {
|
|
|
|
let targetWidth = null;
|
|
|
|
if (img.getAttribute('width')) {
|
|
|
|
targetWidth = img.getAttribute('width');
|
|
|
|
} else {
|
|
|
|
targetWidth = img.width;
|
|
|
|
}
|
|
|
|
if (targetWidth) imgUrl += `?width=${targetWidth}`;
|
|
|
|
}
|
|
|
|
img.setAttribute('src', imgUrl);
|
2017-07-24 13:36:52 -04:00
|
|
|
img.removeAttribute('data-src');
|
|
|
|
img.classList.remove('lazy');
|
|
|
|
img.classList.add('js-lazy-loaded');
|
2019-01-18 09:12:00 -05:00
|
|
|
img.classList.add('qa-js-lazy-loaded');
|
2017-07-24 13:36:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|