Get Et2Date loading & formatting value

This commit is contained in:
nathan 2021-08-19 15:09:00 -06:00
parent b4a13037e2
commit 381d12dfa2
3 changed files with 53 additions and 29 deletions

View File

@ -9,8 +9,9 @@
*/ */
import {css, html} from "../../../node_modules/@lion/core/index.js" import {css, html} from "../../../node_modules/@lion/core/index.js";
import {LionInputDatepicker} from "../../../node_modules/@lion/input-datepicker/index.js" import {LionInputDatepicker} from "../../../node_modules/@lion/input-datepicker/index.js";
import {Unparseable} from "../../../node_modules/@lion/form-core/src/validate/Unparseable.js";
import {Et2InputWidget} from "./et2_core_inputWidget"; import {Et2InputWidget} from "./et2_core_inputWidget";
import {Et2Widget} from "./Et2Widget"; import {Et2Widget} from "./Et2Widget";
@ -23,13 +24,27 @@ import {Et2Widget} from "./Et2Widget";
*/ */
export function parseDate(dateString) export function parseDate(dateString)
{ {
debugger; // First try the server format
try
{
let date = new Date(dateString);
if(date instanceof Date)
{
return date;
}
}
catch(e)
{
// Nope, that didn't parse directly
}
let formatString = <string>(egw.preference("dateformat") || 'Y-m-d'); let formatString = <string>(egw.preference("dateformat") || 'Y-m-d');
formatString = formatString.replaceAll(/-\/\./ig, '-'); formatString = formatString.replaceAll(/-\/\./ig, '-');
let parsedString = ""; let parsedString = "";
switch (formatString) switch(formatString)
{ {
case 'd-m-Y': case 'd-m-Y':
case 'd/m/Y':
parsedString = `${dateString.slice(6, 10)}/${dateString.slice( parsedString = `${dateString.slice(6, 10)}/${dateString.slice(
3, 3,
5, 5,
@ -42,6 +57,7 @@ export function parseDate(dateString)
)}/${dateString.slice(3, 5)}`; )}/${dateString.slice(3, 5)}`;
break; break;
case 'Y-m-d': case 'Y-m-d':
case 'Y/m/d':
parsedString = `${dateString.slice(0, 4)}/${dateString.slice( parsedString = `${dateString.slice(0, 4)}/${dateString.slice(
5, 5,
7, 7,
@ -83,7 +99,6 @@ export function parseDate(dateString)
*/ */
export function formatDate(date: Date, options): string export function formatDate(date: Date, options): string
{ {
debugger;
if (!date || !(date instanceof Date)) if (!date || !(date instanceof Date))
{ {
return ""; return "";
@ -95,8 +110,8 @@ export function formatDate(date: Date, options): string
let dateformat = options.dateFormat || <string>egw.preference("dateformat") || 'Y-m-d'; let dateformat = options.dateFormat || <string>egw.preference("dateformat") || 'Y-m-d';
var replace_map = { var replace_map = {
d: "" + date.getUTCDate(), d: (date.getUTCDate() < 10 ? "0" : "") + date.getUTCDate(),
m: "" + date.getUTCMonth() + 1, m: (date.getUTCMonth() < 9 ? "0" : "") + (date.getUTCMonth() + 1),
Y: "" + date.getUTCFullYear() Y: "" + date.getUTCFullYear()
} }
var re = new RegExp(Object.keys(replace_map).join("|"), "gi"); var re = new RegExp(Object.keys(replace_map).join("|"), "gi");
@ -122,20 +137,7 @@ export class Et2Date extends Et2InputWidget(Et2Widget(LionInputDatepicker))
static get properties() static get properties()
{ {
return { return {
...super.properties, ...super.properties
value: {
attribute: true,
converter: {
toAttribute(value)
{
return value ? value.toJSON().replace(/\.\d{3}Z$/, 'Z') : "";
},
fromAttribute(value)
{
return new Date(value);
}
}
},
} }
} }
@ -149,14 +151,36 @@ export class Et2Date extends Et2InputWidget(Et2Widget(LionInputDatepicker))
connectedCallback() connectedCallback()
{ {
super.connectedCallback(); super.connectedCallback();
} }
/**
* @param {Date} modelValue
*/
// eslint-disable-next-line class-methods-use-this
serializer(modelValue : Date)
{
// isValidDate() is hidden inside LionInputDate, and not exported
//if(!isValidDate(modelValue))
if(!(modelValue instanceof Date) || isNaN(modelValue))
{
return '';
}
// modelValue is localized, so we take the timezone offset in milliseconds and subtract it
// before converting it to ISO string.
const offset = modelValue.getTimezoneOffset() * 60000;
return new Date(modelValue.getTime() - offset).toJSON().replace(/\.\d{3}Z$/, 'Z');
}
getValue() getValue()
{ {
debugger; // The supplied value was not understandable, return null
return this.modelValue ? this.modelValue.toJSON().replace(/\.\d{3}Z$/, 'Z') : ""; if(this.modelValue instanceof Unparseable)
{
return null;
}
// It isn't always the case that we want the serializer value, but for Et2Date we do
return this.serializer(this.modelValue);
} }
} }

View File

@ -66,12 +66,13 @@ export const Et2Widget = <T extends Constructor<LitElement>>(superClass : T) =>
*/ */
statustext: {type: String}, statustext: {type: String},
label: {type: String}, // Defined in parent hierarchy
//label: {type: String},
onclick: { onclick: {
type: Function, type: Function,
converter: (value) => converter: (value) =>
{ {
debugger; // TODO: Check to see if this is a static converter so "this" gives the class, not the instance
return et2_compileLegacyJS(value, this, this); return et2_compileLegacyJS(value, this, this);
} }
} }

View File

@ -418,8 +418,7 @@ export const Et2InputWidget = <T extends Constructor>(superClass: T) =>
readonly: { readonly: {
type: Boolean, type: Boolean,
reflect: true reflect: true
}, }
value: {attribute: false}
}; };
} }
@ -436,7 +435,7 @@ export const Et2InputWidget = <T extends Constructor>(superClass: T) =>
getValue() getValue()
{ {
return this.getInputNode().value; return typeof this.serializedValue !== "undefined" ? this.serializedValue : this.modalValue;
} }
isDirty() isDirty()