| @@ -1,5 +1,35 @@ | ||
| 1 | 1 | document.addEventListener("DOMContentLoaded", function () { |
| 2 | + const COMMISSION_VALUE = "commission"; | |
| 3 | + // Range in which a value is read as a percentage; outside it the value is a fixed Toman amount | |
| 4 | + const PERCENT_RANGE_MIN = -100; | |
| 5 | + const PERCENT_RANGE_MAX = 100; | |
| 6 | + // Maximum allowed increase and decrease percentage | |
| 7 | + const MAX_PERCENT = 35; | |
| 8 | + const MIN_PERCENT = -35; | |
| 9 | + | |
| 10 | + // Parses the raw input text into an integer, keeping a leading minus sign | |
| 11 | + const parseValue = function (rawText) { | |
| 12 | + const isNegative = /^\s*-/.test(rawText); | |
| 13 | + const digits = rawText.replace(/[^\d]/g, ""); | |
| 14 | + | |
| 15 | + if (digits === "") { | |
| 16 | + return { raw: isNegative ? "-" : "", value: 0, empty: true }; | |
| 17 | + } | |
| 18 | + | |
| 19 | + const value = (isNegative ? -1 : 1) * parseInt(digits, 10); | |
| 20 | + | |
| 21 | + return { raw: digits, value: value, empty: false }; | |
| 22 | + }; | |
| 23 | + | |
| 24 | + const formatValue = function (value) { | |
| 25 | + return value.toLocaleString("en-US"); | |
| 26 | + }; | |
| 27 | + | |
| 28 | + const isPercent = function (value) { | |
| 29 | + return value >= PERCENT_RANGE_MIN && value <= PERCENT_RANGE_MAX; | |
| 30 | + }; | |
| 31 | + | |
| 2 | 32 | const groups = document.querySelectorAll( |
| 3 | 33 | ".basalam-form-group, .basalam-form-group-full" |
| 4 | 34 | ); |
| 5 | 35 | |
| @@ -9,17 +39,17 @@ | ||
| 9 | 39 | return; |
| 10 | 40 | } |
| 11 | 41 | |
| 12 | 42 | const input = |
| 13 | - group.querySelector('[data-role="increase-price-input"]') || | |
| 43 | + group.querySelector('[data-role="price-change-input"]') || | |
| 14 | 44 | group.querySelector( |
| 15 | - 'input.percentage-input[type="text"][name="sync_basalam_settings[increase_price_value]"]' | |
| 45 | + 'input.percentage-input[type="text"][name="sync_basalam_settings[price_change_value]"]' | |
| 16 | 46 | ); |
| 17 | 47 | const unit = group.querySelector(".percentage-unit"); |
| 18 | 48 | const hiddenInput = |
| 19 | - group.querySelector('[data-role="increase-price-hidden"]') || | |
| 49 | + group.querySelector('[data-role="price-change-hidden"]') || | |
| 20 | 50 | group.querySelector( |
| 21 | - 'input[type="hidden"][name="sync_basalam_settings[increase_price_value]"]' | |
| 51 | + 'input[type="hidden"][name="sync_basalam_settings[price_change_value]"]' | |
| 22 | 52 | ); |
| 23 | 53 | |
| 24 | 54 | if (!input || !unit || !hiddenInput) { |
| 25 | 55 | return; |
| @@ -25,15 +55,21 @@ | ||
| 25 | 55 | return; |
| 26 | 56 | } |
| 27 | 57 | |
| 28 | 58 | const normalizeInput = function () { |
| 29 | - const rawValue = input.value.replace(/[^\d]/g, ""); | |
| 30 | - const numberValue = parseInt(rawValue, 10) || 0; | |
| 59 | + const parsed = parseValue(input.value); | |
| 31 | 60 | |
| 32 | - input.value = rawValue === "" ? "" : numberValue.toLocaleString("en-US"); | |
| 33 | - unit.textContent = numberValue <= 100 ? "درصد" : "تومان"; | |
| 61 | + if (parsed.empty) { | |
| 62 | + input.value = parsed.raw; | |
| 63 | + unit.textContent = "درصد"; | |
| 34 | 64 | |
| 35 | - return rawValue === "" ? "" : String(numberValue); | |
| 65 | + return ""; | |
| 66 | + } | |
| 67 | + | |
| 68 | + input.value = formatValue(parsed.value); | |
| 69 | + unit.textContent = isPercent(parsed.value) ? "درصد" : "تومان"; | |
| 70 | + | |
| 71 | + return String(parsed.value); | |
| 36 | 72 | }; |
| 37 | 73 | |
| 38 | 74 | const updateHiddenValue = function (value) { |
| 39 | 75 | if (hiddenInput.value === String(value)) { |
| @@ -46,9 +82,9 @@ | ||
| 46 | 82 | }; |
| 47 | 83 | |
| 48 | 84 | const syncHiddenInput = function () { |
| 49 | 85 | if (checkbox.checked) { |
| 50 | - updateHiddenValue("-1"); | |
| 86 | + updateHiddenValue(COMMISSION_VALUE); | |
| 51 | 87 | return; |
| 52 | 88 | } |
| 53 | 89 | |
| 54 | 90 | updateHiddenValue(normalizeInput()); |
| @@ -67,9 +103,9 @@ | ||
| 67 | 103 | input.disabled = checkbox.checked; |
| 68 | 104 | |
| 69 | 105 | if (checkbox.checked) { |
| 70 | 106 | input.value = ""; |
| 71 | - updateHiddenValue("-1"); | |
| 107 | + updateHiddenValue(COMMISSION_VALUE); | |
| 72 | 108 | unit.textContent = "درصد"; |
| 73 | 109 | return; |
| 74 | 110 | } |
| 75 | 111 | |
| @@ -76,9 +112,9 @@ | ||
| 76 | 112 | syncHiddenInput(); |
| 77 | 113 | }); |
| 78 | 114 | |
| 79 | 115 | const form = group.closest("form"); |
| 80 | - if (form && !form.dataset.increasePriceBound) { | |
| 116 | + if (form && !form.dataset.priceChangeBound) { | |
| 81 | 117 | form.addEventListener("submit", function () { |
| 82 | 118 | const toggles = form.querySelectorAll(".toggle-percentage"); |
| 83 | 119 | let clampedNotice = false; |
| 84 | 120 | |
| @@ -91,16 +127,16 @@ | ||
| 91 | 127 | return; |
| 92 | 128 | } |
| 93 | 129 | |
| 94 | 130 | const toggleInput = |
| 95 | - toggleGroup.querySelector('[data-role="increase-price-input"]') || | |
| 131 | + toggleGroup.querySelector('[data-role="price-change-input"]') || | |
| 96 | 132 | toggleGroup.querySelector( |
| 97 | - 'input.percentage-input[type="text"][name="sync_basalam_settings[increase_price_value]"]' | |
| 133 | + 'input.percentage-input[type="text"][name="sync_basalam_settings[price_change_value]"]' | |
| 98 | 134 | ); |
| 99 | 135 | const toggleHidden = |
| 100 | - toggleGroup.querySelector('[data-role="increase-price-hidden"]') || | |
| 136 | + toggleGroup.querySelector('[data-role="price-change-hidden"]') || | |
| 101 | 137 | toggleGroup.querySelector( |
| 102 | - 'input[type="hidden"][name="sync_basalam_settings[increase_price_value]"]' | |
| 138 | + 'input[type="hidden"][name="sync_basalam_settings[price_change_value]"]' | |
| 103 | 139 | ); |
| 104 | 140 | |
| 105 | 141 | if (!toggleInput || !toggleHidden) { |
| 106 | 142 | return; |
| @@ -106,19 +142,25 @@ | ||
| 106 | 142 | return; |
| 107 | 143 | } |
| 108 | 144 | |
| 109 | 145 | if (toggle.checked) { |
| 110 | - toggleHidden.value = "-1"; | |
| 146 | + toggleHidden.value = COMMISSION_VALUE; | |
| 111 | 147 | return; |
| 112 | 148 | } |
| 113 | 149 | |
| 114 | - const rawValue = toggleInput.value.replace(/[^\d]/g, ""); | |
| 115 | - let numberValue = parseInt(rawValue, 10) || 0; | |
| 150 | + const parsed = parseValue(toggleInput.value); | |
| 116 | 151 | |
| 117 | - if (numberValue > 35 && numberValue <= 100) { | |
| 152 | + if (parsed.empty) { | |
| 153 | + toggleHidden.value = ""; | |
| 154 | + return; | |
| 155 | + } | |
| 156 | + | |
| 157 | + let numberValue = parsed.value; | |
| 158 | + | |
| 159 | + if (isPercent(numberValue) && (numberValue > MAX_PERCENT || numberValue < MIN_PERCENT)) { | |
| 118 | 160 | clampedNotice = true; |
| 119 | - numberValue = 35; | |
| 120 | - toggleInput.value = numberValue.toLocaleString("en-US"); | |
| 161 | + numberValue = numberValue > MAX_PERCENT ? MAX_PERCENT : MIN_PERCENT; | |
| 162 | + toggleInput.value = formatValue(numberValue); | |
| 121 | 163 | const unitEl = toggleGroup.querySelector(".percentage-unit"); |
| 122 | 164 | if (unitEl) { |
| 123 | 165 | unitEl.textContent = "درصد"; |
| 124 | 166 | } |
| @@ -123,18 +165,18 @@ | ||
| 123 | 165 | unitEl.textContent = "درصد"; |
| 124 | 166 | } |
| 125 | 167 | } |
| 126 | 168 | |
| 127 | - toggleHidden.value = rawValue === "" ? "" : String(numberValue); | |
| 169 | + toggleHidden.value = String(numberValue); | |
| 128 | 170 | }); |
| 129 | 171 | |
| 130 | 172 | if (clampedNotice) { |
| 131 | 173 | BasalamToast.info( |
| 132 | - "مقدار افزایش قیمت در حالت درصد نمیتواند بیشتر از ۳۵٪ باشد. مقدار وارد شده به ۳۵ درصد تغییر داده شد و ذخیره میشود." | |
| 174 | + "مقدار تغییر قیمت در حالت درصد نمیتواند بیشتر از ۳۵٪ افزایش یا ۳۵٪ کاهش باشد. مقدار وارد شده به سقف مجاز تغییر داده شد و ذخیره میشود." | |
| 133 | 175 | ); |
| 134 | 176 | } |
| 135 | 177 | }); |
| 136 | 178 | |
| 137 | - form.dataset.increasePriceBound = "1"; | |
| 179 | + form.dataset.priceChangeBound = "1"; | |
| 138 | 180 | } |
| 139 | 181 | }); |
| 140 | 182 | }); |