1
/*******************************************************************************
2
See __openerp__.py file for Copyright and Licence Informations.
3
*******************************************************************************/
5
openerp.web_widget_float_formula = function (instance) {
7
instance.web.FormView = instance.web.FormView.extend({
8
/***********************************************************************
10
***********************************************************************/
13
* Overload : '_process_save' function
14
1: to force computation of formula if the user realize a keydown directly after the formula input in a tree view ;
15
2: to clean up the '_formula_text' value in all case to avoid bugs in tree view ;
17
_process_save: function(save_obj) {
18
for (var f in this.fields) {
19
if (!this.fields.hasOwnProperty(f)) { continue; }
21
if (f.hasOwnProperty('_formula_text')){
22
currentval = f.$('input').attr('value')
23
if (typeof currentval != 'undefined'){
24
formula = f._get_valid_expression(currentval);
29
f._clean_formula_text();
32
return this._super(save_obj);
37
instance.web.form.FieldFloat = instance.web.form.FieldFloat.extend({
38
/***********************************************************************
40
***********************************************************************/
43
* Overload : 'start' function to catch 'blur' and 'focus' events.
46
this.on("blurred", this, this._compute_result);
47
this.on("focused", this, this._display_formula);
52
* Overload : 'initialize_content' function to clean '_formula_text' value.
54
initialize_content: function() {
55
this._clean_formula_text();
59
/***********************************************************************
61
***********************************************************************/
64
* keep in memory the formula to allow user to edit it again.
65
The formula has to be keeped in memory until a 'save' action.
70
* Clean '_formula_text' value.
72
_clean_formula_text: function() {
73
this._formula_text = '';
77
* Return a valid formula from a val, if possible.
78
Otherwise, return false.
80
_get_valid_expression: function(val) {
82
currenttxt = val.toString().replace(/^\s+|\s+$/g, '');
83
// Test if the value is a formula
84
if (currenttxt[0] == '=') {
85
// allowed chars : [0-9] .,+-/*() and spaces
86
myreg = RegExp('[0-9]|\\s|\\.|,|\\(|\\)|\\+|\\-|\\*|\\/','g')
87
// Test to avoid code injonction in eval function.
88
if (currenttxt.substring(1).replace(myreg, '') == ''){
91
formula = currenttxt.substring(1).replace(/,/g,'.');
92
var floatval = eval(formula);
94
if (typeof (floatval) != 'undefined'){
103
* test if the content of the field is a valid formula,
104
* compute the result, and replace the current value by the final result.
106
_compute_result: function() {
109
this._formula_text = '';
111
formula = this._get_valid_expression(this.$el.find('input').attr('value'));
114
this._formula_text = "=" + formula;
115
// put the result in the field
116
this.set_value(eval(formula));
117
// Force rendering anyway to avoid format loss if no change
123
* Display the stored formula in the field, to allow modification.
125
_display_formula: function() {
126
if (this._formula_text != ''){
127
this.$el.find('input').val(this._formula_text);