1/** 2 * Shopify Modal JavaScript 3 * 4 * Handles modal functionality and product events for Shopify components 5 */ 6 7// Listen for all Shopify-related events 8document.addEventListener('DOMContentLoaded', function() { 9 10 // Store the original addEventListener method 11 const originalAddEventListener = EventTarget.prototype.addEventListener; 12 13 // Override addEventListener to capture all events 14 EventTarget.prototype.addEventListener = function(type, listener, options) { 15 // Call the original method 16 originalAddEventListener.call(this, type, listener, options); 17 18 // Check if this is a Shopify event 19 if (type.toLowerCase().includes('shopify')) { 20 console.log(`ðµ Shopify Event Listener Added: ${type}`, { 21 type: type, 22 target: this, 23 timestamp: new Date().toISOString() 24 }); 25 } 26 }; 27 28 // Also listen for events that are dispatched 29 const originalDispatchEvent = EventTarget.prototype.dispatchEvent; 30 EventTarget.prototype.dispatchEvent = function(event) { 31 // Check if this is a Shopify event before dispatching 32 if (event.type.toLowerCase().includes('shopify')) { 33 console.log(`ðµ Shopify Event Dispatched: ${event.type}`, { 34 event: event, 35 detail: event.detail, 36 target: event.target, 37 currentTarget: event.currentTarget, 38 timestamp: new Date().toISOString() 39 }); 40 } 41 42 // Call the original method 43 return originalDispatchEvent.call(this, event); 44 }; 45 46 // Listener for shopify-update-data event (working on body) 47 document.body.addEventListener('shopify-update-data', function(event) { 48 console.log('ð¢ shopify-update-data event received:', event); 49 50 // Placeholder constants 51 const UPDATE_DELAY = 1000; // milliseconds 52 const MAX_RETRIES = 3; 53 const RETRY_INTERVAL = 500; // milliseconds 54 const SUCCESS_THRESHOLD = 0.8; // 80% success rate 55 56 // Placeholder logic 57 const handleShopifyUpdateData = (eventData) => { 58 // Get the selected variant from the event data 59 let selectedVariant = eventData?.product?.selectedOrFirstAvailableVariant || eventData?.selectedVariant; 60 61 62 // Get the selected variant from the shadow DOM 63 const variantSelector = document.querySelector('shopify-variant-selector'); 64 let selectedRadio = null; 65 66 if (variantSelector && variantSelector.shadowRoot) { 67 // Access the shadow DOM and find the selected radio button 68 selectedRadio = variantSelector.shadowRoot.querySelector('[part*="radio-selected"]'); 69 } 70 71 72 73 // Check if the selected variant is disabled/unavailable 74 const isVariantDisabled = selectedRadio && selectedRadio.getAttribute('part')?.includes('radio-disabled'); 75 76 // Get the buttons 77 const addToCartButton = document.querySelector('.product-modal__add-button'); 78 const buyNowButton = document.querySelector('.product-modal__buy-button'); 79 80 if (isVariantDisabled) { 81 // Disable buttons and update text 82 if (addToCartButton) { 83 addToCartButton.disabled = true; 84 addToCartButton.textContent = 'Unavailable'; 85 addToCartButton.style.cursor = 'not-allowed'; 86 } 87 if (buyNowButton) { 88 buyNowButton.disabled = true; 89 buyNowButton.textContent = 'Unavailable'; 90 buyNowButton.style.cursor = 'not-allowed'; 91 } 92 } else { 93 // Enable buttons and restore original text 94 if (addToCartButton) { 95 addToCartButton.disabled = false; 96 addToCartButton.textContent = 'Add to cart'; 97 addToCartButton.style.cursor = 'pointer'; 98 } 99 if (buyNowButton) { 100 buyNowButton.disabled = false; 101 buyNowButton.textContent = 'Buy now'; 102 buyNowButton.style.cursor = 'pointer'; 103 } 104 } 105 }; 106 107 // Execute the logic with the event data 108 handleShopifyUpdateData(event.detail || event); 109 }, true); 110 111 console.log('ð´ Ready to capture Shopify events. shopify-update-data listener is active on body.'); 112 113 // Initialize variant selector visibility for modal using Shopify's update event 114 initializeModalVariantVisibility(); 115}); 116 117/** Variant Selector Visibility for Modal **/ 118function initializeModalVariantVisibility() { 119 const modal = document.getElementById('product-modal'); 120 if (!modal) return; 121 122 document.body.addEventListener('shopify-update-data', (event) => { 123 const selector = modal.querySelector('shopify-variant-selector'); 124 if (!selector) return; 125 126 const product = event.detail?.product; 127 const hasVariants = product?.options?.length > 0 || false; 128 129 selector.classList.toggle('has-variants', hasVariants); 130 }, true); 131}
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.