2 openerp.web.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.web.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.abs(Math.floor((value % 1) * Math.pow(10, precision)));
32 return _.sprintf('%d%s%d',
34 openerp.web._t.database.parameters.decimal_point, dec_part);
36 return _.sprintf("%02d:%02d",
38 Math.round((value % 1) * 60));
41 '<progress value="%.2f" max="100.0">%.2f%%</progress>',
44 // name_get value format
47 if (typeof(value) == "string")
48 value = openerp.web.auto_str_to_date(value);
50 return value.toString(_.sprintf("%s %s", Date.CultureInfo.formatPatterns.shortDate,
51 Date.CultureInfo.formatPatterns.longTime));
53 return value.format("%m/%d/%Y %H:%M:%S");
57 if (typeof(value) == "string")
58 value = openerp.web.auto_str_to_date(value);
60 return value.toString(Date.CultureInfo.formatPatterns.shortDate);
62 return value.format("%m/%d/%Y");
65 if (typeof(value) == "string")
66 value = openerp.web.auto_str_to_date(value);
68 return value.toString(Date.CultureInfo.formatPatterns.longTime);
70 return value.format("%H:%M:%S");
73 // Each choice is [value, label]
74 var result = _(descriptor.selection).detect(function (choice) {
75 return choice[0] === value;
77 if (result) { return result[1]; }
84 openerp.web.parse_value = function (value, descriptor, value_if_empty) {
88 return value_if_empty === undefined ? false : value_if_empty;
90 switch (descriptor.widget || descriptor.type) {
95 value = value.replace(openerp.web._t.database.parameters.thousands_sep, "");
96 } while(tmp !== value);
99 throw value + " is not a correct integer";
102 var tmp = Number(value);
105 tmp = value.replace(openerp.web._t.database.parameters.decimal_point, ".");
109 tmp2 = tmp.replace(openerp.web._t.database.parameters.thousands_sep, "");
110 } while(tmp !== tmp2);
113 throw value + " is not a correct float";
116 var tmp = value.split(":");
118 throw value + " is not a correct float_time";
119 var tmp1 = openerp.web.parse_value(tmp[0], {type: "integer"});
120 var tmp2 = openerp.web.parse_value(tmp[1], {type: "integer"});
121 return tmp1 + (tmp2 / 60);
123 return openerp.web.parse_value(value, {type: "float"});
125 var tmp = Date.parseExact(value, _.sprintf("%s %s", Date.CultureInfo.formatPatterns.shortDate,
126 Date.CultureInfo.formatPatterns.longTime));
128 return openerp.web.datetime_to_str(tmp);
129 tmp = Date.parse(value);
131 return openerp.web.datetime_to_str(tmp);
132 throw value + " is not a valid datetime";
134 var tmp = Date.parseExact(value, Date.CultureInfo.formatPatterns.shortDate);
136 return openerp.web.date_to_str(tmp);
137 tmp = Date.parse(value);
139 return openerp.web.date_to_str(tmp);
140 throw value + " is not a valid date";
142 var tmp = Date.parseExact(value, Date.CultureInfo.formatPatterns.longTime);
144 return openerp.web.time_to_str(tmp);
145 tmp = Date.parse(value);
147 return openerp.web.time_to_str(tmp);
148 throw value + " is not a valid time";
153 openerp.web.auto_str_to_date = function(value, type) {
155 return openerp.web.str_to_datetime(value);
158 return openerp.web.str_to_date(value);
161 return openerp.web.str_to_time(value);
163 throw "'" + value + "' is not a valid date, datetime nor time"
166 openerp.web.auto_date_to_str = function(value, type) {
169 return openerp.web.datetime_to_str(value);
171 return openerp.web.date_to_str(value);
173 return openerp.web.time_to_str(value);
175 throw type + " is not convertible to date, datetime nor time"
180 * Formats a provided cell based on its field type
182 * @param {Object} row_data record whose values should be displayed in the cell
183 * @param {Object} column column descriptor
184 * @param {"button"|"field"} column.tag base control type
185 * @param {String} column.type widget type for a field control
186 * @param {String} [column.string] button label
187 * @param {String} [column.icon] button icon
188 * @param {String} [value_if_empty=''] what to display if the field's value is ``false``
189 * @param {Boolean} [process_modifiers=true] should the modifiers be computed ?
191 openerp.web.format_cell = function (row_data, column, value_if_empty, process_modifiers) {
193 if (process_modifiers !== false) {
194 attrs = column.modifiers_for(row_data);
196 if (attrs.invisible) { return ''; }
197 if (column.tag === 'button') {
199 '<button type="button" title="', column.string || '', '">',
200 '<img src="/web/static/src/img/icons/', column.icon, '.png"',
201 ' alt="', column.string || '', '"/>',
206 if (!row_data[column.id]) {
207 return value_if_empty === undefined ? '' : value_if_empty;
209 return openerp.web.format_value(
210 row_data[column.id].value, column, value_if_empty);