2011-04-28 13:50:45 +00:00
|
|
|
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.formats = function(openerp) {
|
2011-11-14 14:00:07 +00:00
|
|
|
var _t = openerp.web._t;
|
2011-11-14 15:34:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Intersperses ``separator`` in ``str`` at the positions indicated by
|
|
|
|
* ``indices``.
|
|
|
|
*
|
|
|
|
* ``indices`` is an array of relative offsets (from the previous insertion
|
|
|
|
* position, starting from the end of the string) at which to insert
|
|
|
|
* ``separator``.
|
|
|
|
*
|
|
|
|
* There are two special values:
|
|
|
|
*
|
|
|
|
* ``-1``
|
|
|
|
* indicates the insertion should end now
|
|
|
|
* ``0``
|
|
|
|
* indicates that the previous section pattern should be repeated (until all
|
|
|
|
* of ``str`` is consumed)
|
|
|
|
*
|
|
|
|
* @param {String} str
|
|
|
|
* @param {Array<Number>} indices
|
|
|
|
* @param {String} separator
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
openerp.web.intersperse = function (str, indices, separator) {
|
|
|
|
separator = separator || '';
|
|
|
|
var result = [], last = str.length;
|
|
|
|
|
|
|
|
for(var i=0; i<indices.length; ++i) {
|
|
|
|
var section = indices[i];
|
|
|
|
if (section === -1 || last <= 0) {
|
|
|
|
// Done with string, or -1 (stops formatting string)
|
|
|
|
break;
|
|
|
|
} else if(section === 0 && i === 0) {
|
|
|
|
// repeats previous section, which there is none => stop
|
|
|
|
break;
|
|
|
|
} else if (section === 0) {
|
|
|
|
// repeat previous section forever
|
|
|
|
//noinspection AssignmentToForLoopParameterJS
|
|
|
|
section = indices[--i];
|
|
|
|
}
|
|
|
|
result.push(str.substring(last-section, last));
|
|
|
|
last -= section;
|
|
|
|
}
|
|
|
|
|
|
|
|
var s = str.substring(0, last);
|
|
|
|
if (s) { result.push(s); }
|
|
|
|
return result.reverse().join(separator);
|
|
|
|
};
|
2011-08-09 15:30:21 +00:00
|
|
|
/**
|
|
|
|
* Formats a single atomic value based on a field descriptor
|
|
|
|
*
|
|
|
|
* @param {Object} value read from OpenERP
|
|
|
|
* @param {Object} descriptor union of orm field and view field
|
|
|
|
* @param {Object} [descriptor.widget] widget to use to display the value
|
|
|
|
* @param {Object} descriptor.type fallback if no widget is provided, or if the provided widget is unknown
|
|
|
|
* @param {Object} [descriptor.digits] used for the formatting of floats
|
|
|
|
* @param {String} [value_if_empty=''] returned if the ``value`` argument is considered empty
|
|
|
|
*/
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.format_value = function (value, descriptor, value_if_empty) {
|
2011-08-09 15:30:21 +00:00
|
|
|
// If NaN value, display as with a `false` (empty cell)
|
|
|
|
if (typeof value === 'number' && isNaN(value)) {
|
|
|
|
value = false;
|
|
|
|
}
|
|
|
|
switch (value) {
|
|
|
|
case false:
|
|
|
|
case Infinity:
|
|
|
|
case -Infinity:
|
|
|
|
return value_if_empty === undefined ? '' : value_if_empty;
|
|
|
|
}
|
|
|
|
switch (descriptor.widget || descriptor.type) {
|
|
|
|
case 'integer':
|
|
|
|
return _.sprintf('%d', value);
|
|
|
|
case 'float':
|
|
|
|
var precision = descriptor.digits ? descriptor.digits[1] : 2;
|
2011-10-27 12:54:48 +00:00
|
|
|
return _.sprintf('%.' + precision + 'f', value)
|
|
|
|
.replace('.', openerp.web._t.database.parameters.decimal_point);
|
2011-08-09 15:30:21 +00:00
|
|
|
case 'float_time':
|
|
|
|
return _.sprintf("%02d:%02d",
|
|
|
|
Math.floor(value),
|
|
|
|
Math.round((value % 1) * 60));
|
|
|
|
case 'progressbar':
|
|
|
|
return _.sprintf(
|
|
|
|
'<progress value="%.2f" max="100.0">%.2f%%</progress>',
|
|
|
|
value, value);
|
|
|
|
case 'many2one':
|
|
|
|
// name_get value format
|
|
|
|
return value[1];
|
2011-08-19 15:47:26 +00:00
|
|
|
case 'datetime':
|
|
|
|
if (typeof(value) == "string")
|
2011-09-05 11:03:09 +00:00
|
|
|
value = openerp.web.auto_str_to_date(value);
|
2011-11-14 14:00:07 +00:00
|
|
|
|
|
|
|
return value.format(_t.database.parameters.date_format
|
|
|
|
+ ' ' + _t.database.parameters.time_format);
|
2011-08-19 15:47:26 +00:00
|
|
|
case 'date':
|
|
|
|
if (typeof(value) == "string")
|
2011-09-05 11:03:09 +00:00
|
|
|
value = openerp.web.auto_str_to_date(value);
|
2011-11-14 14:00:07 +00:00
|
|
|
return value.format(_t.database.parameters.date_format);
|
2011-08-22 12:35:16 +00:00
|
|
|
case 'time':
|
2011-08-19 15:47:26 +00:00
|
|
|
if (typeof(value) == "string")
|
2011-09-05 11:03:09 +00:00
|
|
|
value = openerp.web.auto_str_to_date(value);
|
2011-11-14 14:00:07 +00:00
|
|
|
return value.format(_t.database.parameters.time_format);
|
2011-10-12 12:09:38 +00:00
|
|
|
case 'selection':
|
|
|
|
// Each choice is [value, label]
|
|
|
|
var result = _(descriptor.selection).detect(function (choice) {
|
|
|
|
return choice[0] === value;
|
|
|
|
});
|
|
|
|
if (result) { return result[1]; }
|
|
|
|
return;
|
2011-08-09 15:30:21 +00:00
|
|
|
default:
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.parse_value = function (value, descriptor, value_if_empty) {
|
2011-11-14 14:00:07 +00:00
|
|
|
var date_pattern = Date.normalizeFormat(_t.database.parameters.date_format),
|
|
|
|
time_pattern = Date.normalizeFormat(_t.database.parameters.time_format);
|
2011-08-22 12:35:16 +00:00
|
|
|
switch (value) {
|
|
|
|
case false:
|
|
|
|
case "":
|
|
|
|
return value_if_empty === undefined ? false : value_if_empty;
|
|
|
|
}
|
|
|
|
switch (descriptor.widget || descriptor.type) {
|
|
|
|
case 'integer':
|
2011-09-19 13:46:15 +00:00
|
|
|
var tmp;
|
|
|
|
do {
|
|
|
|
tmp = value;
|
|
|
|
value = value.replace(openerp.web._t.database.parameters.thousands_sep, "");
|
|
|
|
} while(tmp !== value);
|
|
|
|
tmp = Number(value);
|
2011-08-22 12:35:16 +00:00
|
|
|
if (isNaN(tmp))
|
2011-11-10 13:29:40 +00:00
|
|
|
throw new Error(value + " is not a correct integer");
|
2011-08-22 12:35:16 +00:00
|
|
|
return tmp;
|
|
|
|
case 'float':
|
|
|
|
var tmp = Number(value);
|
|
|
|
if (!isNaN(tmp))
|
|
|
|
return tmp;
|
2011-09-05 11:03:09 +00:00
|
|
|
tmp = value.replace(openerp.web._t.database.parameters.decimal_point, ".");
|
2011-08-22 12:35:16 +00:00
|
|
|
var tmp2 = tmp;
|
|
|
|
do {
|
|
|
|
tmp = tmp2;
|
2011-09-05 11:03:09 +00:00
|
|
|
tmp2 = tmp.replace(openerp.web._t.database.parameters.thousands_sep, "");
|
2011-08-22 12:35:16 +00:00
|
|
|
} while(tmp !== tmp2);
|
2011-08-22 12:53:55 +00:00
|
|
|
tmp = Number(tmp);
|
2011-08-22 12:35:16 +00:00
|
|
|
if (isNaN(tmp))
|
2011-11-10 13:29:40 +00:00
|
|
|
throw new Error(value + " is not a correct float");
|
2011-08-22 12:35:16 +00:00
|
|
|
return tmp;
|
|
|
|
case 'float_time':
|
2011-11-10 13:29:40 +00:00
|
|
|
var float_time_pair = value.split(":");
|
|
|
|
if (float_time_pair.length != 2)
|
2011-11-10 13:31:00 +00:00
|
|
|
return openerp.web.parse_value(value, {type: "float"});
|
2011-11-10 13:29:40 +00:00
|
|
|
var hours = openerp.web.parse_value(float_time_pair[0], {type: "integer"});
|
|
|
|
var minutes = openerp.web.parse_value(float_time_pair[1], {type: "integer"});
|
|
|
|
return hours + (minutes / 60);
|
2011-08-22 12:35:16 +00:00
|
|
|
case 'progressbar':
|
2011-09-05 11:03:09 +00:00
|
|
|
return openerp.web.parse_value(value, {type: "float"});
|
2011-08-22 12:35:16 +00:00
|
|
|
case 'datetime':
|
2011-11-14 14:00:07 +00:00
|
|
|
var datetime = Date.parseExact(
|
|
|
|
value, (date_pattern + ' ' + time_pattern));
|
2011-11-10 13:29:40 +00:00
|
|
|
if (datetime !== null)
|
|
|
|
return openerp.web.datetime_to_str(datetime);
|
|
|
|
datetime = Date.parse(value);
|
|
|
|
if (datetime !== null)
|
|
|
|
return openerp.web.datetime_to_str(datetime);
|
|
|
|
throw new Error(value + " is not a valid datetime");
|
2011-08-22 12:35:16 +00:00
|
|
|
case 'date':
|
2011-11-14 14:00:07 +00:00
|
|
|
var date = Date.parseExact(value, date_pattern);
|
2011-11-10 13:29:40 +00:00
|
|
|
if (date !== null)
|
|
|
|
return openerp.web.date_to_str(date);
|
|
|
|
date = Date.parse(value);
|
|
|
|
if (date !== null)
|
|
|
|
return openerp.web.date_to_str(date);
|
|
|
|
throw new Error(value + " is not a valid date");
|
2011-08-22 12:35:16 +00:00
|
|
|
case 'time':
|
2011-11-14 14:00:07 +00:00
|
|
|
var time = Date.parseExact(value, time_pattern);
|
2011-11-10 13:29:40 +00:00
|
|
|
if (time !== null)
|
|
|
|
return openerp.web.time_to_str(time);
|
|
|
|
time = Date.parse(value);
|
|
|
|
if (time !== null)
|
|
|
|
return openerp.web.time_to_str(time);
|
|
|
|
throw new Error(value + " is not a valid time");
|
2011-08-22 12:35:16 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.auto_str_to_date = function(value, type) {
|
2011-09-01 15:22:24 +00:00
|
|
|
try {
|
2011-09-05 11:03:09 +00:00
|
|
|
return openerp.web.str_to_datetime(value);
|
2011-09-01 15:22:24 +00:00
|
|
|
} catch(e) {}
|
|
|
|
try {
|
2011-09-05 11:03:09 +00:00
|
|
|
return openerp.web.str_to_date(value);
|
2011-09-01 15:22:24 +00:00
|
|
|
} catch(e) {}
|
|
|
|
try {
|
2011-09-05 11:03:09 +00:00
|
|
|
return openerp.web.str_to_time(value);
|
2011-09-01 15:22:24 +00:00
|
|
|
} catch(e) {}
|
2011-11-10 15:23:13 +00:00
|
|
|
throw new Error("'" + value + "' is not a valid date, datetime nor time");
|
2011-09-01 15:22:24 +00:00
|
|
|
};
|
|
|
|
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.auto_date_to_str = function(value, type) {
|
2011-09-01 14:21:56 +00:00
|
|
|
switch(type) {
|
|
|
|
case 'datetime':
|
2011-09-05 11:03:09 +00:00
|
|
|
return openerp.web.datetime_to_str(value);
|
2011-09-01 14:21:56 +00:00
|
|
|
case 'date':
|
2011-09-05 11:03:09 +00:00
|
|
|
return openerp.web.date_to_str(value);
|
2011-09-01 14:21:56 +00:00
|
|
|
case 'time':
|
2011-09-05 11:03:09 +00:00
|
|
|
return openerp.web.time_to_str(value);
|
2011-09-01 14:21:56 +00:00
|
|
|
default:
|
2011-11-10 15:23:13 +00:00
|
|
|
throw new Error(type + " is not convertible to date, datetime nor time");
|
2011-09-01 14:21:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-09 15:30:21 +00:00
|
|
|
/**
|
|
|
|
* Formats a provided cell based on its field type
|
|
|
|
*
|
|
|
|
* @param {Object} row_data record whose values should be displayed in the cell
|
|
|
|
* @param {Object} column column descriptor
|
|
|
|
* @param {"button"|"field"} column.tag base control type
|
|
|
|
* @param {String} column.type widget type for a field control
|
|
|
|
* @param {String} [column.string] button label
|
|
|
|
* @param {String} [column.icon] button icon
|
|
|
|
* @param {String} [value_if_empty=''] what to display if the field's value is ``false``
|
2011-10-05 15:32:02 +00:00
|
|
|
* @param {Boolean} [process_modifiers=true] should the modifiers be computed ?
|
2011-08-09 15:30:21 +00:00
|
|
|
*/
|
2011-10-05 15:32:02 +00:00
|
|
|
openerp.web.format_cell = function (row_data, column, value_if_empty, process_modifiers) {
|
|
|
|
var attrs = {};
|
|
|
|
if (process_modifiers !== false) {
|
|
|
|
attrs = column.modifiers_for(row_data);
|
|
|
|
}
|
2011-08-09 15:30:21 +00:00
|
|
|
if (attrs.invisible) { return ''; }
|
|
|
|
if (column.tag === 'button') {
|
|
|
|
return [
|
|
|
|
'<button type="button" title="', column.string || '', '">',
|
2011-09-05 11:03:09 +00:00
|
|
|
'<img src="/web/static/src/img/icons/', column.icon, '.png"',
|
2011-08-09 15:30:21 +00:00
|
|
|
' alt="', column.string || '', '"/>',
|
|
|
|
'</button>'
|
|
|
|
].join('')
|
|
|
|
}
|
|
|
|
|
2011-08-22 09:04:58 +00:00
|
|
|
if (!row_data[column.id]) {
|
|
|
|
return value_if_empty === undefined ? '' : value_if_empty;
|
|
|
|
}
|
2011-09-05 11:03:09 +00:00
|
|
|
return openerp.web.format_value(
|
2011-08-09 15:30:21 +00:00
|
|
|
row_data[column.id].value, column, value_if_empty);
|
|
|
|
}
|
2011-04-28 13:50:45 +00:00
|
|
|
|
2011-05-12 14:38:35 +00:00
|
|
|
};
|