Skip to content

Commit

Permalink
MAGETWO-58437: [Backport] [github #6195, 4101] Gallery doesn't show a…
Browse files Browse the repository at this point in the history
…ll images added to configurable options - for 2.1.x
  • Loading branch information
Denys Rul authored and eug123 committed Oct 28, 2016
1 parent de3b4df commit b316fbd
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ $_attributes = $block->decorateArray($block->getAllowAttributes());
"#product_addtocart_form": {
"configurable": {
"spConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>,
"onlyMainImg": <?php /* @escapeNotVerified */ echo $block->getVar('change_only_base_image', 'Magento_ConfigurableProduct') ?: 'false'; ?>
"gallerySwitchStrategy": "<?php /* @escapeNotVerified */ echo $block->getVar('gallery_switch_strategy',
'Magento_ConfigurableProduct') ?: 'replace'; ?>"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ define([
mediaGallerySelector: '[data-gallery-role=gallery-placeholder]',
mediaGalleryInitial: null,
slyOldPriceSelector: '.sly-old-price',
onlyMainImg: false

/**
* Defines the mechanism of how images of a gallery should be
* updated when user switches between configurations of a product.
*
* As for now value of this option can be either 'replace' or 'prepend'.
*
* @type {String}
*/
gallerySwitchStrategy: 'replace'
},

/**
Expand All @@ -54,6 +63,8 @@ define([

// Setup/configure values to inputs
this._configureForValues();

$(this.element).trigger('configurable.initialized');
},

/**
Expand Down Expand Up @@ -83,10 +94,10 @@ define([

this.inputSimpleProduct = this.element.find(options.selectSimpleProduct);

gallery.on('gallery:loaded', function () {
var galleryObject = gallery.data('gallery');
options.mediaGalleryInitial = galleryObject.returnCurrentImages();
});
gallery.data('gallery') ?
this._onGalleryLoaded(gallery) :
gallery.on('gallery:loaded', this._onGalleryLoaded.bind(this, gallery));

},

/**
Expand Down Expand Up @@ -257,46 +268,33 @@ define([
*/
_changeProductImage: function () {
var images,
initialImages = $.extend(true, [], this.options.mediaGalleryInitial),
initialImages = this.options.mediaGalleryInitial,
galleryObject = $(this.options.mediaGallerySelector).data('gallery');

if (this.options.spConfig.images[this.simpleProduct]) {
images = $.extend(true, [], this.options.spConfig.images[this.simpleProduct]);
if (!galleryObject) {
return;
}

function updateGallery(imagesArr) {
var imgToUpdate,
mainImg;
images = this.options.spConfig.images[this.simpleProduct];

mainImg = imagesArr.filter(function (img) {
return img.isMain;
});
if (images) {
if (this.options.gallerySwitchStrategy === 'prepend') {
images = images.concat(initialImages);
}

imgToUpdate = mainImg.length ? mainImg[0] : imagesArr[0];
galleryObject.updateDataByIndex(0, imgToUpdate);
galleryObject.seek(1);
}
images = $.extend(true, [], images);

if (galleryObject) {
if (images) {
images.map(function (img) {
img.type = 'image';
});
images.forEach(function (img) {
img.type = 'image';
});

if (this.options.onlyMainImg) {
updateGallery(images);
} else {
galleryObject.updateData(images)
}
} else {
if (this.options.onlyMainImg) {
updateGallery(initialImages);
} else {
galleryObject.updateData(this.options.mediaGalleryInitial);
$(this.options.mediaGallerySelector).AddFotoramaVideoEvents();
}
}
galleryObject.updateData(images);
} else {
galleryObject.updateData(initialImages);
$(this.options.mediaGallerySelector).AddFotoramaVideoEvents();
}

galleryObject.first();
},

/**
Expand Down Expand Up @@ -504,8 +502,18 @@ define([
} else {
$(this.options.slyOldPriceSelector).hide();
}
}
},

/**
* Callback which fired after gallery gets initialized.
*
* @param {HTMLElement} element - DOM element associated with gallery.
*/
_onGalleryLoaded: function (element) {
var galleryObject = element.data('gallery');

this.options.mediaGalleryInitial = galleryObject.returnCurrentImages();
}
});

return $.mage.configurable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"jsonSwatchConfig": <?php /* @escapeNotVerified */
echo $swatchOptions = $block->getJsonSwatchConfig(); ?>,
"mediaCallback": "<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>",
"onlyMainImg": <?php /* @escapeNotVerified */ echo $block->getVar('change_only_base_image',
'Magento_Swatches') ?: 'false'; ?>
"gallerySwitchStrategy": "<?php /* @escapeNotVerified */ echo $block->getVar('gallery_switch_strategy',
'Magento_ConfigurableProduct') ?: 'replace'; ?>"
}
}
}
Expand Down
55 changes: 36 additions & 19 deletions app/code/Magento/Swatches/view/frontend/web/js/swatch-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,15 @@ define([
// Cache for BaseProduct images. Needed when option unset
mediaGalleryInitial: [{}],

//
onlyMainImg: false
/**
* Defines the mechanism of how images of a gallery should be
* updated when user switches between configurations of a product.
*
* As for now value of this option can be either 'replace' or 'prepend'.
*
* @type {String}
*/
gallerySwitchStrategy: 'replace'
},

/**
Expand Down Expand Up @@ -236,11 +243,9 @@ define([
this.element.parents('.product-item-info');

if (isProductViewExist) {
gallery.on('gallery:loaded', function () {
var galleryObject = gallery.data('gallery');

options.mediaGalleryInitial = galleryObject.returnCurrentImages();
});
gallery.data('gallery') ?
this._onGalleryLoaded(gallery) :
gallery.on('gallery:loaded', this._onGalleryLoaded.bind(this, gallery));
} else {
options.mediaGalleryInitial = [{
'img': $main.find('.product-image-photo').attr('src')
Expand Down Expand Up @@ -900,26 +905,27 @@ define([
*/
updateBaseImage: function (images, context, isProductViewExist) {
var justAnImage = images[0],
updateImg,
imagesToUpdate,
initialImages = this.options.mediaGalleryInitial,
gallery = context.find(this.options.mediaGallerySelector).data('gallery'),
item;
imagesToUpdate,
isInitial;

if (isProductViewExist) {
imagesToUpdate = images.length ? this._setImageType($.extend(true, [], images)) : [];
isInitial = _.isEqual(imagesToUpdate, initialImages);

if (this.options.onlyMainImg) {
updateImg = imagesToUpdate.filter(function (img) {
return img.isMain;
});
item = updateImg.length ? updateImg[0] : imagesToUpdate[0];
gallery.updateDataByIndex(0, item);
if (this.options.gallerySwitchStrategy === 'prepend' && !isInitial) {
imagesToUpdate = imagesToUpdate.concat(initialImages);
}

gallery.seek(1);
} else {
gallery.updateData(imagesToUpdate);
gallery.updateData(imagesToUpdate);

if (isInitial) {
$(this.options.mediaGallerySelector).AddFotoramaVideoEvents();
}

gallery.first();

} else if (justAnImage && justAnImage.img) {
context.find('.product-image-photo').attr('src', justAnImage.img);
}
Expand Down Expand Up @@ -971,6 +977,17 @@ define([
}

return selectedAttributes;
},

/**
* Callback which fired after gallery gets initialized.
*
* @param {HTMLElement} element - DOM element associated with a gallery.
*/
_onGalleryLoaded: function (element) {
var galleryObject = element.data('gallery');

this.options.mediaGalleryInitial = galleryObject.returnCurrentImages();
}
});

Expand Down
5 changes: 1 addition & 4 deletions app/design/frontend/Magento/luma/etc/view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,7 @@
</vars>

<vars module="Magento_ConfigurableProduct">
<var name="change_only_base_image">true</var>
</vars>
<vars module="Magento_Swatches">
<var name="change_only_base_image">true</var>
<var name="gallery_switch_strategy">prepend</var>
</vars>

<vars module="Js_Bundle">
Expand Down

0 comments on commit b316fbd

Please sign in to comment.