summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/fields/field_utils.js
diff options
context:
space:
mode:
authorChristophe Matthieu <chm@odoo.com>2017-02-23 14:08:33 +0100
committerAaron Bohy <aab@odoo.com>2017-04-11 19:44:38 +0200
commit3c0190d0a6c0d5ccc130fd9b685681fa9bac1de3 (patch)
tree9dda80c321a23ce122b092fb8b62a4f3e44bd271 /addons/web/static/src/js/fields/field_utils.js
parentcce579434b172eeb63049a471334e18df15fa401 (diff)
[IMP] web: use moment object instead of string for date
Before this commit, the odoo web client used strings to represent dates. Now, the date is parsed into a moment object when the value is read from the server side. The widget formats the object to display the value. When the widget value changes, the value is parsed to a moment object. The value is automatically formatted to server value in ajax calls (toJSON method).
Diffstat (limited to 'addons/web/static/src/js/fields/field_utils.js')
-rw-r--r--addons/web/static/src/js/fields/field_utils.js38
1 files changed, 32 insertions, 6 deletions
diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js
index fb20839fa2d..61e31aeb66f 100644
--- a/addons/web/static/src/js/fields/field_utils.js
+++ b/addons/web/static/src/js/fields/field_utils.js
@@ -35,18 +35,26 @@ function format_char(value) {
return typeof value === 'string' ? value : '';
}
+/**
+ * @params {Moment}
+ * @returns {string}
+ */
function format_date(value) {
var l10n = core._t.database.parameters;
var date_format = time.strftime_to_moment_format(l10n.date_format);
- return value && moment(time.str_to_date(value), moment.ISO_8601).format(date_format);
+ return value.format(date_format);
}
+/**
+ * @params {Moment}
+ * @returns {string}
+ */
function format_datetime(value) {
var l10n = core._t.database.parameters;
var date_format = time.strftime_to_moment_format(l10n.date_format);
var time_format = time.strftime_to_moment_format(l10n.time_format);
var datetime_format = date_format + ' ' + time_format;
- return value && moment(time.str_to_datetime(value), moment.ISO_8601).format(datetime_format);
+ return value.format(datetime_format);
}
// Format a float, according to the local settings
@@ -147,12 +155,20 @@ function format_selection(value, field) {
// Parse
////////////////////////////////////////////////////////////////////////////////
+/**
+ * create an Date object
+ * The method toJSON return the formated value to send value server side
+ *
+ * @params {string}
+ * @returns {Moment} Moment date object
+ */
function parse_date(value) {
var date_pattern = time.strftime_to_moment_format(core._t.database.parameters.date_format);
var date_pattern_wo_zero = date_pattern.replace('MM','M').replace('DD','D');
var date = moment(value, [date_pattern, date_pattern_wo_zero, moment.ISO_8601], true);
if (date.isValid() && date.year() >= 1900) {
- return time.date_to_str(date.toDate());
+ date.toJSON = time.date_to_str.bind(time, date.toDate());
+ return date;
}
date = moment(value, [date_pattern, date_pattern_wo_zero, moment.ISO_8601]);
if (date.isValid()) {
@@ -160,12 +176,20 @@ function parse_date(value) {
date.year(moment.utc().year());
}
if (date.year() >= 1900) {
- return time.date_to_str(date.toDate());
+ date.toJSON = time.date_to_str.bind(time, date.toDate());
+ return date;
}
}
throw new Error(_.str.sprintf(core._t("'%s' is not a correct date"), value));
}
+/**
+ * create an Date object
+ * The method toJSON return the formated value to send value server side
+ *
+ * @params {string}
+ * @returns {Moment} Moment date object
+ */
function parse_datetime(value) {
var date_pattern = time.strftime_to_moment_format(core._t.database.parameters.date_format),
time_pattern = time.strftime_to_moment_format(core._t.database.parameters.time_format);
@@ -175,7 +199,8 @@ function parse_datetime(value) {
var pattern2 = date_pattern_wo_zero + ' ' + time_pattern_wo_zero;
var datetime = moment(value, [pattern1, pattern2, moment.ISO_8601], true);
if (datetime.isValid() && datetime.year() >= 1900) {
- return time.datetime_to_str(datetime.toDate());
+ datetime.toJSON = time.datetime_to_str.bind(time, datetime.toDate());
+ return datetime;
}
datetime = moment(value, [pattern1, pattern2, moment.ISO_8601]);
if (datetime.isValid()) {
@@ -183,7 +208,8 @@ function parse_datetime(value) {
datetime.year(moment.utc().year());
}
if (datetime.year() >= 1900) {
- return time.datetime_to_str(datetime.toDate());
+ datetime.toJSON = time.datetime_to_str.bind(time, datetime.toDate());
+ return datetime;
}
}
throw new Error(_.str.sprintf(core._t("'%s' is not a correct datetime"), value));