mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-04-02 15:11:58 -04:00
Allow doing math in price field for each markups
This commit is contained in:
33
assets/javascripts/blur.js
Normal file
33
assets/javascripts/blur.js
Normal file
@@ -0,0 +1,33 @@
|
||||
function evaluateMathExpression(expr) {
|
||||
if (!expr) return null;
|
||||
|
||||
// allow only digits, decimal, operators, parentheses, spaces
|
||||
if (!/^[0-9+\-*/().\s]+$/.test(expr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return Function('"use strict"; return (' + expr + ')')();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("blur", function(e) {
|
||||
|
||||
if (!e.target.classList.contains("price-field")) return;
|
||||
|
||||
const field = e.target;
|
||||
const value = field.value.trim();
|
||||
|
||||
const result = evaluateMathExpression(value);
|
||||
|
||||
if (result !== null && !isNaN(result)) {
|
||||
field.value = Number(result).toFixed(2);
|
||||
}
|
||||
|
||||
if (typeof updateLineItemTotals === "function") {
|
||||
updateLineItemTotals();
|
||||
}
|
||||
|
||||
}, true);
|
||||
Reference in New Issue
Block a user