feat(datepicker): helper functions and updated tail.datetime fork

This commit is contained in:
Sarah Vaupel 2019-08-27 16:51:41 +02:00
parent 9ea7b2e3f7
commit 2512d69e67
2 changed files with 22 additions and 15 deletions

View File

@ -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]);
}
/**

2
package-lock.json generated
View File

@ -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": {