framework/js/lib/components/AlertManager.js

69 lines
1.3 KiB
JavaScript
Raw Normal View History

import Component from 'flarum/Component';
import Alert from 'flarum/components/Alert';
2015-04-25 20:58:39 +08:00
/**
* The `AlertManager` component provides an area in which `Alert` components can
* be shown and dismissed.
*/
export default class AlertManager extends Component {
init() {
/**
* An array of Alert components which are currently showing.
*
* @type {Alert[]}
* @protected
*/
2015-04-25 20:58:39 +08:00
this.components = [];
}
view() {
return (
<div className="AlertManager">
{this.components.map(component => <div className="AlertManager-alert">{component}</div>)}
</div>
);
2015-04-25 20:58:39 +08:00
}
/**
* Show an Alert in the alerts area.
*
* @param {Alert} component
* @public
*/
2015-04-25 20:58:39 +08:00
show(component) {
if (!(component instanceof Alert)) {
throw new Error('The AlertManager component can only show Alert components');
}
component.props.ondismiss = this.dismiss.bind(this, component);
2015-04-25 20:58:39 +08:00
this.components.push(component);
m.redraw();
}
/**
* Dismiss an alert.
*
* @param {Alert} component
* @public
*/
2015-04-25 20:58:39 +08:00
dismiss(component) {
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
}
}
/**
* Clear all alerts.
*
* @public
*/
2015-04-25 20:58:39 +08:00
clear() {
this.components = [];
m.redraw();
}
}