Skip to content

Commit 8bc6448

Browse files
committed
Merge branch 'JS-323' into JS-334
2 parents 1451bb7 + 40d09ba commit 8bc6448

File tree

6 files changed

+185
-68
lines changed

6 files changed

+185
-68
lines changed

app/code/Magento/ConfigurableProduct/view/frontend/templates/product/view/type/options/configurable.phtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ $_attributes = $block->decorateArray($block->getAllowAttributes());
3333
{
3434
"#product_addtocart_form": {
3535
"configurable": {
36-
"spConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>
36+
"spConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>,
37+
"onlyMainImg": true
3738
}
3839
}
3940
}

app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable.js

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ define([
77
'jquery',
88
'underscore',
99
'mage/template',
10+
'mage/gallery/preloadImages',
1011
'priceUtils',
1112
'priceBox',
1213
'jquery/ui',
1314
'jquery/jquery.parsequery'
14-
], function ($, _, mageTemplate) {
15+
], function ($, _, mageTemplate, preloadImages) {
1516
'use strict';
1617

1718
$.widget('mage.configurable', {
@@ -38,6 +39,10 @@ define([
3839
// Initial setting of various option values
3940
this._initializeOptions();
4041

42+
//Preload all gallery images
43+
this._preloadImages();
44+
this._preloadOptionalImages();
45+
4146
// Override defaults with URL query parameters and/or inputs values
4247
this._overrideDefaults();
4348

@@ -87,6 +92,43 @@ define([
8792
});
8893
},
8994

95+
/**
96+
* Preloads default configuration images.
97+
* @private
98+
*/
99+
_preloadImages: function () {
100+
var options = this.options,
101+
fullImagesList = [],
102+
imagesList = [];
103+
104+
_.each(options.mediaGalleryInitial, function (item) {
105+
imagesList.push(item.img);
106+
fullImagesList.push(item.full);
107+
});
108+
preloadImages(imagesList);
109+
preloadImages(fullImagesList);
110+
},
111+
112+
/**
113+
* Preloads optional configuration images.
114+
* @private
115+
*/
116+
_preloadOptionalImages: function () {
117+
var options = this.options;
118+
119+
_.each(options.spConfig.images, function (array) {
120+
var fullImagesList = [],
121+
imagesList = [];
122+
123+
_.each(array, function (item) {
124+
imagesList.push(item.img);
125+
fullImagesList.push(item.full);
126+
});
127+
preloadImages(imagesList);
128+
preloadImages(fullImagesList);
129+
});
130+
},
131+
90132
/**
91133
* Override default options values settings with either URL query parameters or
92134
* initialized inputs values.
@@ -253,14 +295,33 @@ define([
253295
* @private
254296
*/
255297
_changeProductImage: function () {
256-
var images = this.options.spConfig.images[this.simpleProduct],
257-
galleryObject = $(this.options.mediaGallerySelector).data('gallery');
298+
var images,
299+
initialImages = $.extend(true, [], this.options.mediaGalleryInitial),
300+
galleryObject = $(this.options.mediaGallerySelector).data('gallery'),
301+
updateGallery;
302+
303+
if (this.options.spConfig.images[this.simpleProduct]) {
304+
images = $.extend(true, [], this.options.spConfig.images[this.simpleProduct]);
305+
}
306+
307+
updateGallery = function (imagesArr) {
308+
var mainImg = imagesArr.filter(function (img) {
309+
return img.isMain;
310+
});
311+
312+
galleryObject.updateDataByIndex(0, mainImg[0]);
313+
galleryObject.seek(1);
314+
};
258315

259316
if (galleryObject) {
260317
if (images) {
261-
galleryObject.updateData(images);
318+
this.options.onlyMainImg ?
319+
updateGallery(images) :
320+
galleryObject.updateData(images);
262321
} else {
263-
galleryObject.updateData(this.options.mediaGalleryInitial);
322+
this.options.onlyMainImg ?
323+
updateGallery(initialImages) :
324+
galleryObject.updateData(this.options.mediaGalleryInitial);
264325
}
265326
}
266327
},

lib/web/fotorama/fotorama.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,14 +1433,8 @@ fotoramaVersion = '4.6.4';
14331433
xWin = (tail.go || tail.x || xyDiff >= 0) && !tail.noSwipe,
14341434
yWin = xyDiff < 0;
14351435

1436-
if (touchFLAG && !tail.checked) {
1437-
if (touchEnabledFLAG = xWin) {
1438-
stopEvent(e);
1439-
}
1440-
} else {
1441-
stopEvent(e);
1442-
(options.onMove || noop).call(el, e, {touch: touchFLAG});
1443-
}
1436+
stopEvent(e);
1437+
(options.onMove || noop).call(el, e, {touch: touchFLAG});
14441438

14451439
if (!moved && Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2)) > tolerance) {
14461440
moved = true;
@@ -1481,16 +1475,12 @@ fotoramaVersion = '4.6.4';
14811475

14821476
function onOtherStart() {
14831477
if (tail.flow) return;
1484-
setTimeout(function () {
1485-
tail.flow = true;
1486-
}, 10);
1478+
tail.flow = true;
14871479
}
14881480

14891481
function onOtherEnd() {
14901482
if (!tail.flow) return;
1491-
setTimeout(function () {
1492-
tail.flow = false;
1493-
}, TOUCH_TIMEOUT);
1483+
tail.flow = false;
14941484
}
14951485

14961486
if (MS_POINTER) {
@@ -2250,7 +2240,7 @@ fotoramaVersion = '4.6.4';
22502240

22512241
if (!$frame) return;
22522242

2253-
var fullFLAG = that.fullScreen && dataFrame.full && dataFrame.full !== dataFrame.img && !frameData.$full && type === 'stage';
2243+
var fullFLAG = that.fullScreen && (!frameData.$full || dataFrame.full) && type === 'stage';
22542244

22552245
if (frameData.$img && !again && !fullFLAG) return;
22562246

@@ -2262,7 +2252,7 @@ fotoramaVersion = '4.6.4';
22622252

22632253
var srcKey = type === 'stage' ? (fullFLAG ? 'full' : 'img') : 'thumb',
22642254
src = dataFrame[srcKey],
2265-
dummy = fullFLAG ? null : dataFrame[type === 'stage' ? 'thumb' : 'img'];
2255+
dummy = fullFLAG ? dataFrame['img'] : dataFrame[type === 'stage' ? 'thumb' : 'img'];
22662256

22672257
if (type === 'navThumb') $frame = frameData.$wrap;
22682258

@@ -3428,6 +3418,17 @@ fotoramaVersion = '4.6.4';
34283418
return this;
34293419
};
34303420

3421+
that.spliceByIndex = function (index, newImgObj) {
3422+
newImgObj.img && $.ajax({
3423+
url: newImgObj.img,
3424+
type: 'HEAD',
3425+
success: function() {
3426+
data.splice(index, 1, newImgObj);
3427+
reset();
3428+
}
3429+
});
3430+
};
3431+
34313432
function unloadVideo($video, unloadActiveFLAG, releaseAutoplayFLAG) {
34323433
if (unloadActiveFLAG) {
34333434
$wrap.removeClass(wrapVideoClass);

lib/web/mage/gallery/gallery.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,19 @@ define([
453453
var images = [];
454454

455455
_.each(this.fotorama.data, function (item) {
456-
images.push(_.omit(item, '$navThumbFrame', '$navDotFrame', '$stageFrame'));
456+
images.push(_.omit(item, '$navThumbFrame', '$navDotFrame', '$stageFrame', 'labelledby'));
457457
});
458458

459459
return images;
460+
},
461+
/**
462+
* Updates gallery data partially by index
463+
* @param {Number} index - Index of image in data array to be updated.
464+
* @param {Object} item - Standart gallery image object.
465+
*
466+
*/
467+
updateDataByIndex: function(index, item){
468+
settings.fotoramaApi.spliceByIndex(index, item);
460469
}
461470
};
462471

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright © 2015 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define([], function () {
6+
7+
/**
8+
* Loads images into browser's cash.
9+
* @param {Array <String>} array - List of sources of images.
10+
*/
11+
var preloadImages = function (array) {
12+
if (!preloadImages.list) {
13+
preloadImages.list = [];
14+
}
15+
var list = preloadImages.list;
16+
17+
for (var i = 0; i < array.length; i++) {
18+
var img = new Image();
19+
20+
img.onload = function() {
21+
var index = list.indexOf(this);
22+
23+
if (index !== -1) {
24+
list.splice(index, 1);
25+
}
26+
}
27+
28+
list.push(img);
29+
img.src = array[i];
30+
}
31+
};
32+
33+
return preloadImages;
34+
35+
});

0 commit comments

Comments
 (0)