Skip to content

Commit

Permalink
Update: Add event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto committed Feb 7, 2024
1 parent 93dcf5f commit a8b3a11
Show file tree
Hide file tree
Showing 24 changed files with 91 additions and 42 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ There is one global variable who get set during the intialisation:
`lightbox.init()`). With this, you can add you own [event listener](https://photoswipe.com/events/) or trigger
[methods](https://photoswipe.com/methods/).

## Javascript event listener

If you dispatch the event `neos-photoswipe:init` all type of lightboxes get initialized. Great if you work in AJAX
enviroments. You can also activate a specific type of lightbox: `neos-photoswipe.images:init`,
`neos-photoswipe.fetch:init` or `neos-photoswipe.template:init`.

Example:

```js
window.dispatchEvent(new Event("neos-photoswipe:init"));
```

## Alter generated markup

### Adding custom attributes to the PhotoSwipe container
Expand Down
17 changes: 15 additions & 2 deletions Resources/Private/Assets/Fetch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import PhotoSwipeLightbox from "photoswipe/lightbox";
import { getPswpContainer, dispatchEvent, createElement, getDataOptions, setPswpContainerAttributes } from "./Helper";
import {
getPswpContainer,
dispatchEvent,
createElement,
getDataOptions,
setPswpContainerAttributes,
addEventListener,
} from "./Helper";

const optionsFromNeos = { ...getDataOptions("photoswipeI18n"), ...getDataOptions("photoswipeFetchOptions") };

Expand Down Expand Up @@ -116,8 +123,14 @@ function toggleLoadingClass(show = true) {
getPswpContainer()?.classList.toggle("pswp--fetch-loading", show);
}

const lightbox = init();

addEventListener("fetch", () => {
lightbox.init();
});

window.neosPhotoSwipe = window.neosPhotoSwipe || {};
window.neosPhotoSwipe.fetch = {
init,
lightbox: init(),
lightbox,
};
7 changes: 6 additions & 1 deletion Resources/Private/Assets/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ function createElement(markupOrNode, wrappingClass) {
return element;
}

export { getPswpContainer, dispatchEvent, createElement, getDataOptions, setPswpContainerAttributes };
function addEventListener(type, callback) {
const listener = ["neos-photoswipe:init", `neos-photoswipe.${type}:init`];
listener.forEach((event) => window.addEventListener(event, callback, true));
}

export { getPswpContainer, dispatchEvent, createElement, getDataOptions, setPswpContainerAttributes, addEventListener };
10 changes: 8 additions & 2 deletions Resources/Private/Assets/Main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PhotoSwipeLightbox from "photoswipe/lightbox";
import { dispatchEvent, getDataOptions, setPswpContainerAttributes } from "./Helper";
import { dispatchEvent, getDataOptions, setPswpContainerAttributes, addEventListener } from "./Helper";

const optionsFromNeos = { ...getDataOptions("photoswipeI18n"), ...getDataOptions("photoswipeOptions") };

Expand All @@ -21,10 +21,16 @@ function init(options = {}) {
return lightbox;
}

const lightbox = init();

addEventListener("images", () => {
lightbox.init();
});

window.neosPhotoSwipe = window.neosPhotoSwipe || {};
window.neosPhotoSwipe.images = {
init,
lightbox: init(),
lightbox,
};

export default init;
17 changes: 15 additions & 2 deletions Resources/Private/Assets/Template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import PhotoSwipeLightbox from "photoswipe/lightbox";
import { getPswpContainer, dispatchEvent, createElement, getDataOptions, setPswpContainerAttributes } from "./Helper";
import {
getPswpContainer,
dispatchEvent,
createElement,
getDataOptions,
setPswpContainerAttributes,
addEventListener,
} from "./Helper";

const optionsFromNeos = { ...getDataOptions("photoswipeI18n"), ...getDataOptions("photoswipeTemplateOptions") };

Expand Down Expand Up @@ -63,8 +70,14 @@ function init(options = {}) {
return lightbox;
}

const lightbox = init();

addEventListener("template", () => {
lightbox.init();
});

window.neosPhotoSwipe = window.neosPhotoSwipe || {};
window.neosPhotoSwipe.template = {
init,
lightbox: init(),
lightbox,
};
2 changes: 1 addition & 1 deletion Resources/Public/Modules/Fetch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a8b3a11

Please sign in to comment.