From 2512d69e675c31192c6de96f14d07306e82ff64c Mon Sep 17 00:00:00 2001 From: Sarah Vaupel Date: Tue, 27 Aug 2019 16:51:41 +0200 Subject: [PATCH] feat(datepicker): helper functions and updated tail.datetime fork --- frontend/src/utils/form/datepicker.js | 35 ++++++++++++++++----------- package-lock.json | 2 +- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/frontend/src/utils/form/datepicker.js b/frontend/src/utils/form/datepicker.js index b2e724261..fc6b254c1 100644 --- a/frontend/src/utils/form/datepicker.js +++ b/frontend/src/utils/form/datepicker.js @@ -14,11 +14,6 @@ const FORM_DATE_FORMAT = { // FANCY (tail.datetime specific) formats for displaying dates and/or times const FORM_DATE_FORMAT_DATE_DT = 'dd.mm.YYYY'; const FORM_DATE_FORMAT_TIME_DT = 'HH:ii:ss'; -// const FORM_DATE_FORMAT_DT = { -// 'date': FORM_DATE_FORMAT_DATE_DT, -// 'time': FORM_DATE_FORMAT_TIME_DT, -// 'datetime-local': `${FORM_DATE_FORMAT_DATE_DT} ${FORM_DATE_FORMAT_TIME_DT}`, -// }; // FANCY (moment specific) formats for displaying dates and/or times const FORM_DATE_FORMAT_DATE_MOMENT = 'DD.MM.YYYY'; @@ -29,6 +24,25 @@ const FORM_DATE_FORMAT_MOMENT = { 'datetime-local': `${FORM_DATE_FORMAT_DATE_MOMENT} ${FORM_DATE_FORMAT_TIME_MOMENT}`, }; +/** + * Takes a string representation of a date and a format string and parses the given date to a Date object. + * @param {*} dateStr string representation of a date + * @param {*} dateFormat format string of the date + */ +function parseDateWithFormat(dateStr, dateFormat) { + return moment(dateStr, dateFormat).toDate(); +} + +/** + * Takes a string representation of a date, an input ('previous') format and a desired output format and returns a reformatted date string. + * @param {*} dateStr string representation of a date (needs to be in format formatIn) + * @param {*} formatIn input format string + * @param {*} formatOut format string of the desired output date string + */ +function reformatDateString(dateStr, formatIn, formatOut) { + return moment(dateStr, formatIn).format(formatOut); +} + const DATEPICKER_UTIL_SELECTOR = 'input[type="date"], input[type="time"], input[type="datetime-local"]'; const DATEPICKER_INITIALIZED_CLASS = 'datepicker--initialized'; @@ -174,19 +188,12 @@ export class Datepicker { * @param {*} toFancy optional target format switch (boolean value; default is false). If set to a truthy value, formats the element value to fancy instead of internal date format. */ formatElementValue(toFancy) { - // console.log("Hello from formatElementValue with toFancy ", toFancy, " and element value ", this._element.value, " and datepicker instance ", this.datepickerInstance); - const dp = this.datepickerInstance; if (this._element.value) { if (toFancy) { - // console.log(" toFancy"); - const d = new Date(this._element.value); - // console.log(" calling selectDate with ", d); - dp.selectDate(d); + dp.selectDate(parseDateWithFormat(this._element.value, FORM_DATE_FORMAT[this.elementType])); } else { - // console.log(" not toFancy"); this._element.value = this.unformat(); - // console.log(" new element value is ", this._element.value); } } } @@ -195,7 +202,7 @@ export class Datepicker { * Returns a datestring in internal format from the current state of the input element value. */ unformat() { - return moment(this._element.value, FORM_DATE_FORMAT_MOMENT[this.elementType]).format(FORM_DATE_FORMAT[this.elementType]); + return reformatDateString(this._element.value, FORM_DATE_FORMAT_MOMENT[this.elementType], FORM_DATE_FORMAT[this.elementType]); } /** diff --git a/package-lock.json b/package-lock.json index 4797508cf..0bc368ac1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15220,7 +15220,7 @@ } }, "tail.datetime": { - "version": "git+https://git@github.com/uni2work/tail.DateTime.git#7574f916023833ff7ee9e2aa070e0bbc767ed070", + "version": "git+https://git@github.com/uni2work/tail.DateTime.git#9d7140ad1ca8a28dbc514230127bddf48312f073", "from": "git+https://git@github.com/uni2work/tail.DateTime.git#update-view-on-select" }, "tapable": {