[MERGE] project_issue: days since creation date added
[odoo/odoo.git] / addons / base / static / src / js / formats.js
1
2 openerp.base.formats = function(openerp) {
3
4 /**
5  * Converts a string to a Date javascript object using OpenERP's
6  * datetime string format (exemple: '2011-12-01 15:12:35').
7  * 
8  * The timezone is assumed to be UTC (standard for OpenERP 6.1)
9  * and will be converted to the browser's timezone.
10  * 
11  * @param {String} str A string representing a datetime.
12  * @returns {Date}
13  */
14 openerp.base.parse_datetime = function(str) {
15     if(!str) {
16         return str;
17     }
18     var regex = /\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/;
19     var res = regex.exec(str);
20     if ( res[0] != str ) {
21         throw "'" + str + "' is not a valid datetime";
22     }
23     var obj = Date.parse(str + " GMT");
24     if (! obj) {
25         throw "'" + str + "' is not a valid datetime";
26     }
27     return obj;
28 };
29
30 /**
31  * Converts a string to a Date javascript object using OpenERP's
32  * date string format (exemple: '2011-12-01').
33  * 
34  * @param {String} str A string representing a date.
35  * @returns {Date}
36  */
37 openerp.base.parse_date = function(str) {
38     if(!str) {
39         return str;
40     }
41     var regex = /\d\d\d\d-\d\d-\d\d/;
42     var res = regex.exec(str);
43     if ( res[0] != str ) {
44         throw "'" + str + "' is not a valid date";
45     }
46     var obj = Date.parse(str);
47     if (! obj) {
48         throw "'" + str + "' is not a valid date";
49     }
50     return obj;
51 };
52
53 /**
54  * Converts a string to a Date javascript object using OpenERP's
55  * time string format (exemple: '15:12:35').
56  * 
57  * @param {String} str A string representing a time.
58  * @returns {Date}
59  */
60 openerp.base.parse_time = function(str) {
61     if(!str) {
62         return str;
63     }
64     var regex = /\d\d:\d\d:\d\d/;
65     var res = regex.exec(str);
66     if ( res[0] != str ) {
67         throw "'" + str + "' is not a valid time";
68     }
69     var obj = Date.parse(str);
70     if (! obj) {
71         throw "'" + str + "' is not a valid time";
72     }
73     return obj;
74 };
75
76 /*
77  * Left-pad provided arg 1 with zeroes until reaching size provided by second
78  * argument.
79  *
80  * @param {Number|String} str value to pad
81  * @param {Number} size size to reach on the final padded value
82  * @returns {String} padded string
83  */
84 var zpad = function(str, size) {
85     str = "" + str;
86     return new Array(size - str.length + 1).join('0') + str;
87 };
88
89 /**
90  * Converts a Date javascript object to a string using OpenERP's
91  * datetime string format (exemple: '2011-12-01 15:12:35').
92  * 
93  * The timezone of the Date object is assumed to be the one of the
94  * browser and it will be converted to UTC (standard for OpenERP 6.1).
95  * 
96  * @param {Date} obj
97  * @returns {String} A string representing a datetime.
98  */
99 openerp.base.format_datetime = function(obj) {
100     if (!obj) {
101         return false;
102     }
103     return zpad(obj.getUTCFullYear(),4) + "-" + zpad(obj.getUTCMonth() + 1,2) + "-"
104          + zpad(obj.getUTCDate(),2) + " " + zpad(obj.getUTCHours(),2) + ":"
105          + zpad(obj.getUTCMinutes(),2) + ":" + zpad(obj.getUTCSeconds(),2);
106 };
107
108 /**
109  * Converts a Date javascript object to a string using OpenERP's
110  * date string format (exemple: '2011-12-01').
111  * 
112  * @param {Date} obj
113  * @returns {String} A string representing a date.
114  */
115 openerp.base.format_date = function(obj) {
116     if (!obj) {
117         return false;
118     }
119     return zpad(obj.getFullYear(),4) + "-" + zpad(obj.getMonth() + 1,2) + "-"
120          + zpad(obj.getDate(),2);
121 };
122
123 /**
124  * Converts a Date javascript object to a string using OpenERP's
125  * time string format (exemple: '15:12:35').
126  * 
127  * @param {Date} obj
128  * @returns {String} A string representing a time.
129  */
130 openerp.base.format_time = function(obj) {
131     if (!obj) {
132         return false;
133     }
134     return zpad(obj.getHours(),2) + ":" + zpad(obj.getMinutes(),2) + ":"
135          + zpad(obj.getSeconds(),2);
136 };
137
138 /**
139  * Formats a single atomic value based on a field descriptor
140  *
141  * @param {Object} value read from OpenERP
142  * @param {Object} descriptor union of orm field and view field
143  * @param {Object} [descriptor.widget] widget to use to display the value
144  * @param {Object} descriptor.type fallback if no widget is provided, or if the provided widget is unknown
145  * @param {Object} [descriptor.digits] used for the formatting of floats
146  * @param {String} [value_if_empty=''] returned if the ``value`` argument is considered empty
147  */
148 openerp.base.format_value = function (value, descriptor, value_if_empty) {
149     // If NaN value, display as with a `false` (empty cell)
150     if (typeof value === 'number' && isNaN(value)) {
151         value = false;
152     }
153     switch (value) {
154         case false:
155         case Infinity:
156         case -Infinity:
157             return value_if_empty === undefined ?  '' : value_if_empty;
158     }
159     switch (descriptor.widget || descriptor.type) {
160         case 'integer':
161             return _.sprintf('%d', value);
162         case 'float':
163             var precision = descriptor.digits ? descriptor.digits[1] : 2;
164             return _.sprintf('%.' + precision + 'f', value);
165         case 'float_time':
166             return _.sprintf("%02d:%02d",
167                     Math.floor(value),
168                     Math.round((value % 1) * 60));
169         case 'progressbar':
170             return _.sprintf(
171                 '<progress value="%.2f" max="100.0">%.2f%%</progress>',
172                     value, value);
173         case 'many2one':
174             // name_get value format
175             return value[1];
176         default:
177             return value;
178     }
179 };
180
181 /**
182  * Formats a provided cell based on its field type
183  *
184  * @param {Object} row_data record whose values should be displayed in the cell
185  * @param {Object} column column descriptor
186  * @param {"button"|"field"} column.tag base control type
187  * @param {String} column.type widget type for a field control
188  * @param {String} [column.string] button label
189  * @param {String} [column.icon] button icon
190  * @param {String} [value_if_empty=''] what to display if the field's value is ``false``
191  */
192 openerp.base.format_cell = function (row_data, column, value_if_empty) {
193     var attrs = column.modifiers_for(row_data);
194     if (attrs.invisible) { return ''; }
195     if (column.tag === 'button') {
196         return [
197             '<button type="button" title="', column.string || '', '">',
198                 '<img src="/base/static/src/img/icons/', column.icon, '.png"',
199                     ' alt="', column.string || '', '"/>',
200             '</button>'
201         ].join('')
202     }
203
204     return openerp.base.format_value(
205             row_data[column.id].value, column, value_if_empty);
206 }
207     
208 };