diff options
| author | Michael Mattiello (mcm) <mcm@odoo.com> | 2020-08-03 12:17:25 +0000 |
|---|---|---|
| committer | Michael Mattiello (mcm) <mcm@odoo.com> | 2020-08-11 14:15:43 +0000 |
| commit | a5cca2cda3b5dc9468961fe7bebeb1257d67ecdd (patch) | |
| tree | 1d2aa2b4c2ffeff4c6981f9abcb725a337ab0550 /addons/web/static/src/js/fields/field_utils.js | |
| parent | 6184070923b618a6923822a2b2c37c7d9835825f (diff) | |
[IMP] web: add smart date input on date field
This commit adds smart date inputs for date and datetime fields.
The goal of smart date input is to provide the user some shortcuts
when setting dates.
The rule is [+-]\d+[dwmy]?
So we can enter inputs like:
+3 to have today + 3 days
-2w to have today - 2 weeks
+1y to have today + 1 year
+5m to have today + 5 months
-4d to have today - 4 days
closes odoo/odoo#55602
Task: 2270347
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
Diffstat (limited to 'addons/web/static/src/js/fields/field_utils.js')
| -rw-r--r-- | addons/web/static/src/js/fields/field_utils.js | 66 |
1 files changed, 56 insertions, 10 deletions
diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js index 8f8b9bbb2ab..4ffceb93408 100644 --- a/addons/web/static/src/js/fields/field_utils.js +++ b/addons/web/static/src/js/fields/field_utils.js @@ -414,6 +414,42 @@ function formatSelection(value, field, options) { //////////////////////////////////////////////////////////////////////////////// /** + * Smart date inputs are shortcuts to write dates quicker. + * These shortcuts should respect the format ^[+-]\d+[dmwy]?$ + * + * e.g. + * "+1d" or "+1" will return now + 1 day + * "-2w" will return now - 2 weeks + * "+3m" will return now + 3 months + * "-4y" will return now + 4 years + * + * @param {string} value + * @returns {Moment|false} Moment date object + */ +function parseSmartDateInput(value) { + const units = { + d: 'days', + m: 'months', + w: 'weeks', + y: 'years', + }; + const re = new RegExp(`^([+-])(\\d+)([${Object.keys(units).join('')}]?)$`); + const match = re.exec(value); + if (match) { + let date = moment(); + const offset = parseInt(match[2], 10); + const unit = units[match[3] || 'd']; + if (match[1] === '+') { + date.add(offset, unit); + } else { + date.subtract(offset, unit); + } + return date; + } + return false; +} + +/** * Create an Date object * The method toJSON return the formated value to send value server side * @@ -433,11 +469,16 @@ function parseDate(value, field, options) { var datePattern = time.getLangDateFormat(); var datePatternWoZero = datePattern.replace('MM', 'M').replace('DD', 'D'); var date; - if (options && options.isUTC) { - value = value.padStart(10, "0"); // server may send "932-10-10" for "0932-10-10" on some OS - date = moment.utc(value); + const smartDate = parseSmartDateInput(value); + if (smartDate) { + date = smartDate; } else { - date = moment.utc(value, [datePattern, datePatternWoZero, moment.ISO_8601]); + if (options && options.isUTC) { + value = value.padStart(10, "0"); // server may send "932-10-10" for "0932-10-10" on some OS + date = moment.utc(value); + } else { + date = moment.utc(value, [datePattern, datePatternWoZero, moment.ISO_8601]); + } } if (date.isValid()) { if (date.year() === 0) { @@ -475,13 +516,18 @@ function parseDateTime(value, field, options) { var pattern1 = datePattern + ' ' + timePattern; var pattern2 = datePatternWoZero + ' ' + timePatternWoZero; var datetime; - if (options && options.isUTC) { - // phatomjs crash if we don't use this format - datetime = moment.utc(value.replace(' ', 'T') + 'Z'); + const smartDate = parseSmartDateInput(value); + if (smartDate) { + datetime = smartDate; } else { - datetime = moment.utc(value, [pattern1, pattern2, moment.ISO_8601]); - if (options && options.timezone) { - datetime.add(-session.getTZOffset(datetime), 'minutes'); + if (options && options.isUTC) { + // phatomjs crash if we don't use this format + datetime = moment.utc(value.replace(' ', 'T') + 'Z'); + } else { + datetime = moment.utc(value, [pattern1, pattern2, moment.ISO_8601]); + if (options && options.timezone) { + datetime.add(-session.getTZOffset(datetime), 'minutes'); + } } } if (datetime.isValid()) { |
