2015-09-17 19:10:01 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-09-17 19:10:01 +08: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
const React = require('react');
|
|
|
|
const ReactDOM = require('react-dom');
|
2017-12-26 09:03:18 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2017-05-30 00:39:03 +08:00
|
|
|
import Analytics from './Analytics';
|
2017-01-17 01:01:26 +08:00
|
|
|
import sdk from './index';
|
2018-02-09 05:51:07 +08:00
|
|
|
import dis from './dispatcher';
|
2018-11-22 00:56:44 +08:00
|
|
|
import { _t } from './languageHandler';
|
2017-01-17 01:01:26 +08:00
|
|
|
|
2016-11-04 22:37:02 +08:00
|
|
|
const DIALOG_CONTAINER_ID = "mx_Dialog_Container";
|
2019-03-01 06:55:58 +08:00
|
|
|
const STATIC_DIALOG_CONTAINER_ID = "mx_Dialog_StaticContainer";
|
2016-11-04 22:37:02 +08:00
|
|
|
|
2017-01-17 01:01:26 +08:00
|
|
|
/**
|
|
|
|
* Wrap an asynchronous loader function with a react component which shows a
|
|
|
|
* spinner until the real component loads.
|
|
|
|
*/
|
|
|
|
const AsyncWrapper = React.createClass({
|
|
|
|
propTypes: {
|
2018-11-22 00:56:44 +08:00
|
|
|
/** A promise which resolves with the real component
|
2017-01-17 01:01:26 +08:00
|
|
|
*/
|
2018-11-22 00:56:44 +08:00
|
|
|
prom: PropTypes.object.isRequired,
|
2017-01-17 01:01:26 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
component: null,
|
2018-11-22 00:56:44 +08:00
|
|
|
error: null,
|
2017-01-20 22:22:27 +08:00
|
|
|
};
|
2017-01-17 01:01:26 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
|
|
|
this._unmounted = false;
|
2017-02-07 00:01:25 +08:00
|
|
|
// XXX: temporary logging to try to diagnose
|
|
|
|
// https://github.com/vector-im/riot-web/issues/3148
|
|
|
|
console.log('Starting load of AsyncWrapper for modal');
|
2018-11-22 00:56:44 +08:00
|
|
|
this.props.prom.then((result) => {
|
2017-01-17 01:01:26 +08:00
|
|
|
if (this._unmounted) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-22 00:56:44 +08:00
|
|
|
// Take the 'default' member if it's there, then we support
|
|
|
|
// passing in just an import()ed module, since ES6 async import
|
|
|
|
// always returns a module *namespace*.
|
|
|
|
const component = result.default ? result.default : result;
|
|
|
|
this.setState({component});
|
|
|
|
}).catch((e) => {
|
2018-11-22 01:35:28 +08:00
|
|
|
console.warn('AsyncWrapper promise failed', e);
|
2018-11-22 00:56:44 +08:00
|
|
|
this.setState({error: e});
|
2017-01-17 01:01:26 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
this._unmounted = true;
|
|
|
|
},
|
|
|
|
|
2018-11-22 00:56:44 +08:00
|
|
|
_onWrapperCancelClick: function() {
|
|
|
|
this.props.onFinished(false);
|
|
|
|
},
|
|
|
|
|
2017-01-17 01:01:26 +08:00
|
|
|
render: function() {
|
|
|
|
if (this.state.component) {
|
|
|
|
const Component = this.state.component;
|
2019-04-05 17:18:41 +08:00
|
|
|
return <Component {...this.props} />;
|
2018-11-22 00:56:44 +08:00
|
|
|
} else if (this.state.error) {
|
|
|
|
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
|
|
|
|
const DialogButtons = sdk.getComponent('views.elements.DialogButtons');
|
|
|
|
return <BaseDialog onFinished={this.props.onFinished}
|
|
|
|
title={_t("Error")}
|
|
|
|
>
|
|
|
|
{_t("Unable to load! Check your network connectivity and try again.")}
|
|
|
|
<DialogButtons primaryButton={_t("Dismiss")}
|
|
|
|
onPrimaryButtonClick={this._onWrapperCancelClick}
|
|
|
|
hasCancel={false}
|
|
|
|
/>
|
|
|
|
</BaseDialog>;
|
2017-01-17 01:01:26 +08:00
|
|
|
} else {
|
|
|
|
// show a spinner until the component is loaded.
|
|
|
|
const Spinner = sdk.getComponent("elements.Spinner");
|
|
|
|
return <Spinner />;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2015-09-17 19:10:01 +08:00
|
|
|
|
2016-11-04 22:37:02 +08:00
|
|
|
class ModalManager {
|
|
|
|
constructor() {
|
|
|
|
this._counter = 0;
|
|
|
|
|
2018-05-22 22:47:05 +08: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-03-01 06:55:58 +08: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 22:47:05 +08:00
|
|
|
// A list of the modals we have stacked up, with the most recent at [0]
|
2019-03-01 06:55:58 +08:00
|
|
|
// Neither the static nor priority modal will be in this list.
|
2016-11-04 22:37:02 +08: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 20:18:41 +08:00
|
|
|
|
2016-11-04 22:37:02 +08:00
|
|
|
this.closeAll = this.closeAll.bind(this);
|
|
|
|
}
|
2015-09-17 19:10:01 +08:00
|
|
|
|
2019-03-23 10:22:13 +08:00
|
|
|
hasDialogs() {
|
|
|
|
return this._priorityModal || this._staticModal || this._modals.length > 0;
|
|
|
|
}
|
|
|
|
|
2016-11-04 22:37:02 +08:00
|
|
|
getOrCreateContainer() {
|
|
|
|
let container = document.getElementById(DIALOG_CONTAINER_ID);
|
2015-09-17 19:10:01 +08:00
|
|
|
|
|
|
|
if (!container) {
|
|
|
|
container = document.createElement("div");
|
2016-11-04 22:37:02 +08:00
|
|
|
container.id = DIALOG_CONTAINER_ID;
|
2015-09-17 19:10:01 +08:00
|
|
|
document.body.appendChild(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
return container;
|
2016-11-04 22:37:02 +08:00
|
|
|
}
|
2015-09-17 19:10:01 +08:00
|
|
|
|
2019-03-01 06:55:58 +08: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 22:47:05 +08:00
|
|
|
createTrackedDialog(analyticsAction, analyticsInfo, ...rest) {
|
2017-07-28 00:19:18 +08:00
|
|
|
Analytics.trackEvent('Modal', analyticsAction, analyticsInfo);
|
2018-05-22 22:47:05 +08:00
|
|
|
return this.createDialog(...rest);
|
2017-07-28 00:19:18 +08:00
|
|
|
}
|
|
|
|
|
2019-06-29 04:34:19 +08:00
|
|
|
appendTrackedDialog(analyticsAction, analyticsInfo, ...rest) {
|
|
|
|
Analytics.trackEvent('Modal', analyticsAction, analyticsInfo);
|
|
|
|
return this.appendDialog(...rest);
|
|
|
|
}
|
|
|
|
|
2018-05-22 22:47:05 +08:00
|
|
|
createDialog(Element, ...rest) {
|
2019-04-03 23:27:45 +08:00
|
|
|
return this.createDialogAsync(Promise.resolve(Element), ...rest);
|
2016-11-04 22:37:02 +08:00
|
|
|
}
|
2015-09-17 19:10:01 +08:00
|
|
|
|
2019-06-29 04:34:19 +08:00
|
|
|
appendDialog(Element, ...rest) {
|
|
|
|
return this.appendDialogAsync(Promise.resolve(Element), ...rest);
|
|
|
|
}
|
|
|
|
|
2018-05-22 22:47:05 +08:00
|
|
|
createTrackedDialogAsync(analyticsAction, analyticsInfo, ...rest) {
|
2017-08-15 22:52:38 +08:00
|
|
|
Analytics.trackEvent('Modal', analyticsAction, analyticsInfo);
|
2018-05-22 22:47:05 +08:00
|
|
|
return this.createDialogAsync(...rest);
|
2017-07-28 00:19:18 +08:00
|
|
|
}
|
|
|
|
|
2019-06-29 04:34:19 +08:00
|
|
|
appendTrackedDialogAsync(analyticsAction, analyticsInfo, ...rest) {
|
|
|
|
Analytics.trackEvent('Modal', analyticsAction, analyticsInfo);
|
|
|
|
return this.appendDialogAsync(...rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
_buildModal(prom, props, className) {
|
|
|
|
const modal = {};
|
|
|
|
|
|
|
|
// never call this from onFinished() otherwise it will loop
|
|
|
|
const closeDialog = this._getCloseFn(modal, props);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
return {modal, closeDialog};
|
|
|
|
}
|
|
|
|
|
|
|
|
_getCloseFn(modal, props) {
|
|
|
|
return (...args) => {
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-17 01:01:26 +08: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-22 00:56:44 +08:00
|
|
|
* @param {Promise} prom a promise which resolves with a React component
|
|
|
|
* which will be displayed as the modal view.
|
2017-01-17 01:01:26 +08: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 22:47:05 +08: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-03-01 06:55:58 +08: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.
|
2019-04-05 17:18:41 +08:00
|
|
|
* @returns {object} Object with 'close' parameter being a function that will close the dialog
|
2017-01-17 01:01:26 +08:00
|
|
|
*/
|
2019-03-01 06:55:58 +08:00
|
|
|
createDialogAsync(prom, props, className, isPriorityModal, isStaticModal) {
|
2019-06-29 04:34:19 +08:00
|
|
|
const {modal, closeDialog} = this._buildModal(prom, props, className);
|
2016-11-04 22:37:02 +08:00
|
|
|
|
2018-05-22 22:47:05 +08:00
|
|
|
if (isPriorityModal) {
|
|
|
|
// XXX: This is destructive
|
|
|
|
this._priorityModal = modal;
|
2019-03-01 06:55:58 +08:00
|
|
|
} else if (isStaticModal) {
|
|
|
|
// This is intentionally destructive
|
|
|
|
this._staticModal = modal;
|
2018-05-22 22:47:05 +08:00
|
|
|
} else {
|
|
|
|
this._modals.unshift(modal);
|
|
|
|
}
|
2016-11-04 22:37:02 +08:00
|
|
|
|
|
|
|
this._reRender();
|
|
|
|
return {close: closeDialog};
|
|
|
|
}
|
|
|
|
|
2019-06-29 04:34:19 +08:00
|
|
|
appendDialogAsync(prom, props, className) {
|
|
|
|
const {modal, closeDialog} = this._buildModal(prom, props, className);
|
|
|
|
|
|
|
|
this._modals.push(modal);
|
|
|
|
this._reRender();
|
|
|
|
return {close: closeDialog};
|
|
|
|
}
|
|
|
|
|
2016-11-04 22:37:02 +08:00
|
|
|
closeAll() {
|
2019-03-01 06:55:58 +08:00
|
|
|
const modalsToClose = [...this._modals, this._priorityModal];
|
2016-11-04 22:37:02 +08:00
|
|
|
this._modals = [];
|
2019-03-01 06:55:58 +08:00
|
|
|
this._priorityModal = null;
|
|
|
|
|
|
|
|
if (this._staticModal && modalsToClose.length === 0) {
|
|
|
|
modalsToClose.push(this._staticModal);
|
|
|
|
this._staticModal = null;
|
|
|
|
}
|
2016-11-04 22:37:02 +08:00
|
|
|
|
2019-03-01 06:55:58 +08:00
|
|
|
for (let i = 0; i < modalsToClose.length; i++) {
|
|
|
|
const m = modalsToClose[i];
|
|
|
|
if (m && m.onFinished) {
|
2016-11-04 22:37:02 +08:00
|
|
|
m.onFinished(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._reRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
_reRender() {
|
2019-03-01 06:55:58 +08:00
|
|
|
if (this._modals.length === 0 && !this._priorityModal && !this._staticModal) {
|
2018-02-09 05:51:07 +08: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 22:37:02 +08:00
|
|
|
ReactDOM.unmountComponentAtNode(this.getOrCreateContainer());
|
2019-03-01 06:55:58 +08:00
|
|
|
ReactDOM.unmountComponentAtNode(this.getOrCreateStaticContainer());
|
2016-11-04 22:37:02 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-09 05:51:07 +08: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 17:09:26 +08:00
|
|
|
|
2019-03-01 06:55:58 +08: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>
|
|
|
|
<div className="mx_Dialog_background mx_Dialog_staticBackground" onClick={this.closeAll}></div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
ReactDOM.render(staticDialog, this.getOrCreateStaticContainer());
|
|
|
|
} else {
|
|
|
|
// This is safe to call repeatedly if we happen to do that
|
|
|
|
ReactDOM.unmountComponentAtNode(this.getOrCreateStaticContainer());
|
|
|
|
}
|
|
|
|
|
2018-05-22 22:47:05 +08:00
|
|
|
const modal = this._priorityModal ? this._priorityModal : this._modals[0];
|
2019-03-01 06:55:58 +08:00
|
|
|
if (modal) {
|
|
|
|
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>
|
|
|
|
<div className="mx_Dialog_background" onClick={this.closeAll}></div>
|
2017-12-05 15:50:40 +08:00
|
|
|
</div>
|
2019-03-01 06:55:58 +08:00
|
|
|
);
|
2015-09-17 19:10:01 +08:00
|
|
|
|
2019-03-01 06:55:58 +08: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 22:37:02 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-17 19:10:01 +08:00
|
|
|
|
2017-06-14 16:31:16 +08:00
|
|
|
if (!global.singletonModalManager) {
|
|
|
|
global.singletonModalManager = new ModalManager();
|
|
|
|
}
|
|
|
|
export default global.singletonModalManager;
|