[IMP] point_of_sale: put the POS js files include function into the openerp namespace
[odoo/odoo.git] / addons / point_of_sale / static / src / js / widget_base.js
1 openerp.point_of_sale.load_basewidget = function load_basewidget(instance, module){ //module is instance.point_of_sale
2     "use strict";
3
4     var round_di = instance.web.round_decimals;
5     var round_pr = instance.web.round_precision;
6
7     // This is a base class for all Widgets in the POS. It exposes relevant data to the 
8     // templates : 
9     // - widget.currency : { symbol: '$' | '€' | ..., position: 'before' | 'after }
10     // - widget.format_currency(amount) : this method returns a formatted string based on the
11     //   symbol, the position, and the amount of money.
12     // if the PoS is not fully loaded when you instanciate the widget, the currency might not
13     // yet have been initialized. Use __build_currency_template() to recompute with correct values
14     // before rendering.
15
16     module.PosBaseWidget = instance.web.Widget.extend({
17         init:function(parent,options){
18             this._super(parent);
19             options = options || {};
20             this.pos = options.pos || (parent ? parent.pos : undefined);
21             this.pos_widget = options.pos_widget || (parent ? parent.pos_widget : undefined);
22         },
23         format_currency: function(amount,precision){
24             var currency = (this.pos && this.pos.currency) ? this.pos.currency : {symbol:'$', position: 'after', rounding: 0.01, decimals: 2};
25
26             amount = this.format_currency_no_symbol(amount,precision);
27
28             if (currency.position === 'after') {
29                 return amount + ' ' + (currency.symbol || '');
30             } else {
31                 return (currency.symbol || '') + ' ' + amount;
32             }
33         },
34         format_currency_no_symbol: function(amount, precision) {
35             var currency = (this.pos && this.pos.currency) ? this.pos.currency : {symbol:'$', position: 'after', rounding: 0.01, decimals: 2};
36             var decimals = currency.decimals;
37
38             if (precision && (typeof this.pos.dp[precision]) !== undefined) {
39                 decimals = this.pos.dp[precision];
40             }
41
42             this.format_currency_no_symbol = function(amount){
43                 amount = round_pr(amount,currency.rounding);
44                 amount = amount.toFixed(decimals);
45                 return amount;
46             };
47
48             if (typeof amount === 'number') {
49                 amount = round_di(amount,decimals).toFixed(decimals);
50             }
51
52             return amount;
53         },
54         show: function(){
55             this.$el.removeClass('oe_hidden');
56         },
57         hide: function(){
58             this.$el.addClass('oe_hidden');
59         },
60         format_pr: function(value,precision){
61             var decimals = precision > 0 ? Math.max(0,Math.ceil(Math.log(1.0/precision) / Math.log(10))) : 0;
62             return value.toFixed(decimals);
63         },
64         format_fixed: function(value,integer_width,decimal_width){
65             value = value.toFixed(decimal_width || 0);
66             var width = value.indexOf('.');
67             if (width < 0 ) {
68                 width = value.length;
69             }
70             var missing = integer_width - width;
71             while (missing > 0) {
72                 value = '0' + value;
73                 missing--;
74             }
75             return value;
76         },
77     });
78
79 }