2021-12-08 07:54:28 +01:00
|
|
|
import {formatISO} from "date-fns";
|
|
|
|
import ConfirmDatePlugin from "flatpickr/dist/plugins/confirmDate/confirmDate";
|
|
|
|
import $ from "jquery";
|
|
|
|
|
|
|
|
import {$t} from "./i18n";
|
2022-04-06 19:42:15 +02:00
|
|
|
import {user_settings} from "./user_settings";
|
2021-12-08 07:54:28 +01:00
|
|
|
|
|
|
|
function is_numeric_key(key) {
|
|
|
|
return ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].includes(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function show_flatpickr(element, callback, default_timestamp, options = {}) {
|
2022-08-16 06:50:20 +02:00
|
|
|
const $flatpickr_input = $("<input>").attr("id", "#timestamp_flatpickr");
|
2021-12-08 07:54:28 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const instance = $flatpickr_input.flatpickr({
|
2021-12-08 07:54:28 +01:00
|
|
|
mode: "single",
|
|
|
|
enableTime: true,
|
|
|
|
clickOpens: false,
|
|
|
|
defaultDate: default_timestamp,
|
|
|
|
plugins: [
|
|
|
|
new ConfirmDatePlugin({
|
|
|
|
showAlways: true,
|
|
|
|
confirmText: $t({defaultMessage: "Confirm"}),
|
|
|
|
confirmIcon: "",
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
positionElement: element,
|
|
|
|
dateFormat: "Z",
|
|
|
|
formatDate: (date) => formatISO(date),
|
|
|
|
disableMobile: true,
|
2022-04-06 19:42:15 +02:00
|
|
|
time_24hr: user_settings.twenty_four_hour_time,
|
2023-04-14 04:55:27 +02:00
|
|
|
minuteIncrement: 1,
|
2023-06-29 21:59:08 +02:00
|
|
|
onKeyDown(_selectedDates, _dateStr, instance, event) {
|
2023-03-21 18:14:45 +01:00
|
|
|
// See also the keydown handler below.
|
|
|
|
//
|
|
|
|
// TODO: Add a clear explanation of exactly how key
|
|
|
|
// interactions are dispatched; it seems that keyboard
|
|
|
|
// logic from this function, the built-in flatpickr
|
|
|
|
// onKeyDown function, and the below keydown handler are
|
|
|
|
// used, but it's not at all clear in what order they are
|
|
|
|
// called, or what the overall control flow is.
|
2023-06-09 03:33:37 +02:00
|
|
|
if (event.key === "Tab") {
|
2023-03-21 18:14:45 +01:00
|
|
|
// Ensure that tab/shift_tab navigation work to
|
|
|
|
// navigate between the elements in flatpickr itself
|
|
|
|
// and the confirmation button at the bottom of the
|
|
|
|
// popover.
|
2021-12-08 07:54:28 +01:00
|
|
|
const elems = [
|
|
|
|
instance.selectedDateElem,
|
|
|
|
instance.hourElement,
|
|
|
|
instance.minuteElement,
|
|
|
|
instance.amPM,
|
|
|
|
$(".flatpickr-confirm")[0],
|
|
|
|
];
|
|
|
|
const i = elems.indexOf(event.target);
|
|
|
|
const n = elems.length;
|
|
|
|
const remain = (i + (event.shiftKey ? -1 : 1)) % n;
|
|
|
|
const target = elems[Math.floor(remain >= 0 ? remain : remain + n)];
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
target.focus();
|
2023-03-21 18:14:45 +01:00
|
|
|
} else {
|
|
|
|
// Prevent keypresses from propagating to our general hotkey.js
|
|
|
|
// logic. Without this, `Up` will navigate both in the
|
|
|
|
// flatpickr instance and in the message feed behind
|
|
|
|
// it.
|
|
|
|
event.stopPropagation();
|
2021-12-08 07:54:28 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
|
2022-08-18 20:49:42 +02:00
|
|
|
const $container = $(instance.innerContainer).parent();
|
2021-12-08 07:54:28 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.on("keydown", (e) => {
|
2023-03-21 18:14:45 +01:00
|
|
|
// Main keyboard UI implementation.
|
|
|
|
|
2021-12-08 07:54:28 +01:00
|
|
|
if (is_numeric_key(e.key)) {
|
|
|
|
// Let users type numeric values
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-09 03:33:37 +02:00
|
|
|
if (e.key === "Backspace" || e.key === "Delete") {
|
2021-12-08 07:54:28 +01:00
|
|
|
// Let backspace or delete be handled normally
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-09 03:33:37 +02:00
|
|
|
if (e.key === "Enter") {
|
2021-12-08 07:54:28 +01:00
|
|
|
if (e.target.classList[0] === "flatpickr-day") {
|
2023-03-21 18:14:45 +01:00
|
|
|
// use flatpickr's built-in behavior to choose the selected day.
|
|
|
|
return true;
|
2021-12-08 07:54:28 +01:00
|
|
|
}
|
|
|
|
$(element).toggleClass("has_popover");
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.find(".flatpickr-confirm").trigger("click");
|
2021-12-08 07:54:28 +01:00
|
|
|
}
|
|
|
|
|
2023-06-09 03:33:37 +02:00
|
|
|
if (e.key === "Escape") {
|
2021-12-08 07:54:28 +01:00
|
|
|
$(element).toggleClass("has_popover");
|
|
|
|
instance.close();
|
|
|
|
instance.destroy();
|
|
|
|
}
|
|
|
|
|
2023-06-09 03:33:37 +02:00
|
|
|
if (e.key === "Tab") {
|
2023-03-21 18:14:45 +01:00
|
|
|
// Use flatpickr's built-in navigation between elements.
|
|
|
|
return true;
|
2021-12-08 07:54:28 +01:00
|
|
|
}
|
|
|
|
|
2023-06-09 03:33:37 +02:00
|
|
|
if (["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown"].includes(e.key)) {
|
2023-03-21 18:14:45 +01:00
|
|
|
// use flatpickr's built-in navigation of the date grid.
|
|
|
|
return true;
|
2021-12-08 07:54:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.on("click", ".flatpickr-confirm", () => {
|
|
|
|
callback($flatpickr_input.val());
|
2021-12-08 07:54:28 +01:00
|
|
|
instance.close();
|
|
|
|
instance.destroy();
|
|
|
|
});
|
|
|
|
instance.open();
|
|
|
|
instance.selectedDateElem.focus();
|
|
|
|
|
|
|
|
return instance;
|
2022-01-28 20:51:25 +01:00
|
|
|
}
|
2023-05-10 19:34:19 +02:00
|
|
|
|
|
|
|
export function close_all() {
|
|
|
|
$(".flatpickr-calendar").removeClass("open");
|
|
|
|
}
|