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) {
|
|
|
|
object.typeClass = 'notification-' + object.type;
|
|
|
|
// This should be somewhere else.
|
|
|
|
if (object.type === 'success') {
|
2014-06-02 00:53:16 +04:00
|
|
|
object.typeClass = object.typeClass + ' notification-passive';
|
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-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-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();
|
|
|
|
notification.save().then(function () {
|
|
|
|
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;
|