1jQuery(document).ready(function($) { 2 'use strict'; 3 4 // Prescription functionality 5 var prescriptionContainers = $('.cpap-prescription-container'); 6 7 prescriptionContainers.each(function() { 8 var container = $(this); 9 var productId = container.data('product-id'); 10 var fileInput = $('#cpap-prescription-file-' + productId); 11 var fileNameSpan = container.find('.cpap-file-name'); 12 var removeBtn = container.find('.cpap-remove-prescription'); 13 var displayArea = container.find('.cpap-prescription-display'); 14 15 // Check if user is in US and show prescription area 16 function checkGeolocationAndShow() { 17 $.ajax({ 18 url: cpap_prescription_ajax.ajax_url, 19 type: 'POST', 20 data: { 21 action: 'cpap_prescription_geolocation' 22 }, 23 success: function(response) { 24 if (response && response.data) { 25 // If country is US or NL, show prescription area 26 if (response.data === 'US' || response.data === 'NL') { 27 container.show(); 28 } 29 } 30 }, 31 error: function() { 32 // If geolocation fails, default to showing for US users 33 // container.show(); 34 } 35 }); 36 } 37 38 // Initialize on page load 39 checkGeolocationAndShow(); 40 41 // File input change handler 42 fileInput.on('change', function() { 43 var file = this.files[0]; 44 if (file) { 45 handleFileSelection(file); 46 } else { 47 handleFileRemoval(); 48 } 49 }); 50 51 // Handle file selection 52 function handleFileSelection(file) { 53 // Validate file type 54 var allowedTypes = cpap_prescription_ajax.allowed_types; 55 var fileExtension = file.name.split('.').pop().toLowerCase(); 56 57 if (!allowedTypes.includes(fileExtension)) { 58 alert('Please select a valid file type: ' + allowedTypes.join(', ')); 59 fileInput.val(''); 60 return; 61 } 62 63 // Validate file size (10MB) 64 var maxSize = cpap_prescription_ajax.max_file_size; 65 if (file.size > maxSize) { 66 alert('File size must be less than 10MB'); 67 fileInput.val(''); 68 return; 69 } 70 71 // Show uploading state 72 fileNameSpan.text('Uploading...'); 73 displayArea.addClass('active'); 74 container.addClass('has-file'); 75 76 // Upload file immediately 77 uploadPrescriptionFile(file); 78 } 79 80 // Upload prescription file to temp storage 81 function uploadPrescriptionFile(file) { 82 // Create FormData to upload the file 83 var formData = new FormData(); 84 formData.append('action', 'cpap_prescription_upload_temp'); 85 formData.append('cpap_prescription_file', file); 86 formData.append('product_id', productId); 87 formData.append('nonce', cpap_prescription_ajax.nonce); 88 89 // Upload file 90 $.ajax({ 91 url: cpap_prescription_ajax.ajax_url, 92 type: 'POST', 93 data: formData, 94 processData: false, 95 contentType: false, 96 success: function(response) { 97 if (response.success) { 98 // Show the actual filename 99 fileNameSpan.text(response.data.filename); 100 101 // Remove any existing hidden input 102 $('form.cart').find('input[name="cpap_prescription_temp_id"]').remove(); 103 104 // Add hidden input with file ID to form 105 var hiddenInput = $('<input type="hidden" name="cpap_prescription_temp_id" value="' + response.data.file_id + '">'); 106 $('form.cart').append(hiddenInput); 107 } else { 108 alert('Failed to upload prescription: ' + (response.data.message || 'Unknown error')); 109 handleFileRemoval(); 110 } 111 }, 112 error: function(xhr, status, error) { 113 alert('Failed to upload prescription. Please try again.'); 114 handleFileRemoval(); 115 } 116 }); 117 } 118 119 // Handle file removal 120 function handleFileRemoval() { 121 fileInput.val(''); 122 fileNameSpan.text(''); 123 displayArea.removeClass('active'); 124 container.removeClass('has-file'); 125 126 // Remove hidden input 127 $('form.cart').find('input[name="cpap_prescription_temp_id"]').remove(); 128 } 129 130 // Remove button handler 131 removeBtn.on('click', function(e) { 132 e.preventDefault(); 133 handleFileRemoval(); 134 }); 135 }); 136 137 // Hook into all AJAX requests to add prescription temp ID 138 $(document).ajaxSend(function(event, jqxhr, settings) { 139 // Check if this is an add to cart request (only check string data, not FormData) 140 if (settings.data && typeof settings.data === 'string' && settings.data.indexOf('action=new_ajax_add_to_cart') !== -1) { 141 // Get the temp ID from the hidden input 142 var tempId = $('form.cart').find('input[name="cpap_prescription_temp_id"]').val(); 143 144 if (tempId) { 145 // Add the prescription temp ID to the data 146 settings.data += '&cpap_prescription_temp_id=' + encodeURIComponent(tempId); 147 } 148 } 149 }); 150 151 // Listen for successful add to cart to update prescription UI 152 $(document.body).on('added_to_cart', function(event, fragments, cart_hash, button) { 153 // Check if there was a prescription with this add to cart 154 var prescriptionDisplay = $('.cpap-prescription-display.active'); 155 156 if (prescriptionDisplay.length > 0) { 157 // Change the filename to "Prescription added" 158 prescriptionDisplay.find('.cpap-file-name').text('Prescription added'); 159 160 // Remove the remove button 161 prescriptionDisplay.find('.cpap-remove-prescription').remove(); 162 163 // Remove the hidden input as it's no longer needed 164 $('form.cart').find('input[name="cpap_prescription_temp_id"]').remove(); 165 } 166 }); 167});
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.