1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00
onion/js/components/global_notification.js

110 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-06-08 18:14:25 +02:00
'use strict';
import React from 'react';
import classNames from 'classnames';
2015-06-08 18:14:25 +02:00
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';
import GlobalNotificationStore from '../stores/global_notification_store';
import { safeMerge } from '../utils/general';
const MAX_NOTIFICATION_BUBBLE_CONTAINER_WIDTH = 768;
2015-06-08 18:14:25 +02:00
let GlobalNotification = React.createClass({
2015-07-08 15:40:32 +02:00
getInitialState() {
const notificationStore = GlobalNotificationStore.getState();
return safeMerge(
2015-07-08 15:40:32 +02:00
{
2015-07-08 17:19:06 +02:00
containerWidth: 0
2015-07-08 15:40:32 +02:00
},
notificationStore
2015-07-08 15:40:32 +02:00
);
},
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
},
onChange(state) {
this.setState(state);
2015-06-08 18:14:25 +02:00
},
handleContainerResize() {
this.setState({
2016-05-09 11:12:46 +02:00
containerWidth: this.refs.notificationWrapper.offsetWidth
});
},
renderNotification() {
const { containerWidth,
notificationsPaused,
notificationQueue: [notification],
notificationStatus } = this.state;
const notificationClasses = [];
if (this.state.containerWidth > MAX_NOTIFICATION_BUBBLE_CONTAINER_WIDTH) {
notificationClasses.push('ascribe-global-notification-bubble');
notificationClasses.push(notificationStatus === 'show' ? 'ascribe-global-notification-bubble-on'
: 'ascribe-global-notification-bubble-off');
} else {
notificationClasses.push('ascribe-global-notification');
notificationClasses.push(notificationStatus === 'show' ? 'ascribe-global-notification-on'
: 'ascribe-global-notification-off');
}
let textClass;
let message;
if (notification && !notificationsPaused) {
message = notification.message;
2015-07-08 17:06:53 +02:00
switch(notification.type) {
2015-07-08 17:19:06 +02:00
case 'success':
textClass = 'ascribe-global-notification-success';
break;
case 'danger':
textClass = 'ascribe-global-notification-danger';
break;
default:
console.warn('Could not find a matching notification type in global_notification.js');
2015-07-08 17:19:06 +02:00
}
2015-07-08 15:40:32 +02:00
}
return (
<div className={classNames(...notificationClasses)}>
<div className={textClass}>{message}</div>
</div>
);
},
2015-06-09 13:36:09 +02:00
render() {
2015-07-08 15:40:32 +02:00
return (
<div ref="notificationWrapper">
2015-06-09 13:36:09 +02:00
<Row>
<Col>
{this.renderNotification()}
2015-06-09 13:36:09 +02:00
</Col>
</Row>
2015-07-08 15:40:32 +02:00
</div>
);
2015-06-08 18:14:25 +02:00
}
});
export default GlobalNotification;