2018-06-20 11:50:31 +08:00
|
|
|
import Component from '../Component';
|
|
|
|
import Modal from './Modal';
|
2015-07-15 12:30:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The `ModalManager` component manages a modal dialog. Only one modal dialog
|
|
|
|
* can be shown at once; loading a new component into the ModalManager will
|
|
|
|
* overwrite the previous one.
|
|
|
|
*/
|
|
|
|
export default class ModalManager extends Component {
|
2015-10-13 14:25:56 +08:00
|
|
|
init() {
|
2015-07-22 08:24:00 +08:00
|
|
|
this.showing = false;
|
|
|
|
this.component = null;
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
view() {
|
2020-04-17 17:57:55 +08:00
|
|
|
return <div className="ModalManager modal fade">{this.component && this.component.render()}</div>;
|
2015-07-15 12:30:11 +08:00
|
|
|
}
|
|
|
|
|
2015-07-22 08:24:00 +08:00
|
|
|
config(isInitialized, context) {
|
2015-07-15 12:30:11 +08:00
|
|
|
if (isInitialized) return;
|
|
|
|
|
2016-04-08 12:08:16 +08:00
|
|
|
// Since this component is 'above' the content of the page (that is, it is a
|
|
|
|
// part of the global UI that persists between routes), we will flag the DOM
|
|
|
|
// to be retained across route changes.
|
2015-07-22 08:24:00 +08:00
|
|
|
context.retain = true;
|
|
|
|
|
2020-04-17 17:57:55 +08:00
|
|
|
this.$().on('hidden.bs.modal', this.clear.bind(this)).on('shown.bs.modal', this.onready.bind(this));
|
2015-07-15 12:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a modal dialog.
|
|
|
|
*
|
|
|
|
* @param {Modal} component
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
show(component) {
|
|
|
|
if (!(component instanceof Modal)) {
|
|
|
|
throw new Error('The ModalManager component can only show Modal components');
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeout(this.hideTimeout);
|
|
|
|
|
2015-07-22 08:24:00 +08:00
|
|
|
this.showing = true;
|
2015-07-15 12:30:11 +08:00
|
|
|
this.component = component;
|
|
|
|
|
2018-08-14 05:03:39 +08:00
|
|
|
if (app.current) app.current.retain = true;
|
2016-03-11 10:48:16 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
m.redraw(true);
|
|
|
|
|
2019-10-25 01:38:11 +08:00
|
|
|
const dismissible = !!this.component.isDismissible();
|
2020-04-17 17:57:55 +08:00
|
|
|
this.$()
|
|
|
|
.modal({
|
|
|
|
backdrop: dismissible || 'static',
|
|
|
|
keyboard: dismissible,
|
|
|
|
})
|
|
|
|
.modal('show');
|
2015-07-15 12:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the modal dialog.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
close() {
|
2015-07-22 08:24:00 +08:00
|
|
|
if (!this.showing) return;
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
// Don't hide the modal immediately, because if the consumer happens to call
|
|
|
|
// the `show` method straight after to show another modal dialog, it will
|
|
|
|
// cause Bootstrap's modal JS to misbehave. Instead we will wait for a tiny
|
|
|
|
// bit to give the `show` method the opportunity to prevent this from going
|
|
|
|
// ahead.
|
2015-07-22 08:24:00 +08:00
|
|
|
this.hideTimeout = setTimeout(() => {
|
|
|
|
this.$().modal('hide');
|
|
|
|
this.showing = false;
|
|
|
|
});
|
2015-07-15 12:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear content from the modal area.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
clear() {
|
2015-11-05 13:47:00 +08:00
|
|
|
if (this.component) {
|
|
|
|
this.component.onhide();
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
this.component = null;
|
|
|
|
|
2016-03-11 10:48:16 +08:00
|
|
|
app.current.retain = false;
|
|
|
|
|
2015-08-31 10:34:51 +08:00
|
|
|
m.lazyRedraw();
|
2015-07-15 12:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the modal dialog is ready to be used, tell it!
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
onready() {
|
|
|
|
if (this.component && this.component.onready) {
|
|
|
|
this.component.onready(this.$());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|