You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.8 KiB

  1. /*******************************************************************************
  2. See __openerp__.py file for Copyright and Licence Informations.
  3. *******************************************************************************/
  4. openerp.web_widget_float_formula = function (instance) {
  5. instance.web.FormView = instance.web.FormView.extend({
  6. /***********************************************************************
  7. Overload section
  8. ***********************************************************************/
  9. /**
  10. * Overload : '_process_save' function
  11. 1: to force computation of formula if the user realize a keydown directly after the formula input in a tree view ;
  12. 2: to clean up the '_formula_text' value in all case to avoid bugs in tree view ;
  13. */
  14. _process_save: function(save_obj) {
  15. for (var f in this.fields) {
  16. if (!this.fields.hasOwnProperty(f)) { continue; }
  17. f = this.fields[f];
  18. if (f.hasOwnProperty('_formula_text')){
  19. currentval = f.$('input').attr('value')
  20. if (typeof currentval != 'undefined'){
  21. formula = f._get_valid_expression(currentval);
  22. if (formula){
  23. f._compute_result();
  24. }
  25. }
  26. f._clean_formula_text();
  27. }
  28. }
  29. return this._super(save_obj);
  30. },
  31. });
  32. instance.web.form.FieldFloat = instance.web.form.FieldFloat.extend({
  33. /***********************************************************************
  34. Overload section
  35. ***********************************************************************/
  36. /**
  37. * Overload : 'start' function to catch 'blur' and 'focus' events.
  38. */
  39. start: function() {
  40. this.on("blurred", this, this._compute_result);
  41. this.on("focused", this, this._display_formula);
  42. return this._super();
  43. },
  44. /**
  45. * Overload : 'initialize_content' function to clean '_formula_text' value.
  46. */
  47. initialize_content: function() {
  48. this._clean_formula_text();
  49. return this._super();
  50. },
  51. /***********************************************************************
  52. Custom section
  53. ***********************************************************************/
  54. /**
  55. * keep in memory the formula to allow user to edit it again.
  56. The formula has to be keeped in memory until a 'save' action.
  57. */
  58. _formula_text: '',
  59. /**
  60. * Clean '_formula_text' value.
  61. */
  62. _clean_formula_text: function() {
  63. this._formula_text = '';
  64. },
  65. /**
  66. * Return a valid formula from a val, if possible.
  67. Otherwise, return false.
  68. */
  69. _get_valid_expression: function(val) {
  70. // Trim the value
  71. currenttxt = val.toString().replace(/^\s+|\s+$/g, '');
  72. // Test if the value is a formula
  73. if (currenttxt[0] == '=') {
  74. // allowed chars : [0-9] .,+-/*() and spaces
  75. myreg = RegExp('[0-9]|\\s|\\.|,|\\(|\\)|\\+|\\-|\\*|\\/','g')
  76. // Test to avoid code injonction in eval function.
  77. if (currenttxt.substring(1).replace(myreg, '') == ''){
  78. try {
  79. // Try to compute
  80. formula = currenttxt.substring(1).replace(/,/g,'.');
  81. var floatval = eval(formula);
  82. }catch (e) {}
  83. if (typeof (floatval) != 'undefined'){
  84. return formula;
  85. }
  86. }
  87. }
  88. return false;
  89. },
  90. /**
  91. * test if the content of the field is a valid formula,
  92. * compute the result, and replace the current value by the final result.
  93. */
  94. _compute_result: function() {
  95. var formula
  96. // Erase old formula
  97. this._formula_text = '';
  98. formula = this._get_valid_expression(this.$el.find('input').attr('value'));
  99. if (formula){
  100. // Store new formula
  101. this._formula_text = "=" + formula;
  102. // put the result in the field
  103. this.set_value(eval(formula));
  104. // Force rendering anyway to avoid format loss if no change
  105. this.render_value();
  106. }
  107. },
  108. /**
  109. * Display the stored formula in the field, to allow modification.
  110. */
  111. _display_formula: function() {
  112. if (this._formula_text != ''){
  113. this.$el.find('input').val(this._formula_text);
  114. }
  115. },
  116. });
  117. };