2015-09-17 11:10:01 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2020-07-12 20:13:28 +00:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-09-17 11:10:01 +00:00
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2015-09-17 11:10:01 +00:00
|
|
|
*/
|
|
|
|
|
2024-10-25 11:20:25 +00:00
|
|
|
import React, { StrictMode } from "react";
|
2024-11-04 11:34:00 +00:00
|
|
|
import { createRoot, Root } from "react-dom/client";
|
2020-07-12 20:13:28 +00:00
|
|
|
import classNames from "classnames";
|
2024-11-04 11:34:00 +00:00
|
|
|
import { IDeferred, defer } from "matrix-js-sdk/src/utils";
|
2023-08-03 12:56:30 +00:00
|
|
|
import { TypedEventEmitter } from "matrix-js-sdk/src/matrix";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { Glass, TooltipProvider } from "@vector-im/compound-web";
|
2020-07-12 20:13:28 +00:00
|
|
|
|
2024-11-05 19:12:43 +00:00
|
|
|
import defaultDispatcher from "./dispatcher/dispatcher";
|
2020-01-24 12:28:03 +00:00
|
|
|
import AsyncWrapper from "./AsyncWrapper";
|
2023-04-26 09:36:00 +00:00
|
|
|
import { Defaultize } from "./@types/common";
|
2024-07-23 08:08:22 +00:00
|
|
|
import { ActionPayload } from "./dispatcher/payloads";
|
2017-01-16 17:01:26 +00:00
|
|
|
|
2016-11-04 14:37:02 +00:00
|
|
|
const DIALOG_CONTAINER_ID = "mx_Dialog_Container";
|
2019-02-28 22:55:58 +00:00
|
|
|
const STATIC_DIALOG_CONTAINER_ID = "mx_Dialog_StaticContainer";
|
2016-11-04 14:37:02 +00:00
|
|
|
|
2023-02-28 10:31:48 +00:00
|
|
|
// Type which accepts a React Component which looks like a Modal (accepts an onFinished prop)
|
2023-06-29 11:17:05 +00:00
|
|
|
export type ComponentType =
|
|
|
|
| React.ComponentType<{
|
|
|
|
onFinished(...args: any): void;
|
|
|
|
}>
|
|
|
|
| React.ComponentType<any>;
|
2023-02-28 10:31:48 +00:00
|
|
|
|
|
|
|
// Generic type which returns the props of the Modal component with the onFinished being optional.
|
2023-03-14 11:09:35 +00:00
|
|
|
export type ComponentProps<C extends ComponentType> = Defaultize<
|
|
|
|
Omit<React.ComponentProps<C>, "onFinished">,
|
|
|
|
C["defaultProps"]
|
|
|
|
> &
|
2023-02-28 10:31:48 +00:00
|
|
|
Partial<Pick<React.ComponentProps<C>, "onFinished">>;
|
|
|
|
|
|
|
|
export interface IModal<C extends ComponentType> {
|
2020-07-12 20:13:28 +00:00
|
|
|
elem: React.ReactNode;
|
|
|
|
className?: string;
|
|
|
|
beforeClosePromise?: Promise<boolean>;
|
2024-07-05 13:39:13 +00:00
|
|
|
closeReason?: ModalCloseReason;
|
|
|
|
onBeforeClose?(reason?: ModalCloseReason): Promise<boolean>;
|
2023-02-28 10:31:48 +00:00
|
|
|
onFinished: ComponentProps<C>["onFinished"];
|
|
|
|
close(...args: Parameters<ComponentProps<C>["onFinished"]>): void;
|
2021-03-30 21:37:06 +00:00
|
|
|
hidden?: boolean;
|
2024-07-05 13:39:13 +00:00
|
|
|
deferred?: IDeferred<Parameters<ComponentProps<C>["onFinished"]>>;
|
2020-07-12 20:13:28 +00:00
|
|
|
}
|
2015-09-17 11:10:01 +00:00
|
|
|
|
2023-02-28 10:31:48 +00:00
|
|
|
export interface IHandle<C extends ComponentType> {
|
|
|
|
finished: Promise<Parameters<ComponentProps<C>["onFinished"]>>;
|
|
|
|
close(...args: Parameters<ComponentProps<C>["onFinished"]>): void;
|
2020-07-12 20:13:28 +00:00
|
|
|
}
|
2019-03-23 02:22:13 +00:00
|
|
|
|
2023-02-28 10:31:48 +00:00
|
|
|
interface IOptions<C extends ComponentType> {
|
|
|
|
onBeforeClose?: IModal<C>["onBeforeClose"];
|
2020-07-12 20:13:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 15:33:09 +00:00
|
|
|
export enum ModalManagerEvent {
|
|
|
|
Opened = "opened",
|
2024-05-29 07:22:50 +00:00
|
|
|
Closed = "closed",
|
2022-11-09 15:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HandlerMap = {
|
|
|
|
[ModalManagerEvent.Opened]: () => void;
|
2024-05-29 07:22:50 +00:00
|
|
|
[ModalManagerEvent.Closed]: () => void;
|
2022-11-09 15:33:09 +00:00
|
|
|
};
|
|
|
|
|
2024-07-05 13:39:13 +00:00
|
|
|
type ModalCloseReason = "backgroundClick";
|
|
|
|
|
2024-11-04 11:34:00 +00:00
|
|
|
function getOrCreateContainer(id: string): HTMLDivElement {
|
|
|
|
let container = document.getElementById(id) as HTMLDivElement | null;
|
|
|
|
if (!container) {
|
|
|
|
container = document.createElement("div");
|
|
|
|
container.id = id;
|
|
|
|
document.body.appendChild(container);
|
|
|
|
}
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
2022-11-09 15:33:09 +00:00
|
|
|
export class ModalManager extends TypedEventEmitter<ModalManagerEvent, HandlerMap> {
|
2020-07-12 20:13:28 +00:00
|
|
|
private counter = 0;
|
|
|
|
// The modal to prioritise over all others. If this is set, only show
|
|
|
|
// this modal. Remove all other modals from the stack when this modal
|
|
|
|
// is closed.
|
2023-02-15 13:36:22 +00:00
|
|
|
private priorityModal: IModal<any> | null = null;
|
2020-07-12 20:13:28 +00:00
|
|
|
// The modal to keep open underneath other modals if possible. Useful
|
|
|
|
// for cases like Settings where the modal should remain open while the
|
|
|
|
// user is prompted for more information/errors.
|
2023-02-15 13:36:22 +00:00
|
|
|
private staticModal: IModal<any> | null = null;
|
2020-07-12 20:13:28 +00:00
|
|
|
// A list of the modals we have stacked up, with the most recent at [0]
|
|
|
|
// Neither the static nor priority modal will be in this list.
|
2020-07-12 20:17:51 +00:00
|
|
|
private modals: IModal<any>[] = [];
|
2020-07-12 20:13:28 +00:00
|
|
|
|
2024-11-04 11:34:00 +00:00
|
|
|
private static root?: Root;
|
|
|
|
private static getOrCreateRoot(): Root {
|
|
|
|
if (!ModalManager.root) {
|
|
|
|
const container = getOrCreateContainer(DIALOG_CONTAINER_ID);
|
|
|
|
ModalManager.root = createRoot(container);
|
2015-09-17 11:10:01 +00:00
|
|
|
}
|
2024-11-04 11:34:00 +00:00
|
|
|
return ModalManager.root;
|
2016-11-04 14:37:02 +00:00
|
|
|
}
|
2015-09-17 11:10:01 +00:00
|
|
|
|
2024-11-04 11:34:00 +00:00
|
|
|
private static staticRoot?: Root;
|
|
|
|
private static getOrCreateStaticRoot(): Root {
|
|
|
|
if (!ModalManager.staticRoot) {
|
|
|
|
const container = getOrCreateContainer(STATIC_DIALOG_CONTAINER_ID);
|
|
|
|
ModalManager.staticRoot = createRoot(container);
|
2019-02-28 22:55:58 +00:00
|
|
|
}
|
2024-11-04 11:34:00 +00:00
|
|
|
return ModalManager.staticRoot;
|
2019-02-28 22:55:58 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 08:08:22 +00:00
|
|
|
public constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
// We never unregister this, but the Modal class is a singleton so there would
|
|
|
|
// never be an opportunity to do so anyway, except in the entirely theoretical
|
|
|
|
// scenario of instantiating a non-singleton instance of the Modal class.
|
|
|
|
defaultDispatcher.register(this.onAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
private onAction = (payload: ActionPayload): void => {
|
|
|
|
if (payload.action === "logout") {
|
|
|
|
this.forceCloseAllModals();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public toggleCurrentDialogVisibility(): void {
|
2021-03-30 21:37:06 +00:00
|
|
|
const modal = this.getCurrentModal();
|
|
|
|
if (!modal) return;
|
|
|
|
modal.hidden = !modal.hidden;
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public hasDialogs(): boolean {
|
|
|
|
return !!this.priorityModal || !!this.staticModal || this.modals.length > 0;
|
2020-07-12 20:13:28 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 19:39:24 +00:00
|
|
|
/**
|
2024-07-05 13:39:13 +00:00
|
|
|
* DEPRECATED.
|
|
|
|
* This is used only for tests. They should be using forceCloseAllModals but that
|
|
|
|
* caused a chunk of tests to fail, so for now they continue to use this.
|
|
|
|
*
|
2023-03-24 19:39:24 +00:00
|
|
|
* @param reason either "backgroundClick" or undefined
|
|
|
|
* @return whether a modal was closed
|
|
|
|
*/
|
2024-07-05 13:39:13 +00:00
|
|
|
public closeCurrentModal(reason?: ModalCloseReason): boolean {
|
2020-10-13 16:26:48 +00:00
|
|
|
const modal = this.getCurrentModal();
|
|
|
|
if (!modal) {
|
2023-03-24 19:39:24 +00:00
|
|
|
return false;
|
2020-10-13 16:26:48 +00:00
|
|
|
}
|
|
|
|
modal.closeReason = reason;
|
|
|
|
modal.close();
|
2023-03-24 19:39:24 +00:00
|
|
|
return true;
|
2020-10-13 16:26:48 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 13:39:13 +00:00
|
|
|
/**
|
|
|
|
* Forces closes all open modals. The modals onBeforeClose function will not be
|
|
|
|
* run and the modal will not have a chance to prevent closing. Intended for
|
|
|
|
* situations like the user logging out of the app.
|
|
|
|
*/
|
|
|
|
public forceCloseAllModals(): void {
|
|
|
|
for (const modal of this.modals) {
|
|
|
|
modal.deferred?.resolve([]);
|
|
|
|
if (modal.onFinished) modal.onFinished.apply(null);
|
|
|
|
this.emitClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.modals = [];
|
|
|
|
this.reRender();
|
|
|
|
}
|
|
|
|
|
2024-11-12 21:19:11 +00:00
|
|
|
/**
|
|
|
|
* @typeParam C - the component type
|
|
|
|
*/
|
2023-02-28 10:31:48 +00:00
|
|
|
private buildModal<C extends ComponentType>(
|
2024-11-12 21:19:11 +00:00
|
|
|
Component: C,
|
2023-02-28 10:31:48 +00:00
|
|
|
props?: ComponentProps<C>,
|
2020-07-12 20:13:28 +00:00
|
|
|
className?: string,
|
2023-02-28 10:31:48 +00:00
|
|
|
options?: IOptions<C>,
|
2023-01-12 13:25:14 +00:00
|
|
|
): {
|
2023-02-28 10:31:48 +00:00
|
|
|
modal: IModal<C>;
|
|
|
|
closeDialog: IHandle<C>["close"];
|
|
|
|
onFinishedProm: IHandle<C>["finished"];
|
2023-01-12 13:25:14 +00:00
|
|
|
} {
|
2023-02-15 13:36:22 +00:00
|
|
|
const modal = {
|
|
|
|
onFinished: props?.onFinished,
|
|
|
|
onBeforeClose: options?.onBeforeClose,
|
2020-07-12 20:13:28 +00:00
|
|
|
className,
|
|
|
|
|
|
|
|
// these will be set below but we need an object reference to pass to getCloseFn before we can do that
|
|
|
|
elem: null,
|
2023-02-28 10:31:48 +00:00
|
|
|
} as IModal<C>;
|
2019-06-28 20:34:19 +00:00
|
|
|
|
|
|
|
// never call this from onFinished() otherwise it will loop
|
2023-02-28 10:31:48 +00:00
|
|
|
const [closeDialog, onFinishedProm] = this.getCloseFn<C>(modal, props);
|
2019-06-28 20:34:19 +00:00
|
|
|
|
|
|
|
// don't attempt to reuse the same AsyncWrapper for different dialogs,
|
|
|
|
// otherwise we'll get confused.
|
2020-07-12 20:13:28 +00:00
|
|
|
const modalCount = this.counter++;
|
2019-06-28 20:34:19 +00:00
|
|
|
|
2024-11-12 21:19:11 +00:00
|
|
|
// Typescript doesn't like us passing props as any here, but we know that they are well typed due to the rigorous generics.
|
|
|
|
modal.elem = (
|
|
|
|
<AsyncWrapper key={modalCount} onFinished={closeDialog}>
|
|
|
|
<Component {...(props as any)} onFinished={closeDialog} />
|
|
|
|
</AsyncWrapper>
|
|
|
|
);
|
2020-02-06 12:07:13 +00:00
|
|
|
modal.close = closeDialog;
|
2019-06-28 20:34:19 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
return { modal, closeDialog, onFinishedProm };
|
2019-06-28 20:34:19 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 10:31:48 +00:00
|
|
|
private getCloseFn<C extends ComponentType>(
|
|
|
|
modal: IModal<C>,
|
|
|
|
props?: ComponentProps<C>,
|
|
|
|
): [IHandle<C>["close"], IHandle<C>["finished"]] {
|
2024-07-05 13:39:13 +00:00
|
|
|
modal.deferred = defer<Parameters<ComponentProps<C>["onFinished"]>>();
|
2020-07-12 20:13:28 +00:00
|
|
|
return [
|
2023-02-28 10:31:48 +00:00
|
|
|
async (...args: Parameters<ComponentProps<C>["onFinished"]>): Promise<void> => {
|
2020-02-06 12:10:06 +00:00
|
|
|
if (modal.beforeClosePromise) {
|
|
|
|
await modal.beforeClosePromise;
|
|
|
|
} else if (modal.onBeforeClose) {
|
|
|
|
modal.beforeClosePromise = modal.onBeforeClose(modal.closeReason);
|
|
|
|
const shouldClose = await modal.beforeClosePromise;
|
2023-02-15 13:36:22 +00:00
|
|
|
modal.beforeClosePromise = undefined;
|
2020-02-06 12:10:06 +00:00
|
|
|
if (!shouldClose) {
|
|
|
|
return;
|
2022-12-12 11:24:14 +00:00
|
|
|
}
|
2020-02-06 12:10:06 +00:00
|
|
|
}
|
2024-07-05 13:39:13 +00:00
|
|
|
modal.deferred?.resolve(args);
|
2023-02-15 13:36:22 +00:00
|
|
|
if (props?.onFinished) props.onFinished.apply(null, args);
|
2020-07-12 20:13:28 +00:00
|
|
|
const i = this.modals.indexOf(modal);
|
2019-06-28 20:34:19 +00:00
|
|
|
if (i >= 0) {
|
2020-07-12 20:13:28 +00:00
|
|
|
this.modals.splice(i, 1);
|
2019-06-28 20:34:19 +00:00
|
|
|
}
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2020-07-12 20:13:28 +00:00
|
|
|
if (this.priorityModal === modal) {
|
|
|
|
this.priorityModal = null;
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2019-06-28 20:34:19 +00:00
|
|
|
// XXX: This is destructive
|
2020-07-12 20:13:28 +00:00
|
|
|
this.modals = [];
|
2019-06-28 20:34:19 +00:00
|
|
|
}
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2020-07-12 20:13:28 +00:00
|
|
|
if (this.staticModal === modal) {
|
|
|
|
this.staticModal = null;
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2019-06-28 20:34:19 +00:00
|
|
|
// XXX: This is destructive
|
2020-07-12 20:13:28 +00:00
|
|
|
this.modals = [];
|
2019-06-28 20:34:19 +00:00
|
|
|
}
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2020-07-12 20:13:28 +00:00
|
|
|
this.reRender();
|
2024-05-29 07:22:50 +00:00
|
|
|
this.emitClosed();
|
2019-08-09 15:47:22 +00:00
|
|
|
},
|
2024-07-05 13:39:13 +00:00
|
|
|
modal.deferred.promise,
|
2019-08-09 15:47:22 +00:00
|
|
|
];
|
2019-06-28 20:34:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 14:29:35 +00:00
|
|
|
/**
|
|
|
|
* @callback onBeforeClose
|
|
|
|
* @param {string?} reason either "backgroundClick" or null
|
|
|
|
* @return {Promise<bool>} whether the dialog should close
|
|
|
|
*/
|
|
|
|
|
2017-01-16 17:01:26 +00:00
|
|
|
/**
|
|
|
|
* Open a modal view.
|
|
|
|
*
|
|
|
|
* This can be used to display a react component which is loaded as an asynchronous
|
|
|
|
* webpack component. To do this, set 'loader' as:
|
|
|
|
*
|
|
|
|
* (cb) => {
|
|
|
|
* require(['<module>'], cb);
|
|
|
|
* }
|
|
|
|
*
|
2024-11-12 21:19:11 +00:00
|
|
|
* @param component The component to render as a dialog. This component must accept an `onFinished` prop function as
|
|
|
|
* per the type {@link ComponentType}. If loading a component with esoteric dependencies consider
|
|
|
|
* using React.lazy to async load the component.
|
|
|
|
* e.g. `lazy(() => import('./MyComponent'))`
|
2017-01-16 17:01:26 +00:00
|
|
|
*
|
2024-11-12 21:19:11 +00:00
|
|
|
* @param props properties to pass to the displayed component. (We will also pass an 'onFinished' property.)
|
2017-01-16 17:01:26 +00:00
|
|
|
*
|
2024-11-12 21:19:11 +00:00
|
|
|
* @param className CSS class to apply to the modal wrapper
|
2018-05-22 14:47:05 +00:00
|
|
|
*
|
2024-11-12 21:19:11 +00:00
|
|
|
* @param isPriorityModal if true, this modal will be displayed regardless
|
2018-05-22 14:47:05 +00:00
|
|
|
* of other modals that are currently in the stack.
|
|
|
|
* Also, when closed, all modals will be removed
|
|
|
|
* from the stack.
|
2024-11-12 21:19:11 +00:00
|
|
|
* @param isStaticModal if true, this modal will be displayed under other
|
2019-02-28 22:55:58 +00:00
|
|
|
* modals in the stack. When closed, all modals will
|
|
|
|
* also be removed from the stack. This is not compatible
|
|
|
|
* with being a priority modal. Only one modal can be
|
|
|
|
* static at a time.
|
2024-11-12 21:19:11 +00:00
|
|
|
* @param options? extra options for the dialog
|
|
|
|
* @param options.onBeforeClose a callback to decide whether to close the dialog
|
|
|
|
* @returns Object with 'close' parameter being a function that will close the dialog
|
2017-01-16 17:01:26 +00:00
|
|
|
*/
|
2024-11-12 21:19:11 +00:00
|
|
|
public createDialog<C extends ComponentType>(
|
|
|
|
component: C,
|
2023-02-28 10:31:48 +00:00
|
|
|
props?: ComponentProps<C>,
|
2020-07-12 20:13:28 +00:00
|
|
|
className?: string,
|
|
|
|
isPriorityModal = false,
|
|
|
|
isStaticModal = false,
|
2023-02-28 10:31:48 +00:00
|
|
|
options: IOptions<C> = {},
|
|
|
|
): IHandle<C> {
|
2022-11-09 15:33:09 +00:00
|
|
|
const beforeModal = this.getCurrentModal();
|
2024-11-12 21:19:11 +00:00
|
|
|
const { modal, closeDialog, onFinishedProm } = this.buildModal<C>(component, props, className, options);
|
2018-05-22 14:47:05 +00:00
|
|
|
if (isPriorityModal) {
|
|
|
|
// XXX: This is destructive
|
2020-07-12 20:13:28 +00:00
|
|
|
this.priorityModal = modal;
|
2019-02-28 22:55:58 +00:00
|
|
|
} else if (isStaticModal) {
|
|
|
|
// This is intentionally destructive
|
2020-07-12 20:13:28 +00:00
|
|
|
this.staticModal = modal;
|
2018-05-22 14:47:05 +00:00
|
|
|
} else {
|
2020-07-12 20:13:28 +00:00
|
|
|
this.modals.unshift(modal);
|
2018-05-22 14:47:05 +00:00
|
|
|
}
|
2016-11-04 14:37:02 +00:00
|
|
|
|
2020-07-12 20:13:28 +00:00
|
|
|
this.reRender();
|
2022-11-09 15:33:09 +00:00
|
|
|
this.emitIfChanged(beforeModal);
|
|
|
|
|
2019-08-09 15:47:22 +00:00
|
|
|
return {
|
|
|
|
close: closeDialog,
|
2019-08-09 16:29:22 +00:00
|
|
|
finished: onFinishedProm,
|
2019-08-09 15:47:22 +00:00
|
|
|
};
|
2016-11-04 14:37:02 +00:00
|
|
|
}
|
|
|
|
|
2024-11-12 21:19:11 +00:00
|
|
|
public appendDialog<C extends ComponentType>(
|
|
|
|
component: C,
|
2023-02-28 10:31:48 +00:00
|
|
|
props?: ComponentProps<C>,
|
2020-08-29 00:11:08 +00:00
|
|
|
className?: string,
|
2023-02-28 10:31:48 +00:00
|
|
|
): IHandle<C> {
|
2022-11-09 15:33:09 +00:00
|
|
|
const beforeModal = this.getCurrentModal();
|
2024-11-12 21:19:11 +00:00
|
|
|
const { modal, closeDialog, onFinishedProm } = this.buildModal<C>(component, props, className, {});
|
2019-06-28 20:34:19 +00:00
|
|
|
|
2020-07-12 20:13:28 +00:00
|
|
|
this.modals.push(modal);
|
2022-11-09 15:33:09 +00:00
|
|
|
|
2020-07-12 20:13:28 +00:00
|
|
|
this.reRender();
|
2022-11-09 15:33:09 +00:00
|
|
|
this.emitIfChanged(beforeModal);
|
|
|
|
|
2019-08-09 15:47:22 +00:00
|
|
|
return {
|
|
|
|
close: closeDialog,
|
2019-08-09 16:29:22 +00:00
|
|
|
finished: onFinishedProm,
|
2019-08-09 15:47:22 +00:00
|
|
|
};
|
2019-06-28 20:34:19 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 15:33:09 +00:00
|
|
|
private emitIfChanged(beforeModal?: IModal<any>): void {
|
|
|
|
if (beforeModal !== this.getCurrentModal()) {
|
|
|
|
this.emit(ModalManagerEvent.Opened);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 07:22:50 +00:00
|
|
|
/**
|
|
|
|
* Emit the closed event
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private emitClosed(): void {
|
|
|
|
this.emit(ModalManagerEvent.Closed);
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onBackgroundClick = (): void => {
|
2020-07-12 20:13:28 +00:00
|
|
|
const modal = this.getCurrentModal();
|
2020-02-06 12:07:13 +00:00
|
|
|
if (!modal) {
|
|
|
|
return;
|
2016-11-04 14:37:02 +00:00
|
|
|
}
|
2020-02-06 12:10:06 +00:00
|
|
|
// we want to pass a reason to the onBeforeClose
|
|
|
|
// callback, but close is currently defined to
|
|
|
|
// pass all number of arguments to the onFinished callback
|
|
|
|
// so, pass the reason to close through a member variable
|
|
|
|
modal.closeReason = "backgroundClick";
|
2020-02-06 12:07:13 +00:00
|
|
|
modal.close();
|
2023-02-15 13:36:22 +00:00
|
|
|
modal.closeReason = undefined;
|
2020-07-12 20:13:28 +00:00
|
|
|
};
|
2016-11-04 14:37:02 +00:00
|
|
|
|
2020-07-12 20:13:28 +00:00
|
|
|
private getCurrentModal(): IModal<any> {
|
|
|
|
return this.priorityModal ? this.priorityModal : this.modals[0] || this.staticModal;
|
2016-11-04 14:37:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private async reRender(): Promise<void> {
|
2020-07-12 20:13:28 +00:00
|
|
|
if (this.modals.length === 0 && !this.priorityModal && !this.staticModal) {
|
2020-08-03 15:02:26 +00:00
|
|
|
// If there is no modal to render, make all of Element available
|
2018-02-08 21:51:07 +00:00
|
|
|
// to screen reader users again
|
2024-11-05 19:12:43 +00:00
|
|
|
defaultDispatcher.dispatch({
|
2018-02-08 21:51:07 +00:00
|
|
|
action: "aria_unhide_main_app",
|
|
|
|
});
|
2024-11-04 11:34:00 +00:00
|
|
|
ModalManager.getOrCreateRoot().render(<></>);
|
|
|
|
ModalManager.getOrCreateStaticRoot().render(<></>);
|
2016-11-04 14:37:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-08 21:51:07 +00:00
|
|
|
// Hide the content outside the modal to screen reader users
|
|
|
|
// so they won't be able to navigate into it and act on it using
|
|
|
|
// screen reader specific features
|
2024-11-05 19:12:43 +00:00
|
|
|
defaultDispatcher.dispatch({
|
2018-02-08 21:51:07 +00:00
|
|
|
action: "aria_hide_main_app",
|
|
|
|
});
|
2017-12-20 09:09:26 +00:00
|
|
|
|
2020-07-12 20:13:28 +00:00
|
|
|
if (this.staticModal) {
|
|
|
|
const classes = classNames("mx_Dialog_wrapper mx_Dialog_staticWrapper", this.staticModal.className);
|
2019-02-28 22:55:58 +00:00
|
|
|
|
|
|
|
const staticDialog = (
|
2024-10-25 11:20:25 +00:00
|
|
|
<StrictMode>
|
|
|
|
<TooltipProvider>
|
|
|
|
<div className={classes}>
|
|
|
|
<Glass className="mx_Dialog_border">
|
|
|
|
<div className="mx_Dialog">{this.staticModal.elem}</div>
|
|
|
|
</Glass>
|
|
|
|
<div
|
|
|
|
data-testid="dialog-background"
|
|
|
|
className="mx_Dialog_background mx_Dialog_staticBackground"
|
|
|
|
onClick={this.onBackgroundClick}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</TooltipProvider>
|
|
|
|
</StrictMode>
|
2019-02-28 22:55:58 +00:00
|
|
|
);
|
|
|
|
|
2024-11-04 11:34:00 +00:00
|
|
|
ModalManager.getOrCreateStaticRoot().render(staticDialog);
|
2019-02-28 22:55:58 +00:00
|
|
|
} else {
|
|
|
|
// This is safe to call repeatedly if we happen to do that
|
2024-11-04 11:34:00 +00:00
|
|
|
ModalManager.getOrCreateStaticRoot().render(<></>);
|
2019-02-28 22:55:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 20:13:28 +00:00
|
|
|
const modal = this.getCurrentModal();
|
2021-03-30 21:37:06 +00:00
|
|
|
if (modal !== this.staticModal && !modal.hidden) {
|
2020-07-12 20:13:28 +00:00
|
|
|
const classes = classNames("mx_Dialog_wrapper", modal.className, {
|
|
|
|
mx_Dialog_wrapperWithStaticUnder: this.staticModal,
|
|
|
|
});
|
2019-02-28 22:55:58 +00:00
|
|
|
|
|
|
|
const dialog = (
|
2024-10-25 11:20:25 +00:00
|
|
|
<StrictMode>
|
|
|
|
<TooltipProvider>
|
|
|
|
<div className={classes}>
|
|
|
|
<Glass className="mx_Dialog_border">
|
|
|
|
<div className="mx_Dialog">{modal.elem}</div>
|
|
|
|
</Glass>
|
|
|
|
<div
|
|
|
|
data-testid="dialog-background"
|
|
|
|
className="mx_Dialog_background"
|
|
|
|
onClick={this.onBackgroundClick}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</TooltipProvider>
|
|
|
|
</StrictMode>
|
2019-02-28 22:55:58 +00:00
|
|
|
);
|
2015-09-17 11:10:01 +00:00
|
|
|
|
2024-11-04 11:34:00 +00:00
|
|
|
ModalManager.getOrCreateRoot().render(dialog);
|
2019-02-28 22:55:58 +00:00
|
|
|
} else {
|
|
|
|
// This is safe to call repeatedly if we happen to do that
|
2024-11-04 11:34:00 +00:00
|
|
|
ModalManager.getOrCreateRoot().render(<></>);
|
2019-02-28 22:55:58 +00:00
|
|
|
}
|
2016-11-04 14:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-17 11:10:01 +00:00
|
|
|
|
2020-07-12 20:13:28 +00:00
|
|
|
if (!window.singletonModalManager) {
|
|
|
|
window.singletonModalManager = new ModalManager();
|
2017-06-14 08:31:16 +00:00
|
|
|
}
|
2020-07-12 20:13:28 +00:00
|
|
|
export default window.singletonModalManager;
|