1( function ( document, $ ) { 2 var $cache = {}; 3 4 /** 5 * Cache our DOM selectors. 6 */ 7 function generate_cache() { 8 $cache.document = $( document ); 9 $cache.first_payment_date = $( '.first-payment-date' ); 10 $cache.subscription_details = $( 11 '.woocommerce-subscriptions-product-details' 12 ); 13 // Match the product-type class wherever it lands: on the product wrapper in classic themes, or on the <body> 14 // in block themes (where the wrapper is a generic wp-block-group). 15 $cache.is_variable_subscription = 16 0 < $( '.product-type-variable-subscription' ).length; 17 } 18 19 /** 20 * Attach DOM events. 21 */ 22 function attach_events() { 23 if ( $cache.is_variable_subscription ) { 24 $cache.document.on( 'found_variation', update_variation_elements ); 25 $cache.document.on( 'reset_data', clear_variation_elements ); 26 } 27 } 28 29 /** 30 * Update the variation's first payment date and subscription detail lines. 31 * 32 * Shown only once a variation is found (i.e. the add to cart button is enabled). 33 * 34 * @param {jQuery.Event} event 35 * @param {object} variation_data 36 */ 37 function update_variation_elements( event, variation_data ) { 38 var details = variation_data.subscription_details_html || ''; 39 var has_details = '' !== details; 40 41 $cache.first_payment_date.html( variation_data.first_payment_html ); 42 $cache.subscription_details.html( details ).toggle( has_details ); 43 tighten_variation_price_spacing( has_details ); 44 } 45 46 /** 47 * Clear and hide the variation's first payment date and subscription detail lines. 48 */ 49 function clear_variation_elements() { 50 $cache.first_payment_date.html( '' ); 51 $cache.subscription_details.empty().hide(); 52 tighten_variation_price_spacing( false ); 53 } 54 55 /** 56 * Toggle the variation price's bottom margin. 57 * 58 * When the detail lines are shown, the price's bottom margin is removed (via CSS keyed off this class) so they sit 59 * directly beneath it â matching the spacing between the detail lines themselves. When there are no detail lines, 60 * the margin is kept so the price retains its normal spacing above the add to cart button. 61 * 62 * The class is toggled on the (stable) variation wrapper rather than setting the margin on the price element 63 * directly, because WooCommerce re-renders the price element on every variation change â often after this handler 64 * runs â which would wipe an inline style. 65 * 66 * @param {boolean} tighten Whether to remove the price's bottom margin. 67 */ 68 function tighten_variation_price_spacing( tighten ) { 69 $cache.subscription_details 70 .closest( '.single_variation_wrap' )
71 .toggleClass( 'wcsatt-variation-has-details', tighten ); 72 } 73 74 /** 75 * Initialise. 76 */ 77 function init() { 78 generate_cache(); 79 attach_events(); 80 } 81 82 $( init ); 83} )( document, jQuery );
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.