@@ -98,17 +98,10 @@ define([
98
98
if ( ! rows . length ) {
99
99
return ;
100
100
}
101
-
102
- //initialize row observables
103
- this . rows ( ) . forEach ( function ( image ) {
104
- image . styles = ko . observable ( { } ) ;
105
- image . css = ko . observable ( { } ) ;
106
- } ) ;
107
-
108
101
this . imageMargin = parseInt ( this . imageMargin , 10 ) ;
109
102
this . container = $ ( '[data-id="' + this . containerId + '"]' ) [ 0 ] ;
110
103
111
- this . setLayoutStylesWhenLoaded ( ) ;
104
+ this . setLayoutStyles ( ) ;
112
105
this . setEventListener ( ) ;
113
106
114
107
return this ;
@@ -130,7 +123,7 @@ define([
130
123
* Set layout styles inside the container
131
124
*/
132
125
setLayoutStyles : function ( ) {
133
- var containerWidth = parseInt ( this . container . clientWidth , 10 ) ,
126
+ var containerWidth = parseInt ( this . container . clientWidth , 10 ) - 15 ,
134
127
rowImages = [ ] ,
135
128
ratio = 0 ,
136
129
rowHeight = 0 ,
@@ -188,37 +181,25 @@ define([
188
181
} ,
189
182
190
183
/**
191
- * Call method when condition is true
184
+ * Wait for container to initialize
192
185
*/
193
- conditionCallback : function ( condition , callback ) {
194
- if ( condition ( ) ) {
186
+ waitForContainer : function ( callback ) {
187
+ if ( typeof this . container === 'undefined' ) {
195
188
setTimeout ( function ( ) {
196
- this . conditionCallback ( condition , callback ) ;
197
- } . bind ( this ) , 100 ) ;
189
+ this . waitForContainer ( callback ) ;
190
+ } . bind ( this ) , 500 ) ;
198
191
} else {
199
- callback ( ) ;
192
+ setTimeout ( callback , 0 ) ;
200
193
}
201
194
} ,
202
195
203
196
/**
204
- * Set layout styles when last image in grid is loaded.
197
+ * Set layout styles when container element is loaded.
205
198
*/
206
199
setLayoutStylesWhenLoaded : function ( ) {
207
- var images = '[data-id="' + this . containerId + '"] img' ;
208
-
209
- this . conditionCallback (
210
- function ( ) {
211
- return $ ( images ) . length === 0 ;
212
- } ,
213
- function ( ) {
214
- $ ( images ) . last ( ) . load ( function ( ) {
215
- this . setLayoutStyles ( ) ;
216
- } . bind ( this ) ) . each ( function ( ) {
217
- if ( this . complete ) {
218
- $ ( this ) . load ( ) ;
219
- }
220
- } ) ;
221
- } . bind ( this ) ) ;
200
+ this . waitForContainer ( function ( ) {
201
+ this . setLayoutStyles ( ) ;
202
+ } . bind ( this ) ) ;
222
203
} ,
223
204
224
205
/**
@@ -229,6 +210,9 @@ define([
229
210
* @param {Number } height
230
211
*/
231
212
setImageStyles : function ( img , width , height ) {
213
+ if ( ! img . styles ) {
214
+ img . styles = ko . observable ( ) ;
215
+ }
232
216
img . styles ( {
233
217
width : parseInt ( width , 10 ) + 'px' ,
234
218
height : parseInt ( height , 10 ) + 'px'
@@ -242,6 +226,9 @@ define([
242
226
* @param {Object } classes
243
227
*/
244
228
setImageClass : function ( image , classes ) {
229
+ if ( ! image . css ) {
230
+ image . css = ko . observable ( classes ) ;
231
+ }
245
232
image . css ( classes ) ;
246
233
} ,
247
234
0 commit comments