-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathNotificationSystem.jsx
More file actions
227 lines (186 loc) · 6.34 KB
/
NotificationSystem.jsx
File metadata and controls
227 lines (186 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var React = require('react');
var merge = require('object-assign');
var NotificationContainer = require('./NotificationContainer');
var Constants = require('./constants');
var Styles = require('./styles');
var NotificationSystem = React.createClass({
uid: 3400,
_getStyles: {
overrideStyle: {},
overrideWidth: null,
setOverrideStyle: function(style) {
this.overrideStyle = style;
},
wrapper: function() {
if (!this.overrideStyle) return {};
return merge({}, Styles.Wrapper, this.overrideStyle.Wrapper);
},
container: function(position) {
var override = this.overrideStyle.Containers || {};
if (!this.overrideStyle) return {};
this.overrideWidth = Styles.Containers.DefaultStyle.width;
if (override.DefaultStyle && override.DefaultStyle.width) {
this.overrideWidth = override.DefaultStyle.width;
}
if (override[position] && override[position].width) {
this.overrideWidth = override[position].width;
}
return merge({}, Styles.Containers.DefaultStyle, Styles.Containers[position], override.DefaultStyle, override[position]);
},
elements: {
notification: 'NotificationItem',
title: 'Title',
messageWrapper: 'MessageWrapper',
dismiss: 'Dismiss',
action: 'Action',
actionWrapper: 'ActionWrapper'
},
byElement: function(element) {
var self = this;
return function(level) {
var _element = self.elements[element];
var override = self.overrideStyle[_element] || {};
if (!self.overrideStyle) return {};
return merge({}, Styles[_element].DefaultStyle, Styles[_element][level], override.DefaultStyle, override[level]);
};
}
},
_didNotificationRemoved: function(uid) {
var notification;
var notifications = this.state.notifications.filter(function(toCheck) {
if (toCheck.uid === uid) {
notification = toCheck;
}
return toCheck.uid !== uid;
});
if (notification && notification.onRemove) {
notification.onRemove(notification);
}
this.setState({ notifications: notifications });
},
getInitialState: function() {
return {
notifications: []
};
},
propTypes: {
style: React.PropTypes.oneOfType([
React.PropTypes.bool,
React.PropTypes.object
]),
noAnimation: React.PropTypes.bool,
allowHTML: React.PropTypes.bool
},
getDefaultProps: function() {
return {
style: {},
noAnimation: false,
allowHTML: false
};
},
addNotification: function(notification) {
var _notification = merge({}, Constants.notification, notification);
var notifications = this.state.notifications;
var i;
if (!_notification.level) {
throw new Error('notification level is required.');
}
if (Object.keys(Constants.levels).indexOf(_notification.level) === -1) {
throw new Error('\'' + _notification.level + '\' is not a valid level.');
}
if (isNaN(_notification.autoDismiss)) {
throw new Error('\'autoDismiss\' must be a number.');
}
if (Object.keys(Constants.positions).indexOf(_notification.position) === -1) {
throw new Error('\'' + _notification.position + '\' is not a valid position.');
}
// Some preparations
_notification.position = _notification.position.toLowerCase();
_notification.level = _notification.level.toLowerCase();
_notification.autoDismiss = parseInt(_notification.autoDismiss, 10);
_notification.uid = _notification.uid || this.uid;
_notification.ref = 'notification-' + _notification.uid;
this.uid += 1;
// do not add if the notification already exists based on supplied uid
for (i = 0; i < notifications.length; i++) {
if (notifications[i].uid === _notification.uid) {
return false;
}
}
notifications.push(_notification);
if (typeof _notification.onAdd === 'function') {
notification.onAdd(_notification);
}
this.setState({
notifications: notifications
});
return _notification;
},
removeNotification: function(notification) {
var self = this;
Object.keys(this.refs).forEach(function(container) {
if (container.indexOf('container') > -1) {
Object.keys(self.refs[container].refs).forEach(function(_notification) {
var uid = notification.uid ? notification.uid : notification;
if (_notification === 'notification-' + uid) {
self.refs[container].refs[_notification]._hideNotification();
}
});
}
});
},
deleteNotification: function(notification) {
var self = this;
var uid = notification.uid ? notification.uid : notification;
Object.keys(this.refs).forEach(function(container) {
if (container.indexOf('container') > -1) {
Object.keys(self.refs[container].refs).forEach(function(_notification) {
if (_notification === 'notification-' + uid) {
console.log('remove ', _notification);
self.refs[container].refs[_notification]._removeNotification();
}
});
}
});
this.state.notifications.forEach(function(item, index) {
if (item.uid === uid) {
self.state.notifications.splice(index, 1);
}
});
},
componentDidMount: function() {
this._getStyles.setOverrideStyle(this.props.style);
},
render: function() {
var self = this;
var containers = null;
var notifications = this.state.notifications;
if (notifications.length) {
containers = Object.keys(Constants.positions).map(function(position) {
var _notifications = notifications.filter(function(notification) {
return position === notification.position;
});
if (_notifications.length) {
return (
<NotificationContainer
ref={ 'container-' + position }
key={ position }
position={ position }
notifications={ _notifications }
getStyles={ self._getStyles }
onRemove={ self._didNotificationRemoved }
noAnimation={ self.props.noAnimation }
allowHTML={ self.props.allowHTML }
/>
);
}
});
}
return (
<div className="notifications-wrapper" style={ this._getStyles.wrapper() }>
{ containers }
</div>
);
}
});
module.exports = NotificationSystem;