IDEMPIERE-4783 Changing the way number field with calculator are handled (#678)

* Fix not filling the box when the initial value is zero
* Fix removing  the leading zeroes to avoid octal calculations
This commit is contained in:
Carlos Ruiz 2021-05-09 10:23:21 +02:00 committed by GitHub
parent 3211c449cb
commit 759473f6ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 5 deletions

View File

@ -48,7 +48,7 @@ Copyright (C) 2007 Ashley G Ramdass (ADempiere WebUI).
<javascript-module name="jawwa.atmosphere" version="202102091500"/>
<javascript-module name="adempiere.local.storage" version="202011151100"/>
<javascript-moudle name="html2canvas" version="1.0.0.rc7"/>
<javascript-module name="org.idempiere.commons" version="202012030330"/>
<javascript-module name="org.idempiere.commons" version="202105072207"/>
<javascript-module name="jquery.maskedinput" version="1.4.1" />
<javascript-module name="photobooth" version="0.7-rsd3" />
<javascript-module name="chosenbox" version="202012041500"/>

View File

@ -137,6 +137,8 @@ public class NumberBox extends Div
@Override
public void onEvent(Event event) throws Exception {
if (btn.getPopup() != null) {
// Fill the calculator with the actual value of the field
// TODO: this could be made a user preference
String curValue = "";
if (decimalBox.getValue() != null) {
curValue = decimalBox.getValue().toString();
@ -146,6 +148,9 @@ public class NumberBox extends Div
String separator = Character.toString(separatorChar);
curValue = curValue.replace(".", separator);
}
if ("0".equals(curValue)) {
curValue = "";
}
}
String txtCalcId = txtCalc.getId();
Clients.evalJavaScript("calc.append('" + txtCalcId + "', '" + curValue + "')");

View File

@ -73,10 +73,14 @@ function Calc()
var re = new RegExp("[" + separator + "]", "g");
value = value.replace(re,'.');
}
var reclean = new RegExp("[^1234567890+-/*%() ]", "g"); // sanitize
value = value.replace(reclean,'');
var reperc = new RegExp("[%]", "g"); // percentage
value = value.replace(reperc,'/100 ');
value = value
.replace(/[^1234567890+-/*%() ]/g, '') // sanitize
.replace(/[%]/g, '/100 ') // percentage
// now replace leading zeroes
.replace(/\b0+\b/g, 'z') // replace bare zeros with sentinel
.replace(/[1-9\.]0+/g, m => m.replace(/0/g, 'z')) // save these too
.replace(/0/g, '') // throw away the rest of the zeros
.replace(/z/g, '0'); // turn sentinels back to zeros
newValue = value;
var result = "" + eval(value);
if (separator != '.')