Commit 819f77ec authored by Tomáš Peterka's avatar Tomáš Peterka Committed by Tomáš Peterka

[renderjs_ui] Refactor FloatField

-  Remove field_json.value because that one is never send by ERP5 backend
-  Set comprehensive initial state and avoid sneaking state variables afterwards
-  Handle better NaNs which represent empty numerical value
-  Refactor for shorter and simpler code
-  Rename "percents" -> "percentage" according to coding style guidelines
parent fdc408cb
...@@ -5,49 +5,51 @@ ...@@ -5,49 +5,51 @@
rJS(window) rJS(window)
.setState({ .setState({
tag: 'p', type: "number",
step: 1, // `step` is used for browser-level validation thus a mandatory value
type: "number" // HTML5 default is 1.0 which is not feasible most of the time thus we
// default to over-sufficiently small value
step: 0.00000001,
required: false,
editable: true,
hidden: false,
name: undefined,
title: undefined,
value: undefined,
text_content: undefined,
// `append` is a string to display next to the field (%, currency...)
append: undefined
}) })
.declareMethod('render', function (options) { .declareMethod('render', function (options) {
var field_json = options.field_json || {}, var field_json = options.field_json || {},
value = field_json.value || field_json.default || "", percentage = (field_json.input_style || "").endsWith("%"),
percents = (field_json.input_style || "").endsWith("%"),
state_dict = { state_dict = {
editable: field_json.editable, editable: field_json.editable,
required: field_json.required, required: field_json.required,
name: field_json.key, name: field_json.key,
title: field_json.title, title: field_json.title,
precision: field_json.precision, precision: window.parseFloat(field_json.precision),
hidden: field_json.hidden hidden: field_json.hidden,
// erp5 always put value into "default"
value: window.parseFloat(field_json.default)
}; };
// if value is 0.0 we assign empty instead - so we fix it here if (!window.isNaN(state_dict.precision)) {
if (field_json.value !== undefined && field_json.value !== '') { state_dict.step = Math.pow(10, -state_dict.precision);
value = field_json.value; state_dict.value = state_dict.value.toFixed(state_dict.precision);
} else if (field_json.default !== undefined && field_json.default !== '') {
value = field_json.default;
} }
value = window.parseFloat(value); // at this step we finished joggling with value if (percentage) {
// ERP5 always devides the value by 100 if it is set to pe percentages
if (field_json.precision) {
state_dict.step = Math.pow(10, -field_json.precision);
value = value.toFixed(field_json.precision);
} else {
state_dict.step = 0.00000001;
}
if (percents) {
// ERP5 always devides the value by 10 if it is set to pe percentages
// thus we have to mitigate that in javascript here // thus we have to mitigate that in javascript here
value *= 100.0; state_dict.value *= 100.0;
state_dict.append = "%"; state_dict.append = "%";
} }
state_dict.value = value;
if (window.isNaN(value)) { if (window.isNaN(state_dict.value)) {
state_dict.text_content = ""; state_dict.text_content = ""; // show empty value insted of ugly "NaN"
} else { } else {
state_dict.text_content = value.toString(); state_dict.text_content = state_dict.value.toString();
} }
return this.changeState(state_dict); return this.changeState(state_dict);
}) })
...@@ -102,4 +104,4 @@ ...@@ -102,4 +104,4 @@
return true; return true;
}); });
}(window, rJS, Math)); }(window, rJS, Math));
\ No newline at end of file
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>961.41941.4473.55415</string> </value> <value> <string>962.12584.16558.54920</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1503645555.71</float> <float>1505816402.11</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment