[MERGE] forward port of branch 8.0 up to 2b192be
[odoo/odoo.git] / addons / point_of_sale / static / src / js / widget_base.js
1 function openerp_pos_basewidget(instance, module){ //module is instance.point_of_sale
2
3     var round_pr = instance.web.round_precision
4
5     // This is a base class for all Widgets in the POS. It exposes relevant data to the 
6     // templates : 
7     // - widget.currency : { symbol: '$' | '€' | ..., position: 'before' | 'after }
8     // - widget.format_currency(amount) : this method returns a formatted string based on the
9     //   symbol, the position, and the amount of money.
10     // if the PoS is not fully loaded when you instanciate the widget, the currency might not
11     // yet have been initialized. Use __build_currency_template() to recompute with correct values
12     // before rendering.
13
14     module.PosBaseWidget = instance.web.Widget.extend({
15         init:function(parent,options){
16             this._super(parent);
17             options = options || {};
18             this.pos = options.pos || (parent ? parent.pos : undefined);
19             this.pos_widget = options.pos_widget || (parent ? parent.pos_widget : undefined);
20             this.build_currency_template();
21         },
22         build_currency_template: function(){
23
24             if(this.pos && this.pos.currency){
25                 this.currency = this.pos.currency;
26             }else{
27                 this.currency = {symbol: '$', position: 'after', rounding: 0.01};
28             }
29
30             var decimals = Math.max(0,Math.ceil(Math.log(1.0 / this.currency.rounding) / Math.log(10)));
31
32             this.format_currency_no_symbol = function(amount){
33                 amount = round_pr(amount,this.currency.rounding);
34                 amount = amount.toFixed(decimals);
35                 return amount;
36             };
37
38             this.format_currency = function(amount){
39                 if(typeof amount === 'number'){
40                     amount = Math.round(amount*100)/100;
41                     amount = amount.toFixed(decimals);
42                 }
43                 if(this.currency.position === 'after'){
44                     return amount + ' ' + (this.currency.symbol || '');
45                 }else{
46                     return (this.currency.symbol || '') + ' ' + amount;
47                 }
48             };
49
50         },
51         show: function(){
52             this.$el.removeClass('oe_hidden');
53         },
54         hide: function(){
55             this.$el.addClass('oe_hidden');
56         },
57         format_pr: function(value,precision){
58             var decimals = precision > 0 ? Math.max(0,Math.ceil(Math.log(1.0/precision) / Math.log(10))) : 0;
59             return value.toFixed(decimals);
60         },
61     });
62
63 }