-
Notifications
You must be signed in to change notification settings - Fork 232
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
Amr Wagdy
committed
Mar 17, 2022
1 parent
341aed9
commit 041645a
Showing
13 changed files
with
146 additions
and
127 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
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
28 changes: 12 additions & 16 deletions
28
src/utils/load-images/images-from-folder/prepare-images-from-folder.js
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,25 +1,21 @@ | ||
import { AND_SYMBOL_REGEX, ORGINAL_SIZE_REGEX } from '../../../constants/regex'; | ||
import { pad } from '../pad'; | ||
|
||
export const prepareImagesFromFolder = (imagesSrc, srcConfig) => { | ||
export const prepareImagesFromFolder = (imagesSrc, srcConfig, loadOriginalImages) => { | ||
const { amount, indexZeroBase } = srcConfig || {}; | ||
|
||
const resultSrc = []; | ||
const originalSrc = []; | ||
|
||
[...new Array(amount)].forEach((_item, index) => { | ||
return [...new Array(amount)].map((_item, index) => { | ||
const nextZeroFilledIndex = pad(index + 1, indexZeroBase); | ||
const imageResultSrc = imagesSrc.replace('{index}', nextZeroFilledIndex); | ||
const imageOriginalSrc = imageResultSrc | ||
.replace(ORGINAL_SIZE_REGEX, '') | ||
.replace(AND_SYMBOL_REGEX, '?'); | ||
const imageSrc = imagesSrc.replace('{index}', nextZeroFilledIndex); | ||
|
||
resultSrc.push(imageResultSrc); | ||
originalSrc.push(imageOriginalSrc); | ||
}); | ||
if (loadOriginalImages) { | ||
const imageOriginalSrc = imageSrc | ||
.replace(ORGINAL_SIZE_REGEX, '') | ||
.replace(AND_SYMBOL_REGEX, '?'); | ||
|
||
return { | ||
resultSrc, | ||
originalSrc, | ||
}; | ||
return imageOriginalSrc; | ||
} | ||
|
||
return imageSrc; | ||
}); | ||
}; |
21 changes: 9 additions & 12 deletions
21
src/utils/load-images/images-from-list/prepare-images-from-list.js
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,22 +1,19 @@ | ||
import { generateImagesPath } from '../../image-src/generate-images-path'; | ||
|
||
export const prepareImagesFromList = (images, srcConfig) => { | ||
export const prepareImagesFromList = (images, srcConfig, loadOriginalImages ) => { | ||
const { folder } = srcConfig; | ||
const resultSrc = []; | ||
const originalSrc = []; | ||
|
||
images.forEach((src) => { | ||
return images.map((src) => { | ||
const nextSrcConfig = { ...srcConfig }; | ||
nextSrcConfig.folder = /(http(s?)):\/\//gi.test(src) ? '' : folder; | ||
nextSrcConfig.filename = src; | ||
const lastIndex = resultSrc.lastIndexOf('//'); | ||
|
||
resultSrc.push(generateImagesPath(nextSrcConfig)); | ||
originalSrc.push(resultSrc.slice(lastIndex)); | ||
}); | ||
if (loadOriginalImages) { | ||
const lastIndex = resultSrc.lastIndexOf('//'); | ||
|
||
return resultSrc.slice(lastIndex); | ||
} | ||
|
||
return { | ||
resultSrc, | ||
originalSrc, | ||
}; | ||
return generateImagesPath(nextSrcConfig); | ||
}); | ||
}; |
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,38 @@ | ||
import { prepareFirstImageFromFolder } from "./prepare-first-image/prepare-first-image-from-folder"; | ||
import { prepareFirstImageFromList } from "./prepare-first-image/prepare-first-image-from-list"; | ||
|
||
export const initLazyload = (imagesSrc, srcConfig, cb) => { | ||
const { imageList, lazySelector, innerBox } = srcConfig || {}; | ||
let firstImageSrc; | ||
|
||
if (imageList) { | ||
try { | ||
const images = JSON.parse(imageList); | ||
|
||
firstImageSrc = prepareFirstImageFromList(images, srcConfig); | ||
} catch (error) { | ||
console.error(`Wrong format in image-list attribute: ${error.message}`); | ||
} | ||
} else { | ||
firstImageSrc = prepareFirstImageFromFolder(imagesSrc, srcConfig); | ||
} | ||
|
||
const image = new Image(); | ||
|
||
image.setAttribute('data-src', firstImageSrc); | ||
image.style.position = 'absolute'; | ||
image.style.top = 0; | ||
image.style.left = 0; | ||
image.style.width = '100%'; | ||
image.style.maxWidth = '100%'; | ||
image.style.maxHeight = '100%'; | ||
|
||
|
||
if (lazySelector) image.className = lazySelector; | ||
|
||
innerBox.appendChild(image); | ||
|
||
if (cb) { | ||
image.onload = () => cb(image); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/utils/load-images/lazyload/prepare-first-image/prepare-first-image-from-folder.js
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,8 @@ | ||
import { pad } from "../../pad"; | ||
|
||
export const prepareFirstImageFromFolder = (imagesSrcs, srcConfig) => { | ||
const {indexZeroBase } = srcConfig || {}; | ||
const nextZeroFilledIndex = pad(1, indexZeroBase); | ||
|
||
return imagesSrcs.replace('{index}', nextZeroFilledIndex); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/utils/load-images/lazyload/prepare-first-image/prepare-first-image-from-list.js
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,13 @@ | ||
import { generateImagesPath } from "../../../image-src/generate-images-path"; | ||
|
||
export const prepareFirstImageFromList = (images, srcConfig) => { | ||
const { folder } = srcConfig; | ||
|
||
const firstImageSrc = images[0]; | ||
|
||
const nextSrcConfig = { ...srcConfig }; | ||
nextSrcConfig.folder = /(http(s?)):\/\//gi.test(firstImageSrc) ? '' : folder; | ||
nextSrcConfig.filename = firstImageSrc; | ||
|
||
return generateImagesPath(nextSrcConfig); | ||
} |
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,37 +1,23 @@ | ||
import { ORIENTATIONS } from '../../constants/orientations'; | ||
|
||
export const loadImageAsPromise = async (src, index, srcConfig, onImageLoadCallback = () => {}) => { | ||
const { | ||
lazyload, lazySelector, fullscreenView, innerBox, orientation, | ||
} = srcConfig || {}; | ||
export const loadImageAsPromise = async (src, index, cb) => { | ||
const image = new Image(); | ||
|
||
if (lazyload && !fullscreenView) { | ||
image.setAttribute('data-src', src); | ||
image.className = image.className.length ? `${image.className} ${lazySelector}` : lazySelector; | ||
|
||
if (index === 0 && orientation !== ORIENTATIONS.Y) { | ||
image.style.position = 'absolute'; | ||
image.style.top = '0'; | ||
image.style.left = '0'; | ||
|
||
innerBox.appendChild(image); | ||
} | ||
|
||
image.onload = () => onImageLoadCallback(image, index); | ||
} else { | ||
image.src = src; | ||
} | ||
image.src = src; | ||
|
||
return new Promise((reslove) => { | ||
image.onload = () => { | ||
onImageLoadCallback(image, index); | ||
reslove(image); | ||
|
||
if (cb) { | ||
cb(image, index); | ||
} | ||
}; | ||
|
||
image.onerror = () => { | ||
onImageLoadCallback(image, index); | ||
reslove(image); | ||
|
||
if (cb) { | ||
cb(image, index); | ||
} | ||
}; | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
src/utils/load-images/load-images-relative-to-container-size.js
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,7 +1,7 @@ | ||
import { loadImageAsPromise } from './load-image-as-promise'; | ||
|
||
export const loadImagesRelativeToContainerSize = async (imagesSrcs, srcConfig, onImageLoadCallback) => { | ||
export const loadImagesRelativeToContainerSize = async (imagesSrcs, cb) => { | ||
await Promise.all(imagesSrcs.map(async (src, index) => { | ||
await loadImageAsPromise(src, index, srcConfig, onImageLoadCallback); | ||
await loadImageAsPromise(src, index, cb); | ||
})); | ||
}; |
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,7 +1,7 @@ | ||
import { loadImageAsPromise } from './load-image-as-promise'; | ||
|
||
export const loadOriginalImages = async (imagesSrcs, onImageLoadCallback) => { | ||
export const loadOriginalImages = async (imagesSrcs, cb) => { | ||
await Promise.all(imagesSrcs.map(async (src, index) => { | ||
await loadImageAsPromise(src, index, null, onImageLoadCallback); | ||
await loadImageAsPromise(src, index, cb); | ||
})); | ||
}; |
Oops, something went wrong.