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
|
|
|
/**
|
|
|
|
* 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 {
|
2015-07-15 12:30:11 +08:00
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
2015-04-25 20:58:39 +08:00
|
|
|
|
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 (
|
|
|
|
<div className="alerts">
|
|
|
|
{this.components.map(component => <div className="alerts-item">{component}</div>)}
|
|
|
|
</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)) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|