-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Showing
36 changed files
with
254 additions
and
46 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
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,40 @@ | ||
let pswpContainer = null; | ||
|
||
function getPswpContainer() { | ||
if (pswpContainer) { | ||
return pswpContainer; | ||
} | ||
pswpContainer = document.querySelector(".pswp"); | ||
return pswpContainer; | ||
} | ||
|
||
function dispatchEvent(detail) { | ||
const event = new CustomEvent("neosphotoswipe", { detail }); | ||
document.dispatchEvent(event); | ||
} | ||
|
||
function isNode(element) { | ||
return element instanceof Node; | ||
} | ||
|
||
function createElement(markupOrNode, wrappingClass) { | ||
let node = isNode(markupOrNode) ? markupOrNode : null; | ||
let appendElement = null; | ||
|
||
if (!node && typeof markupOrNode === "string") { | ||
node = document.createElement("template"); | ||
node.innerHTML = markupOrNode; | ||
} | ||
if (node.tagName === "TEMPLATE") { | ||
appendElement = node.content.cloneNode(true); | ||
} else if (isNode(node)) { | ||
appendElement = node; | ||
} | ||
|
||
const element = document.createElement("div"); | ||
element.classList.add(wrappingClass); | ||
element.append(appendElement); | ||
return element; | ||
} | ||
|
||
export { getPswpContainer, dispatchEvent, createElement }; |
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
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,74 @@ | ||
import PhotoSwipeLightbox from "photoswipe/lightbox"; | ||
import { getPswpContainer, dispatchEvent, createElement } from "./Helper"; | ||
|
||
const i18n = JSON.parse(document.querySelector("[data-photoswipe-i18n]")?.dataset?.photoswipeI18n || "{}"); | ||
let optionsFromNeos = JSON.parse( | ||
document.querySelector("[data-photoswipe-template-options]")?.dataset?.photoswipeTemplateOptions || "{}", | ||
); | ||
optionsFromNeos = { ...i18n, ...optionsFromNeos }; | ||
|
||
const wrappingClass = optionsFromNeos.wrappingClass || "jonnitto-photoswipe-content"; | ||
delete optionsFromNeos.wrappingClass; | ||
|
||
let currentContent = null; | ||
|
||
function init(options = {}) { | ||
options = { ...optionsFromNeos, ...options }; | ||
const lightbox = new PhotoSwipeLightbox({ | ||
preloadFirstSlide: false, | ||
allowPanToNext: false, | ||
initialZoomLevel: 1, | ||
secondaryZoomLevel: 1, | ||
maxZoomLevel: 1, | ||
wheelToZoom: false, | ||
closeOnVerticalDrag: false, | ||
mainClass: "pswp--template", | ||
arrowKeys: false, | ||
preloaderDelay: 1000, | ||
|
||
pswpModule: () => import("photoswipe"), | ||
...options, | ||
}); | ||
|
||
lightbox.addFilter("isContentZoomable", () => false); | ||
lightbox.addFilter("preventPointerEvent", () => true); | ||
|
||
lightbox.on("firstUpdate", (event) => { | ||
const container = getPswpContainer(); | ||
container.setAttribute("data-turbo-temporary", true); | ||
// This enables default scrolling | ||
container?.addEventListener( | ||
"wheel", | ||
(event) => { | ||
event.stopImmediatePropagation(); | ||
event.stopPropagation(); | ||
}, | ||
{ passive: true }, | ||
); | ||
}); | ||
|
||
lightbox.on("destroy", () => { | ||
dispatchEvent({ type: "template", action: "close" }); | ||
}); | ||
|
||
lightbox.on("contentLoad", async (event) => { | ||
const { content } = event; | ||
const element = content?.data?.element; | ||
const template = element?.querySelector("template"); | ||
if (!element || !template) { | ||
return; | ||
} | ||
content.state = "loading"; | ||
event.preventDefault(); | ||
content.element = createElement(template, wrappingClass); | ||
content.onLoaded(); | ||
}); | ||
lightbox.init(); | ||
return lightbox; | ||
} | ||
|
||
window.neosPhotoSwipe = window.neosPhotoSwipe || {}; | ||
window.neosPhotoSwipe.template = { | ||
init, | ||
lightbox: init(), | ||
}; |
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.