Skip to content

Commit 8bfc104

Browse files
committed
Initialize observable for image first, load images then apply styles
1 parent 56ae4d5 commit 8bfc104

File tree

2 files changed

+65
-17
lines changed
  • app/code/Magento/Ui/view/base/web/js/grid
  • dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/grid

2 files changed

+65
-17
lines changed

app/code/Magento/Ui/view/base/web/js/grid/masonry.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,17 @@ define([
9898
if (!rows.length) {
9999
return;
100100
}
101+
102+
//initialize row observables
103+
this.rows().forEach(function (image, index) {
104+
image.styles = ko.observable({});
105+
image.css = ko.observable({});
106+
});
107+
101108
this.imageMargin = parseInt(this.imageMargin, 10);
102109
this.container = $('[data-id="' + this.containerId + '"]')[0];
103110

104-
this.setLayoutStyles();
111+
this.setLayoutStylesWhenLoaded();
105112
this.setEventListener();
106113

107114
return this;
@@ -181,25 +188,33 @@ define([
181188
},
182189

183190
/**
184-
* Wait for container to initialize
191+
* Call method when condition is true
185192
*/
186-
waitForContainer: function (callback) {
187-
if (typeof this.container === 'undefined') {
193+
conditionCallback: function (condition, callback) {
194+
if (condition()) {
188195
setTimeout(function () {
189-
this.waitForContainer(callback);
190-
}.bind(this), 500);
196+
this.conditionCallback(condition, callback);
197+
}.bind(this), 100);
191198
} else {
192-
setTimeout(callback, 0);
199+
callback();
193200
}
194201
},
195202

196203
/**
197-
* Set layout styles when container element is loaded.
204+
* Set layout styles when last image in grid is loaded.
198205
*/
199206
setLayoutStylesWhenLoaded: function () {
200-
this.waitForContainer(function () {
201-
this.setLayoutStyles();
202-
}.bind(this));
207+
var images = '[data-id="' + this.containerId + '"] img';
208+
209+
this.conditionCallback(
210+
function () {
211+
return $(images).length === 0;
212+
}.bind(this),
213+
function () {
214+
$(images).last().load(function () {
215+
this.setLayoutStyles();
216+
}.bind(this));
217+
}.bind(this));
203218
},
204219

205220
/**
@@ -210,9 +225,6 @@ define([
210225
* @param {Number} height
211226
*/
212227
setImageStyles: function (img, width, height) {
213-
if (!img.styles) {
214-
img.styles = ko.observable();
215-
}
216228
img.styles({
217229
width: parseInt(width, 10) + 'px',
218230
height: parseInt(height, 10) + 'px'
@@ -226,9 +238,6 @@ define([
226238
* @param {Object} classes
227239
*/
228240
setImageClass: function (image, classes) {
229-
if (!image.css) {
230-
image.css = ko.observable(classes);
231-
}
232241
image.css(classes);
233242
},
234243

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'Magento_Ui/js/grid/masonry',
8+
'jquery'
9+
], function (Masonry, $) {
10+
'use strict';
11+
12+
describe('Magento_Ui/js/grid/masonry', function () {
13+
var model;
14+
15+
beforeEach(function () {
16+
$(document.body).append(
17+
$('<div id="masonry_grid"><div class="masonry-image-column"></div></div>')
18+
);
19+
model = new Masonry({
20+
defaults: {
21+
containerId: '#masonry_grid'
22+
}
23+
});
24+
});
25+
26+
afterEach(function () {
27+
$('#masonry_grid').remove();
28+
});
29+
30+
describe('check initComponent', function () {
31+
it('verify setLayoutstyles called and grid iniztilized', function () {
32+
33+
});
34+
it('verify events triggered', function () {
35+
36+
});
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)