123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import Ember from 'ember';
- import ENV from '../config/environment';
- export default Ember.Service.extend({
- id: null,
- type: null,
- message: null,
- isVisible: false,
- isInfo: Ember.computed.equal('type', 'info'),
- isSuccess: Ember.computed.equal('type', 'success'),
- isWarning: Ember.computed.equal('type', 'warning'),
- isError: Ember.computed.equal('type', 'error'),
- _showToast: function(type, message) {
- var toastId = this.incrementProperty('id');
- this.setProperties({
- 'type': type,
- 'message': message,
- 'isVisible': true
- });
- var displayTime = ENV.APP.TOAST_BASE_DISPLAY_TIME;
- displayTime += message.length * ENV.APP.TOAST_LENGTH_FACTOR;
- var self = this;
- Ember.run.later(function () {
- if (self.get('id') === toastId) {
- self.set('isVisible', false);
- }
- }, displayTime);
- },
- _setToast: function(type, message) {
- var self = this;
- if (this.get('isVisible')) {
- this.set('isVisible', false);
- Ember.run.later(function () {
- self._showToast(type, message);
- }, ENV.APP.TOAST_HIDE_ANIMATION_LENGTH);
- } else {
- this._showToast(type, message);
- }
- },
- // Public api
- info: function(message) {
- this._setToast('info', message);
- },
- success: function(message) {
- this._setToast('success', message);
- },
- warning: function(message) {
- this._setToast('warning', message);
- },
- error: function(message) {
- this._setToast('error', message);
- },
- apiError: function(reason) {
- reason = Ember.Object.create(reason);
- var errorMessage = gettext('Unknown error has occured.');
- if (reason.get('status') === 0) {
- errorMessage = gettext('Lost connection with application.');
- }
- if (reason.get('status') === 403) {
- errorMessage = reason.get('responseJSON.detail');
- if (errorMessage === 'Permission denied') {
- errorMessage = gettext("You don't have permission to perform this action.");
- }
- }
- if (reason.get('status') === 404) {
- errorMessage = gettext('Action link is invalid.');
- }
- this.error(errorMessage);
- }
- });
|