2015-09-17 13:10:01 +02:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-09-17 13:10:01 +02: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-11 18:56:17 +02:00
|
|
|
const React = require('react');
|
|
|
|
const ReactDOM = require('react-dom');
|
2017-12-26 02:03:18 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2017-05-29 18:39:03 +02:00
|
|
|
import Analytics from './Analytics';
|
2017-01-16 18:01:26 +01:00
|
|
|
import sdk from './index';
|
2018-02-08 22:51:07 +01:00
|
|
|
import dis from './dispatcher';
|
2017-01-16 18:01:26 +01:00
|
|
|
|
2016-11-04 15:37:02 +01:00
|
|
|
const DIALOG_CONTAINER_ID = "mx_Dialog_Container";
|
|
|
|
|
2017-01-16 18:01:26 +01:00
|
|
|
/**
|
|
|
|
* Wrap an asynchronous loader function with a react component which shows a
|
|
|
|
* spinner until the real component loads.
|
|
|
|
*/
|
|
|
|
const AsyncWrapper = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
/** A function which takes a 'callback' argument which it will call
|
|
|
|
* with the real component once it loads.
|
|
|
|
*/
|
2017-12-26 02:03:18 +01:00
|
|
|
loader: PropTypes.func.isRequired,
|
2017-01-16 18:01:26 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
component: null,
|
2017-01-20 15:22:27 +01:00
|
|
|
};
|
2017-01-16 18:01:26 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
|
|
|
this._unmounted = false;
|
2017-02-06 17:01:25 +01: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');
|
2017-01-16 18:01:26 +01:00
|
|
|
this.props.loader((e) => {
|
2017-02-06 17:01:25 +01:00
|
|
|
// XXX: temporary logging to try to diagnose
|
|
|
|
// https://github.com/vector-im/riot-web/issues/3148
|
|
|
|
console.log('AsyncWrapper load completed with '+e.displayName);
|
2017-01-16 18:01:26 +01:00
|
|
|
if (this._unmounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({component: e});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
this._unmounted = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
const {loader, ...otherProps} = this.props;
|
|
|
|
if (this.state.component) {
|
|
|
|
const Component = this.state.component;
|
|
|
|
return <Component {...otherProps} />;
|
|
|
|
} else {
|
|
|
|
// show a spinner until the component is loaded.
|
|
|
|
const Spinner = sdk.getComponent("elements.Spinner");
|
|
|
|
return <Spinner />;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2015-09-17 13:10:01 +02:00
|
|
|
|
2016-11-04 15:37:02 +01:00
|
|
|
class ModalManager {
|
|
|
|
constructor() {
|
|
|
|
this._counter = 0;
|
|
|
|
|
2018-05-22 16:47:05 +02: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;
|
|
|
|
// A list of the modals we have stacked up, with the most recent at [0]
|
2016-11-04 15:37:02 +01: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 13:18:41 +01:00
|
|
|
|
2016-11-04 15:37:02 +01:00
|
|
|
this.closeAll = this.closeAll.bind(this);
|
|
|
|
}
|
2015-09-17 13:10:01 +02:00
|
|
|
|
2016-11-04 15:37:02 +01:00
|
|
|
getOrCreateContainer() {
|
|
|
|
let container = document.getElementById(DIALOG_CONTAINER_ID);
|
2015-09-17 13:10:01 +02:00
|
|
|
|
|
|
|
if (!container) {
|
|
|
|
container = document.createElement("div");
|
2016-11-04 15:37:02 +01:00
|
|
|
container.id = DIALOG_CONTAINER_ID;
|
2015-09-17 13:10:01 +02:00
|
|
|
document.body.appendChild(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
return container;
|
2016-11-04 15:37:02 +01:00
|
|
|
}
|
2015-09-17 13:10:01 +02:00
|
|
|
|
2018-05-22 16:47:05 +02:00
|
|
|
createTrackedDialog(analyticsAction, analyticsInfo, ...rest) {
|
2017-07-27 18:19:18 +02:00
|
|
|
Analytics.trackEvent('Modal', analyticsAction, analyticsInfo);
|
2018-05-22 16:47:05 +02:00
|
|
|
return this.createDialog(...rest);
|
2017-07-27 18:19:18 +02:00
|
|
|
}
|
|
|
|
|
2018-05-22 16:47:05 +02:00
|
|
|
createDialog(Element, ...rest) {
|
|
|
|
return this.createDialogAsync((cb) => {cb(Element);}, ...rest);
|
2016-11-04 15:37:02 +01:00
|
|
|
}
|
2015-09-17 13:10:01 +02:00
|
|
|
|
2018-05-22 16:47:05 +02:00
|
|
|
createTrackedDialogAsync(analyticsAction, analyticsInfo, ...rest) {
|
2017-08-15 16:52:38 +02:00
|
|
|
Analytics.trackEvent('Modal', analyticsAction, analyticsInfo);
|
2018-05-22 16:47:05 +02:00
|
|
|
return this.createDialogAsync(...rest);
|
2017-07-27 18:19:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-16 18:01:26 +01: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);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @param {Function} loader a function which takes a 'callback' argument,
|
|
|
|
* which it should call with a React component which will be displayed as
|
|
|
|
* the modal view.
|
|
|
|
*
|
|
|
|
* @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 16:47:05 +02: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.
|
2017-01-16 18:01:26 +01:00
|
|
|
*/
|
2018-05-22 16:47:05 +02:00
|
|
|
createDialogAsync(loader, props, className, isPriorityModal) {
|
2017-10-11 18:56:17 +02:00
|
|
|
const self = this;
|
2016-11-04 15:37:02 +01:00
|
|
|
const modal = {};
|
|
|
|
|
|
|
|
// never call this from onFinished() otherwise it will loop
|
|
|
|
//
|
|
|
|
// nb explicit function() rather than arrow function, to get `arguments`
|
2017-10-11 18:56:17 +02:00
|
|
|
const closeDialog = function() {
|
2015-09-17 13:10:01 +02:00
|
|
|
if (props && props.onFinished) props.onFinished.apply(null, arguments);
|
2017-10-11 18:56:17 +02:00
|
|
|
const i = self._modals.indexOf(modal);
|
2016-11-04 15:37:02 +01:00
|
|
|
if (i >= 0) {
|
|
|
|
self._modals.splice(i, 1);
|
|
|
|
}
|
2018-05-22 16:47:05 +02:00
|
|
|
|
|
|
|
if (self._priorityModal === modal) {
|
|
|
|
self._priorityModal = null;
|
|
|
|
|
|
|
|
// XXX: This is destructive
|
|
|
|
self._modals = [];
|
|
|
|
}
|
|
|
|
|
2016-11-04 15:37:02 +01:00
|
|
|
self._reRender();
|
2015-09-17 13:10:01 +02:00
|
|
|
};
|
|
|
|
|
2017-01-23 13:18:41 +01:00
|
|
|
// don't attempt to reuse the same AsyncWrapper for different dialogs,
|
|
|
|
// otherwise we'll get confused.
|
2016-11-04 15:37:02 +01:00
|
|
|
const modalCount = this._counter++;
|
2017-01-23 13:18:41 +01:00
|
|
|
|
2015-09-17 13:10:01 +02:00
|
|
|
// FIXME: If a dialog uses getDefaultProps it clobbers the onFinished
|
|
|
|
// property set here so you can't close the dialog from a button click!
|
2016-11-04 15:37:02 +01:00
|
|
|
modal.elem = (
|
|
|
|
<AsyncWrapper key={modalCount} loader={loader} {...props}
|
2017-10-11 18:56:17 +02:00
|
|
|
onFinished={closeDialog} />
|
2016-11-04 15:37:02 +01:00
|
|
|
);
|
|
|
|
modal.onFinished = props ? props.onFinished : null;
|
|
|
|
modal.className = className;
|
|
|
|
|
2018-05-22 16:47:05 +02:00
|
|
|
if (isPriorityModal) {
|
|
|
|
// XXX: This is destructive
|
|
|
|
this._priorityModal = modal;
|
|
|
|
} else {
|
|
|
|
this._modals.unshift(modal);
|
|
|
|
}
|
2016-11-04 15:37:02 +01:00
|
|
|
|
|
|
|
this._reRender();
|
|
|
|
return {close: closeDialog};
|
|
|
|
}
|
|
|
|
|
|
|
|
closeAll() {
|
|
|
|
const modals = this._modals;
|
|
|
|
this._modals = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < modals.length; i++) {
|
|
|
|
const m = modals[i];
|
|
|
|
if (m.onFinished) {
|
|
|
|
m.onFinished(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._reRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
_reRender() {
|
2018-05-22 16:47:05 +02:00
|
|
|
if (this._modals.length == 0 && !this._priorityModal) {
|
2018-02-08 22:51:07 +01: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 15:37:02 +01:00
|
|
|
ReactDOM.unmountComponentAtNode(this.getOrCreateContainer());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-08 22:51:07 +01: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 10:09:26 +01:00
|
|
|
|
2018-05-22 16:47:05 +02:00
|
|
|
const modal = this._priorityModal ? this._priorityModal : this._modals[0];
|
2017-10-11 18:56:17 +02:00
|
|
|
const dialog = (
|
|
|
|
<div className={"mx_Dialog_wrapper " + (modal.className ? modal.className : '')}>
|
2017-12-05 08:50:40 +01:00
|
|
|
<div className="mx_Dialog">
|
2017-10-11 18:56:17 +02:00
|
|
|
{ modal.elem }
|
2017-12-05 08:50:40 +01:00
|
|
|
</div>
|
2017-10-11 18:56:17 +02:00
|
|
|
<div className="mx_Dialog_background" onClick={this.closeAll}></div>
|
2015-09-17 13:10:01 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2015-11-10 00:13:43 +01:00
|
|
|
ReactDOM.render(dialog, this.getOrCreateContainer());
|
2016-11-04 15:37:02 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-17 13:10:01 +02:00
|
|
|
|
2017-06-14 10:31:16 +02:00
|
|
|
if (!global.singletonModalManager) {
|
|
|
|
global.singletonModalManager = new ModalManager();
|
|
|
|
}
|
|
|
|
export default global.singletonModalManager;
|