1"use strict"; 2(function ($) { 3 jQuery(document).ready(function ($) { 4 const localizeData = window.yaydp_frontend_data ?? {}; 5 const discount_based_on = localizeData.discount_based_on ?? "regular_price"; 6 const currencySettings = localizeData.currency_settings ?? {}; 7 8 function formatPrice(price) { 9 if (isNaN(price)) { 10 return price; 11 } 12 13 const { 14 symbol = "$", 15 symbolPosition = "left", 16 decimalSeparator = ".", 17 thousandSeparator = ",", 18 precision = 2, 19 } = currencySettings; 20 21 const priceString = parseFloat(price).toFixed(precision); 22 23 const parts = priceString.split("."); 24 25 parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator); 26 27 const formattedNumber = parts.join(decimalSeparator); 28 29 let formattedPrice; 30 switch (symbolPosition) { 31 case "left": 32 formattedPrice = `${symbol}${formattedNumber}`; 33 break; 34 case "right": 35 formattedPrice = `${formattedNumber}${symbol}`; 36 break; 37 case "left_space": 38 formattedPrice = `${symbol} ${formattedNumber}`; 39 break; 40 case "right_space": 41 formattedPrice = `${formattedNumber} ${symbol}`; 42 break; 43 default: 44 formattedPrice = `${symbol}${formattedNumber}`; 45 } 46 47 return formattedPrice; 48 } 49 50 function updateSaleTagRules($saleTag, ruleNames) { 51 if (!$saleTag.length || !ruleNames || ruleNames.length === 0) { 52 return; 53 } 54 55 let saleTagTemplate = $saleTag.attr("data-sale-tag-template"); 56 let discountAmount = $saleTag.attr("data-discount-amount"); 57 58 if (!saleTagTemplate) { 59 saleTagTemplate = $saleTag.data("sale-tag-template"); 60 } 61 if (!discountAmount) { 62 discountAmount = $saleTag.data("discount-amount"); 63 } 64 65 if (!saleTagTemplate) { 66 return; 67 } 68 69 let saleText = saleTagTemplate; 70 71 if (discountAmount && discountAmount > 0) { 72 saleText = saleText.replace(/\{amount\}/g, discountAmount + "%"); 73 } 74 75 const ruleNamesText = ruleNames.join(", "); 76 saleText = saleText.replace(/\{rule_name\}/g, ruleNamesText); 77 78 $saleTag.text(saleText); 79 } 80 81 function restoreSaleTagToInitial($saleTag) { 82 if (!$saleTag.length) { 83 return; 84 } 85 86 let initialRules = $saleTag.attr("data-initial-rules"); 87 if (initialRules) { 88 try { 89 initialRules = JSON.parse(initialRules); 90 } catch (e) { 91 initialRules = $saleTag.data("initial-rules"); 92 } 93 } else { 94 initialRules = $saleTag.data("initial-rules"); 95 } 96 97 if ( 98 initialRules && 99 Array.isArray(initialRules) && 100 initialRules.length > 0 101 ) { 102 updateSaleTagRules($saleTag, initialRules); 103 } else { 104 let variationRulesData = $saleTag.attr("data-variation-rules"); 105 if (variationRulesData) { 106 try { 107 variationRulesData = JSON.parse(variationRulesData); 108 } catch (e) { 109 variationRulesData = $saleTag.data("variation-rules"); 110 } 111 } else { 112 variationRulesData = $saleTag.data("variation-rules"); 113 } 114 115 if (variationRulesData && typeof variationRulesData === "object") { 116 const allRuleNames = []; 117 Object.keys(variationRulesData).forEach(function (variationId) { 118 const rules = variationRulesData[variationId]; 119 if (Array.isArray(rules)) {
120 rules.forEach(function (ruleName) { 121 if (allRuleNames.indexOf(ruleName) === -1) { 122 allRuleNames.push(ruleName); 123 } 124 }); 125 } 126 }); 127 128 if (allRuleNames.length > 0) { 129 updateSaleTagRules($saleTag, allRuleNames); 130 } 131 } 132 } 133 } 134 135 $(".single_variation_wrap").on( 136 "show_variation", 137 function (event, variation) { 138 $(".yaydp-pricing-table-wrapper").hide(); 139 const variationId = String(variation.variation_id); 140 let lastMatchingTable = null; 141 $(".yaydp-pricing-table-wrapper").each(function () { 142 const applicableVariationsAttr = $(this).attr( 143 "data-applicable-variations" 144 ); 145 const applicableVariations = applicableVariationsAttr 146 .split(",") 147 .map((item) => item.trim()); 148 if (applicableVariations.includes(variationId)) { 149 lastMatchingTable = $(this); 150 } 151 }); 152 if (lastMatchingTable) { 153 lastMatchingTable.show(); 154 } 155 $( 156 "[data-variable='discount_value'], [data-variable='final_price'], [data-variable='discount_amount'], [data-variable='discounted_price']" 157 ).each((index, item) => { 158 const variation_price = 159 discount_based_on === "regular_price" 160 ? variation.display_regular_price 161 : variation.display_price; 162 const formula = $(item).data("formula"); 163 const final_price = eval(formula.replaceAll("x", variation_price)); 164 165 const formattedPrice = formatPrice(final_price); 166 167 $(item).find(".woocommerce-Price-amount").html(formattedPrice); 168 }); 169 170 const $saleTag = $(".yaydp-sale-tag[data-variable-product='1']"); 171 if ($saleTag.length) { 172 let variationRulesData = $saleTag.attr("data-variation-rules"); 173 if (variationRulesData) { 174 try { 175 variationRulesData = JSON.parse(variationRulesData); 176 } catch (e) { 177 variationRulesData = $saleTag.data("variation-rules"); 178 } 179 } else { 180 variationRulesData = $saleTag.data("variation-rules"); 181 } 182 183 if (variationRulesData && typeof variationRulesData === "object") { 184 let ruleNames = 185 variationRulesData[variationId] || 186 variationRulesData[parseInt(variationId, 10)]; 187 188 if (!ruleNames) { 189 Object.keys(variationRulesData).forEach(function (key) { 190 if ( 191 String(key) === variationId || 192 parseInt(key, 10) === parseInt(variationId, 10) 193 ) { 194 ruleNames = variationRulesData[key]; 195 } 196 }); 197 } 198 199 if (ruleNames && Array.isArray(ruleNames) && ruleNames.length > 0) { 200 updateSaleTagRules($saleTag, ruleNames); 201 } 202 } 203 } 204 } 205 ); 206 $(".single_variation_wrap").on("hide_variation", function (event) { 207 $(".yaydp-pricing-table-wrapper").hide(); 208 209 setTimeout(function () { 210 const $saleTag = $(".yaydp-sale-tag[data-variable-product='1']"); 211 if ($saleTag.length) { 212 restoreSaleTagToInitial($saleTag); 213 } 214 }, 50); 215 }); 216 217 function areAllVariationSelectsEmpty() { 218 let allEmpty = true; 219 $("form.variations_form select").each(function () { 220 if ($(this).val() && $(this).val() !== "") { 221 allEmpty = false; 222 return false; 223 } 224 }); 225 return allEmpty; 226 } 227 228 $(document).on("change", "form.variations_form select", function () { 229 setTimeout(function () { 230 const $saleTag = $(".yaydp-sale-tag[data-variable-product='1']"); 231 if ($saleTag.length && areAllVariationSelectsEmpty()) { 232 restoreSaleTagToInitial($saleTag); 233 } 234 }, 100); 235 }); 236 237 $(document.body).on("reset_data", function () { 238 setTimeout(function () { 239 const $saleTag = $(".yaydp-sale-tag[data-variable-product='1']"); 240 if ($saleTag.length) { 241 restoreSaleTagToInitial($saleTag); 242 } 243 }, 100); 244 }); 245 246 $(document).on("reset", "form.variations_form", function () { 247 setTimeout(function () { 248 const $saleTag = $(".yaydp-sale-tag[data-variable-product='1']"); 249 if ($saleTag.length) { 250 restoreSaleTagToInitial($saleTag); 251 } 252 }, 100); 253 }); 254 }); 255})(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.