[MERGE]merge main view editor branch upto 884 revision.
[odoo/odoo.git] / addons / web / static / src / js / formats.js
1
2 openerp.web.formats = function(openerp) {
3
4 /**
5  * Formats a single atomic value based on a field descriptor
6  *
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
13  */
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)) {
17         value = false;
18     }
19     switch (value) {
20         case false:
21         case Infinity:
22         case -Infinity:
23             return value_if_empty === undefined ?  '' : value_if_empty;
24     }
25     switch (descriptor.widget || descriptor.type) {
26         case 'integer':
27             return _.sprintf('%d', value);
28         case 'float':
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',
33                         int_part,
34                         openerp.web._t.database.parameters.decimal_point, dec_part);
35         case 'float_time':
36             return _.sprintf("%02d:%02d",
37                     Math.floor(value),
38                     Math.round((value % 1) * 60));
39         case 'progressbar':
40             return _.sprintf(
41                 '<progress value="%.2f" max="100.0">%.2f%%</progress>',
42                     value, value);
43         case 'many2one':
44             // name_get value format
45             return value[1];
46         case 'datetime':
47             if (typeof(value) == "string")
48                 value = openerp.web.auto_str_to_date(value);
49             try {
50                 return value.toString(_.sprintf("%s %s", Date.CultureInfo.formatPatterns.shortDate,
51                     Date.CultureInfo.formatPatterns.longTime));
52             } catch (e) {
53                 return value.format("%m/%d/%Y %H:%M:%S");
54             }
55             return value;
56         case 'date':
57             if (typeof(value) == "string")
58                 value = openerp.web.auto_str_to_date(value);
59             try {
60                 return value.toString(Date.CultureInfo.formatPatterns.shortDate);
61             } catch (e) {
62                 return value.format("%m/%d/%Y");
63             }
64         case 'time':
65             if (typeof(value) == "string")
66                 value = openerp.web.auto_str_to_date(value);
67             try {
68                 return value.toString(Date.CultureInfo.formatPatterns.longTime);
69             } catch (e) {
70                 return value.format("%H:%M:%S");
71             }
72         case 'selection':
73             // Each choice is [value, label]
74             var result = _(descriptor.selection).detect(function (choice) {
75                 return choice[0] === value;
76             });
77             if (result) { return result[1]; }
78             return;
79         default:
80             return value;
81     }
82 };
83
84 openerp.web.parse_value = function (value, descriptor, value_if_empty) {
85     switch (value) {
86         case false:
87         case "":
88             return value_if_empty === undefined ?  false : value_if_empty;
89     }
90     switch (descriptor.widget || descriptor.type) {
91         case 'integer':
92             var tmp;
93             do {
94                 tmp = value;
95                 value = value.replace(openerp.web._t.database.parameters.thousands_sep, "");
96             } while(tmp !== value);
97             tmp = Number(value);
98             if (isNaN(tmp))
99                 throw value + " is not a correct integer";
100             return tmp;
101         case 'float':
102             var tmp = Number(value);
103             if (!isNaN(tmp))
104                 return tmp;
105             tmp = value.replace(openerp.web._t.database.parameters.decimal_point, ".");
106             var tmp2 = tmp;
107             do {
108                 tmp = tmp2;
109                 tmp2 = tmp.replace(openerp.web._t.database.parameters.thousands_sep, "");
110             } while(tmp !== tmp2);
111             tmp = Number(tmp);
112             if (isNaN(tmp))
113                 throw value + " is not a correct float";
114             return tmp;
115         case 'float_time':
116             var tmp = value.split(":");
117             if (tmp.length != 2)
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);
122         case 'progressbar':
123             return openerp.web.parse_value(value, {type: "float"});
124         case 'datetime':
125             var tmp = Date.parseExact(value, _.sprintf("%s %s", Date.CultureInfo.formatPatterns.shortDate,
126                     Date.CultureInfo.formatPatterns.longTime));
127             if (tmp !== null)
128                 return openerp.web.datetime_to_str(tmp);
129             tmp = Date.parse(value);
130             if (tmp !== null)
131                 return openerp.web.datetime_to_str(tmp);
132             throw value + " is not a valid datetime";
133         case 'date':
134             var tmp = Date.parseExact(value, Date.CultureInfo.formatPatterns.shortDate);
135             if (tmp !== null)
136                 return openerp.web.date_to_str(tmp);
137             tmp = Date.parse(value);
138             if (tmp !== null)
139                 return openerp.web.date_to_str(tmp);
140             throw value + " is not a valid date";
141         case 'time':
142             var tmp = Date.parseExact(value, Date.CultureInfo.formatPatterns.longTime);
143             if (tmp !== null)
144                 return openerp.web.time_to_str(tmp);
145             tmp = Date.parse(value);
146             if (tmp !== null)
147                 return openerp.web.time_to_str(tmp);
148             throw value + " is not a valid time";
149     }
150     return value;
151 };
152
153 openerp.web.auto_str_to_date = function(value, type) {
154     try {
155         return openerp.web.str_to_datetime(value);
156     } catch(e) {}
157     try {
158         return openerp.web.str_to_date(value);
159     } catch(e) {}
160     try {
161         return openerp.web.str_to_time(value);
162     } catch(e) {}
163     throw "'" + value + "' is not a valid date, datetime nor time"
164 };
165
166 openerp.web.auto_date_to_str = function(value, type) {
167     switch(type) {
168         case 'datetime':
169             return openerp.web.datetime_to_str(value);
170         case 'date':
171             return openerp.web.date_to_str(value);
172         case 'time':
173             return openerp.web.time_to_str(value);
174         default:
175             throw type + " is not convertible to date, datetime nor time"
176     }
177 };
178
179 /**
180  * Formats a provided cell based on its field type
181  *
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 ?
190  */
191 openerp.web.format_cell = function (row_data, column, value_if_empty, process_modifiers) {
192     var attrs = {};
193     if (process_modifiers !== false) {
194         attrs = column.modifiers_for(row_data);
195     }
196     if (attrs.invisible) { return ''; }
197     if (column.tag === 'button') {
198         return [
199             '<button type="button" title="', column.string || '', '">',
200                 '<img src="/web/static/src/img/icons/', column.icon, '.png"',
201                     ' alt="', column.string || '', '"/>',
202             '</button>'
203         ].join('')
204     }
205
206     if (!row_data[column.id]) {
207         return value_if_empty === undefined ? '' : value_if_empty;
208     }
209     return openerp.web.format_value(
210             row_data[column.id].value, column, value_if_empty);
211 }
212     
213 };