[REF] web,*: improve the way field_utils are done
We replace a bunch of formatting and parsing functions by an object (parse or format) which can be used to dispatch on the correct type. It allows us to remove the format_field and parse_field functions. Also, we camelcase our new code and add some comments.
This commit is contained in:
committed by
Aaron Bohy
parent
7dc9eb51d6
commit
b717c67864
@@ -18,9 +18,9 @@ var ShowPaymentLineWidget = AbstractField.extend({
|
||||
if (info !== false) {
|
||||
_.each(info.content, function(k,v){
|
||||
k.index = v;
|
||||
k.amount = field_utils.format_float(k.amount, {digits: k.digits});
|
||||
k.amount = field_utils.format.float(k.amount, {digits: k.digits});
|
||||
if (k.date){
|
||||
k.date = field_utils.format_date(k.date);
|
||||
k.date = field_utils.format.date(k.date);
|
||||
}
|
||||
});
|
||||
this.$el.html(QWeb.render('ShowPaymentInfo', {
|
||||
|
||||
@@ -544,10 +544,10 @@ var StatementModel = BasicModel.extend({
|
||||
});
|
||||
|
||||
prop.amount = tax.base;
|
||||
prop.amount_str = field_utils.format_monetary(Math.abs(prop.amount), {}, format_options);
|
||||
prop.amount_str = field_utils.format.monetary(Math.abs(prop.amount), {}, format_options);
|
||||
prop.invalid = !self._isValid(prop);
|
||||
|
||||
tax_prop.amount_str = field_utils.format_monetary(Math.abs(tax_prop.amount), {}, format_options);
|
||||
tax_prop.amount_str = field_utils.format.monetary(Math.abs(tax_prop.amount), {}, format_options);
|
||||
tax_prop.invalid = prop.invalid;
|
||||
|
||||
reconciliation_proposition.push(tax_prop);
|
||||
@@ -557,7 +557,7 @@ var StatementModel = BasicModel.extend({
|
||||
currency_id: line.st_line.currency_id
|
||||
};
|
||||
|
||||
prop.amount_str = field_utils.format_monetary(Math.abs(prop.amount), {}, currencyData);
|
||||
prop.amount_str = field_utils.format.monetary(Math.abs(prop.amount), {}, currencyData);
|
||||
prop.display = self._isDisplay(prop);
|
||||
prop.invalid = !self._isValid(prop);
|
||||
}
|
||||
@@ -578,7 +578,7 @@ var StatementModel = BasicModel.extend({
|
||||
total = Math.round(total*1000)/1000 || 0;
|
||||
line.balance = {
|
||||
'amount': total,
|
||||
'amount_str': field_utils.format_monetary(Math.abs(total), {}, format_options),
|
||||
'amount_str': field_utils.format.monetary(Math.abs(total), {}, format_options),
|
||||
'account_code': self.accounts[line.st_line.open_balance_account_id],
|
||||
};
|
||||
line.balance.type = line.balance.amount ? (line.balance.amount > 0 && line.st_line.partner_id ? 0 : -1) : 1;
|
||||
@@ -720,8 +720,8 @@ var StatementModel = BasicModel.extend({
|
||||
'__focus': '__focus' in values ? values.__focus : true,
|
||||
};
|
||||
prop.base_amount = Math.sign(prop.base_amount) *
|
||||
field_utils.parse_monetary(
|
||||
field_utils.format_monetary(Math.abs(prop.base_amount), {}, line)
|
||||
field_utils.parse.monetary(
|
||||
field_utils.format.monetary(Math.abs(prop.base_amount), {}, line)
|
||||
);
|
||||
prop.amount = prop.base_amount;
|
||||
return prop;
|
||||
|
||||
@@ -322,11 +322,11 @@ var WeeklyTimesheet = form_common.FormWidget.extend(form_common.ReinitializeWidg
|
||||
},
|
||||
//converts hour value to float
|
||||
parse_client: function(value) {
|
||||
return field_utils.parse_float_time(value);
|
||||
return field_utils.parse.float_time(value);
|
||||
},
|
||||
//converts float value to hour
|
||||
format_client:function(value){
|
||||
return field_utils.format_float_time(value);
|
||||
return field_utils.format.float_time(value);
|
||||
},
|
||||
generate_o2m_value: function() {
|
||||
var ops = [];
|
||||
|
||||
@@ -20,7 +20,7 @@ var LunchPreviousOrdersWidget = FieldMany2Many.extend({
|
||||
this.fields_to_read = ['product_id', 'supplier', 'note', 'price', 'category_id', 'currency_id'];
|
||||
this.format_value = function (value) {
|
||||
var options = _.extend({}, this.nodeOptions, { data: this.recordData });
|
||||
return field_utils.format_monetary(value, this.field, options);
|
||||
return field_utils.format.monetary(value, this.field, options);
|
||||
};
|
||||
},
|
||||
get_line_value: function(id) {
|
||||
|
||||
@@ -437,7 +437,7 @@ var Gui = core.Class.extend({
|
||||
numpad_input: function(buffer, input, options) {
|
||||
var newbuf = buffer.slice(0);
|
||||
options = options || {};
|
||||
var newbuf_float = field_utils.parse_float(newbuf);
|
||||
var newbuf_float = field_utils.parse.float(newbuf);
|
||||
var decimal_point = _t.database.parameters.decimal_point;
|
||||
|
||||
if (input === decimal_point) {
|
||||
|
||||
@@ -1247,7 +1247,7 @@ exports.Orderline = Backbone.Model.extend({
|
||||
if (unit.rounding) {
|
||||
this.quantity = round_pr(quant, unit.rounding);
|
||||
var decimals = this.pos.dp['Product Unit of Measure'];
|
||||
this.quantityStr = field_utils.format_float(round_di(this.quantity, decimals), {digits: [69, decimals]});
|
||||
this.quantityStr = field_utils.format.float(round_di(this.quantity, decimals), {digits: [69, decimals]});
|
||||
} else {
|
||||
this.quantity = round_pr(quant, 1);
|
||||
this.quantityStr = this.quantity.toFixed(0);
|
||||
@@ -1693,7 +1693,7 @@ exports.Paymentline = Backbone.Model.extend({
|
||||
return this.amount;
|
||||
},
|
||||
get_amount_str: function(){
|
||||
return field_utils.format_float(this.amount, {digits: [69, this.pos.currency.decimals]});
|
||||
return field_utils.format.float(this.amount, {digits: [69, this.pos.currency.decimals]});
|
||||
},
|
||||
set_selected: function(selected){
|
||||
if(this.selected !== selected){
|
||||
|
||||
@@ -1627,7 +1627,7 @@ var PaymentScreenWidget = ScreenWidget.extend({
|
||||
var amount = this.inputbuffer;
|
||||
|
||||
if (this.inputbuffer !== "-") {
|
||||
amount = field_utils.parse_float(this.inputbuffer);
|
||||
amount = field_utils.parse.float(this.inputbuffer);
|
||||
}
|
||||
|
||||
order.selected_paymentline.set_amount(amount);
|
||||
@@ -1761,7 +1761,7 @@ var PaymentScreenWidget = ScreenWidget.extend({
|
||||
'title': tip ? _t('Change Tip') : _t('Add Tip'),
|
||||
'value': self.format_currency_no_symbol(value),
|
||||
'confirm': function(value) {
|
||||
order.set_tip(field_utils.parse_float(value));
|
||||
order.set_tip(field_utils.parse.float(value));
|
||||
self.order_changes();
|
||||
self.render_paymentlines();
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ var PosBaseWidget = Widget.extend({
|
||||
|
||||
if (typeof amount === 'number') {
|
||||
amount = round_di(amount,decimals).toFixed(decimals);
|
||||
amount = field_utils.format_float(round_di(amount, decimals), {digits: [69, decimals]});
|
||||
amount = field_utils.format.float(round_di(amount, decimals), {digits: [69, decimals]});
|
||||
}
|
||||
|
||||
return amount;
|
||||
|
||||
@@ -65,14 +65,14 @@ var SalesTeamDashboardRenderer = KanbanRenderer.extend({
|
||||
/**
|
||||
* Called from the template to format the monetary value.
|
||||
*
|
||||
* @todo: use field_utils.format_monetary
|
||||
* @todo: use field_utils.format.monetary
|
||||
* @private
|
||||
* @returns {string} formatted value
|
||||
*/
|
||||
_renderMonetaryField: function (value, currency_id) {
|
||||
var currency = session.get_currency(currency_id);
|
||||
var digits_precision = currency && currency.digits;
|
||||
value = field_utils.format_float(value || 0, {digits: digits_precision});
|
||||
value = field_utils.format.float(value || 0, {digits: digits_precision});
|
||||
if (currency) {
|
||||
if (currency.position === "after") {
|
||||
value += currency.symbol;
|
||||
|
||||
@@ -216,10 +216,10 @@ ExtendedSearchProposition.DateTime = ExtendedSearchProposition.Field.extend({
|
||||
}
|
||||
},
|
||||
toString: function () {
|
||||
var str = field_utils.format_field(this.get_value(), {type: this.attributes.type});
|
||||
var str = field_utils.format[this.attributes.type](this.get_value(), {type: this.attributes.type});
|
||||
var date_1_value = this.datewidget_1 && this.get_value(1);
|
||||
if (date_1_value) {
|
||||
str += _lt(" and ") + field_utils.format_field(date_1_value, {type: this.attributes.type});
|
||||
str += _lt(" and ") + field_utils.format[this.attributes.type](date_1_value, {type: this.attributes.type});
|
||||
}
|
||||
return str;
|
||||
},
|
||||
@@ -271,7 +271,7 @@ ExtendedSearchProposition.Integer = ExtendedSearchProposition.Field.extend({
|
||||
get_value: function () {
|
||||
try {
|
||||
var val =this.$el.val();
|
||||
return field_utils.parse_integer(val === "" ? 0 : val);
|
||||
return field_utils.parse.integer(val === "" ? 0 : val);
|
||||
} catch (e) {
|
||||
return "";
|
||||
}
|
||||
@@ -304,7 +304,7 @@ ExtendedSearchProposition.Float = ExtendedSearchProposition.Field.extend({
|
||||
get_value: function () {
|
||||
try {
|
||||
var val =this.$el.val();
|
||||
return field_utils.parse_float(val === "" ? 0.0 : val);
|
||||
return field_utils.parse.float(val === "" ? 0.0 : val);
|
||||
} catch (e) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ var IntegerField = NumberField.extend(/** @lends instance.web.search.IntegerFiel
|
||||
error_message: _t("not a valid integer"),
|
||||
parse: function (value) {
|
||||
try {
|
||||
return field_utils.parse_interger(value);
|
||||
return field_utils.parse.interger(value);
|
||||
} catch (e) {
|
||||
return NaN;
|
||||
}
|
||||
@@ -247,7 +247,7 @@ var FloatField = NumberField.extend(/** @lends instance.web.search.FloatField# *
|
||||
error_message: _t("not a valid number"),
|
||||
parse: function (value) {
|
||||
try {
|
||||
return field_utils.parse_float(value);
|
||||
return field_utils.parse.float(value);
|
||||
} catch (e) {
|
||||
return NaN;
|
||||
}
|
||||
@@ -349,7 +349,7 @@ var DateField = Field.extend(/** @lends instance.web.search.DateField# */{
|
||||
var t, v;
|
||||
try {
|
||||
t = (this.attrs && this.attrs.type === 'datetime') ? 'datetime' : 'date';
|
||||
v = field_utils.parse_field(needle, {type: t});
|
||||
v = field_utils.parse[t](needle, {type: t});
|
||||
} catch (e) {
|
||||
return $.when(null);
|
||||
}
|
||||
@@ -357,7 +357,7 @@ var DateField = Field.extend(/** @lends instance.web.search.DateField# */{
|
||||
var m = moment(v, t === 'datetime' ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD');
|
||||
if (!m.isValid()) { return $.when(null); }
|
||||
var d = m.toDate();
|
||||
var date_string = field_utils.format_field(d, {type: t.attrs.type});
|
||||
var date_string = field_utils.format[this.attrs.type](d, {type: t.attrs.type});
|
||||
var label = _.str.sprintf(_.str.escapeHTML(
|
||||
_t("Search %(field)s at: %(value)s")), {
|
||||
field: '<em>' + _.escape(this.attrs.string) + '</em>',
|
||||
|
||||
@@ -221,7 +221,7 @@ var AbstractField = Widget.extend({
|
||||
*/
|
||||
_formatValue: function (value) {
|
||||
var options = _.extend({}, this.nodeOptions, { data: this.recordData });
|
||||
return field_utils.format_field(value, this.field, options);
|
||||
return field_utils.format[this.field.type](value, this.field, options);
|
||||
},
|
||||
/**
|
||||
* convert a string representation to a valid value, depending on the field
|
||||
@@ -232,7 +232,7 @@ var AbstractField = Widget.extend({
|
||||
* @returns {any}
|
||||
*/
|
||||
_parseValue: function (value) {
|
||||
return field_utils.parse_field(value, this.field);
|
||||
return field_utils.parse[this.field.type](value, this.field);
|
||||
},
|
||||
/**
|
||||
* main rendering function. Override this if your widget has the same render
|
||||
|
||||
@@ -189,7 +189,7 @@ var FieldMonetary = InputField.extend({
|
||||
// types, such as float.
|
||||
_formatValue: function (value) {
|
||||
var options = _.extend({}, this.nodeOptions, { data: this.recordData });
|
||||
return field_utils.format_monetary(value, this.field, options);
|
||||
return field_utils.format.monetary(value, this.field, options);
|
||||
},
|
||||
isSet: function () {
|
||||
return this.value !== false;
|
||||
@@ -319,10 +319,10 @@ var FieldFloatTime = FieldFloat.extend({
|
||||
}
|
||||
var float_time_pair = value.split(":");
|
||||
if (float_time_pair.length !== 2) {
|
||||
return factor * field_utils.parse_float(value, this.field);
|
||||
return factor * field_utils.parse.float(value, this.field);
|
||||
}
|
||||
var hours = field_utils.parse_integer(float_time_pair[0]);
|
||||
var minutes = field_utils.parse_integer(float_time_pair[1]);
|
||||
var hours = field_utils.parse.integer(float_time_pair[0]);
|
||||
var minutes = field_utils.parse.integer(float_time_pair[1]);
|
||||
return factor * (hours + (minutes / 60));
|
||||
},
|
||||
});
|
||||
|
||||
@@ -9,7 +9,10 @@ odoo.define('web.field_utils', function (require) {
|
||||
*
|
||||
* Each field type has to display in string form at some point, but it should be
|
||||
* stored in memory with the actual value. For example, a float value of 0.5 is
|
||||
* represented as the string "0.5".
|
||||
* represented as the string "0.5" but is kept in memory as a float. A date
|
||||
* (or datetime) value is always stored as a Moment.js object, but displayed as
|
||||
* a string. This file contains all sort of functions necessary to perform the
|
||||
* conversions.
|
||||
*/
|
||||
|
||||
var core = require('web.core');
|
||||
@@ -17,11 +20,19 @@ var session = require('web.session');
|
||||
var time = require('web.time');
|
||||
var utils = require('web.utils');
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Format
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//------------------------------------------------------------------------------
|
||||
// Formatting
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
function format_boolean(value) {
|
||||
/**
|
||||
* @todo Really? it returns a jqueryElement... We should try to move this to a
|
||||
* module with dom helpers functions, such as web.dom, maybe. And replace this
|
||||
* with a function that returns a string
|
||||
*
|
||||
* @param {boolean} value
|
||||
* @returns {jQueryElement}
|
||||
*/
|
||||
function formatBoolean (value) {
|
||||
var $input = $('<input type="checkbox">')
|
||||
.prop('checked', value)
|
||||
.prop('disabled', true);
|
||||
@@ -31,15 +42,25 @@ function format_boolean(value) {
|
||||
.append($('<span>'));
|
||||
}
|
||||
|
||||
function format_char(value) {
|
||||
/**
|
||||
* Returns a string representing a char. If the value is false, then we return
|
||||
* an empty string.
|
||||
*
|
||||
* @param {string|false} value
|
||||
* @returns {string}
|
||||
*/
|
||||
function formatChar (value) {
|
||||
return typeof value === 'string' ? value : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @params {Moment|false}
|
||||
* Returns a string representing a date. If the value is false, then we return
|
||||
* an empty string. Note that this is dependant on the localization settings
|
||||
*
|
||||
* @param {Moment|false}
|
||||
* @returns {string}
|
||||
*/
|
||||
function format_date(value) {
|
||||
function formatDate (value) {
|
||||
if (!value) {
|
||||
return "";
|
||||
}
|
||||
@@ -49,10 +70,14 @@ function format_date(value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representing a datetime. If the value is false, then we
|
||||
* return an empty string. Note that this is dependant on the localization
|
||||
* settings
|
||||
*
|
||||
* @params {Moment|false}
|
||||
* @returns {string}
|
||||
*/
|
||||
function format_datetime(value) {
|
||||
function formatDateTime (value) {
|
||||
if (!value) {
|
||||
return "";
|
||||
}
|
||||
@@ -63,12 +88,17 @@ function format_datetime(value) {
|
||||
return value.format(datetime_format);
|
||||
}
|
||||
|
||||
// Format a float, according to the local settings
|
||||
// Params:
|
||||
// * value: a number describing the raw value of the float
|
||||
// * field [optional]: a description of a field, that may contains extra
|
||||
// information, such as a given precision
|
||||
function format_float(value, field) {
|
||||
/**
|
||||
* Returns a string representing a float. The result takes into account the
|
||||
* user settings (to display the correct decimal separator).
|
||||
*
|
||||
* @param {float} value the value that should be formatted
|
||||
* @param {Object} [field] a description of the field (returned by fields_get
|
||||
* for example). It may contain a description of the number of digits that
|
||||
* should be used.
|
||||
* @returns {string}
|
||||
*/
|
||||
function formatFloat (value, field) {
|
||||
var l10n = core._t.database.parameters;
|
||||
var precision = (field && field.digits) ? field.digits[1] : 2;
|
||||
var formatted = _.str.sprintf('%.' + precision + 'f', value || 0).split('.');
|
||||
@@ -76,7 +106,15 @@ function format_float(value, field) {
|
||||
return formatted.join(l10n.decimal_point);
|
||||
}
|
||||
|
||||
function format_float_time(value) {
|
||||
/**
|
||||
* Returns a string representing a time value, from a float. The idea is that
|
||||
* we sometimes want to display something like 1:45 instead of 1.75, or 0:15
|
||||
* instead of 0.25.
|
||||
*
|
||||
* @param {float} value
|
||||
* @returns {string}
|
||||
*/
|
||||
function formatFloatTime (value) {
|
||||
var pattern = '%02d:%02d';
|
||||
if (value < 0) {
|
||||
value = Math.abs(value);
|
||||
@@ -91,11 +129,25 @@ function format_float_time(value) {
|
||||
return _.str.sprintf(pattern, hour, min);
|
||||
}
|
||||
|
||||
function format_id(value) {
|
||||
return value ? value.toString() : false;
|
||||
/**
|
||||
* Returns a string representing an ID. If the value is false, then we
|
||||
* return an empty string.
|
||||
*
|
||||
* @param {integer|false} value
|
||||
* @returns {string}
|
||||
*/
|
||||
function formatID (value) {
|
||||
return value ? value.toString() : '';
|
||||
}
|
||||
|
||||
function format_integer(value) {
|
||||
/**
|
||||
* Returns a string representing an integer. If the value is false, then we
|
||||
* return an empty string.
|
||||
*
|
||||
* @param {integer|false} value
|
||||
* @returns {string}
|
||||
*/
|
||||
function formatInteger (value) {
|
||||
if (!value && value !== 0) {
|
||||
// previously, it returned 'false'. I don't know why. But for the Pivot
|
||||
// view, I want to display the concept of 'no value' with an empty
|
||||
@@ -105,18 +157,36 @@ function format_integer(value) {
|
||||
return utils.insert_thousand_seps(_.str.sprintf('%d', value));
|
||||
}
|
||||
|
||||
function format_many2one(value) {
|
||||
/**
|
||||
* Returns a string representing an many2one. If the value is false, then we
|
||||
* return an empty string. Note that it accepts two types of input parameters:
|
||||
* an array, in that case we assume that the many2one value is of the form
|
||||
* [id, nameget], and we return the nameget, or it can be an object, and in that
|
||||
* case, we assume that it is a record from a BasicModel.
|
||||
*
|
||||
* @param {Array|Object|false} value
|
||||
* @returns {string}
|
||||
*/
|
||||
function formatMany2one (value) {
|
||||
return value && (_.isArray(value) ? value[1] : value.data.display_name) || '';
|
||||
}
|
||||
|
||||
function format_many2many(value) {
|
||||
/**
|
||||
* Returns a string representing an many2one. That is a string containing all
|
||||
* the display_name, concatenated.
|
||||
*
|
||||
* @param {Object} value a valid element from a BasicModel, that represents a
|
||||
* list of values
|
||||
* @returns {string}
|
||||
*/
|
||||
function formatMany2Many (value) {
|
||||
var names = _.map(value.data, function(p) {
|
||||
return p.data.display_name;
|
||||
});
|
||||
return names.join(', ');
|
||||
}
|
||||
|
||||
function format_monetary(value, field, options) {
|
||||
function formatMonetary (value, field, options) {
|
||||
options = options || {};
|
||||
var currency_id = options.currency_id;
|
||||
if (!currency_id && options.data) {
|
||||
@@ -142,12 +212,7 @@ function format_monetary(value, field, options) {
|
||||
}
|
||||
}
|
||||
|
||||
function format_field(value, field, options) {
|
||||
var formatter = result['format_' + field.type];
|
||||
return formatter(value, field, options);
|
||||
}
|
||||
|
||||
function format_selection(value, field) {
|
||||
function formatSelection (value, field) {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
@@ -168,7 +233,7 @@ function format_selection(value, field) {
|
||||
* @params {string}
|
||||
* @returns {Moment|false} Moment date object
|
||||
*/
|
||||
function parse_date(value) {
|
||||
function parseDate (value) {
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
@@ -199,7 +264,7 @@ function parse_date(value) {
|
||||
* @params {string}
|
||||
* @returns {Moment|false} Moment date object
|
||||
*/
|
||||
function parse_datetime(value) {
|
||||
function parseDateTime (value) {
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
@@ -227,12 +292,7 @@ function parse_datetime(value) {
|
||||
throw new Error(_.str.sprintf(core._t("'%s' is not a correct datetime"), value));
|
||||
}
|
||||
|
||||
function parse_field(value, field) {
|
||||
var parser = result['parse_' + field.type];
|
||||
return parser(value, field);
|
||||
}
|
||||
|
||||
function parse_float(value) {
|
||||
function parseFloat (value) {
|
||||
value = value.replace(new RegExp(core._t.database.parameters.thousands_sep, "g"), '');
|
||||
value = value.replace(core._t.database.parameters.decimal_point, '.');
|
||||
var parsed = Number(value);
|
||||
@@ -242,7 +302,7 @@ function parse_float(value) {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function parse_float_time(value) {
|
||||
function parseFloatTime (value) {
|
||||
var factor = 1;
|
||||
if (value[0] === '-') {
|
||||
value = value.slice(1);
|
||||
@@ -250,13 +310,13 @@ function parse_float_time(value) {
|
||||
}
|
||||
var float_time_pair = value.split(":");
|
||||
if (float_time_pair.length !== 2)
|
||||
return factor * parse_float(value);
|
||||
var hours = parse_integer(float_time_pair[0]);
|
||||
var minutes = parse_integer(float_time_pair[1]);
|
||||
return factor * parseFloat(value);
|
||||
var hours = parseInteger(float_time_pair[0]);
|
||||
var minutes = parseInteger(float_time_pair[1]);
|
||||
return factor * (hours + (minutes / 60));
|
||||
}
|
||||
|
||||
function parse_integer(value) {
|
||||
function parseInteger (value) {
|
||||
value = value.replace(new RegExp(core._t.database.parameters.thousands_sep, "g"), '');
|
||||
var parsed = Number(value);
|
||||
// do not accept not numbers or float values
|
||||
@@ -266,7 +326,7 @@ function parse_integer(value) {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function parse_monetary(formatted_value) {
|
||||
function parseMonetary (formatted_value) {
|
||||
var l10n = core._t.database.parameters;
|
||||
var value = formatted_value.replace(l10n.thousands_sep, '')
|
||||
.replace(l10n.decimal_point, '.')
|
||||
@@ -278,48 +338,46 @@ function identity(value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
var result = {
|
||||
format_binary: identity, // todo
|
||||
format_boolean: format_boolean,
|
||||
format_char: format_char,
|
||||
format_date: format_date,
|
||||
format_datetime: format_datetime,
|
||||
format_float: format_float,
|
||||
format_float_time: format_float_time,
|
||||
format_html: identity, // todo
|
||||
format_id: format_id,
|
||||
format_integer: format_integer,
|
||||
format_many2many: format_many2many,
|
||||
format_many2one: format_many2one,
|
||||
format_monetary: format_monetary,
|
||||
format_one2many: identity, // todo
|
||||
format_reference: identity, // todo
|
||||
format_selection: format_selection,
|
||||
format_text: format_char,
|
||||
|
||||
format_field: format_field,
|
||||
|
||||
parse_binary: identity, // todo
|
||||
parse_boolean: identity, // todo
|
||||
parse_char: identity, // todo
|
||||
parse_date: parse_date, // todo
|
||||
parse_datetime: parse_datetime, // todo
|
||||
parse_float: parse_float,
|
||||
parse_float_time: parse_float_time,
|
||||
parse_html: identity, // todo
|
||||
parse_id: identity,
|
||||
parse_integer: parse_integer,
|
||||
parse_many2many: identity, // todo
|
||||
parse_many2one: identity,
|
||||
parse_monetary: parse_monetary,
|
||||
parse_one2many: identity,
|
||||
parse_reference: identity, // todo
|
||||
parse_selection: identity, // todo
|
||||
parse_text: identity, // todo
|
||||
|
||||
parse_field: parse_field,
|
||||
return {
|
||||
format: {
|
||||
binary: identity, // todo
|
||||
boolean: formatBoolean,
|
||||
char: formatChar,
|
||||
date: formatDate,
|
||||
datetime: formatDateTime,
|
||||
float: formatFloat,
|
||||
float_time: formatFloatTime,
|
||||
html: identity, // todo
|
||||
id: formatID,
|
||||
integer: formatInteger,
|
||||
many2many: formatMany2Many,
|
||||
many2one: formatMany2one,
|
||||
monetary: formatMonetary,
|
||||
one2many: identity, // todo
|
||||
reference: identity, // todo
|
||||
selection: formatSelection,
|
||||
text: formatChar,
|
||||
},
|
||||
parse: {
|
||||
binary: identity,
|
||||
boolean: identity, // todo
|
||||
char: identity, // todo
|
||||
date: parseDate, // todo
|
||||
datetime: parseDateTime, // todo
|
||||
float: parseFloat,
|
||||
float_time: parseFloatTime,
|
||||
html: identity, // todo
|
||||
id: identity,
|
||||
integer: parseInteger,
|
||||
many2many: identity, // todo
|
||||
many2one: identity,
|
||||
monetary: parseMonetary,
|
||||
one2many: identity,
|
||||
reference: identity, // todo
|
||||
selection: identity, // todo
|
||||
text: identity, // todo
|
||||
},
|
||||
};
|
||||
|
||||
return result;
|
||||
|
||||
});
|
||||
|
||||
@@ -128,7 +128,7 @@ var FieldMany2One = AbstractRelationalField.extend(mixins.ServicesMixin, {
|
||||
this.nodeOptions = _.defaults(this.nodeOptions, {
|
||||
quick_create: true,
|
||||
});
|
||||
this.m2o_value = field_utils.format_many2one(this.value);
|
||||
this.m2o_value = field_utils.format.many2one(this.value);
|
||||
},
|
||||
start: function () {
|
||||
// what is 'this.floating' for? add a comment!
|
||||
@@ -279,7 +279,7 @@ var FieldMany2One = AbstractRelationalField.extend(mixins.ServicesMixin, {
|
||||
*/
|
||||
_reset: function () {
|
||||
this._super.apply(this, arguments);
|
||||
this.m2o_value = field_utils.format_many2one(this.value);
|
||||
this.m2o_value = field_utils.format.many2one(this.value);
|
||||
},
|
||||
/**
|
||||
* @private
|
||||
@@ -502,7 +502,7 @@ var KanbanFieldMany2One = AbstractRelationalField.extend({
|
||||
tagName: 'span',
|
||||
init: function () {
|
||||
this._super.apply(this, arguments);
|
||||
this.m2o_value = field_utils.format_many2one(this.value);
|
||||
this.m2o_value = field_utils.format.many2one(this.value);
|
||||
},
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
@@ -1345,7 +1345,7 @@ var FieldSelection = AbstractField.extend({
|
||||
_formatValue: function (value) {
|
||||
if (this.field.type === 'many2one') {
|
||||
var options = _.extend({}, this.nodeOptions);
|
||||
return field_utils.format_many2one(value, this.field, this.recordData, options);
|
||||
return field_utils.format.many2one(value, this.field, this.recordData, options);
|
||||
} else {
|
||||
return this._super(value);
|
||||
}
|
||||
|
||||
@@ -537,10 +537,10 @@ var BasicModel = AbstractModel.extend({
|
||||
});
|
||||
} else if (field.type === 'date') {
|
||||
// process date: convert into Date object
|
||||
record._changes[name] = fieldUtils.parse_date(result[name]);
|
||||
record._changes[name] = fieldUtils.parse.date(result[name]);
|
||||
} else if (field.type === 'datetime') {
|
||||
// process datetime: convert into Date object
|
||||
record._changes[name] = fieldUtils.parse_datetime(result[name]);
|
||||
record._changes[name] = fieldUtils.parse.datetime(result[name]);
|
||||
} else {
|
||||
record._changes[name] = result[name];
|
||||
}
|
||||
@@ -1278,10 +1278,10 @@ var BasicModel = AbstractModel.extend({
|
||||
}
|
||||
} else if (field.type === 'date') {
|
||||
// process data: convert into Date object
|
||||
record.data[name] = fieldUtils.parse_date(val);
|
||||
record.data[name] = fieldUtils.parse.date(val);
|
||||
} else if (field.type === 'datetime') {
|
||||
// process datetime: convert into Date object
|
||||
record.data[name] = fieldUtils.parse_datetime(val);
|
||||
record.data[name] = fieldUtils.parse.datetime(val);
|
||||
}
|
||||
});
|
||||
}).then(function () {
|
||||
@@ -1927,10 +1927,10 @@ var BasicModel = AbstractModel.extend({
|
||||
dataPoint.data[name] = r.id;
|
||||
}
|
||||
if (field.type === 'date') {
|
||||
dataPoint.data[name] = fieldUtils.parse_date(dataPoint.data[name]);
|
||||
dataPoint.data[name] = fieldUtils.parse.date(dataPoint.data[name]);
|
||||
}
|
||||
if (field.type === 'datetime') {
|
||||
dataPoint.data[name] = fieldUtils.parse_datetime(dataPoint.data[name]);
|
||||
dataPoint.data[name] = fieldUtils.parse.datetime(dataPoint.data[name]);
|
||||
}
|
||||
});
|
||||
return dataPoint.id;
|
||||
|
||||
@@ -449,7 +449,7 @@ return AbstractModel.extend({
|
||||
var _value = record[filter.write_field];
|
||||
var value = _.isArray(_value) ? _value[0] : _value;
|
||||
var f = _.find(filter.filters, function (f) {return f.value === value;});
|
||||
var formater = field_utils['format_'+ (_.contains(['many2many', 'one2many'], field.type) ? 'many2one' : field.type)];
|
||||
var formater = field_utils.format[_.contains(['many2many', 'one2many'], field.type) ? 'many2one' : field.type];
|
||||
return {
|
||||
'id': record.id,
|
||||
'value': value,
|
||||
@@ -528,7 +528,7 @@ return AbstractModel.extend({
|
||||
fs.push({
|
||||
'color': self.model_color === (field.relation || element.model) ? value : false,
|
||||
'value': value,
|
||||
'label': field_utils['format_'+ field.type](_value, field),
|
||||
'label': field_utils.format[field.type](_value, field),
|
||||
'avatar_model': field.relation || element.model,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -270,7 +270,7 @@ return AbstractRenderer.extend({
|
||||
*/
|
||||
_format: function (record, fieldName) {
|
||||
var field = this.state.fields[fieldName];
|
||||
return field_utils['format_'+field.type](record[fieldName], field);
|
||||
return field_utils.format[field.type](record[fieldName], field);
|
||||
},
|
||||
/**
|
||||
* Initialize the main calendar
|
||||
|
||||
@@ -175,7 +175,7 @@ return AbstractRenderer.extend({
|
||||
});
|
||||
chart.yAxis.tickFormat(function (d) {
|
||||
var measure_field = self.state.fields[self.measure];
|
||||
return field_utils.format_float(d, {
|
||||
return field_utils.format.float(d, {
|
||||
digits: measure_field && measure_field.digits || [69, 2],
|
||||
});
|
||||
});
|
||||
@@ -319,7 +319,7 @@ return AbstractRenderer.extend({
|
||||
chart.xAxis.tickValues(tickValues)
|
||||
.tickFormat(tickFormat);
|
||||
chart.yAxis.tickFormat(function (d) {
|
||||
return field_utils.format_float(d, {
|
||||
return field_utils.format.float(d, {
|
||||
digits : self.state.fields[self.state.measure] && self.state.fields[self.state.measure].digits || [69, 2],
|
||||
});
|
||||
});
|
||||
|
||||
@@ -130,7 +130,7 @@ var KanbanRecord = Widget.extend({
|
||||
var recordData = self.state.data;
|
||||
var value = recordData[field_name];
|
||||
var options = { data: recordData };
|
||||
var formatted_value = field_utils.format_field(value, field, options);
|
||||
var formatted_value = field_utils.format[field.type](value, field, options);
|
||||
$field.replaceWith(formatted_value);
|
||||
}
|
||||
});
|
||||
@@ -186,7 +186,7 @@ var KanbanRecord = Widget.extend({
|
||||
}
|
||||
|
||||
if (r.type) {
|
||||
var formatter = field_utils['format_' + r.type];
|
||||
var formatter = field_utils.format[r.type];
|
||||
r.value = formatter(value, self.fields[name], record, self.state);
|
||||
} else {
|
||||
r.value = value;
|
||||
|
||||
@@ -243,7 +243,7 @@ var ListRenderer = BasicRenderer.extend({
|
||||
var field = self.state.fields[column.attrs.name];
|
||||
var value = aggregateValues[column.attrs.name].value;
|
||||
var help = aggregateValues[column.attrs.name].help;
|
||||
var formattedValue = field_utils['format_' + field.type](value, field, {});
|
||||
var formattedValue = field_utils.format[field.type](value, field, {});
|
||||
$cell.addClass('o_list_number').attr('title', help).html(formattedValue);
|
||||
}
|
||||
return $cell;
|
||||
@@ -311,7 +311,7 @@ var ListRenderer = BasicRenderer.extend({
|
||||
return $td;
|
||||
}
|
||||
$td.addClass(FIELD_CLASSES[field.type]);
|
||||
var formatted_value = field_utils.format_field(value, field, { data: record.data });
|
||||
var formatted_value = field_utils.format[field.type](value, field, { data: record.data });
|
||||
return $td.html(formatted_value);
|
||||
},
|
||||
/**
|
||||
|
||||
@@ -117,7 +117,7 @@ var PivotRenderer = AbstractRenderer.extend({
|
||||
if (value !== undefined) {
|
||||
name = this.state.measures[j % nbrMeasures];
|
||||
measure = this.state.fields[name];
|
||||
value = field_utils['format_' + measureTypes[j % nbrMeasures]](value, measure);
|
||||
value = field_utils.format[measureTypes[j % nbrMeasures]](value, measure);
|
||||
}
|
||||
$cell = $('<td>')
|
||||
.data('id', rows[i].id)
|
||||
|
||||
@@ -83,10 +83,10 @@ var DateWidget = Widget.extend({
|
||||
}
|
||||
},
|
||||
parse_client: function(v) {
|
||||
return field_utils.parse_field(v, {type: this.type_of_date});
|
||||
return field_utils.parse[this.type_of_date](v, {type: this.type_of_date});
|
||||
},
|
||||
format_client: function(v) {
|
||||
return field_utils.format_field(v, {type: this.type_of_date});
|
||||
return field_utils.format[this.type_of_date](v, {type: this.type_of_date});
|
||||
},
|
||||
set_datetime_default: function() {
|
||||
//when opening datetimepicker the date and time by default should be the one from
|
||||
|
||||
@@ -327,7 +327,7 @@ DebugManager.include({
|
||||
size: 'medium',
|
||||
$content: QWeb.render('WebClient.DebugViewLog', {
|
||||
perm : result[0],
|
||||
format : field_utils.format_field
|
||||
format : field_utils.format
|
||||
})
|
||||
}).open();
|
||||
});
|
||||
|
||||
@@ -384,7 +384,7 @@ var DomainLeaf = DomainNode.extend({
|
||||
try {
|
||||
var f = this.fieldSelector.selectedField;
|
||||
if (!f.relation) { // TODO in this case, the value should be m2o input, etc...
|
||||
this.displayValue = field_utils.format_field(this.value, this.fieldSelector.selectedField);
|
||||
this.displayValue = field_utils.format[f.type](this.value, f);
|
||||
}
|
||||
} catch (err) {/**/}
|
||||
this.displayOperator = this.operator;
|
||||
@@ -470,9 +470,10 @@ var DomainLeaf = DomainNode.extend({
|
||||
/// @param silent - true if the method call should not trigger_up a domain_changed event
|
||||
/// @trigger_up domain_changed event to ask for a re-rendering
|
||||
onValueChange: function (value, silent) {
|
||||
var field = this.fieldSelector.selectedField;
|
||||
var couldNotParse = false;
|
||||
try {
|
||||
this.value = field_utils.parse_field(value, this.fieldSelector.selectedField);
|
||||
this.value = field_utils.parse[field.type](value, field);
|
||||
} catch (err) {
|
||||
this.value = value;
|
||||
couldNotParse = true;
|
||||
@@ -488,7 +489,7 @@ var DomainLeaf = DomainNode.extend({
|
||||
}
|
||||
} else if (_.contains(["date", "datetime"], this.fieldSelector.selectedField.type)) {
|
||||
if (couldNotParse || _.isBoolean(this.value)) {
|
||||
this.value = field_utils.parse_field(field_utils.format_field(Date.now(), this.fieldSelector.selectedField), this.fieldSelector.selectedField);
|
||||
this.value = field_utils.parse[field.type](field_utils.format[field.type](Date.now(), field), field);
|
||||
}
|
||||
} else {
|
||||
if (_.isBoolean(this.value)) { // Never display "true" or "false" strings from boolean value
|
||||
|
||||
@@ -177,19 +177,19 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Creation User:</th>
|
||||
<td><t t-esc="format(perm.create_uid, { 'type' : 'many2one' }, '/')"/></td>
|
||||
<td><t t-esc="format.many2one(perm.create_uid, { 'type' : 'many2one' }, '/')"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Creation Date:</th>
|
||||
<td><t t-esc="format(perm.create_date, { 'type' : 'datetime' }, '/')"/></td>
|
||||
<td><t t-esc="format.datetime(perm.create_date, { 'type' : 'datetime' }, '/')"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Latest Modification by:</th>
|
||||
<td><t t-esc="format(perm.write_uid, { 'type' : 'many2one' }, '/')"/></td>
|
||||
<td><t t-esc="format.many2one(perm.write_uid, { 'type' : 'many2one' }, '/')"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Latest Modification Date:</th>
|
||||
<td><t t-esc="format(perm.write_date, { 'type' : 'datetime' }, '/')"/></td>
|
||||
<td><t t-esc="format.datetime(perm.write_date, { 'type' : 'datetime' }, '/')"/></td>
|
||||
</tr>
|
||||
</table>
|
||||
</t>
|
||||
|
||||
@@ -3,7 +3,6 @@ odoo.define('web.field_utils_tests', function (require) {
|
||||
|
||||
var core = require('web.core');
|
||||
var fieldUtils = require('web.field_utils');
|
||||
var time = require('web.time');
|
||||
|
||||
QUnit.module('fields', {}, function () {
|
||||
|
||||
@@ -15,16 +14,16 @@ QUnit.test('format integer', function(assert) {
|
||||
var originalGrouping = core._t.database.parameters.grouping;
|
||||
|
||||
core._t.database.parameters.grouping = [3, 3, 3, 3];
|
||||
assert.strictEqual(fieldUtils.format_integer(1000000), '1,000,000');
|
||||
assert.strictEqual(fieldUtils.format.integer(1000000), '1,000,000');
|
||||
|
||||
core._t.database.parameters.grouping = [3, 2, -1];
|
||||
assert.strictEqual(fieldUtils.format_integer(106500), '1,06,500');
|
||||
assert.strictEqual(fieldUtils.format.integer(106500), '1,06,500');
|
||||
|
||||
core._t.database.parameters.grouping = [1, 2, -1];
|
||||
assert.strictEqual(fieldUtils.format_integer(106500), '106,50,0');
|
||||
assert.strictEqual(fieldUtils.format.integer(106500), '106,50,0');
|
||||
|
||||
assert.strictEqual(fieldUtils.format_integer(0), "0");
|
||||
assert.strictEqual(fieldUtils.format_integer(undefined), "");
|
||||
assert.strictEqual(fieldUtils.format.integer(0), "0");
|
||||
assert.strictEqual(fieldUtils.format.integer(undefined), "");
|
||||
|
||||
core._t.database.parameters.grouping = originalGrouping;
|
||||
});
|
||||
@@ -35,20 +34,20 @@ QUnit.test('format float', function(assert) {
|
||||
var originalParameters = $.extend(true, {}, core._t.database.parameters);
|
||||
|
||||
core._t.database.parameters.grouping = [3, 3, 3, 3];
|
||||
assert.strictEqual(fieldUtils.format_float(1000000), '1,000,000.00');
|
||||
assert.strictEqual(fieldUtils.format.float(1000000), '1,000,000.00');
|
||||
|
||||
core._t.database.parameters.grouping = [3, 2, -1];
|
||||
assert.strictEqual(fieldUtils.format_float(106500), '1,06,500.00');
|
||||
assert.strictEqual(fieldUtils.format.float(106500), '1,06,500.00');
|
||||
|
||||
core._t.database.parameters.grouping = [1, 2, -1];
|
||||
assert.strictEqual(fieldUtils.format_float(106500), '106,50,0.00');
|
||||
assert.strictEqual(fieldUtils.format.float(106500), '106,50,0.00');
|
||||
|
||||
_.extend(core._t.database.parameters, {
|
||||
grouping: [3, 0],
|
||||
decimal_point: ',',
|
||||
thousands_sep: '.'
|
||||
});
|
||||
assert.strictEqual(fieldUtils.format_float(6000), '6.000,00');
|
||||
assert.strictEqual(fieldUtils.format.float(6000), '6.000,00');
|
||||
|
||||
core._t.database.parameters = originalParameters;
|
||||
});
|
||||
@@ -57,16 +56,16 @@ QUnit.test("format_datetime", function (assert) {
|
||||
assert.expect(1);
|
||||
|
||||
var date_string = "2009-05-04 12:34:23";
|
||||
var date = fieldUtils.parse_datetime(date_string);
|
||||
var str = fieldUtils.format_datetime(date);
|
||||
var date = fieldUtils.parse.datetime(date_string);
|
||||
var str = fieldUtils.format.datetime(date);
|
||||
assert.strictEqual(str, moment(date).format("MM/DD/YYYY HH:mm:ss"));
|
||||
});
|
||||
|
||||
QUnit.test("format_many2one", function (assert) {
|
||||
assert.expect(2);
|
||||
|
||||
assert.strictEqual('', fieldUtils.format_many2one(null));
|
||||
assert.strictEqual('A M2O value', fieldUtils.format_many2one({
|
||||
assert.strictEqual('', fieldUtils.format.many2one(null));
|
||||
assert.strictEqual('A M2O value', fieldUtils.format.many2one({
|
||||
data: { display_name: 'A M2O value' },
|
||||
}));
|
||||
});
|
||||
@@ -75,7 +74,7 @@ QUnit.test("format_many2one", function (assert) {
|
||||
QUnit.test('formatting chars', function(assert) {
|
||||
assert.expect(1);
|
||||
|
||||
assert.strictEqual(fieldUtils.format_char(), '',
|
||||
assert.strictEqual(fieldUtils.format.char(), '',
|
||||
"undefined char should be formatted as an empty string");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user