2021-01-04 19:46:24 +00:00
|
|
|
// Copyright 2015-2021 Signal Messenger, LLC
|
2021-01-04 18:47:14 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
// This file is here temporarily while we're switching off of Backbone into
|
|
|
|
// React. In the future, and in React-land, please just import and use
|
2021-04-27 19:29:59 +00:00
|
|
|
// ConfirmationDialog directly. This is the thin API layer to bridge the gap
|
2021-01-04 18:47:14 +00:00
|
|
|
// while we convert things over. Please delete this file once all usages are
|
|
|
|
// ported over. Note: this file cannot have any imports/exports since it is
|
|
|
|
// being included in a <script /> tag.
|
|
|
|
|
|
|
|
type ConfirmationDialogViewProps = {
|
2021-12-03 23:04:34 +00:00
|
|
|
onTopOfEverything?: boolean;
|
2021-01-04 18:47:14 +00:00
|
|
|
cancelText?: string;
|
|
|
|
confirmStyle?: 'affirmative' | 'negative';
|
|
|
|
message: string;
|
|
|
|
okText: string;
|
2021-02-26 21:06:37 +00:00
|
|
|
reject?: (error: Error) => void;
|
2021-01-04 18:47:14 +00:00
|
|
|
resolve: () => void;
|
|
|
|
};
|
|
|
|
|
2021-06-17 21:15:09 +00:00
|
|
|
let confirmationDialogViewNode: HTMLElement | undefined;
|
|
|
|
let confirmationDialogPreviousFocus: HTMLElement | undefined;
|
2021-01-04 18:47:14 +00:00
|
|
|
|
|
|
|
function removeConfirmationDialog() {
|
|
|
|
if (!confirmationDialogViewNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.ReactDOM.unmountComponentAtNode(confirmationDialogViewNode);
|
|
|
|
document.body.removeChild(confirmationDialogViewNode);
|
|
|
|
|
|
|
|
if (
|
|
|
|
confirmationDialogPreviousFocus &&
|
|
|
|
typeof confirmationDialogPreviousFocus.focus === 'function'
|
|
|
|
) {
|
|
|
|
confirmationDialogPreviousFocus.focus();
|
|
|
|
}
|
2021-06-17 21:15:09 +00:00
|
|
|
confirmationDialogViewNode = undefined;
|
2021-01-04 18:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showConfirmationDialog(options: ConfirmationDialogViewProps) {
|
|
|
|
if (confirmationDialogViewNode) {
|
|
|
|
removeConfirmationDialog();
|
|
|
|
}
|
|
|
|
|
|
|
|
confirmationDialogViewNode = document.createElement('div');
|
|
|
|
document.body.appendChild(confirmationDialogViewNode);
|
|
|
|
|
|
|
|
confirmationDialogPreviousFocus = document.activeElement as HTMLElement;
|
|
|
|
|
|
|
|
window.ReactDOM.render(
|
|
|
|
// eslint-disable-next-line react/react-in-jsx-scope, react/jsx-no-undef
|
2021-04-27 19:29:59 +00:00
|
|
|
<window.Signal.Components.ConfirmationDialog
|
2021-12-03 23:04:34 +00:00
|
|
|
onTopOfEverything={options.onTopOfEverything}
|
2021-01-04 18:47:14 +00:00
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
action: () => {
|
|
|
|
options.resolve();
|
|
|
|
},
|
|
|
|
style: options.confirmStyle,
|
|
|
|
text: options.okText || window.i18n('ok'),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
cancelText={options.cancelText || window.i18n('cancel')}
|
|
|
|
i18n={window.i18n}
|
2021-04-27 19:29:59 +00:00
|
|
|
onCancel={() => {
|
2021-01-04 18:47:14 +00:00
|
|
|
if (options.reject) {
|
2021-04-27 19:29:59 +00:00
|
|
|
options.reject(new Error('showConfirmationDialog: onCancel called'));
|
2021-01-04 18:47:14 +00:00
|
|
|
}
|
|
|
|
}}
|
2021-04-27 19:29:59 +00:00
|
|
|
onClose={() => {
|
|
|
|
removeConfirmationDialog();
|
|
|
|
}}
|
2021-01-04 18:47:14 +00:00
|
|
|
title={options.message}
|
|
|
|
/>,
|
|
|
|
confirmationDialogViewNode
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.showConfirmationDialog = showConfirmationDialog;
|