2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-07-04 08:47:08 +02:00
|
|
|
import Micromodal from "micromodal";
|
2021-03-11 05:43:45 +01:00
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-03-22 16:09:12 +01:00
|
|
|
import * as browser_history from "./browser_history";
|
2021-02-28 01:03:46 +01:00
|
|
|
import * as popovers from "./popovers";
|
2021-02-28 01:03:09 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
let $active_overlay;
|
2019-11-02 00:06:25 +01:00
|
|
|
let close_handler;
|
|
|
|
let open_overlay_name;
|
2017-05-06 02:27:40 +02:00
|
|
|
|
|
|
|
function reset_state() {
|
2022-01-25 11:36:19 +01:00
|
|
|
$active_overlay = undefined;
|
2017-05-06 02:27:40 +02:00
|
|
|
close_handler = undefined;
|
2017-05-27 15:40:54 +02:00
|
|
|
open_overlay_name = undefined;
|
2017-05-06 02:27:40 +02:00
|
|
|
}
|
2017-05-05 23:43:05 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function is_active() {
|
2020-10-07 12:54:16 +02:00
|
|
|
return Boolean(open_overlay_name);
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-05-06 02:40:32 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function is_modal_open() {
|
2022-07-23 00:15:37 +02:00
|
|
|
return $(".micromodal").hasClass("modal--open");
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-08-29 19:05:05 +02:00
|
|
|
|
2022-04-04 06:09:04 +02:00
|
|
|
export function is_overlay_or_modal_open() {
|
|
|
|
return is_active() || is_modal_open();
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function info_overlay_open() {
|
2020-07-15 01:29:15 +02:00
|
|
|
return open_overlay_name === "informationalOverlays";
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-05-09 16:45:02 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function settings_open() {
|
2020-07-15 01:29:15 +02:00
|
|
|
return open_overlay_name === "settings";
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-05-10 00:37:08 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function streams_open() {
|
2020-07-15 01:29:15 +02:00
|
|
|
return open_overlay_name === "subscriptions";
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-05-16 05:28:14 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function lightbox_open() {
|
2020-07-15 01:29:15 +02:00
|
|
|
return open_overlay_name === "lightbox";
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-05-10 15:43:36 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function drafts_open() {
|
2020-07-15 01:29:15 +02:00
|
|
|
return open_overlay_name === "drafts";
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-10-03 00:41:43 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function active_modal() {
|
|
|
|
if (!is_modal_open()) {
|
2017-10-03 00:41:43 +02:00
|
|
|
blueslip.error("Programming error — Called active_modal when there is no modal open");
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-08-29 19:05:05 +02:00
|
|
|
}
|
2021-07-04 08:47:08 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $micromodal = $(".micromodal.modal--open");
|
2022-07-23 00:15:37 +02:00
|
|
|
return `#${CSS.escape($micromodal.attr("id"))}`;
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-08-29 19:05:05 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function open_overlay(opts) {
|
2017-10-31 14:36:54 +01:00
|
|
|
popovers.hide_all();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
if (!opts.name || !opts.$overlay || !opts.on_close) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Programming error in open_overlay");
|
2017-05-06 00:37:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($active_overlay || open_overlay_name || close_handler) {
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.error(
|
|
|
|
"Programming error — trying to open " +
|
|
|
|
opts.name +
|
|
|
|
" before closing " +
|
|
|
|
open_overlay_name,
|
|
|
|
);
|
2017-05-06 02:27:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.debug("open overlay: " + opts.name);
|
2017-06-15 17:03:29 +02:00
|
|
|
|
2017-05-06 00:37:33 +02:00
|
|
|
// Our overlays are kind of crufty...we have an HTML id
|
|
|
|
// attribute for them and then a data-overlay attribute for
|
|
|
|
// them. Make sure they match.
|
2022-01-25 11:36:19 +01:00
|
|
|
if (opts.$overlay.attr("data-overlay") !== opts.name) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Bad overlay setup for " + opts.name);
|
2017-05-06 00:37:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
open_overlay_name = opts.name;
|
2022-01-25 11:36:19 +01:00
|
|
|
$active_overlay = opts.$overlay;
|
|
|
|
opts.$overlay.addClass("show");
|
2017-06-22 17:27:53 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
opts.$overlay.attr("aria-hidden", "false");
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".app").attr("aria-hidden", "true");
|
|
|
|
$(".fixed-app").attr("aria-hidden", "true");
|
|
|
|
$(".header").attr("aria-hidden", "true");
|
2017-05-06 00:37:33 +02:00
|
|
|
|
2017-05-06 02:27:40 +02:00
|
|
|
close_handler = function () {
|
2017-05-06 00:37:33 +02:00
|
|
|
opts.on_close();
|
2017-05-06 02:27:40 +02:00
|
|
|
reset_state();
|
2017-05-06 00:37:33 +02:00
|
|
|
};
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-05-06 00:37:33 +02:00
|
|
|
|
2021-06-19 06:18:09 +02:00
|
|
|
// If conf.autoremove is true, the modal element will be removed from the DOM
|
2021-06-16 07:56:25 +02:00
|
|
|
// once the modal is hidden.
|
2021-11-21 15:28:46 +01:00
|
|
|
// conf also accepts the following optional properties:
|
|
|
|
// on_show: Callback to run when the modal is triggered to show.
|
|
|
|
// on_shown: Callback to run when the modal is shown.
|
|
|
|
// on_hide: Callback to run when the modal is triggered to hide.
|
|
|
|
// on_hidden: Callback to run when the modal is hidden.
|
2022-07-23 00:15:37 +02:00
|
|
|
export function open_modal(selector, conf = {}) {
|
2020-05-09 15:45:54 +02:00
|
|
|
if (selector === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Undefined selector was passed into open_modal");
|
2020-05-09 15:45:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:47:08 +02:00
|
|
|
// Don't accept hash-based selector to enforce modals to have unique ids and
|
|
|
|
// since micromodal doesn't accept hash based selectors.
|
2022-07-23 00:15:37 +02:00
|
|
|
if (selector[0] === "#") {
|
|
|
|
blueslip.error("hash-based selector passed in to open_modal: " + selector);
|
2021-07-04 08:47:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
if (is_modal_open()) {
|
|
|
|
blueslip.error("open_modal() was called while " + active_modal() + " modal was open.");
|
2017-08-29 19:05:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.debug("open modal: " + selector);
|
2017-08-29 19:05:05 +02:00
|
|
|
|
2022-07-23 00:15:37 +02:00
|
|
|
// Micromodal gets elements using the getElementById DOM function
|
|
|
|
// which doesn't require the hash. We add it manually here.
|
|
|
|
const id_selector = `#${selector}`;
|
|
|
|
const $micromodal = $(id_selector);
|
|
|
|
|
|
|
|
$micromodal.find(".modal__container").on("animationend", (event) => {
|
|
|
|
const animation_name = event.originalEvent.animationName;
|
|
|
|
if (animation_name === "mmfadeIn") {
|
|
|
|
// Micromodal adds the is-open class before the modal animation
|
|
|
|
// is complete, which isn't really helpful since a modal is open after the
|
|
|
|
// animation is complete. So, we manually add a class after the
|
|
|
|
// animation is complete.
|
|
|
|
$micromodal.addClass("modal--open");
|
|
|
|
$micromodal.removeClass("modal--opening");
|
|
|
|
|
|
|
|
if (conf.on_shown) {
|
|
|
|
conf.on_shown();
|
2021-07-04 08:47:08 +02:00
|
|
|
}
|
2022-07-23 00:15:37 +02:00
|
|
|
} else if (animation_name === "mmfadeOut") {
|
|
|
|
// Call the on_hidden callback after the modal finishes hiding.
|
2021-07-04 08:47:08 +02:00
|
|
|
|
2022-07-23 00:15:37 +02:00
|
|
|
$micromodal.removeClass("modal--open");
|
|
|
|
if (conf.autoremove) {
|
|
|
|
$micromodal.remove();
|
|
|
|
}
|
|
|
|
if (conf.on_hidden) {
|
|
|
|
conf.on_hidden();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Micromodal.show(selector, {
|
|
|
|
disableFocus: true,
|
|
|
|
openClass: "modal--opening",
|
|
|
|
onShow: conf?.on_show,
|
|
|
|
onClose: conf?.on_hide,
|
|
|
|
});
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-08-29 19:05:05 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function close_overlay(name) {
|
2020-06-26 13:22:11 +02:00
|
|
|
popovers.hide_all();
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
if (name !== open_overlay_name) {
|
2017-10-06 21:36:39 +02:00
|
|
|
blueslip.error("Trying to close " + name + " when " + open_overlay_name + " is open.");
|
2017-05-06 02:27:40 +02:00
|
|
|
return;
|
2017-05-05 23:43:05 +02:00
|
|
|
}
|
2017-05-06 02:27:40 +02:00
|
|
|
|
2017-06-15 17:03:29 +02:00
|
|
|
if (name === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Undefined name was passed into close_overlay");
|
2017-06-15 17:03:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.debug("close overlay: " + name);
|
2017-06-15 17:03:29 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$active_overlay.removeClass("show");
|
2017-06-22 17:27:53 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$active_overlay.attr("aria-hidden", "true");
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".app").attr("aria-hidden", "false");
|
|
|
|
$(".fixed-app").attr("aria-hidden", "false");
|
|
|
|
$(".header").attr("aria-hidden", "false");
|
2017-05-06 02:27:40 +02:00
|
|
|
|
2017-05-12 17:24:16 +02:00
|
|
|
if (!close_handler) {
|
2020-10-13 23:50:18 +02:00
|
|
|
blueslip.error("Overlay close handler for " + name + " not properly set up.");
|
2017-05-12 17:24:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-06 02:27:40 +02:00
|
|
|
close_handler();
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-05-06 02:27:40 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function close_active() {
|
2017-05-27 15:40:54 +02:00
|
|
|
if (!open_overlay_name) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.warn("close_active() called without checking is_active()");
|
2017-05-10 00:37:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
close_overlay(open_overlay_name);
|
|
|
|
}
|
2017-05-10 00:37:28 +02:00
|
|
|
|
2022-01-27 08:05:35 +01:00
|
|
|
// `conf` is an object with the following optional properties:
|
|
|
|
// * on_hidden: Callback to run when the modal finishes hiding.
|
2022-07-23 00:15:37 +02:00
|
|
|
export function close_modal(selector, conf = {}) {
|
2020-05-09 15:45:54 +02:00
|
|
|
if (selector === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Undefined selector was passed into close_modal");
|
2017-08-29 19:05:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
if (!is_modal_open()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.warn("close_active_modal() called without checking is_modal_open()");
|
2017-08-29 19:05:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-23 00:15:37 +02:00
|
|
|
if (active_modal() !== `#${selector}`) {
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.error(
|
2021-02-28 01:03:46 +01:00
|
|
|
"Trying to close " + selector + " modal when " + active_modal() + " is open.",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2017-08-29 19:05:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.debug("close modal: " + selector);
|
2017-08-29 19:05:05 +02:00
|
|
|
|
2022-07-23 00:15:37 +02:00
|
|
|
const id_selector = `#${selector}`;
|
|
|
|
const $micromodal = $(id_selector);
|
|
|
|
|
|
|
|
// On-hidden hooks should typically be registered in
|
|
|
|
// overlays.open_modal. However, we offer this alternative
|
|
|
|
// mechanism as a convenience for hooks only known when
|
|
|
|
// closing the modal.
|
|
|
|
$micromodal.find(".modal__container").on("animationend", (event) => {
|
|
|
|
const animation_name = event.originalEvent.animationName;
|
|
|
|
if (animation_name === "mmfadeOut" && conf.on_hidden) {
|
|
|
|
conf.on_hidden();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Micromodal.close(selector);
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-08-29 19:05:05 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function close_active_modal() {
|
|
|
|
if (!is_modal_open()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.warn("close_active_modal() called without checking is_modal_open()");
|
2017-08-29 19:05:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $micromodal = $(".micromodal.modal--open");
|
2022-07-23 00:15:37 +02:00
|
|
|
Micromodal.close(`${CSS.escape($micromodal.attr("id"))}`);
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-08-29 19:05:05 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function close_for_hash_change() {
|
2021-04-14 17:18:29 +02:00
|
|
|
$("div.overlay.show").removeClass("show");
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($active_overlay) {
|
2020-12-01 09:35:33 +01:00
|
|
|
close_handler();
|
|
|
|
}
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-05-05 23:43:05 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function open_settings() {
|
|
|
|
open_overlay({
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "settings",
|
2022-01-25 11:36:19 +01:00
|
|
|
$overlay: $("#settings_overlay_container"),
|
2020-07-20 22:18:43 +02:00
|
|
|
on_close() {
|
2021-03-22 16:09:12 +01:00
|
|
|
browser_history.exit_overlay();
|
2017-05-06 01:04:45 +02:00
|
|
|
},
|
|
|
|
});
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|
2017-05-06 01:04:45 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
export function initialize() {
|
2021-04-14 17:18:29 +02:00
|
|
|
$("body").on("click", "div.overlay, div.overlay .exit", (e) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let $target = $(e.target);
|
2017-05-05 23:43:05 +02:00
|
|
|
|
2021-06-19 02:00:58 +02:00
|
|
|
if (document.getSelection().type === "Range") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-14 17:18:29 +02:00
|
|
|
// if the target is not the div.overlay element, search up the node tree
|
2017-05-05 23:43:05 +02:00
|
|
|
// until it is found.
|
2017-07-07 17:30:22 +02:00
|
|
|
if ($target.is(".exit, .exit-sign, .overlay-content, .exit span")) {
|
2017-05-05 23:43:05 +02:00
|
|
|
$target = $target.closest("[data-overlay]");
|
2021-04-14 17:18:29 +02:00
|
|
|
} else if (!$target.is("div.overlay")) {
|
2017-05-05 23:43:05 +02:00
|
|
|
// not a valid click target then.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-24 21:27:33 +01:00
|
|
|
if ($target.data("noclose")) {
|
|
|
|
// This overlay has been marked explicitly to not be closed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const target_name = $target.attr("data-overlay");
|
2017-05-05 23:43:05 +02:00
|
|
|
|
2021-02-28 01:03:46 +01:00
|
|
|
close_overlay(target_name);
|
2017-05-05 23:43:05 +02:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2017-03-13 22:02:31 +01:00
|
|
|
});
|
2021-02-28 01:03:46 +01:00
|
|
|
}
|