UNPKG

stimulus-photoswipe

Version:

A Stimulus controller for PhotoSwipe.

88 lines (80 loc) 3.7 kB
/* StimulusPhotoSwipe 5.1.0 */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('photoswipe'), require('photoswipe/lightbox'), require('@hotwired/stimulus')) : typeof define === 'function' && define.amd ? define(['exports', 'photoswipe', 'photoswipe/lightbox', '@hotwired/stimulus'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.StimulusPhotoSwipe = {}, global.PhotoSwipe, global.PhotoSwipeLightbox, global.Stimulus)); })(this, (function (exports, PhotoSwipe, PhotoSwipeLightbox, stimulus) { 'use strict'; function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var PhotoSwipe__default = /*#__PURE__*/_interopDefaultLegacy(PhotoSwipe); var PhotoSwipeLightbox__default = /*#__PURE__*/_interopDefaultLegacy(PhotoSwipeLightbox); class UsePhotoSwipe { constructor(controller, options) { let element = options.element; if (element === undefined) { element = controller.element; } const pswpOptions = options.photoswipe; const lightbox = new PhotoSwipeLightbox__default["default"](Object.assign({ gallery: element, children: 'a', pswpModule: PhotoSwipe__default["default"] }, pswpOptions)); if (options.handlers !== undefined) { Object.entries(options.handlers).forEach(([eventName, handlers]) => { for (let i = 0; i < handlers.length; i++) { const handler = handlers[i].bind(controller); lightbox.on(eventName, handler); } }); } if (options.filters !== undefined) { Object.entries(options.filters).forEach(([eventName, filters]) => { for (let i = 0; i < filters.length; i++) { const filter = filters[i].bind(controller); lightbox.addFilter(eventName, filter); } }); } lightbox.init(); this.lightbox = lightbox; } destroy() { if (this.lightbox !== undefined) { this.lightbox.destroy(); this.lightbox = undefined; } } } function usePhotoSwipe(controller, options) { const photoswipe = new UsePhotoSwipe(controller, options); const controllerDisconnect = controller.disconnect.bind(controller); Object.assign(controller, { disconnect() { controllerDisconnect(); photoswipe.destroy(); } }); return photoswipe; } class PhotoSwipeController extends stimulus.Controller { connect() { this.lightbox = usePhotoSwipe(this, { element: this.element, photoswipe: this.pswpOptionsValue }).lightbox; } loadAndOpen(e) { var _a; const currentTarget = e.currentTarget; const slideIndex = currentTarget.dataset.pswpSlideIndex || '0'; (_a = this.lightbox) === null || _a === void 0 ? void 0 : _a.loadAndOpen(parseInt(slideIndex), { gallery: this.element }); } } PhotoSwipeController.values = { pswpOptions: Object }; exports.PhotoSwipeController = PhotoSwipeController; exports.UsePhotoSwipe = UsePhotoSwipe; exports.usePhotoSwipe = usePhotoSwipe; Object.defineProperty(exports, '__esModule', { value: true }); }));