Skip to content

Commit 49aa35e

Browse files
author
joiecaquicla
committed
magento2/magento2-page-builder#636: Responsive breakpoint switcher - additional fixes for product content type styling and preview issue
1 parent aea1d8b commit 49aa35e

File tree

3 files changed

+59
-22
lines changed
  • app/code/Magento/PageBuilder/view
    • adminhtml/web
    • base/web/js/content-type/products/appearance/carousel

3 files changed

+59
-22
lines changed

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/products/preview.js

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/products/preview.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ export default class Preview extends BasePreview {
190190
private buildSlickConfig() {
191191
const attributes = this.data.main.attributes();
192192
const productCount = $(this.widgetUnsanitizedHtml()).find(this.productItemSelector).length;
193+
const viewports = Config.getConfig("viewports");
194+
const currentViewport = this.viewport();
195+
const carouselMode = attributes["data-carousel-mode"];
193196
const config: {[key: string]: any} = {
194197
slidesToShow: this.slidesToShow,
195198
slidesToScroll: this.slidesToShow,
@@ -199,7 +202,13 @@ export default class Preview extends BasePreview {
199202
autoplaySpeed: parseFloat(attributes["data-autoplay-speed"]),
200203
};
201204

202-
if (attributes["data-carousel-mode"] === "continuous" && productCount > this.slidesToShow) {
205+
const slidesToShow = viewports[currentViewport].options.products[carouselMode] ?
206+
viewports[currentViewport].options.products[carouselMode].slidesToShow :
207+
viewports[currentViewport].options.products.default.slidesToShow;
208+
209+
config.slidesToShow = parseFloat(slidesToShow);
210+
211+
if (attributes["data-carousel-mode"] === "continuous" && productCount > config.slidesToShow) {
203212
config.centerPadding = attributes["data-center-padding"];
204213
config.centerMode = true;
205214
$(this.element).addClass(this.centerModeClass);

app/code/Magento/PageBuilder/view/base/web/js/content-type/products/appearance/carousel/widget.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ define([
99
'matchMedia',
1010
'Magento_PageBuilder/js/utils/breakpoints',
1111
'Magento_PageBuilder/js/events',
12+
'Magento_PageBuilder/js/config',
1213
'slick'
13-
], function ($, _, mediaCheck, breakpointsUtils, events) {
14+
], function ($, _, mediaCheck, breakpointsUtils, events, pageBuilderConfig) {
1415
'use strict';
1516

1617
/**
@@ -31,12 +32,34 @@ define([
3132
$carouselElement.slick(config);
3233
}
3334

34-
return function (config, element) {
35-
var $element = $(element),
35+
function initSlider($element, slickConfig, breakpoint) {
36+
var productCount = $element.find('.product-item').length,
3637
$carouselElement = $($element.children()),
37-
productCount = $(element).find('.product-item').length,
3838
centerModeClass = 'center-mode',
3939
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"),
4063
slickConfig = {
4164
autoplay: $element.data('autoplay'),
4265
autoplaySpeed: $element.data('autoplay-speed') || 0,
@@ -50,31 +73,27 @@ define([
5073

5174
/** @inheritdoc */
5275
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);
6977
}
7078
});
7179
});
7280

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+
7387
// Redraw slide after content type gets redrawn
7488
events.on('contentType:redrawAfter', function (args) {
7589
if ($carouselElement.closest(args.element).length) {
7690
$carouselElement.slick('setPosition');
7791
}
7892
});
93+
94+
events.on('stage:' + stageId + ':viewportChangeAfter', function (args) {
95+
var breakpoint = config.breakpoints[args.viewport];
96+
initSlider($element, slickConfig, breakpoint);
97+
});
7998
};
8099
});

0 commit comments

Comments
 (0)