arnowelzel/lightbox-photoswipe

Add support for PhotoSwipe API

Closed this issue · 4 comments

Hey Arno,
I'd like to propose a small improvement for the plugin: the ability to access the JavaScript PhotoSwipe object in order to use the PhotoSwipe API. This would allow external JavaScript to control the gallery and access its properties and thus allow easier customization of the gallery. It would also complement the already existing ability to change the PhotoSwipe markup.

Currently, the gallery-object is created as a local variable and thus is inaccessible outside of the local scope.

To solve this, I would suggest to create a global variable that points to the current PhotoSwipe object in order to make it easily accessible.

I am currently writing a very simple extension-plugin to create a dedicated download-button in the gallery and adding this functionality would allow me to use the API instead of accessing the gallery in other hacky ways.
I have already implemented this in a branch on my fork if you're interested: branch/feature-psinstance-api - relevant commit

Let me know if you think this would be a good improvement for the project.

Best regards,
Thomas

Generally this is not a bad idea. However I would not name the global object window.PhotoSwipe_GalleryInstance but window.lbwpsPhotoSwipe to be consistent with the other global objects like window.lbwpsCopyToClipboard. I will also refactor the existing lbwps_init to lbwpsInit etc..

If you changed the name, just create a pull request.

Thanks for your response. I agree on your concerns and follow your suggestion on the naming-scheme. Expect a pull request shortly!

After checking this in more detail - sorry, it's not that easy. Just assigning window.lbwpsCopyToClipboard inside the initialization function will not make it available globally. Just check typeof window.lbwpsCopyToClipboard in a developer console after adding window.lbwpsCopyToClipboard = gallery; in the initialization. It seems, it is neccessary to create this object in the ready function first.

I just commited my refactored code which solves this.

Closed with f873c61

Sorry for the confusion. You can use window.lbwpsPhotoSwipe to access the gallery object after the lightbox was initialized.