2013-12-06 12:51:35 +04:00
|
|
|
var when = require('when'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../errors'),
|
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
// Holds the persistent notifications
|
|
|
|
notificationsStore = [],
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// Holds the last used id
|
2014-04-29 00:58:18 +04:00
|
|
|
notificationCounter = 0,
|
2013-12-06 12:51:35 +04:00
|
|
|
notifications;
|
|
|
|
|
|
|
|
// ## Notifications
|
|
|
|
notifications = {
|
|
|
|
|
|
|
|
browse: function browse() {
|
2014-04-29 00:58:18 +04:00
|
|
|
return when({ 'notifications': notificationsStore });
|
2013-12-06 12:51:35 +04:00
|
|
|
},
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
destroy: function destroy(options) {
|
2014-04-29 00:58:18 +04:00
|
|
|
var notification = _.find(notificationsStore, function (element) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return element.id === parseInt(options.id, 10);
|
2014-04-29 00:58:18 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (notification && !notification.dismissable) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return when.reject(
|
|
|
|
new errors.NoPermissionError('You do not have permission to dismiss this notification.')
|
|
|
|
);
|
2014-04-29 00:58:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!notification) {
|
2014-05-09 14:11:29 +04:00
|
|
|
return when.reject(new errors.NotFoundError('Notification does not exist.'));
|
2014-04-29 00:58:18 +04:00
|
|
|
}
|
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
notificationsStore = _.reject(notificationsStore, function (element) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return element.id === parseInt(options.id, 10);
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
2014-04-29 00:58:18 +04:00
|
|
|
// **returns:** a promise for the deleted object
|
|
|
|
return when({notifications: [notification]});
|
2013-12-06 12:51:35 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
destroyAll: function destroyAll() {
|
|
|
|
notificationsStore = [];
|
2014-04-29 00:58:18 +04:00
|
|
|
notificationCounter = 0;
|
2013-12-06 12:51:35 +04:00
|
|
|
return when(notificationsStore);
|
|
|
|
},
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### 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.
|
|
|
|
* 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 12:51:35 +04:00
|
|
|
add: function add(notification) {
|
2014-04-29 00:58:18 +04:00
|
|
|
|
|
|
|
var defaults = {
|
|
|
|
dismissable: true,
|
|
|
|
location: 'bottom',
|
|
|
|
status: 'persistent'
|
|
|
|
};
|
|
|
|
|
|
|
|
notificationCounter = notificationCounter + 1;
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2014-04-29 00:58:18 +04:00
|
|
|
notification = _.assign(defaults, notification, {
|
|
|
|
id: notificationCounter
|
|
|
|
//status: 'persistent'
|
|
|
|
});
|
|
|
|
|
2014-05-02 03:42:23 +04:00
|
|
|
notificationsStore.push(notification);
|
2014-04-29 00:58:18 +04:00
|
|
|
// **returns:** a promise of the new notification object
|
|
|
|
return when({ notifications: [notification]});
|
2013-12-06 12:51:35 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = notifications;
|