IDEMPIERE-4468 Pass the current value of numberbox to the textfield of calculator (#385)

IDEMPIERE-2003 Capturing wrong numbers with numeric keypad when decimal separator is not dot

- Simplify the code for IDEMPIERE-4468 using Clients.evalJavaScript
- Fix IDEMPIERE-4468 to work in sync with IDEMPIERE-2003
- Improve IDEMPIERE-4468 to set the cursor at the end of the passed value
- Improve IDEMPIERE-2003 to insert the comma separator where the cursor is
- Simplify the code for IDEMPIERE-2003, there was a code calling setWidgetListener onKeyDown and doKeyPress_
  Probably required for old browsers, tested without that code with actual chromium and firefox and both worked perfect
This commit is contained in:
Carlos Ruiz 2020-11-18 04:49:48 +01:00 committed by GitHub
parent ca3da22f7b
commit c954a055b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 52 deletions

View File

@ -17,8 +17,6 @@
package org.adempiere.webui.component;
import java.io.IOException;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
@ -30,8 +28,6 @@ import org.adempiere.webui.util.ZKUpdateUtil;
import org.compiere.model.MSysConfig;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.zkoss.zk.au.out.AuOuter;
import org.zkoss.zk.ui.HtmlBasedComponent;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
@ -95,35 +91,6 @@ public class NumberBox extends Div
decimalBox.setSclass("editor-input");
decimalBox.setId(decimalBox.getUuid());
char separatorChar = DisplayType.getNumberFormat(DisplayType.Number, null).getDecimalFormatSymbols().getDecimalSeparator();
String separator = Character.toString(separatorChar);
boolean processDotKeypad = MSysConfig.getBooleanValue(MSysConfig.ZK_DECIMALBOX_PROCESS_DOTKEYPAD, true, Env.getAD_Client_ID(Env.getCtx()));
if (processDotKeypad) {
StringBuffer funct = new StringBuffer();
funct.append("function(evt)");
funct.append("{");
// ignore dot, comma and decimal separator and process them on key down
funct.append(" if (!this._shallIgnore(evt, '0123456789-%'))");
funct.append(" {");
funct.append(" this.$doKeyPress_(evt);");
funct.append(" }");
funct.append("}");
decimalBox.setWidgetOverride("doKeyPress_", funct.toString());
funct = new StringBuffer();
// debug // funct.append("console.log('keyCode='+event.keyCode);");
funct.append("if (window.event)");
funct.append(" key = event.keyCode;");
funct.append("else");
funct.append(" key = event.which;");
funct.append("if (key == 108 || key == 110 || key == 188 || key == 190 || key == 194) {");
funct.append(" var id = '$'.concat('").append(decimalBox.getId()).append("');");
funct.append(" var calcText = jq(id)[0];");
funct.append(" calcText.value += '").append(separator).append("';");
funct.append(" event.stop;");
funct.append("};");
decimalBox.setWidgetListener("onKeyDown", funct.toString());
}
appendChild(decimalBox);
btn = new Button();
@ -137,24 +104,18 @@ public class NumberBox extends Div
@Override
public void onEvent(Event event) throws Exception {
if (btn.getPopup() != null) {
String uid = btn.getPopup();
if (uid.startsWith("uuid("))
uid = uid.substring(5, uid.length()-1);
HtmlBasedComponent comp = (HtmlBasedComponent) btn.getDesktop().getComponentByUuidIfAny(uid);
if (comp != null) {
Textbox ctbox = (Textbox) comp.getLastChild().getFirstChild();
if (ctbox != null && decimalBox.getValue() != null) {
ctbox.setText(decimalBox.getValue().toString());
StringWriter writer = new StringWriter(1024);
try {
ctbox.redraw(writer);
Clients.response(new AuOuter(ctbox, writer.toString()));
} catch (IOException e) {
e.printStackTrace();
String curValue = "";
if (decimalBox.getValue() != null) {
curValue = decimalBox.getValue().toString();
boolean processDotKeypad = MSysConfig.getBooleanValue(MSysConfig.ZK_DECIMALBOX_PROCESS_DOTKEYPAD, true, Env.getAD_Client_ID(Env.getCtx()));
if (processDotKeypad) {
char separatorChar = DisplayType.getNumberFormat(DisplayType.Number, null).getDecimalFormatSymbols().getDecimalSeparator();
String separator = Character.toString(separatorChar);
curValue = curValue.replace(".", separator);
}
}
comp.focus();
}
String txtCalcId = txtCalc.getId();
Clients.evalJavaScript("calc.append('" + txtCalcId + "', '" + curValue + "')");
}
}
});
@ -285,7 +246,7 @@ public class NumberBox extends Div
} else {
// restrict allowed characters
String decimalSep = separator;
if (!processDotKeypad && !".".equals(separator))
if (!".".equals(separator))
decimalSep += ".";
funct.append(" if (!this._shallIgnore(evt, '= -/()*%+0123456789").append(decimalSep).append("'))");
}

View File

@ -6,6 +6,7 @@ function Calc()
this.clearAll = clearAll;
this.evaluate = evaluate;
this.append = append;
this.appendOnCursor = appendOnCursor;
function validateDown(displayTextId, calcTextId, integral, separatorKey, e, processDotKeypad)
{
@ -21,7 +22,7 @@ function Calc()
}
else if (processDotKeypad && (key == 108 || key == 110 || key == 188 || key == 190 || key == 194))
{
append(calcTextId, String.fromCharCode(separatorKey));
appendOnCursor(calcTextId, String.fromCharCode(separatorKey));
e.stop;
}
}
@ -104,6 +105,17 @@ function Calc()
var id = "$".concat(calcTextId);
var calcText = jq(id)[0];
calcText.value += val;
calcText.focus();
}
function appendOnCursor(calcTextId, val)
{
var id = "$".concat(calcTextId);
var calcText = jq(id)[0];
var position = calcText.selectionStart;
var newValue = calcText.value.substring(0, position) + val + calcText.value.substring(position);
calcText.value = newValue;
calcText.setSelectionRange(position+1, position+1);
}
}