~therp-nl/web-addons/7.0-add_percentage_widget

« back to all changes in this revision

Viewing changes to web_widget_float_formula/static/src/js/models.js

[ADD] web_widget_float_formula: improve float field behaviour.
In all the numeric field (float, integer, etc.) the user can tip a formula like in Excel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
See __openerp__.py file for Copyright and Licence Informations.
 
3
*******************************************************************************/
 
4
 
 
5
openerp.web_widget_float_formula = function (instance) {
 
6
 
 
7
    instance.web.FormView = instance.web.FormView.extend({
 
8
        /***********************************************************************
 
9
        Overload section 
 
10
        ***********************************************************************/
 
11
 
 
12
        /**
 
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 ;
 
16
         */
 
17
        _process_save: function(save_obj) {
 
18
            for (var f in this.fields) {
 
19
                if (!this.fields.hasOwnProperty(f)) { continue; }
 
20
                f = this.fields[f];
 
21
                if (f.hasOwnProperty('_formula_text')){
 
22
                    currentval = f.$('input').attr('value')
 
23
                    if (typeof currentval != 'undefined'){
 
24
                        formula = f._get_valid_expression(currentval);
 
25
                        if (formula){
 
26
                            f._compute_result();
 
27
                        }
 
28
                    }
 
29
                    f._clean_formula_text();
 
30
                }
 
31
            }
 
32
            return this._super(save_obj);
 
33
        },
 
34
 
 
35
    });
 
36
 
 
37
    instance.web.form.FieldFloat = instance.web.form.FieldFloat.extend({
 
38
        /***********************************************************************
 
39
        Overload section 
 
40
        ***********************************************************************/
 
41
 
 
42
        /**
 
43
         * Overload : 'start' function to catch 'blur' and 'focus' events.
 
44
         */
 
45
        start: function() {
 
46
            this.on("blurred", this, this._compute_result);
 
47
            this.on("focused", this, this._display_formula);
 
48
            return this._super();
 
49
        },
 
50
 
 
51
        /**
 
52
         * Overload : 'initialize_content' function to clean '_formula_text' value.
 
53
         */
 
54
        initialize_content: function() {
 
55
            this._clean_formula_text();
 
56
            return this._super();
 
57
        },
 
58
 
 
59
        /***********************************************************************
 
60
        Custom section 
 
61
        ***********************************************************************/
 
62
 
 
63
        /**
 
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.
 
66
         */
 
67
        _formula_text: '',
 
68
 
 
69
        /**
 
70
         * Clean '_formula_text' value.
 
71
         */
 
72
        _clean_formula_text: function() {
 
73
            this._formula_text = '';
 
74
        },
 
75
 
 
76
        /**
 
77
         * Return a valid formula from a val, if possible.
 
78
         Otherwise, return false.
 
79
         */
 
80
        _get_valid_expression: function(val) {
 
81
            // Trim the value
 
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, '') == ''){
 
89
                    try {
 
90
                        // Try to compute
 
91
                        formula = currenttxt.substring(1).replace(/,/g,'.');
 
92
                        var floatval = eval(formula);
 
93
                    }catch (e) {}
 
94
                    if (typeof (floatval) != 'undefined'){
 
95
                        return formula;
 
96
                    }
 
97
                }
 
98
            }
 
99
            return false;
 
100
        },
 
101
 
 
102
        /**
 
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.
 
105
         */
 
106
        _compute_result: function() {
 
107
            var formula
 
108
            // Erase old formula
 
109
            this._formula_text = '';
 
110
            
 
111
            formula = this._get_valid_expression(this.$el.find('input').attr('value'));
 
112
            if (formula){
 
113
                // Store new formula
 
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
 
118
                this.render_value();
 
119
            }
 
120
        },
 
121
 
 
122
        /**
 
123
         * Display the stored formula in the field, to allow modification.
 
124
         */
 
125
        _display_formula: function() {
 
126
            if (this._formula_text != ''){
 
127
                this.$el.find('input').val(this._formula_text);
 
128
            }
 
129
        },
 
130
 
 
131
    });
 
132
};