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.updateAction ); 109 $( document ).on( 'updated_wc_div, updated_cart_totals', this.mayBeShowForm ); 110 111 if ( this.wrapper.length > 0 ) { 112 this.wrapper.find( '#enr_subscribe_now' ).change(); 113 maybeProcessSwitchRequest(); 114 } 115 }, 116 showForm : function( node ) { 117 var wrapper = node; 118 wrapper = node.closest( '.enr-subscribe-now-wrapper' ); 119 120 if ( wrapper.length > 0 ) { 121 if ( wrapper.find( '#enr_subscribe_now' ).is( ':checked' ) ) { 122 wrapper.find( 'tr:gt(0)' ).slideDown(); 123 } else {
124 wrapper.find( 'tr:gt(0)' ).slideUp(); 125 } 126 } 127 }, 128 mayBeShowForm : function() { 129 wcs_subscribe_now.showForm( wcs_subscribe_now.wrapper ); 130 }, 131 updateAction : function( evt, refreshed ) { 132 evt.preventDefault(); 133 134 var $this = $( evt.currentTarget ); 135 refreshed = refreshed || false; 136 137 wcs_subscribe_now.showForm( $this ); 138 block( wcs_subscribe_now.getNode() ); 139 140 $.ajax( { 141 type : 'POST', 142 url : enr_frontend_params.ajax_url, 143 data : { 144 action : '_enr_subscribe_now', 145 security : enr_frontend_params.subscribe_now_nonce, 146 is_switch_request : enr_frontend_params.is_switch_request, 147 data : $this.closest( '.enr-subscribe-now-wrapper' ).find( ':input[name]' ).serialize() 148 }, 149 success : function( response ) { 150 if ( response.success ) { 151 $this.closest( response.data.subscribe_wrapper_class ).empty().append( response.data.html ); 152 wcs_subscribe_now.showForm( $this ); 153 154 if ( $( '.single_add_to_cart_button' ).length ) { 155 if ( '1' !== enr_frontend_params.is_switch_request && $this.closest( '.enr-subscribe-now-wrapper' ).find( '#enr_subscribe_now' ).is( ':checked' ) ) { 156 $( '.single_add_to_cart_button' ).addClass( 'enr_single_subscribe_button' ).text( enr_frontend_params.subscribe_now_button_text ); 157 } else { 158 $( '.single_add_to_cart_button' ).removeClass( 'enr_single_subscribe_button' ).text( enr_frontend_params.single_add_to_cart_text ); 159 } 160 } 161 162 if ( false === response.data.refresh || true === refreshed ) { 163 $( document.body ).trigger( 'update_checkout' ); 164 165 if ( $( '.woocommerce-cart-form' ).length ) { 166 $( document.body ).trigger( 'wc_update_cart' ); 167 } 168 } else { 169 $( '#enr_subscribe_now' ).trigger( 'change', [ true ] ); 170 } 171 172 $( document.body ).trigger( 'enr_subscribe_form_submitted_success' ); 173 } 174 }, 175 complete : function() { 176 unblock( wcs_subscribe_now.getNode() ); 177 } 178 } ); 179 return false; 180 }, 181 getNode : function() { 182 if ( $( '.woocommerce-cart-form' ).length ) { 183 return $( 'div.cart_totals' ); 184 } else if ( $( '.woocommerce-checkout' ).length ) { 185 return $( '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ).closest( 'form' ); 186 } else { 187 return $( '.enr-subscribe-now-wrapper' ); 188 } 189 }, 190 }; 191 192 wcs_variations_form.init(); 193 wcs_subscribe_now.init(); 194} );
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.