2014-06-30 01:45:03 +04:00
|
|
|
import Notification from 'ghost/models/notification';
|
|
|
|
|
2014-03-22 16:08:15 +04:00
|
|
|
var Notifications = Ember.ArrayProxy.extend({
|
2014-06-24 14:00:28 +04:00
|
|
|
delayedNotifications: [],
|
2014-03-22 16:08:15 +04:00
|
|
|
content: Ember.A(),
|
|
|
|
timeout: 3000,
|
2014-06-30 01:45:03 +04:00
|
|
|
|
2014-03-22 16:08:15 +04:00
|
|
|
pushObject: function (object) {
|
2014-07-09 08:17:30 +04:00
|
|
|
// object can be either a DS.Model or a plain JS object, so when working with
|
|
|
|
// it, we need to handle both cases.
|
|
|
|
|
|
|
|
// make sure notifications have all the necessary properties set.
|
|
|
|
if (typeof object.toJSON === 'function') {
|
|
|
|
// working with a DS.Model
|
|
|
|
|
|
|
|
if (object.get('location') === '') {
|
|
|
|
object.set('location', 'bottom');
|
|
|
|
}
|
2014-03-22 16:08:15 +04:00
|
|
|
}
|
2014-07-09 08:17:30 +04:00
|
|
|
else {
|
|
|
|
if (!object.location) {
|
|
|
|
object.location = 'bottom';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 16:08:15 +04:00
|
|
|
this._super(object);
|
|
|
|
},
|
2014-06-24 14:00:28 +04:00
|
|
|
handleNotification: function (message, delayed) {
|
2014-06-30 01:45:03 +04:00
|
|
|
if (!message.status) {
|
|
|
|
message.status = 'passive';
|
|
|
|
}
|
|
|
|
|
2014-06-24 14:00:28 +04:00
|
|
|
if (!delayed) {
|
|
|
|
this.pushObject(message);
|
|
|
|
} else {
|
|
|
|
this.delayedNotifications.push(message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
showError: function (message, delayed) {
|
|
|
|
this.handleNotification({
|
2014-03-22 16:08:15 +04:00
|
|
|
type: 'error',
|
|
|
|
message: message
|
2014-06-24 14:00:28 +04:00
|
|
|
}, delayed);
|
2014-03-22 16:08:15 +04:00
|
|
|
},
|
2014-04-20 18:48:34 +04:00
|
|
|
showErrors: function (errors) {
|
|
|
|
for (var i = 0; i < errors.length; i += 1) {
|
|
|
|
this.showError(errors[i].message || errors[i]);
|
|
|
|
}
|
|
|
|
},
|
2014-06-24 14:00:28 +04:00
|
|
|
showAPIError: function (resp, defaultErrorText, delayed) {
|
2014-05-15 03:36:13 +04:00
|
|
|
defaultErrorText = defaultErrorText || 'There was a problem on the server, please try again.';
|
|
|
|
|
|
|
|
if (resp && resp.jqXHR && resp.jqXHR.responseJSON && resp.jqXHR.responseJSON.error) {
|
2014-06-24 14:00:28 +04:00
|
|
|
this.showError(resp.jqXHR.responseJSON.error, delayed);
|
2014-07-29 01:41:45 +04:00
|
|
|
} else if (resp && resp.jqXHR && resp.jqXHR.responseJSON && resp.jqXHR.responseJSON.errors) {
|
|
|
|
this.showErrors(resp.jqXHR.responseJSON.errors, delayed);
|
2014-05-15 03:36:13 +04:00
|
|
|
} else {
|
2014-06-24 14:00:28 +04:00
|
|
|
this.showError(defaultErrorText, delayed);
|
2014-05-15 03:36:13 +04:00
|
|
|
}
|
|
|
|
},
|
2014-06-24 14:00:28 +04:00
|
|
|
showInfo: function (message, delayed) {
|
|
|
|
this.handleNotification({
|
2014-03-22 16:08:15 +04:00
|
|
|
type: 'info',
|
|
|
|
message: message
|
2014-06-24 14:00:28 +04:00
|
|
|
}, delayed);
|
2014-03-22 16:08:15 +04:00
|
|
|
},
|
2014-06-24 14:00:28 +04:00
|
|
|
showSuccess: function (message, delayed) {
|
|
|
|
this.handleNotification({
|
2014-03-22 16:08:15 +04:00
|
|
|
type: 'success',
|
|
|
|
message: message
|
2014-06-24 14:00:28 +04:00
|
|
|
}, delayed);
|
2014-03-22 16:08:15 +04:00
|
|
|
},
|
2014-07-15 08:02:34 +04:00
|
|
|
// @Todo this function isn't referenced anywhere. Should it be removed?
|
2014-06-24 14:00:28 +04:00
|
|
|
showWarn: function (message, delayed) {
|
|
|
|
this.handleNotification({
|
2014-03-22 16:08:15 +04:00
|
|
|
type: 'warn',
|
|
|
|
message: message
|
2014-06-24 14:00:28 +04:00
|
|
|
}, delayed);
|
|
|
|
},
|
|
|
|
displayDelayed: function () {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.delayedNotifications.forEach(function (message) {
|
|
|
|
self.pushObject(message);
|
2014-03-22 16:08:15 +04:00
|
|
|
});
|
2014-06-24 14:00:28 +04:00
|
|
|
self.delayedNotifications = [];
|
2014-06-19 23:44:44 +04:00
|
|
|
},
|
2014-06-30 01:45:03 +04:00
|
|
|
closeNotification: function (notification) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (notification instanceof Notification) {
|
|
|
|
notification.deleteRecord();
|
2014-07-15 08:02:34 +04:00
|
|
|
notification.save().finally(function () {
|
2014-06-30 01:45:03 +04:00
|
|
|
self.removeObject(notification);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.removeObject(notification);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
closePassive: function () {
|
|
|
|
this.set('content', this.rejectBy('status', 'passive'));
|
|
|
|
},
|
|
|
|
closePersistent: function () {
|
|
|
|
this.set('content', this.rejectBy('status', 'persistent'));
|
|
|
|
},
|
2014-06-19 23:44:44 +04:00
|
|
|
closeAll: function () {
|
2014-06-24 04:47:51 +04:00
|
|
|
this.clear();
|
2014-03-22 16:08:15 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-04-20 18:48:34 +04:00
|
|
|
export default Notifications;
|