2015-09-17 11:10:01 +00:00
|
|
|
/*
|
2016-01-07 04:06:39 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-09-17 11:10:01 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2019-08-05 09:08:30 +00:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2017-05-29 16:39:03 +00:00
|
|
|
import Analytics from './Analytics';
|
2018-02-08 21:51:07 +00:00
|
|
|
import dis from './dispatcher';
|
2020-01-24 12:28:03 +00:00
|
|
|
import {defer} from './utils/promise';
|
|
|
|
import AsyncWrapper from './AsyncWrapper';
|
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
|
|
|
|
|
|
|
class ModalManager {
|
|
|
|
constructor() {
|
|
|
|
this._counter = 0;
|
|
|
|
|
2018-05-22 14:47:05 +00:00
|
|
|
// 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.
|
|
|
|
this._priorityModal = null;
|
2019-02-28 22:55:58 +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.
|
|
|
|
this._staticModal = null;
|
2018-05-22 14:47:05 +00:00
|
|
|
// A list of the modals we have stacked up, with the most recent at [0]
|
2019-02-28 22:55:58 +00:00
|
|
|
// Neither the static nor priority modal will be in this list.
|
2016-11-04 14:37:02 +00:00
|
|
|
this._modals = [
|
|
|
|
/* {
|
|
|
|
elem: React component for this dialog
|
|
|
|
onFinished: caller-supplied onFinished callback
|
|
|
|
className: CSS class for the dialog wrapper div
|
|
|
|
} */
|
|
|
|
];
|
2017-01-23 12:18:41 +00:00
|
|
|
|
2020-02-06 12:07:13 +00:00
|
|
|
this.onBackgroundClick = this.onBackgroundClick.bind(this);
|
2016-11-04 14:37:02 +00:00
|
|
|
}
|
2015-09-17 11:10:01 +00:00
|
|
|
|
2019-03-23 02:22:13 +00:00
|
|
|
hasDialogs() {
|
|
|
|
return this._priorityModal || this._staticModal || this._modals.length > 0;
|
|
|
|
}
|
|
|
|
|
2016-11-04 14:37:02 +00:00
|
|
|
getOrCreateContainer() {
|
|
|
|
let container = document.getElementById(DIALOG_CONTAINER_ID);
|
2015-09-17 11:10:01 +00:00
|
|
|
|
|
|
|
if (!container) {
|
|
|
|
container = document.createElement("div");
|
2016-11-04 14:37:02 +00:00
|
|
|
container.id = DIALOG_CONTAINER_ID;
|
2015-09-17 11:10:01 +00:00
|
|
|
document.body.appendChild(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
return container;
|
2016-11-04 14:37:02 +00:00
|
|
|
}
|
2015-09-17 11:10:01 +00:00
|
|
|
|
2019-02-28 22:55:58 +00:00
|
|
|
getOrCreateStaticContainer() {
|
|
|
|
let container = document.getElementById(STATIC_DIALOG_CONTAINER_ID);
|
|
|
|
|
|
|
|
if (!container) {
|
|
|
|
container = document.createElement("div");
|
|
|
|
container.id = STATIC_DIALOG_CONTAINER_ID;
|
|
|
|
document.body.appendChild(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
2018-05-22 14:47:05 +00:00
|
|
|
createTrackedDialog(analyticsAction, analyticsInfo, ...rest) {
|
2017-07-27 16:19:18 +00:00
|
|
|
Analytics.trackEvent('Modal', analyticsAction, analyticsInfo);
|
2018-05-22 14:47:05 +00:00
|
|
|
return this.createDialog(...rest);
|
2017-07-27 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 20:34:19 +00:00
|
|
|
appendTrackedDialog(analyticsAction, analyticsInfo, ...rest) {
|
|
|
|
Analytics.trackEvent('Modal', analyticsAction, analyticsInfo);
|
|
|
|
return this.appendDialog(...rest);
|
|
|
|
}
|
|
|
|
|
2018-05-22 14:47:05 +00:00
|
|
|
createDialog(Element, ...rest) {
|
2019-04-03 15:27:45 +00:00
|
|
|
return this.createDialogAsync(Promise.resolve(Element), ...rest);
|
2016-11-04 14:37:02 +00:00
|
|
|
}
|
2015-09-17 11:10:01 +00:00
|
|
|
|
2019-06-28 20:34:19 +00:00
|
|
|
appendDialog(Element, ...rest) {
|
|
|
|
return this.appendDialogAsync(Promise.resolve(Element), ...rest);
|
|
|
|
}
|
|
|
|
|
2018-05-22 14:47:05 +00:00
|
|
|
createTrackedDialogAsync(analyticsAction, analyticsInfo, ...rest) {
|
2017-08-15 14:52:38 +00:00
|
|
|
Analytics.trackEvent('Modal', analyticsAction, analyticsInfo);
|
2018-05-22 14:47:05 +00:00
|
|
|
return this.createDialogAsync(...rest);
|
2017-07-27 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 20:34:19 +00:00
|
|
|
appendTrackedDialogAsync(analyticsAction, analyticsInfo, ...rest) {
|
|
|
|
Analytics.trackEvent('Modal', analyticsAction, analyticsInfo);
|
|
|
|
return this.appendDialogAsync(...rest);
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:10:06 +00:00
|
|
|
_buildModal(prom, props, className, options) {
|
2019-06-28 20:34:19 +00:00
|
|
|
const modal = {};
|
|
|
|
|
|
|
|
// never call this from onFinished() otherwise it will loop
|
2019-08-09 15:47:22 +00:00
|
|
|
const [closeDialog, onFinishedProm] = this._getCloseFn(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.
|
|
|
|
const modalCount = this._counter++;
|
|
|
|
|
|
|
|
// FIXME: If a dialog uses getDefaultProps it clobbers the onFinished
|
|
|
|
// property set here so you can't close the dialog from a button click!
|
|
|
|
modal.elem = (
|
|
|
|
<AsyncWrapper key={modalCount} prom={prom} {...props}
|
|
|
|
onFinished={closeDialog} />
|
|
|
|
);
|
|
|
|
modal.onFinished = props ? props.onFinished : null;
|
|
|
|
modal.className = className;
|
2020-02-06 12:10:06 +00:00
|
|
|
modal.onBeforeClose = options.onBeforeClose;
|
|
|
|
modal.beforeClosePromise = null;
|
2020-02-06 12:07:13 +00:00
|
|
|
modal.close = closeDialog;
|
2020-02-06 12:10:06 +00:00
|
|
|
modal.closeReason = null;
|
2019-06-28 20:34:19 +00:00
|
|
|
|
2019-08-09 15:47:22 +00:00
|
|
|
return {modal, closeDialog, onFinishedProm};
|
2019-06-28 20:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_getCloseFn(modal, props) {
|
2019-11-12 11:45:28 +00:00
|
|
|
const deferred = defer();
|
2020-02-06 12:10:06 +00:00
|
|
|
return [async (...args) => {
|
|
|
|
if (modal.beforeClosePromise) {
|
|
|
|
await modal.beforeClosePromise;
|
|
|
|
} else if (modal.onBeforeClose) {
|
|
|
|
modal.beforeClosePromise = modal.onBeforeClose(modal.closeReason);
|
|
|
|
const shouldClose = await modal.beforeClosePromise;
|
|
|
|
modal.beforeClosePromise = null;
|
|
|
|
if (!shouldClose) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 15:47:22 +00:00
|
|
|
deferred.resolve(args);
|
2019-06-28 20:34:19 +00:00
|
|
|
if (props && props.onFinished) props.onFinished.apply(null, args);
|
|
|
|
const i = this._modals.indexOf(modal);
|
|
|
|
if (i >= 0) {
|
|
|
|
this._modals.splice(i, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._priorityModal === modal) {
|
|
|
|
this._priorityModal = null;
|
|
|
|
|
|
|
|
// XXX: This is destructive
|
|
|
|
this._modals = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._staticModal === modal) {
|
|
|
|
this._staticModal = null;
|
|
|
|
|
|
|
|
// XXX: This is destructive
|
|
|
|
this._modals = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
this._reRender();
|
2019-08-09 15:47:22 +00:00
|
|
|
}, deferred.promise];
|
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);
|
|
|
|
* }
|
|
|
|
*
|
2018-11-21 16:56:44 +00:00
|
|
|
* @param {Promise} prom a promise which resolves with a React component
|
|
|
|
* which will be displayed as the modal view.
|
2017-01-16 17:01:26 +00:00
|
|
|
*
|
|
|
|
* @param {Object} props properties to pass to the displayed
|
|
|
|
* component. (We will also pass an 'onFinished' property.)
|
|
|
|
*
|
|
|
|
* @param {String} className CSS class to apply to the modal wrapper
|
2018-05-22 14:47:05 +00:00
|
|
|
*
|
|
|
|
* @param {boolean} isPriorityModal if true, this modal will be displayed regardless
|
|
|
|
* of other modals that are currently in the stack.
|
|
|
|
* Also, when closed, all modals will be removed
|
|
|
|
* from the stack.
|
2019-02-28 22:55:58 +00:00
|
|
|
* @param {boolean} isStaticModal if true, this modal will be displayed under other
|
|
|
|
* 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.
|
2020-02-06 14:29:35 +00:00
|
|
|
* @param {Object} options? extra options for the dialog
|
|
|
|
* @param {onBeforeClose} options.onBeforeClose a callback to decide whether to close the dialog
|
2019-04-05 09:18:41 +00:00
|
|
|
* @returns {object} Object with 'close' parameter being a function that will close the dialog
|
2017-01-16 17:01:26 +00:00
|
|
|
*/
|
2020-02-06 12:10:06 +00:00
|
|
|
createDialogAsync(prom, props, className, isPriorityModal, isStaticModal, options = {}) {
|
|
|
|
const {modal, closeDialog, onFinishedProm} = this._buildModal(prom, props, className, options);
|
2018-05-22 14:47:05 +00:00
|
|
|
if (isPriorityModal) {
|
|
|
|
// XXX: This is destructive
|
|
|
|
this._priorityModal = modal;
|
2019-02-28 22:55:58 +00:00
|
|
|
} else if (isStaticModal) {
|
|
|
|
// This is intentionally destructive
|
|
|
|
this._staticModal = modal;
|
2018-05-22 14:47:05 +00:00
|
|
|
} else {
|
|
|
|
this._modals.unshift(modal);
|
|
|
|
}
|
2016-11-04 14:37:02 +00:00
|
|
|
|
|
|
|
this._reRender();
|
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
|
|
|
}
|
|
|
|
|
2019-06-28 20:34:19 +00:00
|
|
|
appendDialogAsync(prom, props, className) {
|
2020-02-06 12:10:06 +00:00
|
|
|
const {modal, closeDialog, onFinishedProm} = this._buildModal(prom, props, className, {});
|
2019-06-28 20:34:19 +00:00
|
|
|
|
|
|
|
this._modals.push(modal);
|
|
|
|
this._reRender();
|
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
|
|
|
}
|
|
|
|
|
2020-02-06 12:07:13 +00:00
|
|
|
onBackgroundClick() {
|
|
|
|
const modal = this._getCurrentModal();
|
|
|
|
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();
|
2020-02-06 12:10:06 +00:00
|
|
|
modal.closeReason = null;
|
2020-02-06 12:07:13 +00:00
|
|
|
}
|
2016-11-04 14:37:02 +00:00
|
|
|
|
2020-02-06 12:07:13 +00:00
|
|
|
_getCurrentModal() {
|
|
|
|
return this._priorityModal ? this._priorityModal : (this._modals[0] || this._staticModal);
|
2016-11-04 14:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_reRender() {
|
2019-02-28 22:55:58 +00:00
|
|
|
if (this._modals.length === 0 && !this._priorityModal && !this._staticModal) {
|
2018-02-08 21:51:07 +00:00
|
|
|
// If there is no modal to render, make all of Riot available
|
|
|
|
// to screen reader users again
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'aria_unhide_main_app',
|
|
|
|
});
|
2016-11-04 14:37:02 +00:00
|
|
|
ReactDOM.unmountComponentAtNode(this.getOrCreateContainer());
|
2019-02-28 22:55:58 +00:00
|
|
|
ReactDOM.unmountComponentAtNode(this.getOrCreateStaticContainer());
|
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
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'aria_hide_main_app',
|
|
|
|
});
|
2017-12-20 09:09:26 +00:00
|
|
|
|
2019-02-28 22:55:58 +00:00
|
|
|
if (this._staticModal) {
|
|
|
|
const classes = "mx_Dialog_wrapper mx_Dialog_staticWrapper "
|
|
|
|
+ (this._staticModal.className ? this._staticModal.className : '');
|
|
|
|
|
|
|
|
const staticDialog = (
|
|
|
|
<div className={classes}>
|
|
|
|
<div className="mx_Dialog">
|
|
|
|
{ this._staticModal.elem }
|
|
|
|
</div>
|
2020-02-06 12:07:13 +00:00
|
|
|
<div className="mx_Dialog_background mx_Dialog_staticBackground" onClick={this.onBackgroundClick}></div>
|
2019-02-28 22:55:58 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
ReactDOM.render(staticDialog, this.getOrCreateStaticContainer());
|
|
|
|
} else {
|
|
|
|
// This is safe to call repeatedly if we happen to do that
|
|
|
|
ReactDOM.unmountComponentAtNode(this.getOrCreateStaticContainer());
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:07:13 +00:00
|
|
|
const modal = this._getCurrentModal();
|
|
|
|
if (modal !== this._staticModal) {
|
2019-02-28 22:55:58 +00:00
|
|
|
const classes = "mx_Dialog_wrapper "
|
|
|
|
+ (this._staticModal ? "mx_Dialog_wrapperWithStaticUnder " : '')
|
|
|
|
+ (modal.className ? modal.className : '');
|
|
|
|
|
|
|
|
const dialog = (
|
|
|
|
<div className={classes}>
|
|
|
|
<div className="mx_Dialog">
|
|
|
|
{modal.elem}
|
|
|
|
</div>
|
2020-02-06 12:07:13 +00:00
|
|
|
<div className="mx_Dialog_background" onClick={this.onBackgroundClick}></div>
|
2017-12-05 07:50:40 +00:00
|
|
|
</div>
|
2019-02-28 22:55:58 +00:00
|
|
|
);
|
2015-09-17 11:10:01 +00:00
|
|
|
|
2019-02-28 22:55:58 +00:00
|
|
|
ReactDOM.render(dialog, this.getOrCreateContainer());
|
|
|
|
} else {
|
|
|
|
// This is safe to call repeatedly if we happen to do that
|
|
|
|
ReactDOM.unmountComponentAtNode(this.getOrCreateContainer());
|
|
|
|
}
|
2016-11-04 14:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-17 11:10:01 +00:00
|
|
|
|
2017-06-14 08:31:16 +00:00
|
|
|
if (!global.singletonModalManager) {
|
|
|
|
global.singletonModalManager = new ModalManager();
|
|
|
|
}
|
|
|
|
export default global.singletonModalManager;
|