1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 17:45:10 +01:00
onion/js/components/global_notification.js

123 lines
3.4 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({
2015-07-08 15:40:32 +02:00
getInitialState() {
return mergeOptions(
{
2015-07-08 17:19:06 +02:00
containerWidth: 0
2015-07-08 15:40:32 +02:00
},
this.extractFirstElem(GlobalNotificationStore.getState().notificationQue)
);
},
2015-06-08 18:14:25 +02:00
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
},
extractFirstElem(l) {
2015-07-08 17:06:53 +02:00
if(l.length > 0) {
return {
show: true,
message: l[0]
};
} else {
return {
show: false,
message: ''
};
}
2015-06-08 18:14:25 +02:00
},
onChange(state) {
let notification = this.extractFirstElem(state.notificationQue);
2015-07-08 17:06:53 +02:00
if(notification.show) {
this.setState(notification);
} else {
2015-07-08 17:06:53 +02:00
this.setState({
show: false
2015-07-08 15:40:32 +02:00
});
2015-06-08 18:14:25 +02:00
}
},
handleContainerResize() {
this.setState({
2015-07-08 15:40:32 +02:00
containerWidth: this.refs.notificationWrapper.getDOMNode().offsetWidth
});
},
2015-06-08 18:14:25 +02:00
render() {
2015-07-08 15:40:32 +02:00
let notificationClass = 'ascribe-global-notification';
let textClass;
2015-07-08 17:06:53 +02:00
if(this.state.containerWidth > 768) {
notificationClass = 'ascribe-global-notification-bubble';
if(this.state.show) {
notificationClass += ' ascribe-global-notification-bubble-on';
} else {
notificationClass += ' ascribe-global-notification-bubble-off';
}
2015-07-08 15:40:32 +02:00
} else {
2015-07-08 17:06:53 +02:00
notificationClass = 'ascribe-global-notification';
2015-06-09 13:36:09 +02:00
2015-07-08 17:06:53 +02:00
if(this.state.show) {
notificationClass += ' ascribe-global-notification-on';
} else {
notificationClass += ' ascribe-global-notification-off';
2015-07-08 15:40:32 +02:00
}
2015-07-08 17:06:53 +02:00
}
2015-07-08 17:19:06 +02:00
if(this.state.message) {
switch(this.state.message.type) {
case 'success':
textClass = 'ascribe-global-notification-success';
break;
case 'danger':
textClass = 'ascribe-global-notification-danger';
break;
default:
console.warn('Could not find a matching type in global_notification.js');
}
2015-07-08 15:40:32 +02:00
}
2015-07-08 17:19:06 +02:00
2015-06-09 13:36:09 +02:00
2015-07-08 15:40:32 +02:00
return (
<div ref="notificationWrapper">
2015-06-09 13:36:09 +02:00
<Row>
<Col>
2015-07-08 15:40:32 +02:00
<div className={notificationClass}>
2015-07-08 17:06:53 +02:00
<div className={textClass}>{this.state.message.message}</div>
2015-06-09 13:36:09 +02:00
</div>
</Col>
</Row>
2015-07-08 15:40:32 +02:00
</div>
);
2015-06-08 18:14:25 +02:00
}
});
export default GlobalNotification;