2015-07-15 12:30:11 +08:00
|
|
|
import Component from 'flarum/Component';
|
|
|
|
import Alert from 'flarum/components/Alert';
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
2015-07-17 13:17:49 +08:00
|
|
|
* The `AlertManager` component provides an area in which `Alert` components can
|
|
|
|
* be shown and dismissed.
|
2015-07-15 12:30:11 +08:00
|
|
|
*/
|
2015-07-17 13:17:49 +08:00
|
|
|
export default class AlertManager extends Component {
|
2015-10-13 14:25:56 +08:00
|
|
|
init() {
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* An array of Alert components which are currently showing.
|
|
|
|
*
|
|
|
|
* @type {Alert[]}
|
|
|
|
* @protected
|
|
|
|
*/
|
2015-04-25 20:58:39 +08:00
|
|
|
this.components = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
view() {
|
2015-07-15 12:30:11 +08:00
|
|
|
return (
|
2015-07-17 13:17:49 +08:00
|
|
|
<div className="AlertManager">
|
|
|
|
{this.components.map(component => <div className="AlertManager-alert">{component}</div>)}
|
2015-07-15 12:30:11 +08:00
|
|
|
</div>
|
|
|
|
);
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Show an Alert in the alerts area.
|
|
|
|
*
|
|
|
|
* @param {Alert} component
|
|
|
|
* @public
|
|
|
|
*/
|
2015-04-25 20:58:39 +08:00
|
|
|
show(component) {
|
2015-07-15 12:30:11 +08:00
|
|
|
if (!(component instanceof Alert)) {
|
2015-07-17 13:17:49 +08:00
|
|
|
throw new Error('The AlertManager component can only show Alert components');
|
2015-07-15 12:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
component.props.ondismiss = this.dismiss.bind(this, component);
|
|
|
|
|
2015-04-25 20:58:39 +08:00
|
|
|
this.components.push(component);
|
|
|
|
m.redraw();
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Dismiss an alert.
|
|
|
|
*
|
|
|
|
* @param {Alert} component
|
|
|
|
* @public
|
|
|
|
*/
|
2015-04-25 20:58:39 +08:00
|
|
|
dismiss(component) {
|
2015-07-15 12:30:11 +08:00
|
|
|
const index = this.components.indexOf(component);
|
|
|
|
|
2015-04-25 20:58:39 +08:00
|
|
|
if (index !== -1) {
|
|
|
|
this.components.splice(index, 1);
|
2015-05-18 16:43:16 +08:00
|
|
|
m.redraw();
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Clear all alerts.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
2015-04-25 20:58:39 +08:00
|
|
|
clear() {
|
|
|
|
this.components = [];
|
|
|
|
m.redraw();
|
|
|
|
}
|
|
|
|
}
|