fancyapps / ui

A library of JavaScript UI components, includes the best lightbox - Fancybox5
https://fancyapps.com/
Other
814 stars 98 forks source link

Repetition of events #554

Closed CodeTappert closed 1 year ago

CodeTappert commented 1 year ago

Describe the bug

I recently noticed that the closing of my fancybox gallery takes a lot longer then usual.

So i started to log the events.

And i see this

grafik

It initializes all of the slides (and removes each one of them)

This isnt behaviour i noticed before.

Is there a way to reduce the amount of slides initialized?

Reproduction

Having a gallery with a lot of images and having custom logic in removeSlide Event that takes time

My code:


            removeSlide: (carousel) => {

              let idx = carousel.slides[carousel.page].idx;
              // For some reason idx is false when it is supposed to be 0
              if (!idx) {
                idx = 0;
              }
              let examId = carousel.slides[carousel.page].examid;
              const currentSlideVideoId = `video-player-${examId}-${idx}`;

              const videoElements = document.querySelectorAll("[id*='video-player']");
              videoElements.forEach((videoElement) => {
                if (videoElement.id !== currentSlideVideoId) {
                  if (Object.keys(this.hlsObj).length !== 0 && this.hlsIdx !== `${examId}-${idx}`) {
                    try {
                      this.hlsObj.destroy();
                      this.hlsObj = {};
                      this.hlsIdx = "";
                    } catch (error) {
                      // Do nothing. This is just to prevent the error showing up in the console (we force this error)
                    }
                  }

                  videoElement.textContent = "";
                }
              });
              // Remove all elements whose IDs contain the string `jpeg-v-div`,
              // except for the current slide's JPEG video player
              const currentSlideJpegVideoId = `jpeg-v-div-${this.examId}-${idx}`;
              const jpegVideoElements = document.querySelectorAll("[id*='jpeg-v-div']");
              jpegVideoElements.forEach((jpegVideoElement) => {
                if (jpegVideoElement.id.value !== currentSlideJpegVideoId) {
                  eventBus.$emit(`stopInterval`);

                }
              });
            },

Additional context

No response

fancyapps commented 1 year ago

Hi,

Indeed, there is a problem with repeating events (especially Carousel.createSlide and Carousel.removeSlide). But the Carousel.initSlide event is expected to be fired for each slide. If you want to perform some action when the slide is created in the DOM, then use the Carousel.createSlide event.

CodeTappert commented 1 year ago

Hello, maybe there is a missunderstanding. I do nothing on createSlide or initSlide.

But on removeSlide. I basically need something similar to removeSlide but only for the Slides that to be removed from the DOM.

I know there is unselctSlide but this doesnt happen when i close the gallery

fancyapps commented 1 year ago

Yes, then use the Carousel.removeSlide event, I will fix the repetition.

fancyapps commented 1 year ago

Hi, It should be fixed in the latest release.