9
9
'matchMedia' ,
10
10
'Magento_PageBuilder/js/utils/breakpoints' ,
11
11
'Magento_PageBuilder/js/events' ,
12
+ 'Magento_PageBuilder/js/config' ,
12
13
'slick'
13
- ] , function ( $ , _ , mediaCheck , breakpointsUtils , events ) {
14
+ ] , function ( $ , _ , mediaCheck , breakpointsUtils , events , pageBuilderConfig ) {
14
15
'use strict' ;
15
16
16
17
/**
@@ -31,12 +32,34 @@ define([
31
32
$carouselElement . slick ( config ) ;
32
33
}
33
34
34
- return function ( config , element ) {
35
- var $element = $ ( element ) ,
35
+ function initSlider ( $element , slickConfig , breakpoint ) {
36
+ var productCount = $element . find ( '.product-item' ) . length ,
36
37
$carouselElement = $ ( $element . children ( ) ) ,
37
- productCount = $ ( element ) . find ( '.product-item' ) . length ,
38
38
centerModeClass = 'center-mode' ,
39
39
carouselMode = $element . data ( 'carousel-mode' ) ,
40
+ slidesToShow = breakpoint . options . products [ carouselMode ] ?
41
+ breakpoint . options . products [ carouselMode ] . slidesToShow :
42
+ breakpoint . options . products . default . slidesToShow ;
43
+
44
+ slickConfig . slidesToShow = parseFloat ( slidesToShow ) ;
45
+
46
+ if ( carouselMode === 'continuous' && productCount > slickConfig . slidesToShow ) {
47
+ $element . addClass ( centerModeClass ) ;
48
+ slickConfig . centerPadding = $element . data ( 'center-padding' ) ;
49
+ slickConfig . centerMode = true ;
50
+ } else {
51
+ $element . removeClass ( centerModeClass ) ;
52
+ slickConfig . infinite = $element . data ( 'infinite-loop' ) ;
53
+ }
54
+
55
+ buildSlick ( $carouselElement , slickConfig ) ;
56
+ }
57
+
58
+ return function ( config , element ) {
59
+ var $element = $ ( element ) ,
60
+ $carouselElement = $ ( $element . children ( ) ) ,
61
+ stageId = $ ( $element ) . parents ( '[data-role="pagebuilder-stage"]' ) . attr ( 'id' ) ,
62
+ currentViewport = pageBuilderConfig . getConfig ( "viewport" ) ,
40
63
slickConfig = {
41
64
autoplay : $element . data ( 'autoplay' ) ,
42
65
autoplaySpeed : $element . data ( 'autoplay-speed' ) || 0 ,
@@ -50,31 +73,27 @@ define([
50
73
51
74
/** @inheritdoc */
52
75
entry : function ( ) {
53
- var slidesToShow = breakpoint . options . products [ carouselMode ] ?
54
- breakpoint . options . products [ carouselMode ] . slidesToShow :
55
- breakpoint . options . products . default . slidesToShow ;
56
-
57
- slickConfig . slidesToShow = parseFloat ( slidesToShow ) ;
58
-
59
- if ( carouselMode === 'continuous' && productCount > slickConfig . slidesToShow ) {
60
- $element . addClass ( centerModeClass ) ;
61
- slickConfig . centerPadding = $element . data ( 'center-padding' ) ;
62
- slickConfig . centerMode = true ;
63
- } else {
64
- $element . removeClass ( centerModeClass ) ;
65
- slickConfig . infinite = $element . data ( 'infinite-loop' ) ;
66
- }
67
-
68
- buildSlick ( $carouselElement , slickConfig ) ;
76
+ initSlider ( $element , slickConfig , breakpoint ) ;
69
77
}
70
78
} ) ;
71
79
} ) ;
72
80
81
+ //initialize slider when content type is added in mobile viewport
82
+ if ( currentViewport === 'mobile' ) {
83
+ var breakpoint = config . breakpoints [ currentViewport ] ;
84
+ initSlider ( $element , slickConfig , breakpoint ) ;
85
+ }
86
+
73
87
// Redraw slide after content type gets redrawn
74
88
events . on ( 'contentType:redrawAfter' , function ( args ) {
75
89
if ( $carouselElement . closest ( args . element ) . length ) {
76
90
$carouselElement . slick ( 'setPosition' ) ;
77
91
}
78
92
} ) ;
93
+
94
+ events . on ( 'stage:' + stageId + ':viewportChangeAfter' , function ( args ) {
95
+ var breakpoint = config . breakpoints [ args . viewport ] ;
96
+ initSlider ( $element , slickConfig , breakpoint ) ;
97
+ } ) ;
79
98
} ;
80
99
} ) ;
0 commit comments