@@ -184,6 +184,9 @@ define([
184
184
// Callback url for media
185
185
mediaCallback : '' ,
186
186
187
+ // Local media cache
188
+ mediaCache : { } ,
189
+
187
190
// Cache for BaseProduct images. Needed when option unset
188
191
mediaGalleryInitial : [ { } ] ,
189
192
@@ -708,7 +711,15 @@ define([
708
711
$this = $widget . element ,
709
712
attributes = { } ,
710
713
productId = 0 ,
711
- additional ;
714
+ mediaCallData ,
715
+ mediaCacheKey ,
716
+ mediaSuccessCallback = function ( data ) {
717
+ if ( ! ( mediaCacheKey in $widget . options . mediaCache ) ) {
718
+ $widget . options . mediaCache [ mediaCacheKey ] = data ;
719
+ }
720
+ $widget . _ProductMediaCallback ( $this , data ) ;
721
+ $widget . _DisableProductMediaLoader ( $this ) ;
722
+ } ;
712
723
713
724
if ( ! $widget . options . mediaCallback ) {
714
725
return ;
@@ -729,24 +740,28 @@ define([
729
740
. find ( '.price-box.price-final_price' ) . attr ( 'data-product-id' ) ;
730
741
}
731
742
732
- additional = $ . parseQuery ( ) ;
733
-
734
- $widget . _XhrKiller ( ) ;
735
- $widget . _EnableProductMediaLoader ( $this ) ;
736
- $widget . xhr = $ . post (
737
- $widget . options . mediaCallback , {
738
- 'product_id' : productId ,
739
- attributes : attributes ,
740
- isAjax : true ,
741
- additional : additional
742
- } , function ( data ) {
743
- $widget . _ProductMediaCallback ( $this , data ) ;
744
- $widget . _DisableProductMediaLoader ( $this ) ;
745
- } ,
746
- 'json'
747
- ) . done ( function ( ) {
743
+ mediaCallData = {
744
+ product_id : productId ,
745
+ attributes : attributes ,
746
+ additional : $ . parseQuery ( )
747
+ } ;
748
+ mediaCacheKey = JSON . stringify ( mediaCallData ) ;
749
+
750
+ if ( mediaCacheKey in $widget . options . mediaCache ) {
751
+ mediaSuccessCallback ( $widget . options . mediaCache [ mediaCacheKey ] ) ;
752
+ } else {
753
+ mediaCallData . isAjax = true ;
754
+ $widget . _XhrKiller ( ) ;
755
+ $widget . _EnableProductMediaLoader ( $this ) ;
756
+ $widget . xhr = $ . post (
757
+ $widget . options . mediaCallback ,
758
+ mediaCallData ,
759
+ mediaSuccessCallback ,
760
+ 'json'
761
+ ) . done ( function ( ) {
748
762
$widget . _XhrKiller ( ) ;
749
763
} ) ;
764
+ }
750
765
} ,
751
766
752
767
/**
@@ -810,32 +825,30 @@ define([
810
825
return e . hasOwnProperty ( 'large' ) && e . hasOwnProperty ( 'medium' ) && e . hasOwnProperty ( 'small' ) ;
811
826
} ;
812
827
813
- if ( _ . size ( $widget ) < 1 ) {
828
+ if ( _ . size ( $widget ) < 1 || ! support ( response ) ) {
814
829
this . updateBaseImage ( this . options . mediaGalleryInitial , $main , isProductViewExist ) ;
815
830
816
831
return ;
817
832
}
818
833
819
- if ( support ( response ) ) {
820
- images . push ( {
821
- full : response . large ,
822
- img : response . medium ,
823
- thumb : response . small ,
824
- isMain : true
825
- } ) ;
834
+ images . push ( {
835
+ full : response . large ,
836
+ img : response . medium ,
837
+ thumb : response . small ,
838
+ isMain : true
839
+ } ) ;
826
840
827
- if ( response . hasOwnProperty ( 'gallery' ) ) {
828
- $ . each ( response . gallery , function ( ) {
829
- if ( ! support ( this ) || response . large === this . large ) {
830
- return ;
831
- }
832
- images . push ( {
833
- full : this . large ,
834
- img : this . medium ,
835
- thumb : this . small
836
- } ) ;
841
+ if ( response . hasOwnProperty ( 'gallery' ) ) {
842
+ $ . each ( response . gallery , function ( ) {
843
+ if ( ! support ( this ) || response . large === this . large ) {
844
+ return ;
845
+ }
846
+ images . push ( {
847
+ full : this . large ,
848
+ img : this . medium ,
849
+ thumb : this . small
837
850
} ) ;
838
- }
851
+ } ) ;
839
852
}
840
853
841
854
this . updateBaseImage ( images , $main , isProductViewExist ) ;
@@ -872,11 +885,8 @@ define([
872
885
gallery = context . find ( this . options . mediaGallerySelector ) . data ( 'gallery' ) ,
873
886
item ;
874
887
875
- if ( images ) {
876
- imagesToUpdate = this . _setImageType ( $ . extend ( true , [ ] , images ) ) ;
877
- }
878
-
879
888
if ( isProductViewExist ) {
889
+ imagesToUpdate = images . length ? this . _setImageType ( $ . extend ( true , [ ] , images ) ) : [ ] ;
880
890
if ( this . options . onlyMainImg ) {
881
891
updateImg = imagesToUpdate . filter ( function ( img ) {
882
892
return img . isMain ;
0 commit comments