@schukai/monster 3.112.4 → 3.114.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -73,6 +73,8 @@ const configSymbol = Symbol("config");
73
73
  * @since 3.74.0
74
74
  * @copyright schukai GmbH
75
75
  * @summary A beautiful Slider that can make your life easier and also looks good.
76
+ * @fires monster-slider-resized
77
+ * @fires monster-slider-moved
76
78
  */
77
79
  class Slider extends CustomElement {
78
80
  /**
@@ -97,12 +99,20 @@ class Slider extends CustomElement {
97
99
  draggingPos: 0,
98
100
  startPos: 0,
99
101
  autoPlayInterval: null,
102
+
103
+ eventHandler: {
104
+ mouseOverPause: null,
105
+ mouseout: null,
106
+ touchstart: null,
107
+ touchend: null,
108
+ },
100
109
  };
101
110
 
102
111
  // set --monster-slides-width
103
112
  const slides = this.shadowRoot.querySelector(
104
113
  `[${ATTRIBUTE_ROLE}="slider"]`,
105
114
  );
115
+
106
116
  const slidesVisible = getVisibleSlidesFromContainerWidth.call(this);
107
117
  slides.style.setProperty(
108
118
  "--monster-slides-width",
@@ -283,6 +293,11 @@ function initThumbnails() {
283
293
  "[data-monster-role='thumbnails']",
284
294
  );
285
295
 
296
+ // remove all thumbnails
297
+ while (thumbnails.firstChild) {
298
+ thumbnails.removeChild(thumbnails.firstChild);
299
+ }
300
+
286
301
  const { originSlides } = getSlidesAndTotal.call(this);
287
302
 
288
303
  originSlides.forEach((x, index) => {
@@ -354,26 +369,57 @@ function initAutoPlay() {
354
369
  }, startDelay);
355
370
 
356
371
  if (autoPlay.mouseOverPause) {
357
- this.addEventListener("mouseover", () => {
358
- clearInterval(this[configSymbol].autoPlayInterval);
359
- });
372
+ if (this[configSymbol].eventHandler.mouseOverPause === null) {
373
+ this[configSymbol].eventHandler.mouseOverPause = () => {
374
+ clearInterval(this[configSymbol].autoPlayInterval);
375
+ };
376
+
377
+ this.addEventListener(
378
+ "mouseover",
379
+ this[configSymbol].eventHandler.mouseOverPause,
380
+ );
381
+ }
360
382
 
361
- this.addEventListener("mouseout", () => {
362
- if (this[configSymbol].isDragging) {
363
- return;
364
- }
365
- start();
366
- });
383
+ if (this[configSymbol].eventHandler.mouseout === null) {
384
+ this[configSymbol].eventHandler.mouseout = () => {
385
+ if (this[configSymbol].isDragging) {
386
+ return;
387
+ }
388
+ start();
389
+ };
390
+
391
+ this.addEventListener(
392
+ "mouseout",
393
+ this[configSymbol].eventHandler.mouseout,
394
+ );
395
+ }
367
396
  }
368
397
 
369
398
  if (autoPlay.touchPause) {
370
- this.addEventListener("touchstart", () => {
371
- clearInterval(this[configSymbol].autoPlayInterval);
372
- });
399
+ if (this[configSymbol].eventHandler.touchstart === null) {
400
+ this[configSymbol].eventHandler.touchstart = () => {
401
+ clearInterval(this[configSymbol].autoPlayInterval);
402
+ };
403
+
404
+ this.addEventListener(
405
+ "touchstart",
406
+ this[configSymbol].eventHandler.touchstart,
407
+ );
408
+ }
373
409
 
374
- this.addEventListener("touchend", () => {
375
- start();
376
- });
410
+ if (this[configSymbol].eventHandler.touchend === null) {
411
+ this[configSymbol].eventHandler.touchend = () => {
412
+ if (this[configSymbol].isDragging) {
413
+ return;
414
+ }
415
+ start();
416
+ };
417
+
418
+ this.addEventListener(
419
+ "touchend",
420
+ this[configSymbol].eventHandler.touchend,
421
+ );
422
+ }
377
423
  }
378
424
  }
379
425
 
@@ -602,6 +648,7 @@ function moveTo(index, animation) {
602
648
  /**
603
649
  * @private
604
650
  * @return {initEventHandler}
651
+ * @fires monster-slider-resized
605
652
  */
606
653
  function initEventHandler() {
607
654
  const self = this;
@@ -632,6 +679,40 @@ function initEventHandler() {
632
679
  );
633
680
  }
634
681
 
682
+ const initialSize = {
683
+ width: this[sliderElementSymbol]?.offsetWidth || 0,
684
+ height: this[sliderElementSymbol]?.offsetHeight || 0,
685
+ };
686
+
687
+ const resizeObserver = new ResizeObserver((entries) => {
688
+ for (let entry of entries) {
689
+ const { width, height } = entry.contentRect;
690
+ if (width !== initialSize.width || height !== initialSize.height) {
691
+ self.stopAutoPlay();
692
+
693
+ if (this.getOption("features.thumbnails")) {
694
+ initThumbnails.call(this);
695
+ }
696
+
697
+ const slidesVisible = getVisibleSlidesFromContainerWidth.call(this);
698
+ this[sliderElementSymbol].style.setProperty(
699
+ "--monster-slides-width",
700
+ `${100 / slidesVisible}%`,
701
+ );
702
+
703
+ moveTo.call(self, 0, false);
704
+ self.startAutoPlay();
705
+
706
+ fireCustomEvent(self, "monster-slider-resized", {
707
+ width: width,
708
+ height: height,
709
+ });
710
+ }
711
+ }
712
+ });
713
+
714
+ resizeObserver.observe(this[sliderElementSymbol]);
715
+
635
716
  return this;
636
717
  }
637
718