Commit 020ad03f authored by Tomáš Peterka's avatar Tomáš Peterka

[renderjs_ui] FloatField supports ERP5 percent numbers

parent 701c496e
/*global window, rJS */ /*global window, rJS, Math */
/*jslint indent: 2, maxerr: 3 */ /*jslint indent: 2, maxerr: 3 */
(function (window, rJS) { (function (window, rJS, Math) {
"use strict"; "use strict";
rJS(window) rJS(window)
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
.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 || "", value = field_json.value || field_json.default || "",
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,
...@@ -21,14 +22,34 @@ ...@@ -21,14 +22,34 @@
precision: field_json.precision, precision: field_json.precision,
hidden: field_json.hidden hidden: field_json.hidden
}; };
// if value is 0.0 we assign empty instead - so we fix it here
if (field_json.value !== undefined && field_json.value !== '') {
value = field_json.value;
} 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 (field_json.precision) { if (field_json.precision) {
state_dict.step = 1 / Math.pow(10, field_json.precision); state_dict.step = Math.pow(10, -field_json.precision);
value = parseFloat(value || "0").toFixed(field_json.precision); value = value.toFixed(field_json.precision);
} else { } else {
state_dict.step = 0.00000001; // XXX did previous default step value make sense? 0.00000001
state_dict.step = 1.0;
}
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
value *= 100.0;
state_dict.append = "%";
} }
state_dict.value = value; state_dict.value = value;
state_dict.text_content = value; if (window.isNaN(value)) {
state_dict.text_content = "";
} else {
state_dict.text_content = value.toString();
}
return this.changeState(state_dict); return this.changeState(state_dict);
}) })
...@@ -82,4 +103,4 @@ ...@@ -82,4 +103,4 @@
return true; return true;
}); });
}(window, rJS)); }(window, rJS, Math));
\ No newline at end of file \ 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>956.15742.1027.50705</string> </value> <value> <string>961.41941.4473.55415</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>1482844714.9</float> <float>1503645555.71</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -1106,6 +1106,20 @@ div[data-gadget-scope='header'] .ui-header ul { ...@@ -1106,6 +1106,20 @@ div[data-gadget-scope='header'] .ui-header ul {
} }
} }
/********************************************** /**********************************************
* Gadget: HTML5 input field
**********************************************/
.gadget-content .ui-field-contain .ui-input-has-appendinx,
.gadget-content .ui-field-contain .ui-input-has-prependinx {
display: flex;
}
.gadget-content .ui-field-contain .ui-input-has-appendinx i,
.gadget-content .ui-field-contain .ui-input-has-prependinx i {
display: block;
padding: 3pt;
color: #777777;
font-weight: 400;
}
/**********************************************
* Gadget: relation field * Gadget: relation field
**********************************************/ **********************************************/
.relation-input { .relation-input {
......
...@@ -242,7 +242,7 @@ ...@@ -242,7 +242,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>961.19210.8471.60620</string> </value> <value> <string>961.42445.27813.34781</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -260,7 +260,7 @@ ...@@ -260,7 +260,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1502444264.19</float> <float>1503676812.98</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -6,7 +6,13 @@ ...@@ -6,7 +6,13 @@
rJS(window) rJS(window)
.setState({ .setState({
tag: 'div', tag: 'div',
text_content: '' text_content: '',
inner_html: '',
name: undefined,
src: undefined,
alt: undefined,
append: '',
prepend: ''
}) })
.declareMethod('render', function (options) { .declareMethod('render', function (options) {
...@@ -15,16 +21,27 @@ ...@@ -15,16 +21,27 @@
inner_html: options.inner_html || "", inner_html: options.inner_html || "",
tag: options.tag || 'div', tag: options.tag || 'div',
src: options.src, src: options.src,
alt: options.alt alt: options.alt,
name: options.name,
append: options.append || '',
prepend: options.prepend || ''
}; };
return this.changeState(state_dict); return this.changeState(state_dict);
}) })
.onStateChange(function () { .onStateChange(function () {
var element = this.element, var element = this.element,
new_element = document.createElement(this.state.tag); new_element = document.createElement(this.state.tag),
content = this.state.text_content;
if (this.state.text_content) { if (this.state.text_content) {
new_element.textContent = this.state.text_content; if (this.state.prepend) {
content = this.state.prepend + "&nbsp;" + content;
}
if (this.state.append) {
content = content + "&nbsp;" + this.state.append;
}
new_element.textContent = content;
} else if (this.state.inner_html) { } else if (this.state.inner_html) {
new_element.innerHTML = this.state.inner_html; new_element.innerHTML = this.state.inner_html;
} }
......
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>955.51162.4768.35123</string> </value> <value> <string>961.46335.40182.23005</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>1482843300.9</float> <float>1503909097.14</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -1285,6 +1285,21 @@ div[data-gadget-scope='header'] .ui-header { ...@@ -1285,6 +1285,21 @@ div[data-gadget-scope='header'] .ui-header {
} }
} }
/**********************************************
* Gadget: HTML5 input field
**********************************************/
.gadget-content .ui-field-contain {
.ui-input-has-appendinx,
.ui-input-has-prependinx {
display: flex;
i {
display: block;
padding: 3pt;
color: #777777;
font-weight: 400;
}
}
}
/********************************************** /**********************************************
* Gadget: relation field * Gadget: relation field
**********************************************/ **********************************************/
......
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