Skip to content

Commit 5a95c20

Browse files
authored
Optimize loading of media gallery when editing a product
1 parent 3f697e0 commit 5a95c20

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

app/code/Magento/Catalog/view/adminhtml/web/js/product-gallery.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,19 @@ define([
6363

6464
this._bind();
6565

66+
this._isInitializingItems = true;
67+
this._initializedItemCount = 0;
68+
this._lastInitializedElement = null;
69+
6670
$.each(this.options.images, $.proxy(function (index, imageData) {
6771
this.element.trigger('addItem', imageData);
6872
}, this));
6973

74+
75+
this._updateImagesRoles();
76+
this._contentUpdated();
77+
78+
this._isInitializingItems = false;
7079
this.options.initialized = true;
7180
},
7281

@@ -186,13 +195,23 @@ define([
186195
* @private
187196
*/
188197
_addItem: function (event, imageData) {
189-
var count = this.element.find(this.options.imageSelector).length,
190-
element,
198+
var element,
191199
imgElement,
192-
position = count + 1,
200+
lastElement,
201+
count,
202+
position;
203+
204+
if (this._isInitializingItems) {
205+
count = this._initializedItemCount++;
206+
lastElement = this._lastInitializedElement;
207+
} else {
208+
count = this.element.find(this.options.imageSelector).length;
193209
lastElement = this.element.find(this.options.imageSelector + ':last');
210+
}
211+
212+
position = count + 1;
194213

195-
if (lastElement.length === 1) {
214+
if (lastElement && (lastElement.length === 1)) {
196215
position = parseInt(lastElement.data('imageData').position || count, 10) + 1;
197216
}
198217
imageData = $.extend({
@@ -214,6 +233,8 @@ define([
214233
element.insertAfter(lastElement);
215234
}
216235

236+
this._lastInitializedElement = element;
237+
217238
if (!this.options.initialized &&
218239
this.options.images.length === 0 ||
219240
this.options.initialized &&
@@ -235,8 +256,10 @@ define([
235256
}
236257
}, this));
237258

238-
this._updateImagesRoles();
239-
this._contentUpdated();
259+
if (!this._isInitializingItems) {
260+
this._updateImagesRoles();
261+
this._contentUpdated();
262+
}
240263
},
241264

242265
/**

0 commit comments

Comments
 (0)