https://maychuhanoi.vn/wp-content/plugins/woocommerce/assets/js/frontend/cart-fragments.js?ver=5.8.0
1/* global wc_cart_fragments_params, Cookies */ 2jQuery( 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 = true, 11 cart_hash_key = wc_cart_fragments_params.cart_hash_key; 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 data: { 42 time: new Date().getTime() 43 }, 44 timeout: wc_cart_fragments_params.request_timeout, 45 success: function( data ) { 46 if ( data && data.fragments ) { 47 48 $.each( data.fragments, function( key, value ) { 49 $( key ).replaceWith( value ); 50 }); 51 52 if ( $supports_html5_storage ) { 53 sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) ); 54 set_cart_hash( data.cart_hash ); 55 56 if ( data.cart_hash ) { 57 set_cart_creation_timestamp(); 58 } 59 } 60 61 $( document.body ).trigger( 'wc_fragments_refreshed' ); 62 } 63 }, 64 error: function() { 65 $( document.body ).trigger( 'wc_fragments_ajax_error' ); 66 } 67 }; 68 69 /* Named callback for refreshing cart fragment */ 70 function refresh_cart_fragment() { 71 $.ajax( $fragment_refresh ); 72 } 73 74 /* Cart Handling */ 75 if ( $supports_html5_storage ) { 76 77 var cart_timeout = null, 78 day_in_ms = ( 24 * 60 * 60 * 1000 ); 79 80 $( document.body ).on( 'wc_fragment_refresh updated_wc_div', function() { 81 refresh_cart_fragment(); 82 }); 83 84 $( document.body ).on( 'added_to_cart removed_from_cart', function( event, fragments, cart_hash ) { 85 var prev_cart_hash = sessionStorage.getItem( cart_hash_key ); 86 87 if ( prev_cart_hash === null || prev_cart_hash === undefined || prev_cart_hash === '' ) { 88 set_cart_creation_timestamp(); 89 } 90 91 sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) ); 92 set_cart_hash( cart_hash ); 93 }); 94 95 $( document.body ).on( 'wc_fragments_refreshed', function() { 96 clearTimeout( cart_timeout ); 97 cart_timeout = setTimeout( refresh_cart_fragment, day_in_ms ); 98 } ); 99 100 // Refresh when storage changes in another tab 101 $( window ).on( 'storage onstorage', function ( e ) { 102 if ( 103 cart_hash_key === e.originalEvent.key && localStorage.getItem( cart_hash_key ) !== sessionStorage.getItem( cart_hash_key ) 104 ) { 105 refresh_cart_fragment(); 106 } 107 }); 108 109 // Refresh when page is shown after back button (safari) 110 $( window ).on( 'pageshow' , function( e ) { 111 if ( e.originalEvent.persisted ) { 112 $( '.widget_shopping_cart_content' ).empty(); 113 $( document.body ).trigger( 'wc_fragment_refresh' ); 114 } 115 } ); 116 117 try { 118 var wc_fragments = JSON.parse( sessionStorage.getItem( wc_cart_fragments_params.fragment_name ) ), 119 cart_hash = sessionStorage.getItem( cart_hash_key ), 120 cookie_hash = Cookies.get( 'woocommerce_cart_hash'), 121 cart_created = sessionStorage.getItem( 'wc_cart_created' ); 122 123 if ( cart_hash === null || cart_hash === undefined || cart_hash === '' ) { 124 cart_hash = ''; 125 } 126 127 if ( cookie_hash === null || cookie_hash === undefined || cookie_hash === '' ) { 128 cookie_hash = ''; 129 } 130 131 if ( cart_hash && ( cart_created === null || cart_created === undefined || cart_created === '' ) ) { 132 throw 'No cart_created'; 133 } 134 135 if ( cart_created ) { 136 var cart_expiration = ( ( 1 * cart_created ) + day_in_ms ), 137 timestamp_now = ( new Date() ).getTime(); 138 if ( cart_expiration < timestamp_now ) { 139 throw 'Fragment expired'; 140 } 141 cart_timeout = setTimeout( refresh_cart_fragment, ( cart_expiration - timestamp_now ) ); 142 } 143 144 if ( wc_fragments && wc_fragments['div.widget_shopping_cart_content'] && cart_hash === cookie_hash ) { 145 146 $.each( wc_fragments, function( key, value ) { 147 $( key ).replaceWith(value); 148 }); 149 150 $( document.body ).trigger( 'wc_fragments_loaded' ); 151 } else { 152 throw 'No fragment'; 153 } 154 155 } catch( err ) { 156 refresh_cart_fragment(); 157 } 158 159 } else { 160 refresh_cart_fragment(); 161 } 162 163 /* Cart Hiding */ 164 if ( Cookies.get( 'woocommerce_items_in_cart' ) > 0 ) { 165 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show(); 166 } else { 167 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).hide(); 168 } 169 170 $( document.body ).on( 'adding_to_cart', function() { 171 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show(); 172 }); 173 174 // Customiser support. 175 var hasSelectiveRefresh = ( 176 'undefined' !== typeof wp && 177 wp.customize && 178 wp.customize.selectiveRefresh && 179 wp.customize.widgetsPreview && 180 wp.customize.widgetsPreview.WidgetPartial 181 ); 182 if ( hasSelectiveRefresh ) { 183 wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function() { 184 refresh_cart_fragment(); 185 } ); 186 } 187});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.