1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-29 00:58:03 +02:00
onion/js/components/global_notification.js

95 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-06-08 18:14:25 +02:00
'use strict';
import React from 'react';
import GlobalNotificationStore from '../stores/global_notification_store';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';
import { mergeOptions } from '../utils/general_utils';
2015-06-08 18:14:25 +02:00
let GlobalNotification = React.createClass({
componentDidMount() {
GlobalNotificationStore.listen(this.onChange);
// init container width
this.handleContainerResize();
// we're using an event listener on window here,
// as it was not possible to listen to the resize events of a dom node
window.addEventListener('resize', this.handleContainerResize);
2015-06-08 18:14:25 +02:00
},
componentWillUnmount() {
GlobalNotificationStore.unlisten(this.onChange);
window.removeEventListener('resize', this.handleContainerResize);
2015-06-08 18:14:25 +02:00
},
getInititalState() {
return mergeOptions(
this.extractFirstElem(GlobalNotificationStore.getState().notificationQue),
{
containerWidth: 0
}
);
2015-06-08 18:14:25 +02:00
},
extractFirstElem(l) {
return l.length > 0 ? l[0] : null;
2015-06-08 18:14:25 +02:00
},
onChange(state) {
let notification = this.extractFirstElem(state.notificationQue);
if(notification) {
this.setState(notification);
} else {
this.replaceState(null);
2015-06-08 18:14:25 +02:00
}
},
handleContainerResize() {
this.setState({
containerWidth: this.refs.containerWrapper.getDOMNode().offsetWidth
});
},
2015-06-08 18:14:25 +02:00
render() {
console.log(this.state);
2015-06-09 13:36:09 +02:00
let notificationClass = 'ascribe-global-notification ';
let message = this.state && this.state.message ? this.state.message : null;
2015-06-09 13:36:09 +02:00
if(message) {
let colors = {
warning: '#f0ad4e',
success: '#5cb85c',
info: 'rgba(2, 182, 163, 1)',
danger: '#d9534f'
};
let text = (<div style={{color: colors[this.state.type]}}>{message ? message : null}</div>);
return (
<Row>
<Col>
<div className={notificationClass + 'ascribe-global-notification-on'}>
{text}
</div>
</Col>
</Row>
);
} else {
return (
<Row>
<Col>
<div className={notificationClass + 'ascribe-global-notification-off'} />
</Col>
</Row>
);
}
2015-06-08 18:14:25 +02:00
}
});
export default GlobalNotification;