From d06ef1fb4bd50e88f9cacbcb3e7013ae795d495a Mon Sep 17 00:00:00 2001 From: "Minh H.G" Date: Sat, 14 Oct 2017 19:49:04 +0800 Subject: [PATCH] [MIG] web_widget_float_formula: Migrate to 11.0 --- web_widget_float_formula/__manifest__.py | 2 +- .../static/src/js/web_widget_float_formula.js | 178 ++++++------- .../tests/js/test_web_widget_float_formula.js | 243 +++++++----------- web_widget_float_formula/tests/test_js.py | 4 +- 4 files changed, 180 insertions(+), 247 deletions(-) diff --git a/web_widget_float_formula/__manifest__.py b/web_widget_float_formula/__manifest__.py index 888b1be3..d759aab8 100644 --- a/web_widget_float_formula/__manifest__.py +++ b/web_widget_float_formula/__manifest__.py @@ -6,7 +6,7 @@ { 'name': 'Web Widget - Formulas in Float Fields', 'summary': 'Allow use of simple formulas in float fields', - 'version': '10.0.1.0.0', + 'version': '11.0.1.0.0', 'category': 'Web', 'author': 'GRAP, LasLabs, Odoo Community Association (OCA)', 'website': 'http://www.grap.coop', diff --git a/web_widget_float_formula/static/src/js/web_widget_float_formula.js b/web_widget_float_formula/static/src/js/web_widget_float_formula.js index 065d5acf..7fee7c6e 100644 --- a/web_widget_float_formula/static/src/js/web_widget_float_formula.js +++ b/web_widget_float_formula/static/src/js/web_widget_float_formula.js @@ -7,105 +7,97 @@ odoo.define('web_widget_float_formula', function(require) { "use strict"; - var form_view = require('web.FormView'); - form_view.include({ - // Ensure that formula is computed even if user saves right away and - // clean up '_formula_text' value to avoid bugs in tree view - _process_save: function(save_obj) { - for (var f in this.fields) { - if (!this.fields.hasOwnProperty(f)) { continue; } - f = this.fields[f]; - if (f.hasOwnProperty('_formula_text') && f.$el.find('input').length > 0) { - f._compute_result(); - f._clean_formula_text(); - } - } + var core = require('web.core'); + var basic_fields = require('web.basic_fields'); + var FieldFloat = basic_fields.FieldFloat; + FieldFloat.include({ + start: function() { + this._super(); + this.on('blurred', this, this._compute_result); + this.on('focused', this, this._display_formula); - return this._super(save_obj); + this.setUpFocus(); }, - }); - var core = require('web.core'); - core.bus.on('web_client_ready', null, function () { - // Import localization values used to eval formula - var translation_params = core._t.database.parameters; - var decimal_point = translation_params.decimal_point; - var thousands_sep = translation_params.thousands_sep; - - var field_float = require('web.form_widgets').FieldFloat; - field_float.include({ - start: function() { - this._super(); - this.on('blurred', this, this._compute_result); - this.on('focused', this, this._display_formula); - return this; - }, - - initialize_content: function() { - this._clean_formula_text(); - return this._super(); - }, - - _formula_text: '', - - _clean_formula_text: function() { - this._formula_text = ''; - }, - - _process_formula: function(formula) { - try{ - formula = formula.toString(); - } catch (ex) { - return false; - } - var clean_formula = formula.toString().replace(/^\s+|\s+$/g, ''); - if (clean_formula[0] == '=') { - clean_formula = clean_formula.substring(1); - var myreg = new RegExp('[0-9]|\\s|\\.|,|\\(|\\)|\\+|\\-|\\*|\\/', 'g'); - if (clean_formula.replace(myreg, '') === '') { - return clean_formula; - } - } - return false; - }, - - _eval_formula: function(formula) { - var value; - formula = formula.replace(thousands_sep, '').replace(decimal_point, '.'); - try { - value = eval(formula); - } - catch(e) {} - - if (typeof value != 'undefined') { - return value; - } + setUpFocus: function() { + var self = this; + this.$el.on({ + focus: function() { self.trigger('focused'); }, + blur: function() { self.trigger('blurred'); } + }); + }, + + commitChanges: function() { + this._compute_result(); + }, + + _formula_text: '', + + _clean_formula_text: function() { + this._formula_text = ''; + }, + + _process_formula: function(formula) { + try{ + formula = formula.toString(); + } catch (ex) { return false; - }, - - _compute_result: function() { - this._clean_formula_text(); - - var input = this.$input.val(); - - var formula = this._process_formula(input); - if (formula !== false) { - var value = this._eval_formula(formula); - if (value !== false) { - this._formula_text = "=" + formula; - this.set_value(value); - // Force rendering to avoid format loss if there's no change - this.render_value(); - } + } + var clean_formula = formula.toString().replace(/^\s+|\s+$/g, ''); + if (clean_formula[0] == '=') { + clean_formula = clean_formula.substring(1); + var myreg = new RegExp('[0-9]|\\s|\\.|,|\\(|\\)|\\+|\\-|\\*|\\/', 'g'); + if (clean_formula.replace(myreg, '') === '') { + return clean_formula; } - }, + } + return false; + }, + + _eval_formula: function(formula) { + // Import localization values used to eval formula + var translation_params = core._t.database.parameters; + var decimal_point = translation_params.decimal_point; + var thousands_sep = translation_params.thousands_sep; + + var value; + formula = formula.replace(thousands_sep, '').replace(decimal_point, '.'); + try { + value = eval(formula); + } + catch(e) {} + + if (typeof value != 'undefined') { + return value; + } + return false; + }, + + _compute_result: function() { + this._clean_formula_text(); - // Display the formula stored in the field to allow modification - _display_formula: function() { - if (this._formula_text !== '') { - this.$input.val(this._formula_text); + var input = this.$input.val(); + + var formula = this._process_formula(input); + if (formula !== false) { + var value = this._eval_formula(formula); + if (value !== false) { + this._formula_text = "=" + formula; + + var decimal_point = core._t.database.parameters.decimal_point; + + // _setValue + this._setValue(value.toString().replace(decimal_point, '.')) + this._render(); } - }, - }); + } + }, + + // Display the formula stored in the field to allow modification + _display_formula: function() { + if (this._formula_text !== '') { + this.$input.val(this._formula_text); + } + }, }); }); diff --git a/web_widget_float_formula/static/tests/js/test_web_widget_float_formula.js b/web_widget_float_formula/static/tests/js/test_web_widget_float_formula.js index 1d7b194d..09f69b95 100644 --- a/web_widget_float_formula/static/tests/js/test_web_widget_float_formula.js +++ b/web_widget_float_formula/static/tests/js/test_web_widget_float_formula.js @@ -3,159 +3,100 @@ * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). **/ -odoo.define_section('web_widget_float_formula', ['web.form_common', 'web.form_widgets', 'web.core'], function(test) { - 'use strict'; - - window.test_setup = function(self, form_common, form_widgets, core) { - core.bus.trigger('web_client_ready'); - var field_manager = new form_common.DefaultFieldManager(null, {}); - var filler = {'attrs': {}}; // Needed to instantiate FieldFloat - self.field = new form_widgets.FieldFloat(field_manager, filler); - self.field.$input = $(''); - self.field.$label = $('