2021-11-03 16:05:16 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Date+Time 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-02-15 19:47:42 +01:00
|
|
|
import {css} from "@lion/core";
|
|
|
|
import {Et2Date} from "./Et2Date";
|
2023-03-20 13:41:25 +01:00
|
|
|
import type {Instance} from "flatpickr/dist/types/instance";
|
|
|
|
import {default as ShortcutButtonsPlugin} from "shortcut-buttons-flatpickr/dist/shortcut-buttons-flatpickr";
|
2021-11-03 16:05:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
export class Et2DateTime extends Et2Date
|
|
|
|
{
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles,
|
|
|
|
css`
|
2023-02-24 19:58:16 +01:00
|
|
|
:host([focused]) ::slotted(button), :host(:hover) ::slotted(button) {
|
2021-11-03 16:05:16 +01:00
|
|
|
display: inline-block;
|
2023-02-24 19:58:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
::slotted([slot='input']) {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
min-width: 17ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
::slotted(.calendar_button) {
|
|
|
|
border: none;
|
|
|
|
background: transparent;
|
|
|
|
margin-left: -20px;
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
`,
|
2021-11-03 16:05:16 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
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();
|
2021-11-03 16:05:16 +01:00
|
|
|
|
2022-03-04 23:37:22 +01:00
|
|
|
let dateFormat = (this.egw()?.preference("dateformat") || "Y-m-d");
|
|
|
|
let timeFormat = ((<string>this.egw()?.preference("timeformat") || "24") == "24" ? "H:i" : "h:i K");
|
2022-02-22 19:23:54 +01:00
|
|
|
options.altFormat = dateFormat + " " + timeFormat;
|
|
|
|
options.enableTime = true;
|
2022-03-04 23:37:22 +01:00
|
|
|
options.time_24hr = this.egw()?.preference("timeformat", "common") == "24";
|
2022-02-22 19:23:54 +01:00
|
|
|
options.dateFormat = "Y-m-dTH:i:00\\Z";
|
|
|
|
options.defaultHour = new Date().getHours();
|
|
|
|
|
|
|
|
return options;
|
2021-11-03 16:05:16 +01:00
|
|
|
}
|
2022-10-14 18:05:59 +02:00
|
|
|
|
2022-10-14 19:18:17 +02:00
|
|
|
/**
|
|
|
|
* Change handler setting modelValue for validation
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* @param selectedDates
|
|
|
|
* @param dateStr
|
|
|
|
* @param instance
|
|
|
|
*/
|
|
|
|
_updateValueOnChange(selectedDates : Date[], dateStr : string, instance : Instance)
|
|
|
|
{
|
|
|
|
super._updateValueOnChange(selectedDates, dateStr, instance);
|
2022-10-20 23:27:24 +02:00
|
|
|
if(!this.freeMinuteEntry && dateStr && instance && instance.config.minuteIncrement > 1)
|
2022-10-14 19:18:17 +02:00
|
|
|
{
|
|
|
|
let i = instance.latestSelectedDateObj;
|
|
|
|
const d = i ? i : new Date();
|
|
|
|
const original = d.getMinutes();
|
|
|
|
|
|
|
|
let bound = Math.round(original / instance.config.minuteIncrement) * instance.config.minuteIncrement;
|
|
|
|
if(bound != original)
|
|
|
|
{
|
|
|
|
d.setMinutes(bound);
|
|
|
|
instance.setDate(d, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 17:34:52 +01:00
|
|
|
/**
|
|
|
|
* For mobile, we use a plain input of the proper type
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
_mobileInputType() : string
|
|
|
|
{
|
|
|
|
return "datetime-local";
|
|
|
|
}
|
|
|
|
|
2022-10-14 18:05:59 +02:00
|
|
|
/**
|
|
|
|
* Add "today" button below calendar
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _buttonPlugin()
|
|
|
|
{
|
|
|
|
return ShortcutButtonsPlugin({
|
2022-10-18 17:08:53 +02:00
|
|
|
button: [
|
|
|
|
{label: this.egw().lang("ok")},
|
|
|
|
{label: this.egw().lang("Now")}
|
|
|
|
],
|
2022-10-14 18:05:59 +02:00
|
|
|
onClick: this._handleShortcutButtonClick
|
|
|
|
})
|
|
|
|
}
|
2022-10-20 23:27:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 * this.getOptions().minuteIncrement, "minute", true);
|
|
|
|
}
|
2021-11-03 16:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore TypeScript is not recognizing that Et2DateTime is a LitElement
|
2023-03-20 13:41:25 +01:00
|
|
|
customElements.define("et2-date-time", Et2DateTime);
|