Skip to content

Commit

Permalink
test (last one works)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdarkle committed Sep 8, 2024
1 parent 666b405 commit 8da7a68
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion js/dist/forum.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/forum.js.map

Large diffs are not rendered by default.

56 changes: 30 additions & 26 deletions js/src/forum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,40 @@ app.initializers.add('darkle/fancybox', () => {
extend(CommentPost.prototype, 'oncreate', function () {
const postBody = this.element.querySelector('.Post-body');

// Function to get the correct image URL
const getImageUrl = (element) => {
const img = element.querySelector('img');
return img ? (img.getAttribute('data-src') || img.getAttribute('src')) : element.href;
};

// Initialize Carousel for each gallery
postBody.querySelectorAll('.fancybox-gallery').forEach((gallery, index) => {
gallery.id = `gallery-${index}`;
new Carousel(gallery, {
const carousel = new Carousel(gallery, {
Dots: false,
on: {
createSlide: (carousel, slide) => {
const link = slide.el.querySelector('a');
if (link) {
link.addEventListener('click', (e) => {
if (!carousel.isDragging) {
e.preventDefault();
const slides = Array.from(gallery.querySelectorAll('.f-carousel__slide'));
const images = slides.map(slide => ({
src: getImageUrl(slide.querySelector('a')),
type: 'image'
}));
Fancybox.show(images, { startIndex: slide.index });
}
});
}
}
}
});
});

// Function to get the correct image URL
const getImageUrl = (element) => {
const img = element.querySelector('img');
return img ? (img.getAttribute('data-src') || img.getAttribute('src')) : element.href;
};

// Initialize Fancybox for both galleries and single images
Fancybox.bind('[data-fancybox]', {
// Initialize Fancybox for single images
Fancybox.bind('a[data-fancybox="single"]', {
Carousel: {
infinite: false,
},
Expand All @@ -43,26 +61,12 @@ app.initializers.add('darkle/fancybox', () => {
},
});

// Prevent page refresh on all Fancybox-enabled image clicks
postBody.querySelectorAll('a[data-fancybox]').forEach(link => {
// Handle clicks on single images
postBody.querySelectorAll('a[data-fancybox="single"]').forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
const imageUrl = getImageUrl(link);
if (link.getAttribute('data-fancybox') === 'single') {
Fancybox.show([{ src: imageUrl, type: 'image' }]);
} else {
// For carousel images, find the correct starting index
const gallery = link.closest('.fancybox-gallery');
if (gallery) {
const slides = Array.from(gallery.querySelectorAll('.f-carousel__slide'));
const index = slides.indexOf(link.closest('.f-carousel__slide'));
const images = slides.map(slide => ({
src: getImageUrl(slide.querySelector('a')),
type: 'image'
}));
Fancybox.show(images, { startIndex: index });
}
}
Fancybox.show([{ src: imageUrl, type: 'image' }]);
});
});
});
Expand Down

0 comments on commit 8da7a68

Please sign in to comment.