@@ -80,18 +80,25 @@ var ConfigurableMediaImages = {
80
80
}
81
81
82
82
//second, get any product which is compatible with currently selected option(s)
83
- $j . each ( fallback [ 'option_labels' ] , function ( key , value ) {
84
- var image = value [ 'configurable_product' ] [ ConfigurableMediaImages . imageType ] ;
85
- var products = value [ 'products' ] ;
86
-
87
- if ( image ) { //configurable product has image in the first place
88
- //if intersection between compatible products and this label's products, we found a match
89
- var isCompatibleProduct = ConfigurableMediaImages . arrayIntersect ( products , compatibleProducts ) . length > 0 ;
90
- if ( isCompatibleProduct ) {
91
- return image ;
83
+ var optionLabels = fallback [ 'option_labels' ] ;
84
+ for ( var key in optionLabels ) {
85
+ if ( optionLabels . hasOwnProperty ( key ) ) {
86
+ var value = optionLabels [ key ] ;
87
+ var image = value [ 'configurable_product' ] [ ConfigurableMediaImages . imageType ] ;
88
+ var products = value [ 'products' ] ;
89
+
90
+ if ( image ) { //configurable product has image in the first place
91
+ //if intersection between compatible products and this label's products, we found a match
92
+ var isCompatibleProduct = products . filter ( function ( productId ) {
93
+ return compatibleProducts . includes ( productId ) ;
94
+ } ) . length > 0 ;
95
+
96
+ if ( isCompatibleProduct ) {
97
+ return image ;
98
+ }
92
99
}
93
100
}
94
- } ) ;
101
+ }
95
102
96
103
//third, get image off of child product which is compatible
97
104
var childSwatchImage = null ;
@@ -118,74 +125,122 @@ var ConfigurableMediaImages = {
118
125
getImageObject : function ( productId , imageUrl ) {
119
126
var key = productId + '-' + imageUrl ;
120
127
if ( ! ConfigurableMediaImages . imageObjects [ key ] ) {
121
- var image = $j ( '< img /> ') ;
122
- image . attr ( ' src' , imageUrl ) ;
128
+ var image = document . createElement ( ' img') ;
129
+ image . src = imageUrl ;
123
130
ConfigurableMediaImages . imageObjects [ key ] = image ;
124
131
}
125
132
return ConfigurableMediaImages . imageObjects [ key ] ;
126
133
} ,
127
134
128
- updateImage : function ( el ) {
129
- var select = $j ( el ) ;
130
- var label = select . find ( 'option:selected' ) . attr ( 'data-label' ) ;
135
+ updateImage ( el ) {
136
+ var select = el ;
137
+ var label = select . options [ select . selectedIndex ] . getAttribute ( 'data-label' ) ;
131
138
var productId = optionsPrice . productId ; //get product ID from options price object
132
139
133
140
//find all selected labels
134
- var selectedLabels = new Array ( ) ;
141
+ var selectedLabels = [ ] ;
135
142
136
- $j ( '.product-options .super-attribute-select' ) . each ( function ( ) {
137
- var $option = $j ( this ) ;
138
- if ( $ option. val ( ) ! = '' ) {
139
- selectedLabels . push ( $ option. find ( ' option:selected' ) . attr ( 'data-label' ) ) ;
143
+ var superAttributeSelects = document . querySelectorAll ( '.product-options .super-attribute-select' ) ;
144
+ superAttributeSelects . forEach ( function ( option ) {
145
+ if ( option . value != = '' ) {
146
+ selectedLabels . push ( option . options [ option . selectedIndex ] . getAttribute ( 'data-label' ) ) ;
140
147
}
141
148
} ) ;
142
149
143
150
var swatchImageUrl = ConfigurableMediaImages . getSwatchImage ( productId , label , selectedLabels ) ;
144
- if ( ! ConfigurableMediaImages . isValidImage ( swatchImageUrl ) ) {
151
+ if ( ! ConfigurableMediaImages . isValidImage ( swatchImageUrl ) ) {
152
+ console . log ( 'no image found' ) ;
145
153
return ;
146
154
}
147
155
148
156
var swatchImage = ConfigurableMediaImages . getImageObject ( productId , swatchImageUrl ) ;
149
157
150
- ProductMediaManager . swapImage ( swatchImage ) ;
158
+ this . swapImage ( swatchImage ) ;
159
+ } ,
160
+
161
+ swapImage : function ( targetImage ) {
162
+ targetImage . classList . add ( 'gallery-image' ) ;
163
+
164
+ var imageGallery = document . querySelector ( '.product-image-gallery' ) ;
165
+
166
+ if ( targetImage . complete ) { // image already loaded -- swap immediately
167
+ var galleryImages = imageGallery . querySelectorAll ( '.gallery-image' ) ;
168
+ galleryImages . forEach ( function ( image ) {
169
+ image . classList . remove ( 'visible' ) ;
170
+ } ) ;
171
+
172
+ // move target image to correct place, in case it's necessary
173
+ imageGallery . appendChild ( targetImage ) ;
174
+
175
+ // reveal new image
176
+ targetImage . classList . add ( 'visible' ) ;
177
+ } else { // need to wait for image to load
178
+ // add spinner
179
+ imageGallery . classList . add ( 'loading' ) ;
180
+
181
+ // move target image to correct place, in case it's necessary
182
+ imageGallery . appendChild ( targetImage ) ;
183
+
184
+ // wait until image is loaded
185
+ targetImage . addEventListener ( 'load' , function ( ) {
186
+ // remove spinner
187
+ imageGallery . classList . remove ( 'loading' ) ;
188
+
189
+ // hide old image
190
+ var galleryImages = imageGallery . querySelectorAll ( '.gallery-image' ) ;
191
+ galleryImages . forEach ( function ( image ) {
192
+ image . classList . remove ( 'visible' ) ;
193
+ } ) ;
194
+
195
+ // reveal new image
196
+ targetImage . classList . add ( 'visible' ) ;
197
+ } ) ;
198
+ }
151
199
} ,
152
200
153
201
wireOptions : function ( ) {
154
- $j ( '.product-options .super-attribute-select' ) . change ( function ( e ) {
155
- ConfigurableMediaImages . updateImage ( this ) ;
202
+ var selectElements = document . querySelectorAll ( '.product-options .super-attribute-select' ) ;
203
+ selectElements . forEach ( function ( selectElement ) {
204
+ selectElement . addEventListener ( 'change' , function ( e ) {
205
+ ConfigurableMediaImages . updateImage ( this ) ;
206
+ } ) ;
156
207
} ) ;
157
208
} ,
158
209
159
210
swapListImage : function ( productId , imageObject ) {
160
- var originalImage = $j ( '#product-collection-image-' + productId ) ;
211
+ var originalImage = document . querySelector ( '#product-collection-image-' + productId ) ;
161
212
162
- if ( imageObject [ 0 ] . complete ) { //swap image immediately
213
+ if ( imageObject . complete ) { // swap image immediately
163
214
164
- //remove old image
165
- originalImage . addClass ( 'hidden' ) ;
166
- $j ( '.product-collection-image-' + productId ) . remove ( ) ;
215
+ // remove old image
216
+ originalImage . classList . add ( 'hidden' ) ;
217
+ document . querySelectorAll ( '.product-collection-image-' + productId ) . forEach ( function ( image ) {
218
+ image . remove ( ) ;
219
+ } ) ;
167
220
168
- //add new image
169
- imageObject . insertAfter ( originalImage ) ;
221
+ // add new image
222
+ originalImage . parentNode . insertBefore ( imageObject , originalImage . nextSibling ) ;
170
223
171
- } else { //need to load image
224
+ } else { // need to load image
172
225
173
- var wrapper = originalImage . parent ( ) ;
226
+ var wrapper = originalImage . parentNode ;
174
227
175
- //add spinner
176
- wrapper . addClass ( 'loading' ) ;
228
+ // add spinner
229
+ wrapper . classList . add ( 'loading' ) ;
177
230
178
- //wait until image is loaded
179
- imagesLoaded ( imageObject , function ( ) {
180
- //remove spinner
181
- wrapper . removeClass ( 'loading' ) ;
231
+ // wait until image is loaded
232
+ imageObject . addEventListener ( 'load' , function ( ) {
233
+ // remove spinner
234
+ wrapper . classList . remove ( 'loading' ) ;
182
235
183
- //remove old image
184
- originalImage . addClass ( 'hidden' ) ;
185
- $j ( '.product-collection-image-' + productId ) . remove ( ) ;
236
+ // remove old image
237
+ originalImage . classList . add ( 'hidden' ) ;
238
+ document . querySelectorAll ( '.product-collection-image-' + productId ) . forEach ( function ( image ) {
239
+ image . remove ( ) ;
240
+ } ) ;
186
241
187
- //add new image
188
- imageObject . insertAfter ( originalImage ) ;
242
+ // add new image
243
+ originalImage . parentNode . insertBefore ( imageObject , originalImage . nextSibling ) ;
189
244
} ) ;
190
245
191
246
}
@@ -198,7 +253,7 @@ var ConfigurableMediaImages = {
198
253
}
199
254
200
255
var newImage = ConfigurableMediaImages . getImageObject ( productId , swatchImageUrl ) ;
201
- newImage . addClass ( 'product-collection-image-' + productId ) ;
256
+ newImage . classList . add ( 'product-collection-image-' + productId ) ;
202
257
203
258
ConfigurableMediaImages . swapListImage ( productId , newImage ) ;
204
259
} ,
0 commit comments