2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2014-06-30 01:45:03 +04:00
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
export default Ember.Service.extend({
|
|
|
|
delayedNotifications: Ember.A(),
|
2014-03-22 16:08:15 +04:00
|
|
|
content: Ember.A(),
|
2014-06-30 01:45:03 +04:00
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
alerts: Ember.computed.filter('content', function (notification) {
|
|
|
|
var status = Ember.get(notification, 'status');
|
|
|
|
return status === 'alert';
|
|
|
|
}),
|
2014-07-09 08:17:30 +04:00
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
notifications: Ember.computed.filter('content', function (notification) {
|
|
|
|
var status = Ember.get(notification, 'status');
|
|
|
|
return status === 'notification';
|
|
|
|
}),
|
2014-07-09 08:17:30 +04:00
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
handleNotification: function (message, delayed) {
|
|
|
|
// If this is an alert message from the server, treat it as html safe
|
|
|
|
if (typeof message.toJSON === 'function' && message.get('status') === 'alert') {
|
|
|
|
message.set('message', message.get('message').htmlSafe());
|
2014-07-09 08:17:30 +04:00
|
|
|
}
|
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
if (!Ember.get(message, 'status')) {
|
|
|
|
Ember.set(message, 'status', 'notification');
|
2014-06-30 01:45:03 +04:00
|
|
|
}
|
|
|
|
|
2014-06-24 14:00:28 +04:00
|
|
|
if (!delayed) {
|
2015-05-26 05:10:50 +03:00
|
|
|
this.get('content').pushObject(message);
|
2014-06-24 14:00:28 +04:00
|
|
|
} else {
|
2015-06-19 00:56:18 +03:00
|
|
|
this.get('delayedNotifications').pushObject(message);
|
2014-06-24 14:00:28 +04:00
|
|
|
}
|
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
showAlert: function (message, options) {
|
2014-08-01 09:40:49 +04:00
|
|
|
options = options || {};
|
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
this.handleNotification({
|
|
|
|
message: message,
|
|
|
|
status: 'alert',
|
|
|
|
type: options.type
|
|
|
|
}, options.delayed);
|
|
|
|
},
|
|
|
|
|
|
|
|
showNotification: function (message, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (!options.doNotCloseNotifications) {
|
|
|
|
this.closeNotifications();
|
2014-08-01 09:40:49 +04:00
|
|
|
}
|
|
|
|
|
2014-06-24 14:00:28 +04:00
|
|
|
this.handleNotification({
|
2015-06-19 00:56:18 +03:00
|
|
|
message: message,
|
|
|
|
status: 'notification',
|
|
|
|
type: options.type
|
2014-08-01 09:40:49 +04:00
|
|
|
}, options.delayed);
|
2014-03-22 16:08:15 +04:00
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
// TODO: review whether this can be removed once no longer used by validations
|
2014-08-01 09:40:49 +04:00
|
|
|
showErrors: function (errors, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
if (!options.doNotCloseNotifications) {
|
|
|
|
this.closeNotifications();
|
2014-08-01 09:40:49 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 18:48:34 +04:00
|
|
|
for (var i = 0; i < errors.length; i += 1) {
|
2015-06-19 00:56:18 +03:00
|
|
|
this.showNotification(errors[i].message || errors[i], {type: 'error', doNotCloseNotifications: true});
|
2014-04-20 18:48:34 +04:00
|
|
|
}
|
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2014-08-01 09:40:49 +04:00
|
|
|
showAPIError: function (resp, options) {
|
|
|
|
options = options || {};
|
2015-06-19 00:56:18 +03:00
|
|
|
options.type = options.type || 'error';
|
2014-08-01 09:40:49 +04:00
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
if (!options.doNotCloseNotifications) {
|
|
|
|
this.closeNotifications();
|
2014-08-01 09:40:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
options.defaultErrorText = options.defaultErrorText || 'There was a problem on the server, please try again.';
|
2014-05-15 03:36:13 +04:00
|
|
|
|
|
|
|
if (resp && resp.jqXHR && resp.jqXHR.responseJSON && resp.jqXHR.responseJSON.error) {
|
2015-06-19 00:56:18 +03:00
|
|
|
this.showAlert(resp.jqXHR.responseJSON.error, options);
|
2014-07-29 01:41:45 +04:00
|
|
|
} else if (resp && resp.jqXHR && resp.jqXHR.responseJSON && resp.jqXHR.responseJSON.errors) {
|
2014-08-01 09:40:49 +04:00
|
|
|
this.showErrors(resp.jqXHR.responseJSON.errors, options);
|
2014-08-01 02:58:32 +04:00
|
|
|
} else if (resp && resp.jqXHR && resp.jqXHR.responseJSON && resp.jqXHR.responseJSON.message) {
|
2015-06-19 00:56:18 +03:00
|
|
|
this.showAlert(resp.jqXHR.responseJSON.message, options);
|
2014-05-15 03:36:13 +04:00
|
|
|
} else {
|
2015-06-19 00:56:18 +03:00
|
|
|
this.showAlert(options.defaultErrorText, {type: options.type, doNotCloseNotifications: true});
|
2014-08-01 09:40:49 +04:00
|
|
|
}
|
2014-06-24 14:00:28 +04:00
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2014-06-24 14:00:28 +04:00
|
|
|
displayDelayed: function () {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.delayedNotifications.forEach(function (message) {
|
2015-05-26 05:10:50 +03:00
|
|
|
self.get('content').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
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2014-06-30 01:45:03 +04:00
|
|
|
closeNotification: function (notification) {
|
2015-05-26 05:10:50 +03:00
|
|
|
var content = this.get('content');
|
2014-06-30 01:45:03 +04:00
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
if (typeof notification.toJSON === 'function') {
|
2014-06-30 01:45:03 +04:00
|
|
|
notification.deleteRecord();
|
2014-07-15 08:02:34 +04:00
|
|
|
notification.save().finally(function () {
|
2015-05-26 05:10:50 +03:00
|
|
|
content.removeObject(notification);
|
2014-06-30 01:45:03 +04:00
|
|
|
});
|
|
|
|
} else {
|
2015-05-26 05:10:50 +03:00
|
|
|
content.removeObject(notification);
|
2014-06-30 01:45:03 +04:00
|
|
|
}
|
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
closeNotifications: function () {
|
|
|
|
this.set('content', this.get('content').rejectBy('status', 'notification'));
|
2014-06-30 01:45:03 +04:00
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2014-06-19 23:44:44 +04:00
|
|
|
closeAll: function () {
|
2015-05-26 05:10:50 +03:00
|
|
|
this.get('content').clear();
|
2014-03-22 16:08:15 +04:00
|
|
|
}
|
|
|
|
});
|