1/* global wc_add_to_cart_params */ 2jQuery( function( $ ) { 3 if ( typeof wc_add_to_cart_params === 'undefined' ) { 4 return false; 5 } 6 7 /** 8 * AddToCartHandler class. 9 */ 10 var AddToCartHandler = function() { 11 this.requests = []; 12 this.addRequest = this.addRequest.bind( this ); 13 this.run = this.run.bind( this ); 14 15 $( document.body ) 16 .on( 'click', '.add_to_cart_button', { addToCartHandler: this }, this.onAddToCart ) 17 .on( 'click', '.remove_from_cart_button', { addToCartHandler: this }, this.onRemoveFromCart ) 18 .on( 'added_to_cart', this.updateButton ) 19 .on( 'ajax_request_not_sent.adding_to_cart', this.updateButton ) 20 .on( 'added_to_cart removed_from_cart', { addToCartHandler: this }, this.updateFragments ); 21 }; 22 23 /** 24 * Add add to cart event. 25 */ 26 AddToCartHandler.prototype.addRequest = function( request ) { 27 this.requests.push( request ); 28 29 if ( 1 === this.requests.length ) { 30 this.run(); 31 } 32 }; 33 34 /** 35 * Run add to cart events. 36 */ 37 AddToCartHandler.prototype.run = function() { 38 var requestManager = this, 39 originalCallback = requestManager.requests[0].complete; 40 41 requestManager.requests[0].complete = function() { 42 if ( typeof originalCallback === 'function' ) { 43 originalCallback(); 44 } 45 46 requestManager.requests.shift(); 47 48 if ( requestManager.requests.length > 0 ) { 49 requestManager.run(); 50 } 51 }; 52 53 $.ajax( this.requests[0] ); 54 }; 55 56 /** 57 * Handle the add to cart event. 58 */ 59 AddToCartHandler.prototype.onAddToCart = function( e ) { 60 var $thisbutton = $( this ); 61 62 if ( $thisbutton.is( '.ajax_add_to_cart' ) ) { 63 if ( ! $thisbutton.attr( 'data-product_id' ) ) { 64 return true; 65 } 66 67 e.preventDefault(); 68 69 $thisbutton.removeClass( 'added' ); 70 $thisbutton.addClass( 'loading' ); 71 72 // Allow 3rd parties to validate and quit early. 73 if ( false === $( document.body ).triggerHandler( 'should_send_ajax_request.adding_to_cart', [ $thisbutton ] ) ) { 74 $( document.body ).trigger( 'ajax_request_not_sent.adding_to_cart', [ false, false, $thisbutton ] ); 75 return true; 76 } 77 78 var data = {}; 79 80 // Fetch changes that are directly added by calling $thisbutton.data( key, value ) 81 $.each( $thisbutton.data(), function( key, value ) { 82 data[ key ] = value; 83 }); 84 85 // Fetch data attributes in $thisbutton. Give preference to data-attributes because they can be directly modified by javascript 86 // while `.data` are jquery specific memory stores. 87 $.each( $thisbutton[0].dataset, function( key, value ) { 88 data[ key ] = value; 89 }); 90 91 // Trigger event. 92 $( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] ); 93 94 e.data.addToCartHandler.addRequest({ 95 type: 'POST', 96 url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), 97 data: data, 98 success: function( response ) { 99 if ( ! response ) { 100 return; 101 } 102 103 if ( response.error && response.product_url ) { 104 window.location = response.product_url; 105 return; 106 } 107 108 // Redirect to cart option 109 if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) { 110 window.location = wc_add_to_cart_params.cart_url; 111 return; 112 } 113 114 // Trigger event so themes can refresh other areas. 115 $( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] ); 116 }, 117 dataType: 'json' 118 }); 119 } 120 }; 121 122 /** 123 * Update fragments after remove from cart event in mini-cart. 124 */ 125 AddToCartHandler.prototype.onRemoveFromCart = function( e ) { 126 var $thisbutton = $( this ), 127 $row = $thisbutton.closest( '.woocommerce-mini-cart-item' ); 128 129 e.preventDefault(); 130 131 $row.block({ 132 message: null, 133 overlayCSS: { 134 opacity: 0.6 135 } 136 }); 137 138 e.data.addToCartHandler.addRequest({ 139 type: 'POST', 140 url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ), 141 data: { 142 cart_item_key : $thisbutton.data( 'cart_item_key' ) 143 }, 144 success: function( response ) { 145 if ( ! response || ! response.fragments ) { 146 window.location = $thisbutton.attr( 'href' ); 147 return; 148 } 149 150 $( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash, $thisbutton ] ); 151 }, 152 error: function() { 153 window.location = $thisbutton.attr( 'href' ); 154 return; 155 }, 156 dataType: 'json' 157 }); 158 }; 159 160 /** 161 * Update cart page elements after add to cart events. 162 */ 163 AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) { 164 $button = typeof $button === 'undefined' ? false : $button; 165 166 if ( $button ) { 167 $button.removeClass( 'loading' ); 168 169 if ( fragments ) { 170 $button.addClass( 'added' ); 171 } 172 173 // View cart text. 174 if ( fragments && ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) { 175 $button.after( '<a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' + 176 wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' ); 177 } 178 179 $( document.body ).trigger( 'wc_cart_button_updated', [ $button ] ); 180 } 181 }; 182 183 /** 184 * Update fragments after add to cart events. 185 */ 186 AddToCartHandler.prototype.updateFragments = function( e, fragments ) { 187 if ( fragments ) { 188 $.each( fragments, function( key ) { 189 $( key ) 190 .addClass( 'updating' ) 191 .fadeTo( '400', '0.6' ) 192 .block({ 193 message: null, 194 overlayCSS: { 195 opacity: 0.6 196 } 197 }); 198 }); 199 200 $.each( fragments, function( key, value ) { 201 $( key ).replaceWith( value ); 202 $( key ).stop( true ).css( 'opacity', '1' ).unblock(); 203 }); 204 205 $( document.body ).trigger( 'wc_fragments_loaded' ); 206 } 207 }; 208 209 /** 210 * Init AddToCartHandler. 211 */ 212 new AddToCartHandler(); 213});
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.