2020-09-25 14:08:27 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2020-09-25 14:08:27 +01:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2024-09-09 14:57:16 +01:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2020-09-25 14:08:27 +01:00
|
|
|
*/
|
|
|
|
|
|
2021-10-22 17:23:32 -05:00
|
|
|
import { IModalWidgetOpenRequestData, IModalWidgetReturnData, Widget } from "matrix-widget-api";
|
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
|
2020-09-25 14:08:27 +01:00
|
|
|
import { AsyncStoreWithClient } from "./AsyncStoreWithClient";
|
|
|
|
|
import defaultDispatcher from "../dispatcher/dispatcher";
|
|
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
2021-06-29 13:11:58 +01:00
|
|
|
import Modal, { IHandle, IModal } from "../Modal";
|
2020-09-25 14:08:27 +01:00
|
|
|
import ModalWidgetDialog from "../components/views/dialogs/ModalWidgetDialog";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { WidgetMessagingStore } from "./widgets/WidgetMessagingStore";
|
2021-10-15 16:30:53 +02:00
|
|
|
|
2020-09-25 14:08:27 +01:00
|
|
|
interface IState {
|
|
|
|
|
modal?: IModal<any>;
|
|
|
|
|
openedFromId?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ModalWidgetStore extends AsyncStoreWithClient<IState> {
|
2022-08-30 15:13:39 -04:00
|
|
|
private static readonly internalInstance = (() => {
|
|
|
|
|
const instance = new ModalWidgetStore();
|
|
|
|
|
return instance;
|
|
|
|
|
})();
|
2023-02-28 10:31:48 +00:00
|
|
|
private modalInstance: IHandle<typeof ModalWidgetDialog> | null = null;
|
2022-08-30 15:13:39 -04:00
|
|
|
private openSourceWidgetId: string | null = null;
|
|
|
|
|
private openSourceWidgetRoomId: string | null = null;
|
2020-09-25 14:08:27 +01:00
|
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
|
super(defaultDispatcher, {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static get instance(): ModalWidgetStore {
|
|
|
|
|
return ModalWidgetStore.internalInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async onAction(payload: ActionPayload): Promise<any> {
|
|
|
|
|
// nothing
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public canOpenModalWidget = (): boolean => {
|
2020-09-25 14:08:27 +01:00
|
|
|
return !this.modalInstance;
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-02 14:22:28 +00:00
|
|
|
public openModalWidget = (
|
|
|
|
|
requestData: IModalWidgetOpenRequestData,
|
|
|
|
|
sourceWidget: Widget,
|
|
|
|
|
widgetRoomId?: string,
|
2023-01-12 13:25:14 +00:00
|
|
|
): void => {
|
2020-09-25 14:08:27 +01:00
|
|
|
if (this.modalInstance) return;
|
2020-10-19 20:39:43 +01:00
|
|
|
this.openSourceWidgetId = sourceWidget.id;
|
2023-03-16 11:07:29 +00:00
|
|
|
this.openSourceWidgetRoomId = widgetRoomId ?? null;
|
2022-06-14 17:51:51 +01:00
|
|
|
this.modalInstance = Modal.createDialog(
|
|
|
|
|
ModalWidgetDialog,
|
|
|
|
|
{
|
2021-06-29 13:11:58 +01:00
|
|
|
widgetDefinition: { ...requestData },
|
2021-02-02 14:22:28 +00:00
|
|
|
widgetRoomId,
|
2020-10-19 20:39:43 +01:00
|
|
|
sourceWidgetId: sourceWidget.id,
|
2023-03-16 11:07:29 +00:00
|
|
|
onFinished: (success, data) => {
|
2023-06-27 17:39:56 +01:00
|
|
|
this.closeModalWidget(sourceWidget, widgetRoomId, success && data ? data : { "m.exited": true });
|
2020-09-25 14:08:27 +01:00
|
|
|
|
|
|
|
|
this.openSourceWidgetId = null;
|
2022-03-15 08:15:26 -04:00
|
|
|
this.openSourceWidgetRoomId = null;
|
2020-09-25 14:08:27 +01:00
|
|
|
this.modalInstance = null;
|
2022-12-12 12:24:14 +01:00
|
|
|
},
|
2020-09-25 14:08:27 +01:00
|
|
|
},
|
2023-03-16 11:07:29 +00:00
|
|
|
undefined,
|
2020-11-25 18:35:00 -07:00
|
|
|
/* priority = */ false,
|
|
|
|
|
/* static = */ true,
|
|
|
|
|
);
|
2020-09-25 14:08:27 +01:00
|
|
|
};
|
|
|
|
|
|
2023-03-16 11:07:29 +00:00
|
|
|
public closeModalWidget = (
|
|
|
|
|
sourceWidget: Widget,
|
|
|
|
|
widgetRoomId: string | undefined,
|
|
|
|
|
data: IModalWidgetReturnData,
|
|
|
|
|
): void => {
|
2020-09-25 14:08:27 +01:00
|
|
|
if (!this.modalInstance) return;
|
2022-03-15 08:15:26 -04:00
|
|
|
if (this.openSourceWidgetId === sourceWidget.id && this.openSourceWidgetRoomId === widgetRoomId) {
|
2020-09-25 14:08:27 +01:00
|
|
|
this.openSourceWidgetId = null;
|
2022-03-15 08:15:26 -04:00
|
|
|
this.openSourceWidgetRoomId = null;
|
2020-09-25 14:08:27 +01:00
|
|
|
this.modalInstance.close();
|
|
|
|
|
this.modalInstance = null;
|
|
|
|
|
|
2022-03-15 08:15:26 -04:00
|
|
|
const sourceMessaging = WidgetMessagingStore.instance.getMessaging(sourceWidget, widgetRoomId);
|
2020-09-25 14:08:27 +01:00
|
|
|
if (!sourceMessaging) {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error("No source widget messaging for modal widget");
|
2020-09-25 14:08:27 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2020-10-19 20:39:43 +01:00
|
|
|
sourceMessaging.notifyModalWidgetClose(data);
|
2020-09-25 14:08:27 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-09-25 15:03:54 +01:00
|
|
|
|
|
|
|
|
window.mxModalWidgetStore = ModalWidgetStore.instance;
|