From 39f504be23c97175231e44cf10b08bb962c3b5fe Mon Sep 17 00:00:00 2001 From: hasnain2808 Date: Thu, 10 Jun 2021 13:51:29 +0530 Subject: [PATCH 1/9] fix: number format converting to decimals --- frappe/public/js/frappe/form/controls/int.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frappe/public/js/frappe/form/controls/int.js b/frappe/public/js/frappe/form/controls/int.js index 12652bf86e..1aa4a49b9a 100644 --- a/frappe/public/js/frappe/form/controls/int.js +++ b/frappe/public/js/frappe/form/controls/int.js @@ -10,12 +10,11 @@ frappe.ui.form.ControlInt = class ControlInt extends frappe.ui.form.ControlData super.make_input(); this.$input // .addClass("text-right") - .on("focus", function () { + .on("focusout", function () { setTimeout(function () { if (!document.activeElement) return; document.activeElement.value = me.validate(document.activeElement.value); - document.activeElement.select(); }, 100); return false; }); From 52941e55b105a64da970de33b7c340422540fbab Mon Sep 17 00:00:00 2001 From: hasnain2808 Date: Tue, 27 Jul 2021 00:29:01 +0530 Subject: [PATCH 2/9] fix: no need for focusout in favour of onchange --- frappe/public/js/frappe/form/controls/int.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/frappe/public/js/frappe/form/controls/int.js b/frappe/public/js/frappe/form/controls/int.js index 1aa4a49b9a..ce2336bb82 100644 --- a/frappe/public/js/frappe/form/controls/int.js +++ b/frappe/public/js/frappe/form/controls/int.js @@ -8,16 +8,6 @@ frappe.ui.form.ControlInt = class ControlInt extends frappe.ui.form.ControlData make_input () { var me = this; super.make_input(); - this.$input - // .addClass("text-right") - .on("focusout", function () { - setTimeout(function () { - if (!document.activeElement) return; - document.activeElement.value - = me.validate(document.activeElement.value); - }, 100); - return false; - }); } validate (value) { return this.parse(value); From 9eff8bee21de05ac23e92457353f005ccb2207d1 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Wed, 8 Sep 2021 15:02:56 +0530 Subject: [PATCH 3/9] test: tests for float control --- cypress/integration/control_float.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 cypress/integration/control_float.js diff --git a/cypress/integration/control_float.js b/cypress/integration/control_float.js new file mode 100644 index 0000000000..25b1d65523 --- /dev/null +++ b/cypress/integration/control_float.js @@ -0,0 +1,28 @@ +context('Control Float', () => { + before(() => { + cy.login(); + cy.visit('/app/website'); + cy.window().its('frappe').then(frappe => { + frappe.boot.sysdefaults.number_format = '#.###,##' + }); + }); + + function get_dialog_with_float() { + return cy.dialog({ + title: 'Float Check', + fields: [{ + 'fieldname': 'float_number', + 'fieldtype': 'Float', + 'Label': 'Float', + }] + }); + } + + it('check value changes', () => { + get_dialog_with_float().as('dialog'); + cy.get_field('float_number', 'Float').clear() + cy.fill_field('float_number', '36487,334', 'Float').blur(); + cy.get_field('float_number', 'Float').should('have.value', '36.487,334'); + + }); +}); From 8aea1254f9023d013fab81bc1f29ce2347a95a7c Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Wed, 8 Sep 2021 15:15:47 +0530 Subject: [PATCH 4/9] chore: add semi colon --- cypress/integration/control_float.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cypress/integration/control_float.js b/cypress/integration/control_float.js index 25b1d65523..45edff0346 100644 --- a/cypress/integration/control_float.js +++ b/cypress/integration/control_float.js @@ -3,7 +3,7 @@ context('Control Float', () => { cy.login(); cy.visit('/app/website'); cy.window().its('frappe').then(frappe => { - frappe.boot.sysdefaults.number_format = '#.###,##' + frappe.boot.sysdefaults.number_format = '#.###,##'; }); }); From 9dc8f84aedfe33f8e1de76f58b9089c89b19582d Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 9 Sep 2021 14:26:40 +0530 Subject: [PATCH 5/9] test: tests for control: float --- cypress/integration/control_float.js | 97 +++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 16 deletions(-) diff --git a/cypress/integration/control_float.js b/cypress/integration/control_float.js index 45edff0346..670d1fe73e 100644 --- a/cypress/integration/control_float.js +++ b/cypress/integration/control_float.js @@ -1,28 +1,93 @@ -context('Control Float', () => { +context("Control Float", () => { before(() => { cy.login(); - cy.visit('/app/website'); - cy.window().its('frappe').then(frappe => { - frappe.boot.sysdefaults.number_format = '#.###,##'; - }); + cy.visit("/app/website"); }); function get_dialog_with_float() { return cy.dialog({ - title: 'Float Check', - fields: [{ - 'fieldname': 'float_number', - 'fieldtype': 'Float', - 'Label': 'Float', - }] + title: "Float Check", + fields: [ + { + fieldname: "float_number", + fieldtype: "Float", + Label: "Float" + } + ] }); } - it('check value changes', () => { - get_dialog_with_float().as('dialog'); - cy.get_field('float_number', 'Float').clear() - cy.fill_field('float_number', '36487,334', 'Float').blur(); - cy.get_field('float_number', 'Float').should('have.value', '36.487,334'); + it("check value changes", () => { + get_dialog_with_float().as("dialog"); + + let data = get_data(); + data.forEach(x => { + cy.window() + .its("frappe") + .then(frappe => { + frappe.boot.sysdefaults.number_format = x.number_format; + }); + x.values.forEach(d => { + cy.get_field("float_number", "Float").clear(); + cy.fill_field("float_number", d.input, "Float").blur(); + cy.get_field("float_number", "Float").should( + "have.value", + d.blur_expected + ); + cy.get_field("float_number", "Float").focus(); + cy.get_field("float_number", "Float").blur(); + cy.get_field("float_number", "Float").focus(); + cy.get_field("float_number", "Float").should( + "have.value", + d.focus_expected + ); + }); + }); }); + + function get_data() { + return [ + { + number_format: "#.###,##", + values: [ + { + input: "364.87,334", + blur_expected: "36.487,334", + focus_expected: "36487.334" + }, + { + input: "36487,334", + blur_expected: "36.487,334", + focus_expected: "36487.334" + }, + { + input: "100", + blur_expected: "100,000", + focus_expected: "100" + } + ] + }, + { + number_format: "#,###.##", + values: [ + { + input: "364,87.334", + blur_expected: "36,487.334", + focus_expected: "36487.334" + }, + { + input: "36487.334", + blur_expected: "36,487.334", + focus_expected: "36487.334" + }, + { + input: "100", + blur_expected: "100.000", + focus_expected: "100" + } + ] + } + ]; + } }); From 21a4331be45ab32a2b6dd12c7d45b5f7d612c60b Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 9 Sep 2021 14:27:23 +0530 Subject: [PATCH 6/9] revert: removal of flt on focus event --- frappe/public/js/frappe/form/controls/int.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/frappe/public/js/frappe/form/controls/int.js b/frappe/public/js/frappe/form/controls/int.js index ce2336bb82..12652bf86e 100644 --- a/frappe/public/js/frappe/form/controls/int.js +++ b/frappe/public/js/frappe/form/controls/int.js @@ -8,6 +8,17 @@ frappe.ui.form.ControlInt = class ControlInt extends frappe.ui.form.ControlData make_input () { var me = this; super.make_input(); + this.$input + // .addClass("text-right") + .on("focus", function () { + setTimeout(function () { + if (!document.activeElement) return; + document.activeElement.value + = me.validate(document.activeElement.value); + document.activeElement.select(); + }, 100); + return false; + }); } validate (value) { return this.parse(value); From f29138d6f853233913bf675655a743f5b642f5ba Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 9 Sep 2021 14:28:17 +0530 Subject: [PATCH 7/9] fix: convert back to number_format on change --- frappe/public/js/frappe/form/controls/float.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/frappe/public/js/frappe/form/controls/float.js b/frappe/public/js/frappe/form/controls/float.js index 89f8f23cc5..b81a988608 100644 --- a/frappe/public/js/frappe/form/controls/float.js +++ b/frappe/public/js/frappe/form/controls/float.js @@ -1,4 +1,17 @@ frappe.ui.form.ControlFloat = class ControlFloat extends frappe.ui.form.ControlInt { + + make_input() { + super.make_input() + const change_handler = e => { + if (this.change) this.change(e); + else { + let value = this.get_input_value(); + this.parse_validate_and_set_in_model(value, e); + } + }; + // convert to number format on focusout since focus converts it to flt. + this.$input.on("focusout", change_handler); + } parse(value) { value = this.eval_expression(value); return isNaN(parseFloat(value)) ? null : flt(value, this.get_precision()); @@ -25,3 +38,5 @@ frappe.ui.form.ControlFloat = class ControlFloat extends frappe.ui.form.ControlI }; frappe.ui.form.ControlPercent = frappe.ui.form.ControlFloat; + +34.4534,45 \ No newline at end of file From 4d0b0d35efa64a1692e075b71ae70c4969b81cda Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 9 Sep 2021 14:29:00 +0530 Subject: [PATCH 8/9] fix: make flt value idempotent --- frappe/public/js/frappe/utils/number_format.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/frappe/public/js/frappe/utils/number_format.js b/frappe/public/js/frappe/utils/number_format.js index 1c39f42ec5..5f60a5a6d8 100644 --- a/frappe/public/js/frappe/utils/number_format.js +++ b/frappe/public/js/frappe/utils/number_format.js @@ -8,7 +8,12 @@ if (!window.frappe) window.frappe = {}; function flt(v, decimals, number_format) { if (v == null || v == '') return 0; - if (typeof v !== "number") { + if (! (typeof v === "number" || String(parseFloat(v)) == v)){ + // cases in which this block should not run + // 1. 'v' is already a number + // 2. v is already parsed but in string form + // if (typeof v !== "number") { + v = v + ""; // strip currency symbol if exists @@ -25,6 +30,7 @@ function flt(v, decimals, number_format) { v = 0; } + v = parseFloat(v); if (decimals != null) return _round(v, decimals); return v; From 906c70b2456f681a6afaa7b56ea84695f364f824 Mon Sep 17 00:00:00 2001 From: Mohammad Hasnain Date: Thu, 9 Sep 2021 14:35:04 +0530 Subject: [PATCH 9/9] chore: sider --- frappe/public/js/frappe/form/controls/float.js | 4 +--- frappe/public/js/frappe/utils/number_format.js | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/frappe/public/js/frappe/form/controls/float.js b/frappe/public/js/frappe/form/controls/float.js index b81a988608..e00f74238c 100644 --- a/frappe/public/js/frappe/form/controls/float.js +++ b/frappe/public/js/frappe/form/controls/float.js @@ -1,7 +1,7 @@ frappe.ui.form.ControlFloat = class ControlFloat extends frappe.ui.form.ControlInt { make_input() { - super.make_input() + super.make_input(); const change_handler = e => { if (this.change) this.change(e); else { @@ -38,5 +38,3 @@ frappe.ui.form.ControlFloat = class ControlFloat extends frappe.ui.form.ControlI }; frappe.ui.form.ControlPercent = frappe.ui.form.ControlFloat; - -34.4534,45 \ No newline at end of file diff --git a/frappe/public/js/frappe/utils/number_format.js b/frappe/public/js/frappe/utils/number_format.js index 5f60a5a6d8..32e3669caf 100644 --- a/frappe/public/js/frappe/utils/number_format.js +++ b/frappe/public/js/frappe/utils/number_format.js @@ -8,7 +8,7 @@ if (!window.frappe) window.frappe = {}; function flt(v, decimals, number_format) { if (v == null || v == '') return 0; - if (! (typeof v === "number" || String(parseFloat(v)) == v)){ + if (!(typeof v === "number" || String(parseFloat(v)) == v)) { // cases in which this block should not run // 1. 'v' is already a number // 2. v is already parsed but in string form