mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[MIG] web_widget_float_formula: Migrate to 10.0
This commit is contained in:
committed by
Dave Lasley
parent
abfea0fff7
commit
f19ee39524
@@ -35,9 +35,10 @@ odoo.define('web_widget_float_formula', function(require) {
|
||||
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._super();
|
||||
return this;
|
||||
},
|
||||
|
||||
initialize_content: function() {
|
||||
@@ -52,6 +53,11 @@ odoo.define('web_widget_float_formula', function(require) {
|
||||
},
|
||||
|
||||
_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);
|
||||
@@ -80,7 +86,9 @@ odoo.define('web_widget_float_formula', function(require) {
|
||||
_compute_result: function() {
|
||||
this._clean_formula_text();
|
||||
|
||||
var formula = this._process_formula(this.$el.find('input').val());
|
||||
var input = this.$input.val();
|
||||
|
||||
var formula = this._process_formula(input);
|
||||
if (formula !== false) {
|
||||
var value = this._eval_formula(formula);
|
||||
if (value !== false) {
|
||||
@@ -95,7 +103,7 @@ odoo.define('web_widget_float_formula', function(require) {
|
||||
// Display the formula stored in the field to allow modification
|
||||
_display_formula: function() {
|
||||
if (this._formula_text !== '') {
|
||||
this.$el.find('input').val(this._formula_text);
|
||||
this.$input.val(this._formula_text);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
@@ -11,8 +11,8 @@ odoo.define_section('web_widget_float_formula', ['web.form_common', 'web.form_wi
|
||||
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.$element = $('<input>');
|
||||
self.field.$el.append(self.$element);
|
||||
self.field.$input = $('<input>');
|
||||
self.field.$label = $('<label>');
|
||||
};
|
||||
|
||||
test('Float fields should have a _formula_text property that defaults to an empty string',
|
||||
@@ -98,28 +98,28 @@ odoo.define_section('web_widget_float_formula', ['web.form_common', 'web.form_wi
|
||||
test('._compute_result() should not change the value of the associated input when it is not a valid formula',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.$element.val('=2*3a');
|
||||
this.field.$input.val('=2*3a');
|
||||
this.field._compute_result();
|
||||
|
||||
assert.strictEqual(this.$element.val(), '=2*3a');
|
||||
assert.strictEqual(this.field.$input.val(), '=2*3a');
|
||||
});
|
||||
|
||||
test('._compute_result() should not change the value of the associated input when it cannot be evaled',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.$element.val('=*/');
|
||||
this.field.$input.val('=*/');
|
||||
this.field._compute_result();
|
||||
|
||||
assert.strictEqual(this.$element.val(), '=*/');
|
||||
assert.strictEqual(this.field.$input.val(), '=*/');
|
||||
});
|
||||
|
||||
test('._compute_result() should behave properly when the current value of the input element is a valid formula',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.$element.val('=2*3');
|
||||
this.field.$input.val('=2*3');
|
||||
this.field._compute_result();
|
||||
|
||||
assert.equal(this.$element.val(), '6');
|
||||
assert.equal(this.field.$input.val(), '6');
|
||||
assert.strictEqual(this.field._formula_text, '=2*3');
|
||||
});
|
||||
|
||||
@@ -129,7 +129,7 @@ odoo.define_section('web_widget_float_formula', ['web.form_common', 'web.form_wi
|
||||
this.field._formula_text = "test";
|
||||
this.field._display_formula();
|
||||
|
||||
assert.equal(this.$element.val(), 'test');
|
||||
assert.equal(this.field.$input.val(), 'test');
|
||||
});
|
||||
|
||||
test('.start() on float fields should add a handler that calls ._compute_result() when the field is blurred',
|
||||
|
||||
Reference in New Issue
Block a user