2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Date widget (WebComponent)
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2022-04-26 23:24:58 +02:00
|
|
|
import {css, html} from "@lion/core";
|
2022-02-15 19:47:42 +01:00
|
|
|
import 'lit-flatpickr';
|
2021-12-15 00:55:02 +01:00
|
|
|
import {dateStyles} from "./DateStyles";
|
2023-03-20 13:41:25 +01:00
|
|
|
import type {Instance} from 'flatpickr/dist/types/instance';
|
|
|
|
import {default as scrollPlugin} from "flatpickr/dist/plugins/scrollPlugin.js";
|
|
|
|
import {default as ShortcutButtonsPlugin} from "shortcut-buttons-flatpickr/dist/shortcut-buttons-flatpickr";
|
2022-05-03 01:10:07 +02:00
|
|
|
import flatpickr from "flatpickr";
|
2022-06-08 00:39:04 +02:00
|
|
|
import {egw} from "../../jsapi/egw_global";
|
2023-03-20 13:41:25 +01:00
|
|
|
import type {HTMLElementWithValue} from "@lion/form-core/types/FormControlMixinTypes";
|
2022-07-22 15:45:47 +02:00
|
|
|
import {Et2Textbox} from "../Et2Textbox/Et2Textbox";
|
2022-10-20 23:27:24 +02:00
|
|
|
import {Et2ButtonIcon} from "../Et2Button/Et2ButtonIcon";
|
|
|
|
import {FormControlMixin} from "@lion/form-core";
|
|
|
|
import {LitFlatpickr} from "lit-flatpickr";
|
|
|
|
import {Et2InputWidget} from "../Et2InputWidget/Et2InputWidget";
|
|
|
|
import shoelace from "../Styles/shoelace";
|
2022-04-29 19:35:04 +02:00
|
|
|
|
2023-02-01 17:46:00 +01:00
|
|
|
const textbox = new Et2Textbox();
|
2022-10-20 23:27:24 +02:00
|
|
|
const button = new Et2ButtonIcon();
|
2021-08-13 23:26:18 +02:00
|
|
|
|
2022-04-29 17:40:43 +02:00
|
|
|
// list of existing localizations from node_modules/flatpicker/dist/l10n directory:
|
|
|
|
const l10n = [
|
|
|
|
'ar', 'at', 'az', 'be', 'bg', 'bn', 'bs', 'cat', 'cs', 'cy', 'da', 'de', 'eo', 'es', 'et', 'fa', 'fi', 'fo',
|
|
|
|
'fr', 'ga', 'gr', 'he', 'hi', 'hr', 'hu', 'id', 'index', 'is', 'it', 'ja', 'ka', 'km', 'ko', 'kz', 'lt', 'lv', 'mk',
|
|
|
|
'mn', 'ms', 'my', 'nl', 'no', 'pa', 'pl', 'pt', 'ro', 'ru', 'si', 'sk', 'sl', 'sq', 'sr-cyr', 'sr', 'sv', 'th', 'tr',
|
|
|
|
'uk', 'uz', 'uz_latn', 'vn', 'zh-tw', 'zh',
|
|
|
|
];
|
2022-06-08 00:39:04 +02:00
|
|
|
const lang = egw ? <string>egw.preference('lang') || "" : "";
|
2022-04-29 17:40:43 +02:00
|
|
|
// only load localization, if we have one
|
|
|
|
if (l10n.indexOf(lang) >= 0)
|
2022-04-28 23:44:11 +02:00
|
|
|
{
|
2022-04-29 17:40:43 +02:00
|
|
|
import(egw.webserverUrl + "/node_modules/flatpickr/dist/l10n/" + lang + ".js").then(() =>
|
2022-04-29 17:11:11 +02:00
|
|
|
{
|
|
|
|
// @ts-ignore
|
2022-04-29 17:40:43 +02:00
|
|
|
flatpickr.localize(flatpickr.l10ns[lang]);
|
2022-04-29 17:11:11 +02:00
|
|
|
});
|
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
|
|
|
|
/**
|
2021-11-03 20:49:39 +01:00
|
|
|
* Parse a date string into a Date object
|
|
|
|
* Time will be 00:00:00 UTC
|
2021-08-13 23:26:18 +02:00
|
|
|
*
|
|
|
|
* @param {string} dateString
|
|
|
|
* @returns {Date | undefined}
|
|
|
|
*/
|
2022-04-26 23:24:58 +02:00
|
|
|
export function parseDate(dateString, formatString?)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-08-19 23:09:00 +02:00
|
|
|
// First try the server format
|
2021-09-23 22:12:15 +02:00
|
|
|
if(dateString.substr(-1) === "Z")
|
2021-08-19 23:09:00 +02:00
|
|
|
{
|
2021-09-23 22:12:15 +02:00
|
|
|
try
|
2021-08-19 23:09:00 +02:00
|
|
|
{
|
2021-09-23 22:12:15 +02:00
|
|
|
let date = new Date(dateString);
|
|
|
|
if(date instanceof Date)
|
|
|
|
{
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
// Nope, that didn't parse directly
|
2021-08-19 23:09:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 23:24:58 +02:00
|
|
|
formatString = formatString || <string>(window.egw.preference("dateformat") || 'Y-m-d');
|
2022-02-15 19:47:42 +01:00
|
|
|
//@ts-ignore replaceAll() does not exist
|
2021-09-23 22:12:15 +02:00
|
|
|
formatString = formatString.replaceAll(new RegExp('[-/\.]', 'ig'), '-');
|
2021-08-13 23:26:18 +02:00
|
|
|
let parsedString = "";
|
2021-08-19 23:09:00 +02:00
|
|
|
switch(formatString)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
case 'd-m-Y':
|
2021-11-03 16:05:16 +01:00
|
|
|
parsedString = `${dateString.slice(6, 10)}/${dateString.slice(3, 5,)}/${dateString.slice(0, 2)}`;
|
2021-08-13 23:26:18 +02:00
|
|
|
break;
|
|
|
|
case 'm-d-Y':
|
2021-11-03 16:05:16 +01:00
|
|
|
parsedString = `${dateString.slice(6, 10)}/${dateString.slice(0, 2,)}/${dateString.slice(3, 5)}`;
|
2021-08-13 23:26:18 +02:00
|
|
|
break;
|
|
|
|
case 'Y-m-d':
|
2021-11-03 16:05:16 +01:00
|
|
|
parsedString = `${dateString.slice(0, 4)}/${dateString.slice(5, 7,)}/${dateString.slice(8, 10)}`;
|
|
|
|
break;
|
|
|
|
case 'Y-d-m':
|
|
|
|
parsedString = `${dateString.slice(0, 4)}/${dateString.slice(8, 10)}/${dateString.slice(5, 7)}`;
|
2021-08-13 23:26:18 +02:00
|
|
|
break;
|
|
|
|
case 'd-M-Y':
|
2021-11-03 16:05:16 +01:00
|
|
|
parsedString = `${dateString.slice(6, 10)}/${dateString.slice(3, 5,)}/${dateString.slice(0, 2)}`;
|
|
|
|
break;
|
2022-04-26 23:24:58 +02:00
|
|
|
case 'Ymd':
|
|
|
|
// Not a preference option, but used by some dates
|
|
|
|
parsedString = `${dateString.slice(0, 4)}/${dateString.slice(4, 6,)}/${dateString.slice(6, 8)}`;
|
|
|
|
break;
|
2021-08-13 23:26:18 +02:00
|
|
|
default:
|
|
|
|
parsedString = '0000/00/00';
|
|
|
|
}
|
|
|
|
|
|
|
|
const [year, month, day] = parsedString.split('/').map(Number);
|
2021-11-03 20:49:39 +01:00
|
|
|
const parsedDate = new Date(`${year}-${month < 10 ? "0" + month : month}-${day < 10 ? "0" + day : day}T00:00:00Z`);
|
2021-08-13 23:26:18 +02:00
|
|
|
|
|
|
|
// Check if parsedDate is not `Invalid Date` or that the date has changed (e.g. the not existing 31.02.2020)
|
2021-11-03 20:49:39 +01:00
|
|
|
if(
|
2021-08-13 23:26:18 +02:00
|
|
|
year > 0 &&
|
|
|
|
month > 0 &&
|
|
|
|
day > 0 &&
|
2021-11-03 20:49:39 +01:00
|
|
|
parsedDate.getUTCDate() === day &&
|
|
|
|
parsedDate.getUTCMonth() === month - 1
|
2021-08-13 23:26:18 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return parsedDate;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:05:16 +01:00
|
|
|
/**
|
2021-11-03 20:49:39 +01:00
|
|
|
* To parse a time into a Date object
|
|
|
|
* Date will be 1970-01-01, time is in UTC to avoid browser issues
|
2021-11-03 16:05:16 +01:00
|
|
|
*
|
|
|
|
* @param {string} timeString
|
|
|
|
* @returns {Date | undefined}
|
|
|
|
*/
|
|
|
|
export function parseTime(timeString)
|
|
|
|
{
|
|
|
|
// First try the server format
|
|
|
|
if(timeString.substr(-1) === "Z")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
let date = new Date(timeString);
|
|
|
|
if(date instanceof Date)
|
|
|
|
{
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
// Nope, that didn't parse directly
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let am_pm = timeString.endsWith("pm") || timeString.endsWith("PM") ? 12 : 0;
|
2021-11-03 20:49:39 +01:00
|
|
|
|
|
|
|
let strippedString = timeString.replaceAll(/[^0-9:]/gi, '');
|
2021-12-10 21:24:06 +01:00
|
|
|
|
2021-11-03 20:49:39 +01:00
|
|
|
if(timeString.startsWith("12") && strippedString != timeString)
|
|
|
|
{
|
|
|
|
// 12:xx am -> 0:xx, 12:xx pm -> 12:xx
|
|
|
|
am_pm -= 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [hour, minute] = strippedString.split(':').map(Number);
|
2021-11-03 16:05:16 +01:00
|
|
|
|
|
|
|
const parsedDate = new Date("1970-01-01T00:00:00Z");
|
|
|
|
parsedDate.setUTCHours(hour + am_pm);
|
|
|
|
parsedDate.setUTCMinutes(minute);
|
|
|
|
|
|
|
|
// Check if parsedDate is not `Invalid Date` or that the time has changed
|
|
|
|
if(
|
|
|
|
parsedDate.getUTCHours() === hour + am_pm &&
|
|
|
|
parsedDate.getUTCMinutes() === minute
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return parsedDate;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To parse a date+time into an object
|
2021-11-03 20:49:39 +01:00
|
|
|
* Time is in UTC to avoid browser issues
|
2021-11-03 16:05:16 +01:00
|
|
|
*
|
|
|
|
* @param {string} dateTimeString
|
|
|
|
* @returns {Date | undefined}
|
|
|
|
*/
|
|
|
|
export function parseDateTime(dateTimeString)
|
|
|
|
{
|
2021-11-08 21:40:28 +01:00
|
|
|
// First try some common invalid values
|
|
|
|
if(dateTimeString === "" || dateTimeString === "0" || dateTimeString === 0)
|
|
|
|
{
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next try server format
|
2021-11-03 16:05:16 +01:00
|
|
|
if(typeof dateTimeString === "string" && dateTimeString.substr(-1) === "Z" || !isNaN(dateTimeString))
|
|
|
|
{
|
|
|
|
if(!isNaN(dateTimeString) && parseInt(dateTimeString) == dateTimeString)
|
|
|
|
{
|
2021-12-15 00:55:02 +01:00
|
|
|
console.warn("Invalid date/time string: " + dateTimeString);
|
2021-11-03 16:05:16 +01:00
|
|
|
dateTimeString *= 1000;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
let date = new Date(dateTimeString);
|
|
|
|
if(date instanceof Date)
|
|
|
|
{
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
// Nope, that didn't parse directly
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const date = parseDate(dateTimeString);
|
|
|
|
|
|
|
|
let explody = dateTimeString.split(" ");
|
|
|
|
explody.shift();
|
|
|
|
const time = parseTime(explody.join(" "));
|
|
|
|
|
|
|
|
if(typeof date === "undefined" || typeof time === "undefined")
|
|
|
|
{
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
date.setUTCHours(time.getUTCHours());
|
|
|
|
date.setUTCMinutes(time.getUTCMinutes());
|
|
|
|
date.setUTCSeconds(time.getUTCSeconds());
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Format dates according to user preference
|
|
|
|
*
|
|
|
|
* @param {Date} date
|
|
|
|
* @param {import('@lion/localize/types/LocalizeMixinTypes').FormatDateOptions} [options] Intl options are available
|
|
|
|
* set 'dateFormat': "Y-m-d" to specify a particular format
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-11-03 20:49:39 +01:00
|
|
|
export function formatDate(date : Date, options = {dateFormat: ""}) : string
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-11-03 16:05:16 +01:00
|
|
|
if(!date || !(date instanceof Date))
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
let _value = '';
|
2021-11-03 16:05:16 +01:00
|
|
|
let dateformat = options.dateFormat || <string>window.egw.preference("dateformat") || 'Y-m-d';
|
2021-08-13 23:26:18 +02:00
|
|
|
|
2022-02-15 19:47:42 +01:00
|
|
|
let replace_map = {
|
2021-08-19 23:09:00 +02:00
|
|
|
d: (date.getUTCDate() < 10 ? "0" : "") + date.getUTCDate(),
|
|
|
|
m: (date.getUTCMonth() < 9 ? "0" : "") + (date.getUTCMonth() + 1),
|
2023-01-17 18:52:02 +01:00
|
|
|
Y: "" + date.getUTCFullYear(),
|
|
|
|
H: ("0" + date.getUTCHours()).slice(-2),
|
|
|
|
i: ("0" + date.getUTCMinutes()).slice(-2)
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2022-05-03 01:10:07 +02:00
|
|
|
if(dateformat.indexOf("M") != -1)
|
|
|
|
{
|
|
|
|
replace_map["M"] = flatpickr.formatDate(date, "M");
|
|
|
|
}
|
|
|
|
|
2022-05-02 23:23:03 +02:00
|
|
|
let re = new RegExp(Object.keys(replace_map).join("|"), "g");
|
2021-11-03 16:05:16 +01:00
|
|
|
_value = dateformat.replace(re, function(matched)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return replace_map[matched];
|
|
|
|
});
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:05:16 +01:00
|
|
|
/**
|
|
|
|
* Format dates according to user preference
|
|
|
|
*
|
|
|
|
* @param {Date} date
|
|
|
|
* @param {import('@lion/localize/types/LocalizeMixinTypes').FormatDateOptions} [options] Intl options are available
|
|
|
|
* set 'timeFormat': "12" to specify a particular format
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-11-03 20:49:39 +01:00
|
|
|
export function formatTime(date : Date, options = {timeFormat: ""}) : string
|
2021-11-03 16:05:16 +01:00
|
|
|
{
|
|
|
|
if(!date || !(date instanceof Date))
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
let _value = '';
|
|
|
|
|
2021-11-03 20:49:39 +01:00
|
|
|
let timeformat = options.timeFormat || <string>window.egw.preference("timeformat") || "24";
|
2021-11-03 16:05:16 +01:00
|
|
|
let hours = (timeformat == "12" && date.getUTCHours() > 12) ? (date.getUTCHours() - 12) : date.getUTCHours();
|
|
|
|
if(timeformat == "12" && hours == 0)
|
|
|
|
{
|
|
|
|
// 00:00 is 12:00 am
|
|
|
|
hours = 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
_value = (timeformat == "24" && hours < 10 ? "0" : "") + hours + ":" +
|
|
|
|
(date.getUTCMinutes() < 10 ? "0" : "") + (date.getUTCMinutes()) +
|
|
|
|
(timeformat == "24" ? "" : (date.getUTCHours() < 12 ? " am" : " pm"));
|
|
|
|
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format date+time according to user preference
|
|
|
|
*
|
|
|
|
* @param {Date} date
|
|
|
|
* @param {import('@lion/localize/types/LocalizeMixinTypes').FormatDateOptions} [options] Intl options are available
|
|
|
|
* set 'dateFormat': "Y-m-d", 'timeFormat': "12" to specify a particular format
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-11-03 20:49:39 +01:00
|
|
|
export function formatDateTime(date : Date, options = {dateFormat: "", timeFormat: ""}) : string
|
2021-11-03 16:05:16 +01:00
|
|
|
{
|
|
|
|
if(!date || !(date instanceof Date))
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return formatDate(date, options) + " " + formatTime(date, options);
|
|
|
|
}
|
|
|
|
|
2022-10-20 23:27:24 +02:00
|
|
|
// !!! ValidateMixin !!!
|
|
|
|
export class Et2Date extends Et2InputWidget(FormControlMixin(LitFlatpickr))
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
2022-10-20 23:27:24 +02:00
|
|
|
...(super.styles ? (Array.isArray(super.styles) ? super.styles : [super.styles]) : []),
|
|
|
|
shoelace,
|
2022-03-04 23:37:22 +01:00
|
|
|
dateStyles,
|
2021-08-13 23:26:18 +02:00
|
|
|
css`
|
2023-02-24 19:58:16 +01:00
|
|
|
:host {
|
2022-02-15 19:47:42 +01:00
|
|
|
width: auto;
|
2023-02-24 19:58:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
::slotted([slot='input']) {
|
2022-10-14 17:32:22 +02:00
|
|
|
flex: 1 1 auto;
|
2023-02-24 19:58:16 +01:00
|
|
|
min-width: 12ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scroll buttons */
|
|
|
|
|
|
|
|
.input-group__container {
|
2022-10-20 23:27:24 +02:00
|
|
|
position: relative;
|
2023-02-24 19:58:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.input-group__container:hover .et2-date-time__scrollbuttons {
|
2022-10-20 23:27:24 +02:00
|
|
|
display: flex;
|
2023-02-24 19:58:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.et2-date-time__scrollbuttons {
|
2022-10-20 23:27:24 +02:00
|
|
|
display: none;
|
|
|
|
flex-direction: column;
|
|
|
|
width: calc(var(--sl-input-height-medium) / 2);
|
|
|
|
position: absolute;
|
|
|
|
right: 0px;
|
2023-02-24 19:58:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.et2-date-time__scrollbuttons > * {
|
2022-10-20 23:27:24 +02:00
|
|
|
font-size: var(--sl-font-size-2x-small);
|
|
|
|
height: calc(var(--sl-input-height-medium) / 2);
|
2023-02-24 19:58:16 +01:00
|
|
|
}
|
2022-10-20 23:27:24 +02:00
|
|
|
.et2-date-time__scrollbuttons > *::part(base) {
|
|
|
|
padding: 3px;
|
|
|
|
}
|
2021-09-24 19:13:14 +02:00
|
|
|
`,
|
2021-08-13 23:26:18 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
2022-04-26 23:24:58 +02:00
|
|
|
...super.properties,
|
|
|
|
/**
|
|
|
|
* Display the calendar inline instead of revealed as needed
|
|
|
|
*/
|
2022-10-04 13:46:15 +02:00
|
|
|
inline: {type: Boolean},
|
|
|
|
/**
|
|
|
|
* Placeholder text for input
|
|
|
|
*/
|
|
|
|
placeholder: {type: String},
|
2022-10-20 23:27:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow value that is not a multiple of minuteIncrement
|
|
|
|
*
|
|
|
|
* eg: 11:23 with default 5 minuteIncrement = 11:25
|
|
|
|
* 16:47 with 30 minuteIncrement = 17:00
|
|
|
|
* If false (default), it is impossible to have a time that is not a multiple of minuteIncrement.
|
|
|
|
* Does not affect scroll, which always goes to nearest multiple.
|
|
|
|
*/
|
2023-02-08 22:36:09 +01:00
|
|
|
freeMinuteEntry: {type: Boolean},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The preferred placement of the calendar popup can be set with the placement attribute. The default
|
|
|
|
* is "auto". Note that the actual position may vary to ensure the calendar remains in the viewport.
|
|
|
|
* Valid placements are "top", "bottom" or "auto".
|
|
|
|
*/
|
|
|
|
placement: {type: String, noAccessor: true}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 21:33:13 +01:00
|
|
|
get slots()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.slots,
|
|
|
|
input: () =>
|
|
|
|
{
|
2023-01-04 20:00:25 +01:00
|
|
|
if(typeof egwIsMobile == "function" && egwIsMobile())
|
2022-11-01 17:34:52 +01:00
|
|
|
{
|
|
|
|
// Plain input for mobile
|
|
|
|
const text = document.createElement('input');
|
|
|
|
text.type = this._mobileInputType();
|
|
|
|
return text;
|
|
|
|
}
|
2022-10-14 17:32:22 +02:00
|
|
|
// This element gets hidden and used for value, but copied by flatpicr and used for input
|
2022-07-22 15:45:47 +02:00
|
|
|
const text = <Et2Textbox>document.createElement('et2-textbox');
|
2022-03-07 21:33:13 +01:00
|
|
|
text.type = "text";
|
2022-10-04 13:46:15 +02:00
|
|
|
text.placeholder = this.placeholder;
|
2022-10-20 23:27:24 +02:00
|
|
|
text.required = this.required;
|
2022-03-07 21:33:13 +01:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
2022-04-29 19:35:04 +02:00
|
|
|
|
2022-10-20 23:27:24 +02:00
|
|
|
// By default, 5 minute resolution (see minuteIncrement to change resolution)
|
|
|
|
this.freeMinuteEntry = false;
|
|
|
|
|
2022-04-29 19:35:04 +02:00
|
|
|
this._onDayCreate = this._onDayCreate.bind(this);
|
2022-10-14 17:32:22 +02:00
|
|
|
this._handleInputChange = this._handleInputChange.bind(this);
|
2023-02-09 22:42:14 +01:00
|
|
|
this._onReady = this._onReady.bind(this);
|
2022-10-20 23:27:24 +02:00
|
|
|
this.handleScroll = this.handleScroll.bind(this);
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 21:45:47 +01:00
|
|
|
|
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
2022-03-07 21:33:13 +01:00
|
|
|
this._updateValueOnChange = this._updateValueOnChange.bind(this);
|
2022-09-12 23:35:24 +02:00
|
|
|
this._handleShortcutButtonClick = this._handleShortcutButtonClick.bind(this);
|
2022-02-28 21:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback()
|
|
|
|
{
|
|
|
|
super.disconnectedCallback();
|
2022-10-14 18:05:59 +02:00
|
|
|
this._inputNode?.removeEventListener('change', this._onChange);
|
2022-10-20 23:27:24 +02:00
|
|
|
this.findInputField()?.removeEventListener("input", this._handleInputChange);
|
2022-02-28 21:45:47 +01:00
|
|
|
}
|
|
|
|
|
2021-08-19 23:09:00 +02:00
|
|
|
/**
|
2022-02-15 19:47:42 +01:00
|
|
|
* Override parent to skip call to CDN
|
|
|
|
* @returns {Promise<void>}
|
2021-08-19 23:09:00 +02:00
|
|
|
*/
|
2022-02-15 19:47:42 +01:00
|
|
|
async init()
|
2021-08-19 23:09:00 +02:00
|
|
|
{
|
2022-11-01 17:34:52 +01:00
|
|
|
// Plain input for mobile
|
2023-01-04 20:00:25 +01:00
|
|
|
if(typeof egwIsMobile == "function" && egwIsMobile())
|
2022-11-01 17:34:52 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:47:42 +01:00
|
|
|
if(this.locale)
|
2021-08-19 23:09:00 +02:00
|
|
|
{
|
2022-02-15 19:47:42 +01:00
|
|
|
// await loadLocale(this.locale);
|
2021-08-19 23:09:00 +02:00
|
|
|
}
|
2022-03-07 21:33:13 +01:00
|
|
|
if(typeof this._instance === "undefined")
|
|
|
|
{
|
2022-10-20 23:27:24 +02:00
|
|
|
if(this.getOptions().allowInput)
|
|
|
|
{
|
|
|
|
// Change this so it uses findInputField() to get the input
|
|
|
|
this._hasSlottedElement = true;
|
|
|
|
|
|
|
|
// Wait for everything to be there before we start flatpickr
|
|
|
|
await this.updateComplete;
|
2023-02-01 17:46:00 +01:00
|
|
|
await this._inputNode.updateComplete;
|
2022-10-20 23:27:24 +02:00
|
|
|
this._inputNode.requestUpdate();
|
|
|
|
await this._inputNode.updateComplete;
|
|
|
|
|
|
|
|
// Set flag attribute on _internal_ input - flatpickr needs an <input>
|
|
|
|
if(this._inputNode.shadowRoot.querySelectorAll("input[type='text']").length == 1)
|
|
|
|
{
|
|
|
|
this.findInputField().setAttribute("data-input", "");
|
|
|
|
}
|
2023-01-17 18:52:02 +01:00
|
|
|
if(this.defaultDate)
|
|
|
|
{
|
|
|
|
this._inputNode.value = flatpickr.formatDate(<Date>this.defaultDate, this.getOptions().dateFormat);
|
|
|
|
}
|
2022-10-20 23:27:24 +02:00
|
|
|
}
|
|
|
|
|
2022-03-07 21:33:13 +01:00
|
|
|
this.initializeComponent();
|
2022-02-28 21:45:47 +01:00
|
|
|
|
2022-03-07 21:33:13 +01:00
|
|
|
// This has to go in init() rather than connectedCallback() because flatpickr creates its nodes in
|
|
|
|
// initializeComponent() so this._inputNode is not available before this
|
2022-10-20 23:27:24 +02:00
|
|
|
this.findInputField().addEventListener('change', this._updateValueOnChange);
|
|
|
|
this.findInputField().addEventListener("input", this._handleInputChange);
|
2022-03-07 21:33:13 +01:00
|
|
|
}
|
2021-08-19 23:09:00 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
|
2022-02-22 19:23:54 +01:00
|
|
|
/**
|
|
|
|
* Override some flatpickr defaults to get things how we like it
|
|
|
|
*
|
|
|
|
* @see https://flatpickr.js.org/options/
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2022-10-20 23:27:24 +02:00
|
|
|
public getOptions()
|
2022-02-22 19:23:54 +01:00
|
|
|
{
|
|
|
|
let options = super.getOptions();
|
|
|
|
|
2022-07-22 16:42:35 +02:00
|
|
|
options.altFormat = <string>this.egw()?.preference("dateformat") || "Y-m-d";
|
2022-02-22 19:23:54 +01:00
|
|
|
options.altInput = true;
|
|
|
|
options.allowInput = true;
|
|
|
|
options.dateFormat = "Y-m-dT00:00:00\\Z";
|
|
|
|
options.weekNumbers = true;
|
2022-10-20 23:27:24 +02:00
|
|
|
// Wrap needs to be false because flatpickr can't look inside et2-textbox and find the <input> it wants
|
|
|
|
// We provide it directly through findInputField()
|
|
|
|
options.wrap = false;
|
2022-02-22 19:23:54 +01:00
|
|
|
|
2022-04-29 19:35:04 +02:00
|
|
|
options.onDayCreate = this._onDayCreate;
|
|
|
|
|
2022-04-28 23:44:11 +02:00
|
|
|
this._localize(options);
|
|
|
|
|
2022-04-26 23:24:58 +02:00
|
|
|
if(this.inline)
|
|
|
|
{
|
|
|
|
options.inline = this.inline;
|
|
|
|
}
|
2023-02-08 22:36:09 +01:00
|
|
|
if(this.placement)
|
|
|
|
{
|
|
|
|
options.position = this._convert_placement(this.placement);
|
|
|
|
}
|
2022-04-26 23:24:58 +02:00
|
|
|
|
2022-09-06 19:32:25 +02:00
|
|
|
options.plugins = [
|
|
|
|
// Turn on scroll wheel support
|
|
|
|
// @ts-ignore TypeScript can't find scrollPlugin, but rollup does
|
2022-10-18 17:08:53 +02:00
|
|
|
new scrollPlugin()
|
2022-09-06 19:32:25 +02:00
|
|
|
];
|
2022-10-18 17:08:53 +02:00
|
|
|
// Add "Ok" and "today" buttons
|
|
|
|
const buttons = this._buttonPlugin();
|
|
|
|
if(buttons)
|
|
|
|
{
|
|
|
|
options.plugins.push(buttons)
|
|
|
|
}
|
|
|
|
|
2022-02-22 19:23:54 +01:00
|
|
|
|
2022-03-07 17:12:32 +01:00
|
|
|
// Listen for flatpickr change so we can update internal value, needed for validation
|
2023-02-09 22:42:14 +01:00
|
|
|
options.onChange = this._updateValueOnChange;
|
|
|
|
options.onReady = this._onReady;
|
2022-03-07 17:12:32 +01:00
|
|
|
|
2022-10-06 00:22:32 +02:00
|
|
|
// Remove Lion's inert attribute so we can work in Et2Dialog
|
|
|
|
options.onOpen = [() =>
|
|
|
|
{
|
2022-12-20 22:04:45 +01:00
|
|
|
this._instance.calendarContainer?.removeAttribute("inert")
|
2022-10-06 00:22:32 +02:00
|
|
|
}];
|
|
|
|
|
2022-02-22 19:23:54 +01:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2022-09-12 23:35:24 +02:00
|
|
|
/**
|
|
|
|
* Handle click on shortcut button(s) like "Today"
|
|
|
|
*
|
|
|
|
* @param button_index
|
|
|
|
* @param fp Flatpickr instance
|
|
|
|
*/
|
|
|
|
public _handleShortcutButtonClick(button_index, fp)
|
|
|
|
{
|
2022-10-18 17:08:53 +02:00
|
|
|
switch(button_index)
|
|
|
|
{
|
|
|
|
case 0: // OK
|
|
|
|
fp.close();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fp.setDate(new Date());
|
|
|
|
}
|
2022-09-12 23:35:24 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 23:44:11 +02:00
|
|
|
/**
|
|
|
|
* Set localize options & translations
|
|
|
|
* @param options
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _localize(options)
|
|
|
|
{
|
2022-06-08 00:39:04 +02:00
|
|
|
let first_dow = <string>this.egw()?.preference('weekdaystarts', 'calendar') || 'Monday';
|
2022-04-28 23:44:11 +02:00
|
|
|
const DOW_MAP = {Monday: 1, Sunday: 0, Saturday: 6};
|
|
|
|
options.locale = {
|
|
|
|
firstDayOfWeek: DOW_MAP[first_dow]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-01 17:34:52 +01:00
|
|
|
/**
|
|
|
|
* For mobile, we use a plain input of the proper type
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
_mobileInputType() : string
|
|
|
|
{
|
|
|
|
return "date";
|
|
|
|
}
|
|
|
|
|
2022-10-14 18:05:59 +02:00
|
|
|
/**
|
|
|
|
* Add "today" button below calendar
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _buttonPlugin()
|
|
|
|
{
|
|
|
|
// @ts-ignore TypeScript can't find ShortcutButtonsPlugin, but rollup does
|
|
|
|
return ShortcutButtonsPlugin({
|
2022-10-18 17:08:53 +02:00
|
|
|
button: [
|
|
|
|
{label: this.egw().lang("ok")},
|
|
|
|
{label: this.egw().lang("Today")}
|
|
|
|
],
|
2022-10-14 18:05:59 +02:00
|
|
|
onClick: this._handleShortcutButtonClick
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:44:28 +02:00
|
|
|
set value(value)
|
2021-10-19 00:03:05 +02:00
|
|
|
{
|
2022-02-15 19:47:42 +01:00
|
|
|
if(!value || value == 0 || value == "0")
|
2021-11-01 17:21:08 +01:00
|
|
|
{
|
2022-02-25 19:29:36 +01:00
|
|
|
value = "";
|
2022-03-16 22:21:15 +01:00
|
|
|
this.clear();
|
2022-02-25 19:29:36 +01:00
|
|
|
return;
|
2021-11-01 17:21:08 +01:00
|
|
|
}
|
2022-08-12 11:38:15 +02:00
|
|
|
let date;
|
|
|
|
// handle relative time (eg. "+3600" or "-3600") used in calendar
|
2022-11-01 17:34:52 +01:00
|
|
|
if(typeof value === 'string' && (value[0] === '+' || value[0] === '-'))
|
2022-08-12 11:38:15 +02:00
|
|
|
{
|
|
|
|
date = new Date(this.getValue());
|
2022-10-20 23:27:24 +02:00
|
|
|
date.setSeconds(date.getSeconds() + parseInt(value));
|
2022-08-12 11:38:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
date = new Date(value);
|
|
|
|
}
|
2023-01-04 20:00:25 +01:00
|
|
|
if(typeof egwIsMobile == "function" && egwIsMobile())
|
2022-11-01 17:34:52 +01:00
|
|
|
{
|
2023-02-24 19:58:16 +01:00
|
|
|
if(this._inputNode)
|
|
|
|
{
|
|
|
|
this._inputNode.value = isNaN(date) ? "" : this.format(date, {dateFormat: 'Y-m-dTH:i'});
|
|
|
|
}
|
|
|
|
else
|
2022-11-01 17:34:52 +01:00
|
|
|
{
|
2023-02-24 19:58:16 +01:00
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
this._inputNode.value = isNaN(date) ? "" : this.format(date, {dateFormat: 'Y-m-dTH:i'});
|
|
|
|
});
|
|
|
|
}
|
2022-11-01 17:34:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-02-24 19:58:16 +01:00
|
|
|
// Handle timezone offset, flatpickr uses local time
|
|
|
|
let formatDate = new Date(date.valueOf() + date.getTimezoneOffset() * 60 * 1000);
|
2022-02-15 19:47:42 +01:00
|
|
|
if(!this._instance)
|
2021-12-10 21:24:06 +01:00
|
|
|
{
|
2022-02-15 19:47:42 +01:00
|
|
|
this.defaultDate = formatDate;
|
2021-08-19 23:09:00 +02:00
|
|
|
}
|
2022-02-15 19:47:42 +01:00
|
|
|
else
|
2021-10-19 00:03:05 +02:00
|
|
|
{
|
2022-02-15 19:47:42 +01:00
|
|
|
this.setDate(formatDate);
|
2021-10-19 00:03:05 +02:00
|
|
|
}
|
2023-01-17 18:52:02 +01:00
|
|
|
|
|
|
|
this.requestUpdate("value", this._oldValue);
|
2021-10-19 00:03:05 +02:00
|
|
|
}
|
|
|
|
|
2022-09-20 18:44:28 +02:00
|
|
|
get value()
|
2021-11-01 17:21:08 +01:00
|
|
|
{
|
2022-10-20 23:27:24 +02:00
|
|
|
if(!this._inputElement)
|
2021-11-01 17:21:08 +01:00
|
|
|
{
|
2023-01-17 18:52:02 +01:00
|
|
|
if(this.defaultDate)
|
|
|
|
{
|
|
|
|
return flatpickr.formatDate(<Date>this.defaultDate, this.getOptions().dateFormat);
|
|
|
|
}
|
2023-03-29 18:21:54 +02:00
|
|
|
if(typeof egwIsMobile == "function" && egwIsMobile() && this._inputNode?.value)
|
2023-02-28 17:05:49 +01:00
|
|
|
{
|
2023-03-29 18:21:54 +02:00
|
|
|
return this._inputNode?.value + "Z";
|
2023-02-28 17:05:49 +01:00
|
|
|
}
|
|
|
|
return this._inputNode?.value || '';
|
2021-11-01 17:21:08 +01:00
|
|
|
}
|
2022-10-20 23:27:24 +02:00
|
|
|
let value = this._inputElement.value;
|
2021-11-01 17:21:08 +01:00
|
|
|
|
2022-02-15 19:47:42 +01:00
|
|
|
// Empty field, return ''
|
|
|
|
if(!value)
|
2021-11-01 17:21:08 +01:00
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
2022-02-15 19:47:42 +01:00
|
|
|
|
|
|
|
return value;
|
2021-09-24 19:13:14 +02:00
|
|
|
}
|
2022-02-23 18:43:39 +01:00
|
|
|
|
2022-10-20 23:27:24 +02:00
|
|
|
get parse() : Function
|
|
|
|
{
|
|
|
|
return parseDate;
|
|
|
|
}
|
|
|
|
|
2022-11-01 17:34:52 +01:00
|
|
|
get format() : Function
|
|
|
|
{
|
|
|
|
return formatDate;
|
|
|
|
}
|
|
|
|
|
2023-03-20 13:41:25 +01:00
|
|
|
set format(_format)
|
|
|
|
{
|
|
|
|
// ignored, trying to fix TypeError opening a new contact
|
|
|
|
}
|
|
|
|
|
2022-04-26 23:24:58 +02:00
|
|
|
/**
|
|
|
|
* Inline calendars need a slot
|
|
|
|
*
|
|
|
|
* @return {TemplateResult}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
_inputGroupAfterTemplate()
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<div class="input-group__after">
|
|
|
|
<slot name="after"></slot>
|
|
|
|
<slot/>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:32:22 +02:00
|
|
|
/**
|
|
|
|
* Update the calendar when the input value changes
|
|
|
|
* Otherwise, user's change will be overwritten by calendar popup when the input loses focus
|
|
|
|
*
|
|
|
|
* @param e
|
|
|
|
*/
|
|
|
|
_handleInputChange(e : InputEvent)
|
|
|
|
{
|
|
|
|
// Update
|
2022-10-20 23:27:24 +02:00
|
|
|
const value = this.findInputField().value;
|
|
|
|
|
|
|
|
if(value === "" && this._instance.selectedDates.length > 0)
|
|
|
|
{
|
2023-01-13 16:39:44 +01:00
|
|
|
// Update the et2-textbox so it will fail a required validation check
|
|
|
|
this._inputNode.value = '';
|
2023-01-31 00:10:49 +01:00
|
|
|
this._instance.clear();
|
|
|
|
this.dispatchEvent(new Event("change", {bubbles: true}));
|
2022-10-20 23:27:24 +02:00
|
|
|
}
|
2022-10-14 17:32:22 +02:00
|
|
|
let parsedDate = null
|
|
|
|
try
|
|
|
|
{
|
2022-12-20 22:04:45 +01:00
|
|
|
parsedDate = this._instance.parseDate(value, this.getOptions().altFormat)
|
2022-10-14 17:32:22 +02:00
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
// Invalid date string
|
|
|
|
}
|
|
|
|
// If they typed a valid date/time, try to update flatpickr
|
|
|
|
if(parsedDate)
|
|
|
|
{
|
2023-02-01 17:46:00 +01:00
|
|
|
const formattedDate = flatpickr.formatDate(parsedDate, this.getOptions().altFormat)
|
2022-10-20 23:27:24 +02:00
|
|
|
if(value === formattedDate &&
|
|
|
|
// Avoid infinite loop of setting the same value back triggering another change
|
2023-02-01 17:46:00 +01:00
|
|
|
this._instance.input.value !== flatpickr.formatDate(parsedDate, this.getOptions().dateFormat))
|
2022-10-14 17:32:22 +02:00
|
|
|
{
|
|
|
|
this._instance.setDate(value, true, this._instance.config.altFormat)
|
|
|
|
}
|
2023-01-13 16:39:44 +01:00
|
|
|
// Update the et2-textbox so it has current value for any (required) validation
|
|
|
|
this._inputNode.value = formattedDate;
|
|
|
|
(<Et2Textbox>this._inputNode).validate();
|
2022-10-14 17:32:22 +02:00
|
|
|
}
|
2023-01-31 00:10:49 +01:00
|
|
|
this.dispatchEvent(new Event("change", {bubbles: true}));
|
2022-10-14 17:32:22 +02:00
|
|
|
}
|
|
|
|
|
2022-03-01 15:09:27 +01:00
|
|
|
/**
|
|
|
|
* Change handler setting modelValue for validation
|
|
|
|
*
|
|
|
|
* @param _ev
|
|
|
|
* @returns
|
|
|
|
*/
|
2022-10-14 19:18:17 +02:00
|
|
|
_updateValueOnChange(selectedDates : Date[], dateStr : string, instance : Instance)
|
2022-03-01 15:09:27 +01:00
|
|
|
{
|
|
|
|
this.modelValue = this.getValue();
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:14 +01:00
|
|
|
_onReady(selectedDates : Date[], dateStr : string, instance : Instance)
|
|
|
|
{
|
|
|
|
this._updateValueOnChange(selectedDates, dateStr, instance);
|
|
|
|
|
|
|
|
// Add any classes we have to the instance
|
|
|
|
instance.calendarContainer.classList.add(...this.classList.values())
|
|
|
|
}
|
|
|
|
|
2022-04-29 19:35:04 +02:00
|
|
|
/**
|
|
|
|
* Customise date rendering
|
|
|
|
*
|
|
|
|
* @see https://flatpickr.js.org/events/
|
|
|
|
*
|
|
|
|
* @param {Date} dates Currently selected date(s)
|
|
|
|
* @param dStr a string representation of the latest selected Date object by the user. The string is formatted as per the dateFormat option.
|
|
|
|
* @param inst flatpickr instance
|
|
|
|
* @param dayElement
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _onDayCreate(dates : Date[], dStr : string, inst, dayElement : HTMLElement)
|
|
|
|
{
|
|
|
|
//@ts-ignore flatpickr adds dateObj to days
|
|
|
|
let date = new Date(dayElement.dateObj);
|
|
|
|
let f_date = new Date(date.valueOf() - date.getTimezoneOffset() * 60 * 1000);
|
|
|
|
if(!f_date)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let set_holiday = function(holidays, element)
|
|
|
|
{
|
|
|
|
let day_holidays = holidays[formatDate(f_date, {dateFormat: "Ymd"})]
|
|
|
|
let tooltip = '';
|
|
|
|
if(typeof day_holidays !== 'undefined' && day_holidays.length)
|
|
|
|
{
|
|
|
|
for(var i = 0; i < day_holidays.length; i++)
|
|
|
|
{
|
|
|
|
if(typeof day_holidays[i]['birthyear'] !== 'undefined')
|
|
|
|
{
|
|
|
|
element.classList.add('calBirthday');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
element.classList.add('calHoliday');
|
|
|
|
}
|
|
|
|
tooltip += day_holidays[i]['name'] + "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(tooltip)
|
|
|
|
{
|
|
|
|
this.egw().tooltipBind(element, tooltip);
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
|
2023-02-27 23:15:36 +01:00
|
|
|
egw.holidays(f_date.getFullYear()).then((h) => set_holiday(h, dayElement));
|
2022-04-29 19:35:04 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 22:06:37 +01:00
|
|
|
/**
|
|
|
|
* Set the minimum allowed date
|
|
|
|
* @param {string | Date} min
|
|
|
|
*/
|
|
|
|
set_min(min : string | Date)
|
|
|
|
{
|
|
|
|
this.minDate = min;
|
|
|
|
}
|
|
|
|
|
2022-03-07 22:33:09 +01:00
|
|
|
set minDate(min : string | Date)
|
|
|
|
{
|
|
|
|
if(this._instance)
|
|
|
|
{
|
2022-04-26 23:24:58 +02:00
|
|
|
if(min)
|
|
|
|
{
|
|
|
|
// Handle timezone offset, flatpickr uses local time
|
|
|
|
let date = new Date(min);
|
|
|
|
let formatDate = new Date(date.valueOf() + date.getTimezoneOffset() * 60 * 1000);
|
|
|
|
this._instance.set("minDate", formatDate)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._instance.set("minDate", "")
|
|
|
|
}
|
2022-03-07 22:33:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 22:06:37 +01:00
|
|
|
/**
|
|
|
|
* Set the minimum allowed date
|
|
|
|
* @param {string | Date} max
|
|
|
|
*/
|
|
|
|
set_max(max : string | Date)
|
|
|
|
{
|
|
|
|
this.maxDate = max;
|
|
|
|
}
|
|
|
|
|
2022-03-07 22:33:09 +01:00
|
|
|
set maxDate(max : string | Date)
|
|
|
|
{
|
|
|
|
if(this._instance)
|
|
|
|
{
|
2022-04-26 23:24:58 +02:00
|
|
|
if(max)
|
|
|
|
{
|
|
|
|
// Handle timezone offset, flatpickr uses local time
|
|
|
|
let date = new Date(max);
|
|
|
|
let formatDate = new Date(date.valueOf() + date.getTimezoneOffset() * 60 * 1000);
|
|
|
|
this._instance.set("maxDate", formatDate)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._instance.set("maxDate", "")
|
|
|
|
}
|
2022-03-07 22:33:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 22:36:09 +01:00
|
|
|
|
|
|
|
private _convert_placement(position : "top" | "bottom" | "auto" | "") : "above" | "below" | "auto"
|
|
|
|
{
|
|
|
|
let placement = "auto";
|
|
|
|
switch(position)
|
|
|
|
{
|
|
|
|
case "top":
|
|
|
|
placement = "above";
|
|
|
|
break;
|
|
|
|
case "bottom":
|
|
|
|
placement = "below";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return placement;
|
|
|
|
}
|
|
|
|
|
|
|
|
get placement() : "top" | "bottom" | "auto"
|
|
|
|
{
|
|
|
|
return this.__placement;
|
|
|
|
}
|
|
|
|
|
|
|
|
set placement(new_placement : "top" | "bottom" | "auto")
|
|
|
|
{
|
|
|
|
if(this._instance)
|
|
|
|
{
|
|
|
|
this._instance.set("position", this._convert_placement(new_placement))
|
|
|
|
}
|
|
|
|
this.__placement = new_placement;
|
|
|
|
}
|
|
|
|
|
2022-02-23 18:43:39 +01:00
|
|
|
/**
|
2022-10-20 23:27:24 +02:00
|
|
|
* Override from flatpickr - This is the node we tell flatpickr to use
|
|
|
|
* It must be an <input>, flatpickr doesn't understand anything else
|
2022-02-23 18:43:39 +01:00
|
|
|
* @returns {any}
|
|
|
|
*/
|
2022-10-14 17:32:22 +02:00
|
|
|
findInputField() : HTMLInputElement
|
2022-02-23 18:43:39 +01:00
|
|
|
{
|
2022-11-18 21:59:20 +01:00
|
|
|
return <HTMLInputElement>this._inputNode?.shadowRoot?.querySelector('input:not([type="hidden"])');
|
2022-02-23 18:43:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-22 16:42:35 +02:00
|
|
|
* The interactive (form) element.
|
2022-10-20 23:27:24 +02:00
|
|
|
* This is an et2-textbox, which causes some problems with flatpickr
|
2022-02-23 18:43:39 +01:00
|
|
|
* @protected
|
|
|
|
*/
|
2022-10-14 17:32:22 +02:00
|
|
|
get _inputNode() : HTMLElementWithValue
|
|
|
|
{
|
2022-10-20 23:27:24 +02:00
|
|
|
return this.querySelector('[slot="input"]');
|
2022-10-14 17:32:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The holder of value for flatpickr
|
|
|
|
*/
|
|
|
|
get _valueNode() : HTMLElementWithValue
|
2022-02-23 18:43:39 +01:00
|
|
|
{
|
2022-10-20 23:27:24 +02:00
|
|
|
return this.querySelector('et2-textbox');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle clicks on scroll buttons
|
|
|
|
*
|
|
|
|
* @param e
|
|
|
|
*/
|
|
|
|
public handleScroll(e)
|
|
|
|
{
|
|
|
|
if(e.target && !e.target.dataset.direction)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
const direction = parseInt(e.target.dataset.direction, 10) || 1;
|
|
|
|
this.increment(direction, "day", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment the current value
|
|
|
|
*
|
|
|
|
* @param {number} delta Amount of change, positive or negative
|
|
|
|
* @param {"day" | "hour" | "minute"} field
|
|
|
|
* @param {boolean} roundToDelta Round the current value to a multiple of delta before incrementing
|
|
|
|
* Useful for keeping things to a multiple of 5, for example.
|
|
|
|
*/
|
|
|
|
public increment(delta : number, field : "day" | "hour" | "minute", roundToDelta = true)
|
|
|
|
{
|
|
|
|
let date;
|
|
|
|
if(this._inputElement.value)
|
|
|
|
{
|
|
|
|
date = new Date(this._inputElement.value);
|
|
|
|
// Handle timezone offset, flatpickr uses local time
|
|
|
|
date = new Date(date.valueOf() + date.getTimezoneOffset() * 60 * 1000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No current value - start with "now", but don't increment at all
|
|
|
|
date = new Date();
|
|
|
|
delta = 0;
|
|
|
|
}
|
|
|
|
const fieldMap = {day: "UTCDate", hour: "UTCHours", minute: "UTCMinutes"};
|
|
|
|
const original = date["get" + fieldMap[field]]();
|
|
|
|
// Avoid divide by 0 in case we have no current value, or delta of 0 passed in
|
|
|
|
const roundResolution = delta || {
|
|
|
|
day: 1,
|
|
|
|
hour: this.getOptions().hourIncrement,
|
|
|
|
minute: this.getOptions().minuteIncrement
|
|
|
|
}[field];
|
|
|
|
|
|
|
|
let bound = roundToDelta ? (Math.round(original / roundResolution) * roundResolution) : original;
|
|
|
|
date["set" + fieldMap[field]](bound + delta);
|
|
|
|
|
|
|
|
|
|
|
|
this.setDate(date, false, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
render()
|
|
|
|
{
|
|
|
|
return html`
|
2022-10-28 19:48:42 +02:00
|
|
|
<div part="form-control" class="form-control">
|
|
|
|
<div class="form-field__group-one" part="form-control-label">${this._groupOneTemplate()}</div>
|
|
|
|
<div class="form-field__group-two" part="form-control-input">${this._groupTwoTemplate()}</div>
|
|
|
|
</div>
|
2022-10-20 23:27:24 +02:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _inputGroupInputTemplate()
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<slot name="input"></slot>
|
2022-11-01 17:34:52 +01:00
|
|
|
${this._incrementButtonTemplate()}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _incrementButtonTemplate()
|
|
|
|
{
|
|
|
|
// No increment buttons on mobile
|
2023-01-04 20:00:25 +01:00
|
|
|
if(typeof egwIsMobile == "function" && egwIsMobile())
|
2022-11-01 17:34:52 +01:00
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return html`
|
2022-10-20 23:27:24 +02:00
|
|
|
<div class="et2-date-time__scrollbuttons" part="scrollbuttons" @click=${this.handleScroll}>
|
|
|
|
<et2-button-icon
|
|
|
|
noSubmit
|
|
|
|
name="chevron-up"
|
|
|
|
data-direction="1"
|
|
|
|
>↑
|
|
|
|
</et2-button-icon>
|
|
|
|
<et2-button-icon
|
|
|
|
noSubmit
|
|
|
|
name="chevron-down"
|
|
|
|
data-direction="-1"
|
|
|
|
>↓
|
|
|
|
</et2-button-icon>
|
2022-11-01 17:34:52 +01:00
|
|
|
</div>`;
|
2022-02-23 18:43:39 +01:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
|
2021-08-27 19:21:40 +02:00
|
|
|
// @ts-ignore TypeScript is not recognizing that Et2Date is a LitElement
|
2022-03-01 15:09:27 +01:00
|
|
|
customElements.define("et2-date", Et2Date);
|