2 openerp.base.formats = function(openerp) {
5 * Formats a single atomic value based on a field descriptor
7 * @param {Object} value read from OpenERP
8 * @param {Object} descriptor union of orm field and view field
9 * @param {Object} [descriptor.widget] widget to use to display the value
10 * @param {Object} descriptor.type fallback if no widget is provided, or if the provided widget is unknown
11 * @param {Object} [descriptor.digits] used for the formatting of floats
12 * @param {String} [value_if_empty=''] returned if the ``value`` argument is considered empty
14 openerp.base.format_value = function (value, descriptor, value_if_empty) {
15 // If NaN value, display as with a `false` (empty cell)
16 if (typeof value === 'number' && isNaN(value)) {
23 return value_if_empty === undefined ? '' : value_if_empty;
25 switch (descriptor.widget || descriptor.type) {
27 return _.sprintf('%d', value);
29 var precision = descriptor.digits ? descriptor.digits[1] : 2;
30 var int_part = Math.floor(value);
31 var dec_part = Math.floor((value % 1) * Math.pow(10, precision));
32 return _.sprintf('%d' + openerp.base._t.database.parameters.decimal_point + '%d', int_part, dec_part);
34 return _.sprintf("%02d:%02d",
36 Math.round((value % 1) * 60));
39 '<progress value="%.2f" max="100.0">%.2f%%</progress>',
42 // name_get value format
45 if (typeof(value) == "string")
46 value = openerp.base.str_to_datetime(value);
48 return value.format(_.sprintf("%s %s", openerp.base._t.database.parameters.date_format,
49 openerp.base._t.database.parameters.time_format));
51 return value.format("%m/%d/%Y %H:%M:%S");
55 if (typeof(value) == "string")
56 value = openerp.base.str_to_date(value);
58 return value.format(openerp.base._t.database.parameters.date_format);
60 return value.format("%m/%d/%Y");
63 if (typeof(value) == "string")
64 value = openerp.base.str_to_time(value);
66 return value.format(openerp.base._t.database.parameters.time_format);
68 return value.format("%H:%M:%S");
75 openerp.base.parse_value = function (value, descriptor, value_if_empty) {
79 return value_if_empty === undefined ? false : value_if_empty;
81 switch (descriptor.widget || descriptor.type) {
83 var tmp = Number(value);
85 throw value + " is not a correct integer";
88 var tmp = Number(value);
91 tmp = value.replace(openerp.base._t.database.parameters.decimal_point, ".");
95 tmp2 = tmp.replace(openerp.base._t.database.parameters.thousands_sep, "");
96 } while(tmp !== tmp2);
99 throw value + " is not a correct float";
102 var tmp = value.split(":");
104 throw value + " is not a correct float_time";
105 var tmp1 = openerp.base.parse_value(tmp[0], {type: "integer"});
106 var tmp2 = openerp.base.parse_value(tmp[1], {type: "integer"});
107 return tmp1 + (tmp2 / 60);
109 return openerp.base.parse_value(value, {type: "float"});
111 var tmp = Date.parse(value);
114 throw value + " is not a valid datetime";
116 var tmp = Date.parse(value);
119 throw value + " is not a valid date";
121 var tmp = Date.parse(value);
124 throw value + " is not a valid time";
130 * Formats a provided cell based on its field type
132 * @param {Object} row_data record whose values should be displayed in the cell
133 * @param {Object} column column descriptor
134 * @param {"button"|"field"} column.tag base control type
135 * @param {String} column.type widget type for a field control
136 * @param {String} [column.string] button label
137 * @param {String} [column.icon] button icon
138 * @param {String} [value_if_empty=''] what to display if the field's value is ``false``
140 openerp.base.format_cell = function (row_data, column, value_if_empty) {
141 var attrs = column.modifiers_for(row_data);
142 if (attrs.invisible) { return ''; }
143 if (column.tag === 'button') {
145 '<button type="button" title="', column.string || '', '">',
146 '<img src="/base/static/src/img/icons/', column.icon, '.png"',
147 ' alt="', column.string || '', '"/>',
152 if (!row_data[column.id]) {
153 return value_if_empty === undefined ? '' : value_if_empty;
155 return openerp.base.format_value(
156 row_data[column.id].value, column, value_if_empty);