@@ -100,7 +100,6 @@ define([
100100 }
101101 this . imageMargin = parseInt ( this . imageMargin , 10 ) ;
102102 this . container = $ ( '[data-id="' + this . containerId + '"]' ) [ 0 ] ;
103-
104103 this . setLayoutStyles ( ) ;
105104 this . setEventListener ( ) ;
106105
@@ -129,38 +128,47 @@ define([
129128 /**
130129 * Set layout styles inside the container
131130 */
132- setLayoutStyles : function ( ) {
133- var containerWidth = parseInt ( this . container . clientWidth , 10 ) ,
131+ setLayoutStyles : function ( callback ) {
132+ var containerWidth , rowImages , ratio , rowHeight , calcHeight , isLastRow , rowNumber ;
133+
134+ if ( typeof this . container != 'undefined' ) {
135+ containerWidth = parseInt ( this . container . clientWidth , 10 ) ,
134136 rowImages = [ ] ,
135137 ratio = 0 ,
136138 rowHeight = 0 ,
137139 calcHeight = 0 ,
138140 isLastRow = false ,
139141 rowNumber = 1 ;
140142
141- this . setMinRatio ( ) ;
143+ this . setMinRatio ( ) ;
142144
143- this . rows ( ) . forEach ( function ( image , index ) {
144- ratio += parseFloat ( ( image . width / image . height ) . toFixed ( 2 ) ) ;
145- rowImages . push ( image ) ;
145+ this . rows ( ) . forEach ( function ( image , index ) {
146+ ratio += parseFloat ( ( image . width / image . height ) . toFixed ( 2 ) ) ;
147+ rowImages . push ( image ) ;
146148
147- if ( ratio < this . minRatio && index + 1 !== this . rows ( ) . length ) {
148- // Row has more space for images and the image is not the last one - proceed to the next iteration
149- return ;
150- }
149+ if ( ratio < this . minRatio && index + 1 !== this . rows ( ) . length ) {
150+ /**
151+ * Row has more space for images and the image is not the last one -
152+ * Proceed to the next iteration
153+ */
154+ return ;
155+ }
151156
152- ratio = Math . max ( ratio , this . minRatio ) ;
153- calcHeight = ( containerWidth - this . imageMargin * rowImages . length ) / ratio ;
154- rowHeight = calcHeight < this . maxImageHeight ? calcHeight : this . maxImageHeight ;
155- isLastRow = index + 1 === this . rows ( ) . length ;
157+ ratio = Math . max ( ratio , this . minRatio ) ;
158+ calcHeight = ( containerWidth - this . imageMargin * rowImages . length ) / ratio ;
159+ rowHeight = calcHeight < this . maxImageHeight ? calcHeight : this . maxImageHeight ;
160+ isLastRow = index + 1 === this . rows ( ) . length ;
156161
157- this . assignImagesToRow ( rowImages , rowNumber , rowHeight , isLastRow ) ;
162+ this . assignImagesToRow ( rowImages , rowNumber , rowHeight , isLastRow ) ;
158163
159- rowImages = [ ] ;
160- ratio = 0 ;
161- rowNumber ++ ;
164+ rowImages = [ ] ;
165+ ratio = 0 ;
166+ rowNumber ++ ;
162167
163- } . bind ( this ) ) ;
168+ } . bind ( this ) ) ;
169+ } else {
170+ setTimeout ( callback , 0 ) ;
171+ }
164172 } ,
165173
166174 /**
0 commit comments