1jQuery(function ($) { 2 function formatMoney(amount, currency, locale) { 3 try { 4 return new Intl.NumberFormat(locale || "nl-NL", { 5 style: "currency", 6 currency: currency || "EUR" 7 }).format(amount); 8 } catch (e) { 9 return "⬠" + Number(amount || 0).toFixed(2).replace(".", ","); 10 } 11 } 12 13 $(".wcpg").each(function () { 14 const root = $(this); 15 const selected = new Set(); 16 const price = parseFloat(root.data("price")) || 0; 17 const currency = WCPG.currency || "EUR"; 18 const locale = (WCPG.locale || "nl_NL").replace("_", "-"); 19 const formId = root.data("formId"); 20 const cellsFieldId = root.data("cellsField"); 21 22 // Only the plain cells field is synced client-side. The Product field's 23 // price/quantity are intentionally left untouched: Gravity Forms runs 24 // tamper-protection on singleproduct/hiddenproduct price sub-inputs 25 // (it rejects a submitted price that differs from the one it rendered), 26 // so the real amount is always computed server-side from the cells 27 // field instead (see WCPG_Plugin::override_product_info()). 28 const $cellsInput = $("#input_" + formId + "_" + cellsFieldId); 29 const $submitBtn = $("#gform_submit_button_" + formId); 30 31 function getSelectedArray() { 32 return Array.from(selected); 33 } 34 35 function syncFormFields(cells) { 36 $cellsInput.val(cells.join(",")).trigger("change"); 37 } 38 39 function refresh() { 40 const cells = getSelectedArray(); 41 const count = cells.length; 42 const total = count * price; 43 44 root.find(".wcpg-count").text(count); 45 root.find(".wcpg-price-per-block").text(formatMoney(price, currency, locale)); 46 root.find(".wcpg-total").text(formatMoney(total, currency, locale)); 47 48 syncFormFields(cells); 49 50 $submitBtn.prop("disabled", count === 0); 51 root.find(".wcpg-clear").prop("disabled", count === 0); 52 } 53 54 root.on("click", ".wcpg-cell:not(:disabled)", function () { 55 const btn = $(this); 56 const cell = String(btn.data("cell")); 57 58 if (selected.has(cell)) { 59 selected.delete(cell); 60 btn.removeClass("wcpg-selected"); 61 } else { 62 selected.add(cell); 63 btn.addClass("wcpg-selected"); 64 } 65 66 root.find(".wcpg-message").text(""); 67 refresh(); 68 }); 69 70 root.on("click", ".wcpg-clear", function () { 71 selected.clear(); 72 root.find(".wcpg-cell").removeClass("wcpg-selected"); 73 root.find(".wcpg-message").text(""); 74 refresh(); 75 }); 76 77 root.find("form").on("submit", function () { 78 // Final safety sync right before the native Gravity Forms submit. 79 syncFormFields(getSelectedArray()); 80 }); 81 82 refresh(); 83 }); 84});
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.