Skip to content

Commit

Permalink
Merge pull request #582 from magento-frontend/pr-develop
Browse files Browse the repository at this point in the history
Fixed issues:
- MAGETWO-59579: [Github #6943] grunt exec:<theme> doesn't work with @import</theme>
- MAGETWO-57210: [github #6195, #4101] Gallery doesn't show all images added to configurable options
- MAGETWO-59003: Js file version regenerated in browser on every page reload in develop mode
  • Loading branch information
MomotenkoNatalia authored Nov 10, 2016
2 parents b56df96 + 50be841 commit 4e14f2e
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 258 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 Down Expand Up @@ -85,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 @@ -259,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 @@ -506,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 @@ -243,8 +243,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',

// whether swatches are rendered in product list or on product page
inProductList: false
Expand Down Expand Up @@ -295,11 +302,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 @@ -1032,26 +1037,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 @@ -1127,6 +1133,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App\View\Deployment;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\State;
use Magento\Framework\App\View\Deployment\Version\Storage\File;
use Magento\Framework\Filesystem\Directory\WriteInterface;

class VersionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var File
*/
private $fileStorage;

/**
* @var WriteInterface
*/
private $directoryWrite;

/**
* @var string
*/
private $fileName = 'deployed_version.txt';

public function setUp()
{
$this->fileStorage = ObjectManager::getInstance()->create(
File::class,
[
'directoryCode' => DirectoryList::STATIC_VIEW,
'fileName' => $this->fileName
]
);
/** @var \Magento\TestFramework\App\Filesystem $filesystem */
$filesystem = ObjectManager::getInstance()->get(\Magento\TestFramework\App\Filesystem::class);
$this->directoryWrite = $filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
$this->removeDeployVersionFile();
}

/**
* @param string $mode
* @return Version
*/
public function getVersionModel($mode)
{
$appState = ObjectManager::getInstance()->create(
State::class,
[
'mode' => $mode
]
);
return ObjectManager::getInstance()->create(
Version::class,
[
'appState' => $appState
]
);
}

protected function tearDown()
{
$this->removeDeployVersionFile();
}

private function removeDeployVersionFile()
{
if ($this->directoryWrite->isExist($this->fileName)) {
$this->directoryWrite->delete($this->fileName);
}
}

/**
* @expectedException \UnexpectedValueException
*/
public function testGetValueInProductionModeWithoutVersion()
{
$this->assertFalse($this->directoryWrite->isExist($this->fileName));
$this->getVersionModel(State::MODE_PRODUCTION)->getValue();
}

public function testGetValueInDeveloperMode()
{
$this->assertFalse($this->directoryWrite->isExist($this->fileName));
$this->getVersionModel(State::MODE_DEVELOPER)->getValue();
$this->assertTrue($this->directoryWrite->isExist($this->fileName));
}

/**
* Assert that version is not regenerated on each request in developer mode
*/
public function testGetValue()
{
$this->assertFalse($this->directoryWrite->isExist($this->fileName));
$versionModel = $this->getVersionModel(State::MODE_DEVELOPER);
$version = $versionModel->getValue();
$this->assertTrue($this->directoryWrite->isExist($this->fileName));
$this->assertEquals($version, $versionModel->getValue());
}
}
12 changes: 8 additions & 4 deletions dev/tools/grunt/configs/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ _.each(themes, function(theme, name) {
"<%= path.tmp %>/cache/**/*",
"<%= combo.autopath(\""+name+"\", path.pub ) %>**/*",
"<%= combo.autopath(\""+name+"\", path.tmpLess) %>**/*",
"<%= combo.autopath(\""+name+"\", path.tmpSource) %>**/*"
"<%= combo.autopath(\""+name+"\", path.tmpSource) %>**/*",
"<%= path.deployedVersion %>"
]
}
]
Expand Down Expand Up @@ -56,7 +57,8 @@ var cleanOptions = {
"dot": true,
"src": [
"<%= path.pub %>frontend/**/*",
"<%= path.pub %>adminhtml/**/*"
"<%= path.pub %>adminhtml/**/*",
"<%= path.deployedVersion %>"
]
}
]
Expand All @@ -73,7 +75,8 @@ var cleanOptions = {
"<%= path.pub %>frontend/**/*.less",
"<%= path.pub %>frontend/**/*.css",
"<%= path.pub %>adminhtml/**/*.less",
"<%= path.pub %>adminhtml/**/*.css"
"<%= path.pub %>adminhtml/**/*.css",
"<%= path.deployedVersion %>"
]
}
]
Expand Down Expand Up @@ -102,7 +105,8 @@ var cleanOptions = {
"src": [
"<%= path.pub %>**/*.js",
"<%= path.pub %>**/*.html",
"<%= path.pub %>_requirejs/**/*"
"<%= path.pub %>_requirejs/**/*",
"<%= path.deployedVersion %>"
]
}
]
Expand Down
1 change: 1 addition & 0 deletions dev/tools/grunt/configs/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
tmpLess: 'var/view_preprocessed/less/',
tmpSource: 'var/view_preprocessed/source/',
tmp: 'var',
deployedVersion: 'pub/static/deployed_version.txt',
css: {
setup: 'setup/pub/styles',
updater: '../magento2-updater/pub/css'
Expand Down
Loading

0 comments on commit 4e14f2e

Please sign in to comment.