framework/js/lib/components/Alerts.js

71 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 `Alerts` component provides an area in which `Alert` components can be
* shown and dismissed.
*/
2015-04-25 20:58:39 +08:00
export default class Alerts extends Component {
constructor(...args) {
super(...args);
2015-04-25 20:58:39 +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() {
return (
<div className="alerts">
{this.components.map(component => <div className="alerts-item">{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 Alerts 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();
}
}