1/* global enr_frontend_params */ 2 3jQuery( function( $ ) { 4 'use strict'; 5 6 var is_blocked = function( $node ) { 7 return $node.is( '.processing' ) || $node.parents( '.processing' ).length; 8 }; 9 10 /** 11 * Block a node visually for processing. 12 * 13 * @param {JQuery Object} $node 14 */ 15 var block = function( $node ) { 16 $.blockUI.defaults.overlayCSS.cursor = 'wait'; 17 18 if ( ! is_blocked( $node ) ) { 19 $node.addClass( 'processing' ).block( { 20 message : null, 21 overlayCSS : { 22 background : '#fff', 23 opacity : 0.6 24 } 25 } ); 26 } 27 }; 28 29 /** 30 * Unblock a node after processing is complete. 31 * 32 * @param {JQuery Object} $node 33 */ 34 var unblock = function( $node ) { 35 $node.removeClass( 'processing' ).unblock(); 36 }; 37 38 // Hide variable level limit notice when it is limited to variation level 39 if ( 'yes' === enr_frontend_params.hide_variable_limited_notice && $( 'form.variations_form' ).length && $( 'form.variations_form' ).find( '.limited-subscription-notice' ).length ) { 40 $( 'form.variations_form' ).find( '.limited-subscription-notice' ).hide(); 41 } 42 43 function maybeProcessSwitchRequest() { 44 if ( window.location.href.indexOf( 'switch-subscription' ) != - 1 && window.location.href.indexOf( 'item' ) != - 1 ) { 45 $( '.product form.cart' ).prop( 'action', '' ); 46 } 47 } 48 49 /** 50 * Handle product page subscribe events. 51 */ 52 var wcs_variations_form = { 53 variationForm : $( '.variations_form' ), 54 cartForm : $( 'form.cart' ), 55 init : function() { 56 $( document ).on( 'found_variation.wc-variation-form', this.onFoundVariation ); 57 $( document ).on( 'reset_data', this.onResetVariation ); 58 }, 59 onFoundVariation : function( evt, variation ) { 60 wcs_variations_form.onResetVariation(); 61 62 if ( variation.enr_limited_subscription_notice ) { 63 wcs_variations_form.variationForm.find( '.woocommerce-variation-add-to-cart' ).after( variation.enr_limited_subscription_notice ); 64 } 65 66 if ( variation.enr_resubscribe_link ) { 67 wcs_variations_form.variationForm.find( '.woocommerce-variation-add-to-cart' ).after( variation.enr_resubscribe_link ); 68 } 69 70 if ( variation.enr_subscribe_now_form ) { 71 wcs_variations_form.variationForm.find( '.woocommerce-variation-add-to-cart' ).before( variation.enr_subscribe_now_form ); 72 wcs_variations_form.variationForm.find( '.single_add_to_cart_button' ).text( variation.enr_single_add_to_cart_text ); 73 wcs_variations_form.variationForm.find( '.enr-subscribe-now-wrapper #enr_subscribe_now' ).change(); 74 } 75 76 maybeProcessSwitchRequest(); 77 }, 78 onResetVariation : function() { 79 if ( wcs_variations_form.variationForm.find( '.enr-variation-wrapper' ).length ) { 80 wcs_variations_form.variationForm.find( '.enr-variation-wrapper' ).remove(); 81 } 82 83 if ( wcs_variations_form.variationForm.find( '.enr-subscribe-now-wrapper' ).length ) { 84 wcs_variations_form.variationForm.find( '.enr-subscribe-now-wrapper' ).parent().remove(); 85 } 86 87 wcs_variations_form.variationForm.find( '.single_add_to_cart_button' ).removeClass( 'enr_single_subscribe_button' ).text( enr_frontend_params.single_add_to_cart_text ); 88 }, 89 }; 90 91 /** 92 * Handle subscribe now events. 93 */ 94 var wcs_subscribe_now = { 95 wrapper : $( '.enr-subscribe-now-wrapper' ), 96 init : function() { 97 if ( ! enr_frontend_params.is_user_logged_in && enr_frontend_params.is_checkout && this.wrapper.length > 0 ) { 98 if ( enr_frontend_params.cart_level_subscribed ) { 99 this.wrapper.find( 'input,select' ).each( function( e, element ) { 100 $( element ).prop( 'disabled', true ); 101 } ); 102 } else { 103 this.wrapper.hide(); 104 } 105 return; 106 } 107 108 $( document ).on( 'change', '#enr_subscribe_now,#enr_subscribe_plans,#enr_subscribe_period,#enr_subscribe_period_interval,#enr_subscribe_length', this.onChange ); 109 $( document ).on( 'updated_wc_div, updated_cart_totals', this.mayBeShowForm ); 110 $( document ).on( 'enr_update_subscribe_now', this.updateAction ); 111 112 if ( this.wrapper.length > 0 ) { 113 this.wrapper.find( '#enr_subscribe_now' ).change(); 114 maybeProcessSwitchRequest(); 115 } 116 }, 117 showForm : function( node ) { 118 var wrapper = node; 119 wrapper = node.closest( '.enr-subscribe-now-wrapper' ); 120 121 if ( wrapper.length > 0 ) { 122 if ( wrapper.find( '#enr_subscribe_now' ).is( ':checked' ) ) { 123 wrapper.find( 'tr:gt(0)' ).slideDown(); 124 } else {
125 wrapper.find( 'tr:gt(0)' ).slideUp(); 126 } 127 } 128 }, 129 mayBeShowForm : function() { 130 wcs_subscribe_now.showForm( wcs_subscribe_now.wrapper ); 131 }, 132 onChange : function( event ) { 133 event.preventDefault(); 134 var args = {}; 135 var $form = $(event.currentTarget).closest('form.cart'); 136 var $composite = $form.find('.composite_data'); 137 var $bundle = $form.find('.bundle_data'); 138 139 if ($composite.length && typeof $composite.data('enr_product_total') !== 'undefined') { 140 args.price = $composite.data('enr_product_total'); 141 } else if ($bundle.length && typeof $bundle.data('enr_product_total') !== 'undefined') { 142 args.price = $bundle.data('enr_product_total'); 143 } 144 145 $(document).trigger('enr_update_subscribe_now', [$(event.currentTarget), args]); 146 }, 147 updateAction : function( e, $this, args = {} ) { 148 var refreshed = args.refreshed || false, 149 $data = { 150 action : '_enr_subscribe_now', 151 security : enr_frontend_params.subscribe_now_nonce, 152 is_switch_request : enr_frontend_params.is_switch_request, 153 data : $this.closest( '.enr-subscribe-now-wrapper' ).find( ':input[name]' ).serialize(), 154 }; 155 156 $data = $.extend( $data, args ); 157 158 wcs_subscribe_now.showForm( $this ); 159 block( wcs_subscribe_now.getNode() ); 160 161 $.ajax( { 162 type : 'POST', 163 url : enr_frontend_params.ajax_url, 164 data : $data, 165 success : function( response ) { 166 if ( response.success ) { 167 $this.closest( response.data.subscribe_wrapper_class ).empty().append( response.data.html ); 168 wcs_subscribe_now.showForm( $this ); 169 170 if ( $( '.single_add_to_cart_button' ).length ) { 171 if ( '1' !== enr_frontend_params.is_switch_request && $this.closest( '.enr-subscribe-now-wrapper' ).find( '#enr_subscribe_now' ).is( ':checked' ) ) { 172 $( '.single_add_to_cart_button' ).addClass( 'enr_single_subscribe_button' ).text( enr_frontend_params.subscribe_now_button_text ); 173 } else { 174 $( '.single_add_to_cart_button' ).removeClass( 'enr_single_subscribe_button' ).text( enr_frontend_params.single_add_to_cart_text ); 175 } 176 } 177 178 if ( false === response.data.refresh || true === refreshed ) { 179 $( document.body ).trigger( 'update_checkout' ); 180 181 if ( $( '.woocommerce-cart-form' ).length ) { 182 $( document.body ).trigger( 'wc_update_cart' ); 183 } 184 } else { 185 $( document ).trigger( 'enr_update_subscribe_now', [ $this, { refreshed : true } ] ); 186 } 187 188 $( document.body ).trigger( 'enr_subscribe_form_submitted_success' ); 189 } 190 }, 191 complete : function() { 192 unblock( wcs_subscribe_now.getNode() ); 193 } 194 } ); 195 return false; 196 }, 197 getNode : function() { 198 if ( $( '.woocommerce-cart-form' ).length ) { 199 return $( 'div.cart_totals' ); 200 } else if ( $( '.woocommerce-checkout' ).length ) { 201 return $( '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ).closest( 'form' ); 202 } else { 203 return $( '.enr-subscribe-now-wrapper' ); 204 } 205 }, 206 }; 207 208 /** 209 * Merge each WCS-native scheme's shipping-cycle message directly into that scheme's own 210 * option/radio label in WCS core's frontend plan selector (see 211 * ENR_Shipping_Cycle::output_scheme_shipping_cycle_messages() for the embedded JSON data 212 * this reads). Runs once at page load, since WCS core's own JS only ever reacts to selection 213 * changes here - it never rebuilds these elements - so the merged label text stays put 214 * regardless of what the customer picks afterwards. 215 */ 216 var wcs_native_scheme_shipping = { 217 init : function() { 218 $( '.wcsatt-options-wrapper' ).each( this.mergeIntoWrapper ); 219 }, 220 mergeIntoWrapper : function() { 221 var $wrapper = $( this ), 222 $data = $wrapper.find( 'script.enr-wcs-scheme-shipping-messages-data' ), 223 messages; 224 225 if ( ! $data.length ) { 226 return; 227 } 228 229 try { 230 messages = JSON.parse( $data.text() ); 231 } catch ( err ) { 232 return; 233 } 234 235 $wrapper.find( 'select.wcsatt-options-product-dropdown option' ).each( function() { 236 var $option = $( this ); 237 238 if ( messages[ $option.val() ] ) { 239 $option.text( $option.text() + ' - ' + messages[ $option.val() ] ); 240 } 241 } ); 242 243 $wrapper.find( '.wcsatt-options-product li' ).each( function() { 244 var $li = $( this ), 245 value = $li.find( 'input[type="radio"]' ).val(), 246 message = messages[ value ]; 247 248 if ( message ) {
249 $li.find( '.subscription-details' ).text( function( i, text ) { 250 return text + ' - ' + message; 251 } ); 252 } 253 } ); 254 } 255 }; 256 257 wcs_variations_form.init(); 258 wcs_subscribe_now.init(); 259 wcs_native_scheme_shipping.init(); 260} );
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.