1
0
mirror of https://github.com/ascribe/onion.git synced 2025-01-03 18:35:09 +01:00

refactor globalnotification

This commit is contained in:
Tim Daubenschütz 2015-07-08 15:40:32 +02:00
parent 16dfe3f224
commit 5e14ea6f87
3 changed files with 70 additions and 37 deletions

View File

@ -11,6 +11,16 @@ import { mergeOptions } from '../utils/general_utils';
let GlobalNotification = React.createClass({ let GlobalNotification = React.createClass({
getInitialState() {
return mergeOptions(
{
containerWidth: 0,
type: 'success'
},
this.extractFirstElem(GlobalNotificationStore.getState().notificationQue)
);
},
componentDidMount() { componentDidMount() {
GlobalNotificationStore.listen(this.onChange); GlobalNotificationStore.listen(this.onChange);
@ -27,17 +37,8 @@ let GlobalNotification = React.createClass({
window.removeEventListener('resize', this.handleContainerResize); window.removeEventListener('resize', this.handleContainerResize);
}, },
getInititalState() {
return mergeOptions(
this.extractFirstElem(GlobalNotificationStore.getState().notificationQue),
{
containerWidth: 0
}
);
},
extractFirstElem(l) { extractFirstElem(l) {
return l.length > 0 ? l[0] : null; return l.length > 0 ? l[0] : {message: ''};
}, },
onChange(state) { onChange(state) {
@ -46,49 +47,59 @@ let GlobalNotification = React.createClass({
if(notification) { if(notification) {
this.setState(notification); this.setState(notification);
} else { } else {
this.replaceState(null); this.replaceState({
message: ''
});
} }
}, },
handleContainerResize() { handleContainerResize() {
this.setState({ this.setState({
containerWidth: this.refs.containerWrapper.getDOMNode().offsetWidth containerWidth: this.refs.notificationWrapper.getDOMNode().offsetWidth
}); });
}, },
render() { render() {
console.log(this.state); let notificationClass = 'ascribe-global-notification';
let notificationClass = 'ascribe-global-notification '; let textClass;
let message = this.state && this.state.message ? this.state.message : null; let message = this.state && this.state.message ? this.state.message : null;
if(message) { if(message) {
let colors = { notificationClass += ' ascribe-global-notification-on';
warning: '#f0ad4e', } else {
success: '#5cb85c', notificationClass += ' ascribe-global-notification-off';
info: 'rgba(2, 182, 163, 1)', }
danger: '#d9534f'
};
let text = (<div style={{color: colors[this.state.type]}}>{message ? message : null}</div>); if(this.state) {
switch(this.state.type) {
case 'warning':
textClass = 'ascribe-global-notification-warning';
break;
case 'success':
textClass = 'ascribe-global-notification-success';
break;
case 'info':
textClass = 'ascribe-global-notification-info';
break;
case 'danger':
textClass = 'ascribe-global-notification-danger';
break;
default:
console.warn('Could not find a matching type in global_notification.js');
}
}
return ( return (
<div ref="notificationWrapper">
<Row> <Row>
<Col> <Col>
<div className={notificationClass + 'ascribe-global-notification-on'}> <div className={notificationClass}>
{text} <div className={textClass}>{message ? message : null}</div>
</div> </div>
</Col> </Col>
</Row> </Row>
</div>
); );
} else {
return (
<Row>
<Col>
<div className={notificationClass + 'ascribe-global-notification-off'} />
</Col>
</Row>
);
}
} }
}); });

View File

@ -70,7 +70,13 @@ export function formatText() {
function _doesObjectListHaveDuplicates(l) { function _doesObjectListHaveDuplicates(l) {
let mergedList = []; let mergedList = [];
l = l.map((obj) => Object.keys(obj)); l = l.map((obj) => {
if(!obj) {
throw new Error('The object you are trying to merge is null instead of an empty object');
}
return Object.keys(obj);
});
// Taken from: http://stackoverflow.com/a/10865042 // Taken from: http://stackoverflow.com/a/10865042
// How to flatten an array of arrays in javascript. // How to flatten an array of arrays in javascript.
@ -102,6 +108,7 @@ export function mergeOptions(...l) {
for(let i = 1; i < l.length; i++) { for(let i = 1; i < l.length; i++) {
newObj = _mergeOptions(newObj, _mergeOptions(l[i - 1], l[i])); newObj = _mergeOptions(newObj, _mergeOptions(l[i - 1], l[i]));
} }
return newObj; return newObj;
} }

View File

@ -21,9 +21,24 @@
.ascribe-global-notification > div { .ascribe-global-notification > div {
display:table-cell; display:table-cell;
vertical-align: middle; vertical-align: middle;
color: $ascribe-color-full;
font-size: 1.25em; font-size: 1.25em;
font-family: 'Source Sans Pro'; font-family: 'Source Sans Pro';
text-align: right; text-align: right;
padding-right: 3em; padding-right: 3em;
} }
.ascribe-global-notification-warning {
color: #f0ad4e;
}
.ascribe-global-notification-danger {
color: #d9534f;
}
.ascribe-global-notification-success {
color: #5cb85c;
}
.ascribe-global-notification-info {
color: rgba(2, 182, 163, 1);
}