OSDN Git Service

WordPressテンプレート機能更新。
[magic3/magic3.git] / scripts / wp / woocommerce / assets / js / frontend / cart-fragments.js
1 /* global wc_cart_fragments_params, Cookies */
2 jQuery( function( $ ) {
3
4         // wc_cart_fragments_params is required to continue, ensure the object exists
5         if ( typeof wc_cart_fragments_params === 'undefined' ) {
6                 return false;
7         }
8
9         /* Storage Handling */
10         var $supports_html5_storage;
11         var cart_hash_key = wc_cart_fragments_params.ajax_url.toString() + '-wc_cart_hash';
12
13         try {
14                 $supports_html5_storage = ( 'sessionStorage' in window && window.sessionStorage !== null );
15                 window.sessionStorage.setItem( 'wc', 'test' );
16                 window.sessionStorage.removeItem( 'wc' );
17                 window.localStorage.setItem( 'wc', 'test' );
18                 window.localStorage.removeItem( 'wc' );
19         } catch( err ) {
20                 $supports_html5_storage = false;
21         }
22
23         /* Cart session creation time to base expiration on */
24         function set_cart_creation_timestamp() {
25                 if ( $supports_html5_storage ) {
26                         sessionStorage.setItem( 'wc_cart_created', ( new Date() ).getTime() );
27                 }
28         }
29
30         /** Set the cart hash in both session and local storage */
31         function set_cart_hash( cart_hash ) {
32                 if ( $supports_html5_storage ) {
33                         localStorage.setItem( cart_hash_key, cart_hash );
34                         sessionStorage.setItem( cart_hash_key, cart_hash );
35                 }
36         }
37
38         var $fragment_refresh = {
39                 url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_refreshed_fragments' ),
40                 type: 'POST',
41                 success: function( data ) {
42                         if ( data && data.fragments ) {
43
44                                 $.each( data.fragments, function( key, value ) {
45                                         $( key ).replaceWith( value );
46                                 });
47
48                                 if ( $supports_html5_storage ) {
49                                         sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
50                                         set_cart_hash( data.cart_hash );
51
52                                         if ( data.cart_hash ) {
53                                                 set_cart_creation_timestamp();
54                                         }
55                                 }
56
57                                 $( document.body ).trigger( 'wc_fragments_refreshed' );
58                         }
59                 }
60         };
61
62         /* Named callback for refreshing cart fragment */
63         function refresh_cart_fragment() {
64                 $.ajax( $fragment_refresh );
65         }
66
67         /* Cart Handling */
68         if ( $supports_html5_storage ) {
69
70                 var cart_timeout = null,
71                         day_in_ms    = ( 24 * 60 * 60 * 1000 );
72
73                 $( document.body ).on( 'wc_fragment_refresh updated_wc_div', function() {
74                         refresh_cart_fragment();
75                 });
76
77                 $( document.body ).on( 'added_to_cart', function( event, fragments, cart_hash ) {
78                         var prev_cart_hash = sessionStorage.getItem( cart_hash_key );
79
80                         if ( prev_cart_hash === null || prev_cart_hash === undefined || prev_cart_hash === '' ) {
81                                 set_cart_creation_timestamp();
82                         }
83
84                         sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) );
85                         set_cart_hash( cart_hash );
86                 });
87
88                 $( document.body ).on( 'wc_fragments_refreshed', function() {
89                         clearTimeout( cart_timeout );
90                         cart_timeout = setTimeout( refresh_cart_fragment, day_in_ms );
91                 } );
92
93                 // Refresh when storage changes in another tab
94                 $( window ).on( 'storage onstorage', function ( e ) {
95                         if ( cart_hash_key === e.originalEvent.key && localStorage.getItem( cart_hash_key ) !== sessionStorage.getItem( cart_hash_key ) ) {
96                                 refresh_cart_fragment();
97                         }
98                 });
99
100                 // Refresh when page is shown after back button (safari)
101                 $( window ).on( 'pageshow' , function( e ) {
102                         if ( e.originalEvent.persisted ) {
103                                 $( '.widget_shopping_cart_content' ).empty();
104                                 $( document.body ).trigger( 'wc_fragment_refresh' );
105                         }
106                 } );
107
108                 try {
109                         var wc_fragments = $.parseJSON( sessionStorage.getItem( wc_cart_fragments_params.fragment_name ) ),
110                                 cart_hash    = sessionStorage.getItem( cart_hash_key ),
111                                 cookie_hash  = Cookies.get( 'woocommerce_cart_hash'),
112                                 cart_created = sessionStorage.getItem( 'wc_cart_created' );
113
114                         if ( cart_hash === null || cart_hash === undefined || cart_hash === '' ) {
115                                 cart_hash = '';
116                         }
117
118                         if ( cookie_hash === null || cookie_hash === undefined || cookie_hash === '' ) {
119                                 cookie_hash = '';
120                         }
121
122                         if ( cart_hash && ( cart_created === null || cart_created === undefined || cart_created === '' ) ) {
123                                 throw 'No cart_created';
124                         }
125
126                         if ( cart_created ) {
127                                 var cart_expiration = ( ( 1 * cart_created ) + day_in_ms ),
128                                         timestamp_now   = ( new Date() ).getTime();
129                                 if ( cart_expiration < timestamp_now ) {
130                                         throw 'Fragment expired';
131                                 }
132                                 cart_timeout = setTimeout( refresh_cart_fragment, ( cart_expiration - timestamp_now ) );
133                         }
134
135                         if ( wc_fragments && wc_fragments['div.widget_shopping_cart_content'] && cart_hash === cookie_hash ) {
136
137                                 $.each( wc_fragments, function( key, value ) {
138                                         $( key ).replaceWith(value);
139                                 });
140
141                                 $( document.body ).trigger( 'wc_fragments_loaded' );
142                         } else {
143                                 throw 'No fragment';
144                         }
145
146                 } catch( err ) {
147                         refresh_cart_fragment();
148                 }
149
150         } else {
151                 refresh_cart_fragment();
152         }
153
154         /* Cart Hiding */
155         if ( Cookies.get( 'woocommerce_items_in_cart' ) > 0 ) {
156                 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
157         } else {
158                 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).hide();
159         }
160
161         $( document.body ).on( 'adding_to_cart', function() {
162                 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
163         });
164 });