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