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