Skip to content

Commit 7371b0a

Browse files
ENGCOM-9068: Optimize loading of media gallery when editing a product #32818
2 parents 1a06923 + be26f09 commit 7371b0a

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ 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+
this._updateImagesRoles();
75+
this._contentUpdated();
76+
77+
this._isInitializingItems = false;
7078
this.options.initialized = true;
7179
},
7280

@@ -186,13 +194,23 @@ define([
186194
* @private
187195
*/
188196
_addItem: function (event, imageData) {
189-
var count = this.element.find(this.options.imageSelector).length,
190-
element,
197+
var element,
191198
imgElement,
192-
position = count + 1,
199+
lastElement,
200+
count,
201+
position;
202+
203+
if (this._isInitializingItems) {
204+
count = this._initializedItemCount++;
205+
lastElement = this._lastInitializedElement;
206+
} else {
207+
count = this.element.find(this.options.imageSelector).length;
193208
lastElement = this.element.find(this.options.imageSelector + ':last');
209+
}
210+
211+
position = count + 1;
194212

195-
if (lastElement.length === 1) {
213+
if (lastElement && lastElement.length === 1) {
196214
position = parseInt(lastElement.data('imageData').position || count, 10) + 1;
197215
}
198216
imageData = $.extend({
@@ -214,6 +232,8 @@ define([
214232
element.insertAfter(lastElement);
215233
}
216234

235+
this._lastInitializedElement = element;
236+
217237
if (!this.options.initialized &&
218238
this.options.images.length === 0 ||
219239
this.options.initialized &&
@@ -235,8 +255,10 @@ define([
235255
}
236256
}, this));
237257

238-
this._updateImagesRoles();
239-
this._contentUpdated();
258+
if (!this._isInitializingItems) {
259+
this._updateImagesRoles();
260+
this._contentUpdated();
261+
}
240262
},
241263

242264
/**

0 commit comments

Comments
 (0)