2 openerp.base.formats = function(openerp) {
5 * Converts a string to a Date javascript object using OpenERP's
6 * datetime string format (exemple: '2011-12-01 15:12:35').
8 * The timezone is assumed to be UTC (standard for OpenERP 6.1)
9 * and will be converted to the browser's timezone.
11 * @param {String} str A string representing a datetime.
14 openerp.base.parse_datetime = function(str) {
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";
23 var obj = Date.parse(str + " GMT");
25 throw "'" + str + "' is not a valid datetime";
31 * Converts a string to a Date javascript object using OpenERP's
32 * date string format (exemple: '2011-12-01').
34 * @param {String} str A string representing a date.
37 openerp.base.parse_date = function(str) {
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";
46 var obj = Date.parse(str);
48 throw "'" + str + "' is not a valid date";
54 * Converts a string to a Date javascript object using OpenERP's
55 * time string format (exemple: '15:12:35').
57 * @param {String} str A string representing a time.
60 openerp.base.parse_time = function(str) {
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";
69 var obj = Date.parse(str);
71 throw "'" + str + "' is not a valid time";
77 * Left-pad provided arg 1 with zeroes until reaching size provided by second
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
84 var zpad = function(str, size) {
86 return new Array(size - str.length + 1).join('0') + str;
90 * Converts a Date javascript object to a string using OpenERP's
91 * datetime string format (exemple: '2011-12-01 15:12:35').
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).
97 * @returns {String} A string representing a datetime.
99 openerp.base.format_datetime = function(obj) {
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);
109 * Converts a Date javascript object to a string using OpenERP's
110 * date string format (exemple: '2011-12-01').
113 * @returns {String} A string representing a date.
115 openerp.base.format_date = function(obj) {
119 return zpad(obj.getFullYear(),4) + "-" + zpad(obj.getMonth() + 1,2) + "-"
120 + zpad(obj.getDate(),2);
124 * Converts a Date javascript object to a string using OpenERP's
125 * time string format (exemple: '15:12:35').
128 * @returns {String} A string representing a time.
130 openerp.base.format_time = function(obj) {
134 return zpad(obj.getHours(),2) + ":" + zpad(obj.getMinutes(),2) + ":"
135 + zpad(obj.getSeconds(),2);
139 * Formats a single atomic value based on a field descriptor
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
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)) {
157 return value_if_empty === undefined ? '' : value_if_empty;
159 switch (descriptor.widget || descriptor.type) {
161 return _.sprintf('%d', value);
163 var precision = descriptor.digits ? descriptor.digits[1] : 2;
164 return _.sprintf('%.' + precision + 'f', value);
166 return _.sprintf("%02d:%02d",
168 Math.round((value % 1) * 60));
171 '<progress value="%.2f" max="100.0">%.2f%%</progress>',
174 // name_get value format
182 * Formats a provided cell based on its field type
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``
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') {
197 '<button type="button" title="', column.string || '', '">',
198 '<img src="/base/static/src/img/icons/', column.icon, '.png"',
199 ' alt="', column.string || '', '"/>',
204 return openerp.base.format_value(
205 row_data[column.id].value, column, value_if_empty);