Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

flatpickr: Use flatpickr instance instead of has_popover to track it.

Aman Agrawalddb0d8d298c0
diff --git a/web/src/compose.js b/web/src/compose.js
index 8d97f42ecfe..8661ba4606d 100644
--- a/web/src/compose.js
+++ b/web/src/compose.js
@@ -696,8 +696,6 @@ export function initialize() {
e.preventDefault();
e.stopPropagation();
- $(e.target).toggleClass("has_popover");
-
let $target_textarea;
let edit_message_id;
const compose_click_target = compose_ui.get_compose_click_target(e);
@@ -708,7 +706,7 @@ export function initialize() {
$target_textarea = $(compose_click_target).closest("form").find("textarea");
}
- if ($(e.target).hasClass("has_popover")) {
+ if (!flatpickr.is_open()) {
const on_timestamp_selection = (val) => {
const timestr = `<time:${val}> `;
compose_ui.insert_syntax_and_focus(timestr, $target_textarea);
@@ -721,8 +719,13 @@ export function initialize() {
{
// place the time picker wherever there is space and center it horizontally
position: "auto center",
+ // Since we want to handle close of flatpickr manually, we don't want
+ // flatpickr to hide automatically on clicking its trigger element.
+ ignoredFocusElements: [e.currentTarget],
},
);
+ } else {
+ flatpickr.flatpickr_instace?.close();
}
});
diff --git a/web/src/flatpickr.ts b/web/src/flatpickr.ts
index 082a6a22a0b..13c9dd57558 100644
--- a/web/src/flatpickr.ts
+++ b/web/src/flatpickr.ts
@@ -7,6 +7,12 @@ import assert from "minimalistic-assert";
import {$t} from "./i18n";
import {user_settings} from "./user_settings";
+export let flatpickr_instace: flatpickr.Instance;
+
+export function is_open(): boolean {
+ return Boolean(flatpickr_instace?.isOpen);
+}
+
function is_numeric_key(key: string): boolean {
return ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].includes(key);
}
@@ -19,7 +25,7 @@ export function show_flatpickr(
): flatpickr.Instance {
const $flatpickr_input = $<HTMLInputElement>("<input>").attr("id", "#timestamp_flatpickr");
- const instance = flatpickr($flatpickr_input[0], {
+ flatpickr_instace = flatpickr($flatpickr_input[0], {
mode: "single",
enableTime: true,
clickOpens: false,
@@ -77,7 +83,7 @@ export function show_flatpickr(
...options,
});
- const $container = $(instance.calendarContainer);
+ const $container = $(flatpickr_instace.calendarContainer);
$container.on("keydown", (e) => {
// Main keyboard UI implementation.
@@ -97,14 +103,12 @@ export function show_flatpickr(
// use flatpickr's built-in behavior to choose the selected day.
return true;
}
- $(element).toggleClass("has_popover");
$container.find(".flatpickr-confirm").trigger("click");
}
if (e.key === "Escape") {
- $(element).toggleClass("has_popover");
- instance.close();
- instance.destroy();
+ flatpickr_instace.close();
+ flatpickr_instace.destroy();
}
if (e.key === "Tab") {
@@ -127,14 +131,14 @@ export function show_flatpickr(
const time = $flatpickr_input.val();
assert(typeof time === "string");
callback(time);
- instance.close();
- instance.destroy();
+ flatpickr_instace.close();
+ flatpickr_instace.destroy();
});
- instance.open();
- assert(instance.selectedDateElem !== undefined);
- instance.selectedDateElem.focus();
+ flatpickr_instace.open();
+ assert(flatpickr_instace.selectedDateElem !== undefined);
+ flatpickr_instace.selectedDateElem.focus();
- return instance;
+ return flatpickr_instace;
}
export function close_all(): void {