2013-12-06 09:51:35 +01:00
|
|
|
var when = require('when'),
|
2014-02-05 00:40:30 -08:00
|
|
|
_ = require('lodash'),
|
2014-05-09 12:11:29 +02:00
|
|
|
errors = require('../errors'),
|
|
|
|
|
2013-12-06 09:51:35 +01:00
|
|
|
// Holds the persistent notifications
|
|
|
|
notificationsStore = [],
|
2014-04-28 22:58:18 +02:00
|
|
|
// Holds the last used id
|
|
|
|
notificationCounter = 0,
|
2013-12-06 09:51:35 +01:00
|
|
|
notifications;
|
|
|
|
|
|
|
|
// ## Notifications
|
|
|
|
notifications = {
|
|
|
|
|
|
|
|
browse: function browse() {
|
2014-04-28 22:58:18 +02:00
|
|
|
return when({ 'notifications': notificationsStore });
|
2013-12-06 09:51:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// #### Destroy
|
|
|
|
|
|
|
|
// **takes:** an identifier object ({id: id})
|
|
|
|
destroy: function destroy(i) {
|
2014-04-28 22:58:18 +02:00
|
|
|
var notification = _.find(notificationsStore, function (element) {
|
|
|
|
return element.id === parseInt(i.id, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (notification && !notification.dismissable) {
|
2014-05-09 12:11:29 +02:00
|
|
|
return when.reject(new errors.NoPermissionError('You do not have permission to dismiss this notification.'));
|
2014-04-28 22:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!notification) {
|
2014-05-09 12:11:29 +02:00
|
|
|
return when.reject(new errors.NotFoundError('Notification does not exist.'));
|
2014-04-28 22:58:18 +02:00
|
|
|
}
|
|
|
|
|
2013-12-06 09:51:35 +01:00
|
|
|
notificationsStore = _.reject(notificationsStore, function (element) {
|
2014-04-28 22:58:18 +02:00
|
|
|
return element.id === parseInt(i.id, 10);
|
2013-12-06 09:51:35 +01:00
|
|
|
});
|
2014-04-28 22:58:18 +02:00
|
|
|
// **returns:** a promise for the deleted object
|
|
|
|
return when({notifications: [notification]});
|
2013-12-06 09:51:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
destroyAll: function destroyAll() {
|
|
|
|
notificationsStore = [];
|
2014-04-28 22:58:18 +02:00
|
|
|
notificationCounter = 0;
|
2013-12-06 09:51:35 +01:00
|
|
|
return when(notificationsStore);
|
|
|
|
},
|
|
|
|
|
|
|
|
// #### Add
|
|
|
|
|
|
|
|
// **takes:** a notification object of the form
|
|
|
|
// ```
|
|
|
|
// msg = {
|
|
|
|
// type: 'error', // this can be 'error', 'success', 'warn' and 'info'
|
|
|
|
// message: 'This is an error', // A string. Should fit in one line.
|
2014-04-28 22:58:18 +02:00
|
|
|
// location: 'bottom', // A string where this notification should appear. can be 'bottom' or 'top'
|
|
|
|
// dismissable: true // A Boolean. Whether the notification is dismissable or not.
|
2013-12-06 09:51:35 +01:00
|
|
|
// };
|
|
|
|
// ```
|
|
|
|
add: function add(notification) {
|
2014-04-28 22:58:18 +02:00
|
|
|
|
|
|
|
var defaults = {
|
|
|
|
dismissable: true,
|
|
|
|
location: 'bottom',
|
|
|
|
status: 'persistent'
|
|
|
|
};
|
|
|
|
|
|
|
|
notificationCounter = notificationCounter + 1;
|
|
|
|
|
|
|
|
notification = _.assign(defaults, notification, {
|
|
|
|
id: notificationCounter
|
|
|
|
//status: 'persistent'
|
|
|
|
});
|
|
|
|
|
2014-05-01 23:42:23 +00:00
|
|
|
notificationsStore.push(notification);
|
2014-04-28 22:58:18 +02:00
|
|
|
// **returns:** a promise of the new notification object
|
|
|
|
return when({ notifications: [notification]});
|
2013-12-06 09:51:35 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = notifications;
|