nfp_sites/nfp_moe/app/media.js

50 lines
No EOL
1.5 KiB
JavaScript

export function generateSource(item, cover) {
if (!item) return
if (item.media_alt_prefix) {
item.pictureFallback = item.media_alt_prefix + '_small.jpg'
item.pictureJpeg = item.media_alt_prefix + '_small.jpg' + ' 720w, '
+ item.media_alt_prefix + '_medium.jpg' + ' 1300w, '
+ item.media_alt_prefix + '_large.jpg 1920w'
item.pictureAvif = item.media_alt_prefix + '_small.avif' + ' 720w, '
+ item.media_alt_prefix + '_medium.avif' + ' 1300w, '
+ item.media_alt_prefix + '_large.avif 1920w'
item.pictureCover = cover
} else {
item.pictureFallback = null
item.pictureJpeg = null
item.pictureAvif = null
item.pictureCover = null
}
}
export function getBannerImage(item, prefix) {
if (!item || !item.banner_alt_prefix) return null
let out = {
path: prefix + item.path,
name: item.name,
original: item.banner_path,
banner: item.banner_alt_prefix
}
var deviceWidth = window.innerWidth
var pixelRatio = window.devicePixelRatio || 1
if ((deviceWidth < 720 && pixelRatio <= 1)
|| (deviceWidth < 360 && pixelRatio <= 2)) {
out.banner += '_small'
} else if ((deviceWidth < 1300 && pixelRatio <= 1)
|| (deviceWidth < 650 && pixelRatio <= 2)) {
out.banner += '_medium'
} else {
out.banner += '_large'
}
if (window.supportsavif) {
out.banner += '.avif'
} else {
out.banner += '.jpg'
}
return out
}