Skip to content

Commit 90f78b6

Browse files
committed
Merge pull request #63 from ValeryYafremau/JS-337
JS-337: [Configurable.JS] Preload all needed images
2 parents a45d524 + d13c192 commit 90f78b6

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

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

Lines changed: 43 additions & 1 deletion
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.

lib/web/mage/gallery/preloadImages.js

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)