forked from forcedotcom/commerce-on-lightning-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproductGalleryUtils.js
More file actions
48 lines (45 loc) · 1.9 KB
/
productGalleryUtils.js
File metadata and controls
48 lines (45 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* For full license text, see the LICENSE file in the repo
* root or https://opensource.org/licenses/apache-2-0/
*/
/**
* @typedef {object} ImageSizes
* @property {number} mobile The image width on mobile phone devices
* @property {number} tablet The image width on tablet devices
* @property {number} desktop The image width on desktop-size devices
*/
/**
* @param {(HTMLElement | null)} el A container that holds the image element to extract the sizes from
* @returns {ImageSizes} An object holding the image sizes for various screen sizes
*/
export function getImageSizes(el) {
const styles = el && getComputedStyle(el);
const mobile = Number(styles?.getPropertyValue('--com-c-image-width-mobile'));
const tablet = Number(styles?.getPropertyValue('--com-c-image-width-tablet'));
const desktop = Number(styles?.getPropertyValue('--com-c-image-width-desktop'));
return {
mobile: mobile && !Number.isNaN(mobile) ? mobile : 0,
tablet: tablet && !Number.isNaN(tablet) ? tablet : 0,
desktop: desktop && !Number.isNaN(desktop) ? desktop : 0,
};
}
/**
* @param {ImageSizes} imageSizes An object holding the image sizes for various screen sizes
* @returns {boolean} Whether all the different image sizes are defined inside the container object
*/
export function imageSizesDefined(imageSizes) {
return imageSizes && Object.values(imageSizes).every((size) => size && size > 0);
}
/**
* @param {(HTMLElement | null)} el A container that holds the image element to extract the sizes from
* @param {ImageSizes} imageSizes An object holding the image sizes for various screen sizes
*/
export function calculateImageSizes(el, imageSizes) {
if (!imageSizesDefined(imageSizes)) {
const sizes = getImageSizes(el);
Object.assign(imageSizes, sizes);
}
}