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 = _.sprintf(
32 '%.' + precision + 'f',
33 Math.abs(value) % 1).substring(2);
34 return _.sprintf('%d%s%s',
36 openerp.web._t.database.parameters.decimal_point,
39 return _.sprintf("%02d:%02d",
41 Math.round((value % 1) * 60));
44 '<progress value="%.2f" max="100.0">%.2f%%</progress>',
47 // name_get value format
50 if (typeof(value) == "string")
51 value = openerp.web.auto_str_to_date(value);
53 return value.toString(_.sprintf("%s %s", Date.CultureInfo.formatPatterns.shortDate,
54 Date.CultureInfo.formatPatterns.longTime));
56 return value.format("%m/%d/%Y %H:%M:%S");
60 if (typeof(value) == "string")
61 value = openerp.web.auto_str_to_date(value);
63 return value.toString(Date.CultureInfo.formatPatterns.shortDate);
65 return value.format("%m/%d/%Y");
68 if (typeof(value) == "string")
69 value = openerp.web.auto_str_to_date(value);
71 return value.toString(Date.CultureInfo.formatPatterns.longTime);
73 return value.format("%H:%M:%S");
76 // Each choice is [value, label]
77 var result = _(descriptor.selection).detect(function (choice) {
78 return choice[0] === value;
80 if (result) { return result[1]; }
87 openerp.web.parse_value = function (value, descriptor, value_if_empty) {
91 return value_if_empty === undefined ? false : value_if_empty;
93 switch (descriptor.widget || descriptor.type) {
98 value = value.replace(openerp.web._t.database.parameters.thousands_sep, "");
99 } while(tmp !== value);
102 throw value + " is not a correct integer";
105 var tmp = Number(value);
108 tmp = value.replace(openerp.web._t.database.parameters.decimal_point, ".");
112 tmp2 = tmp.replace(openerp.web._t.database.parameters.thousands_sep, "");
113 } while(tmp !== tmp2);
116 throw value + " is not a correct float";
119 var tmp = value.split(":");
121 throw value + " is not a correct float_time";
122 var tmp1 = openerp.web.parse_value(tmp[0], {type: "integer"});
123 var tmp2 = openerp.web.parse_value(tmp[1], {type: "integer"});
124 return tmp1 + (tmp2 / 60);
126 return openerp.web.parse_value(value, {type: "float"});
128 var tmp = Date.parseExact(value, _.sprintf("%s %s", Date.CultureInfo.formatPatterns.shortDate,
129 Date.CultureInfo.formatPatterns.longTime));
131 return openerp.web.datetime_to_str(tmp);
132 tmp = Date.parse(value);
134 return openerp.web.datetime_to_str(tmp);
135 throw value + " is not a valid datetime";
137 var tmp = Date.parseExact(value, Date.CultureInfo.formatPatterns.shortDate);
139 return openerp.web.date_to_str(tmp);
140 tmp = Date.parse(value);
142 return openerp.web.date_to_str(tmp);
143 throw value + " is not a valid date";
145 var tmp = Date.parseExact(value, Date.CultureInfo.formatPatterns.longTime);
147 return openerp.web.time_to_str(tmp);
148 tmp = Date.parse(value);
150 return openerp.web.time_to_str(tmp);
151 throw value + " is not a valid time";
156 openerp.web.auto_str_to_date = function(value, type) {
158 return openerp.web.str_to_datetime(value);
161 return openerp.web.str_to_date(value);
164 return openerp.web.str_to_time(value);
166 throw "'" + value + "' is not a valid date, datetime nor time"
169 openerp.web.auto_date_to_str = function(value, type) {
172 return openerp.web.datetime_to_str(value);
174 return openerp.web.date_to_str(value);
176 return openerp.web.time_to_str(value);
178 throw type + " is not convertible to date, datetime nor time"
183 * Formats a provided cell based on its field type
185 * @param {Object} row_data record whose values should be displayed in the cell
186 * @param {Object} column column descriptor
187 * @param {"button"|"field"} column.tag base control type
188 * @param {String} column.type widget type for a field control
189 * @param {String} [column.string] button label
190 * @param {String} [column.icon] button icon
191 * @param {String} [value_if_empty=''] what to display if the field's value is ``false``
192 * @param {Boolean} [process_modifiers=true] should the modifiers be computed ?
194 openerp.web.format_cell = function (row_data, column, value_if_empty, process_modifiers) {
196 if (process_modifiers !== false) {
197 attrs = column.modifiers_for(row_data);
199 if (attrs.invisible) { return ''; }
200 if (column.tag === 'button') {
202 '<button type="button" title="', column.string || '', '">',
203 '<img src="/web/static/src/img/icons/', column.icon, '.png"',
204 ' alt="', column.string || '', '"/>',
209 if (!row_data[column.id]) {
210 return value_if_empty === undefined ? '' : value_if_empty;
212 return openerp.web.format_value(
213 row_data[column.id].value, column, value_if_empty);