2019-05-09 06:48:10 +02:00
|
|
|
|
import PhotoSwipe from "photoswipe";
|
|
|
|
|
import PhotoSwipeUI_Default from "photoswipe/dist/photoswipe-ui-default.js";
|
2019-05-08 04:37:45 +02:00
|
|
|
|
|
2020-04-21 16:34:43 +02:00
|
|
|
|
const thumbs = window.clientConfig.thumbnails;
|
|
|
|
|
|
2019-05-19 20:13:19 +02:00
|
|
|
|
class Viewer {
|
2019-05-08 04:37:45 +02:00
|
|
|
|
constructor() {
|
|
|
|
|
this.el = null;
|
2020-04-15 14:27:05 +02:00
|
|
|
|
this.gallery = null;
|
2019-05-08 04:37:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getEl() {
|
2019-05-13 18:01:50 +02:00
|
|
|
|
if (!this.el) {
|
2019-05-29 01:51:53 +02:00
|
|
|
|
this.el = document.getElementById("p-photo-viewer");
|
2019-05-08 04:37:45 +02:00
|
|
|
|
|
2019-05-19 20:13:19 +02:00
|
|
|
|
if (this.el === null) {
|
|
|
|
|
let err = "no photo viewer element found";
|
2019-12-27 10:36:52 +01:00
|
|
|
|
console.warn(err);
|
2019-05-08 04:37:45 +02:00
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.el;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 16:34:43 +02:00
|
|
|
|
show(items, index = 0) {
|
|
|
|
|
if (!Array.isArray(items) || items.length === 0 || index >= items.length) {
|
|
|
|
|
console.log("Array passed to gallery was empty:", items);
|
2019-05-09 06:18:22 +02:00
|
|
|
|
return;
|
2019-05-08 04:37:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 16:04:17 +02:00
|
|
|
|
const shareButtons = [
|
2019-05-16 04:03:55 +02:00
|
|
|
|
{id: "fit_720", template: "Tiny (size)", label: "Tiny", url: "{{raw_image_url}}", download: true},
|
|
|
|
|
{id: "fit_1280", template: "Small (size)", label: "Small", url: "{{raw_image_url}}", download: true},
|
|
|
|
|
{id: "fit_2048", template: "Medium (size)", label: "Medium", url: "{{raw_image_url}}", download: true},
|
|
|
|
|
{id: "fit_2560", template: "Large (size)", label: "Large", url: "{{raw_image_url}}", download: true},
|
|
|
|
|
{id: "original", template: "Original (size)", label: "Original", url: "{{raw_image_url}}", download: true},
|
2019-05-14 16:04:17 +02:00
|
|
|
|
];
|
|
|
|
|
|
2019-05-08 04:37:45 +02:00
|
|
|
|
const options = {
|
|
|
|
|
index: index,
|
2019-12-27 10:36:52 +01:00
|
|
|
|
history: true,
|
2019-05-14 18:16:35 +02:00
|
|
|
|
preload: [1, 1],
|
2019-05-08 04:37:45 +02:00
|
|
|
|
focus: true,
|
|
|
|
|
modal: true,
|
|
|
|
|
closeEl: true,
|
|
|
|
|
captionEl: true,
|
|
|
|
|
fullscreenEl: true,
|
|
|
|
|
zoomEl: true,
|
2019-05-14 18:16:35 +02:00
|
|
|
|
shareEl: true,
|
2019-05-14 16:04:17 +02:00
|
|
|
|
shareButtons: shareButtons,
|
2019-05-08 04:37:45 +02:00
|
|
|
|
counterEl: false,
|
|
|
|
|
arrowEl: true,
|
|
|
|
|
preloaderEl: true,
|
2019-05-16 04:03:55 +02:00
|
|
|
|
getImageURLForShare: function (button) {
|
2020-04-21 16:34:43 +02:00
|
|
|
|
const item = gallery.currItem;
|
2019-05-16 04:03:55 +02:00
|
|
|
|
|
|
|
|
|
if(button.id === "original") {
|
2020-04-21 16:34:43 +02:00
|
|
|
|
button.label = button.template.replace("size", item.original_w + " × " + item.original_h);
|
|
|
|
|
return item.download_url;
|
2019-05-16 04:03:55 +02:00
|
|
|
|
} else {
|
2020-04-21 16:34:43 +02:00
|
|
|
|
button.label = button.template.replace("size", item[button.id].w + " × " + item[button.id].h);
|
|
|
|
|
return item[button.id].src + "?download=1";
|
2019-05-16 04:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
2019-05-08 04:37:45 +02:00
|
|
|
|
};
|
|
|
|
|
|
2020-04-21 16:34:43 +02:00
|
|
|
|
let gallery = new PhotoSwipe(this.getEl(), PhotoSwipeUI_Default, items, options);
|
2019-05-08 04:37:45 +02:00
|
|
|
|
let realViewportWidth;
|
|
|
|
|
let realViewportHeight;
|
|
|
|
|
let previousSize;
|
|
|
|
|
let nextSize;
|
|
|
|
|
let firstResize = true;
|
|
|
|
|
let photoSrcWillChange;
|
|
|
|
|
|
2020-04-15 14:27:05 +02:00
|
|
|
|
this.gallery = gallery;
|
|
|
|
|
|
2019-05-09 06:48:10 +02:00
|
|
|
|
gallery.listen("beforeResize", () => {
|
2019-05-08 04:37:45 +02:00
|
|
|
|
realViewportWidth = gallery.viewportSize.x * window.devicePixelRatio;
|
|
|
|
|
realViewportHeight = gallery.viewportSize.y * window.devicePixelRatio;
|
|
|
|
|
|
|
|
|
|
if (!previousSize) {
|
2019-05-14 16:04:17 +02:00
|
|
|
|
previousSize = "tile_720";
|
2019-05-08 04:37:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 16:04:17 +02:00
|
|
|
|
nextSize = this.constructor.mapViewportToImageSize(realViewportWidth, realViewportHeight);
|
|
|
|
|
|
2019-05-08 04:37:45 +02:00
|
|
|
|
if (nextSize !== previousSize) {
|
2019-05-09 06:18:22 +02:00
|
|
|
|
photoSrcWillChange = true;
|
2019-05-08 04:37:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (photoSrcWillChange && !firstResize) {
|
|
|
|
|
gallery.invalidateCurrItems();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (firstResize) {
|
|
|
|
|
firstResize = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
photoSrcWillChange = false;
|
|
|
|
|
});
|
|
|
|
|
|
2019-05-09 06:48:10 +02:00
|
|
|
|
gallery.listen("gettingData", function (index, item) {
|
2019-05-08 04:37:45 +02:00
|
|
|
|
item.src = item[nextSize].src;
|
|
|
|
|
item.w = item[nextSize].w;
|
|
|
|
|
item.h = item[nextSize].h;
|
|
|
|
|
previousSize = nextSize;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
gallery.init();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 16:04:17 +02:00
|
|
|
|
static mapViewportToImageSize(viewportWidth, viewportHeight) {
|
|
|
|
|
for (let i = 0; i < thumbs.length; i++) {
|
|
|
|
|
if (thumbs[i].Width >= viewportWidth || thumbs[i].Height >= viewportHeight) {
|
|
|
|
|
return thumbs[i].Name;
|
2019-05-08 04:37:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-14 16:04:17 +02:00
|
|
|
|
|
|
|
|
|
return "fit_720";
|
2019-05-08 04:37:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-19 20:13:19 +02:00
|
|
|
|
export default Viewer;
|