toast-message.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import Ember from 'ember';
  2. import ENV from '../config/environment';
  3. var HIDE_ANIMATION_LENGTH = 200;
  4. export default Ember.Service.extend({
  5. id: null,
  6. type: null,
  7. message: null,
  8. isVisible: false,
  9. isInfo: Ember.computed.equal('type', 'info'),
  10. isSuccess: Ember.computed.equal('type', 'success'),
  11. isWarning: Ember.computed.equal('type', 'warning'),
  12. isError: Ember.computed.equal('type', 'error'),
  13. _showToast: function(type, message) {
  14. var toastId = this.incrementProperty('id');
  15. this.set('type', type);
  16. this.set('message', message);
  17. this.set('isVisible', true);
  18. var self = this;
  19. Ember.run.later(function () {
  20. if (self.get('id') === toastId) {
  21. self.set('isVisible', false);
  22. }
  23. }, ENV.APP.TOAST_BASE_DISPLAY_TIME + (message.length * 110));
  24. },
  25. _setToast: function(type, message) {
  26. var self = this;
  27. if (this.get('isVisible')) {
  28. this.set('isVisible', false);
  29. Ember.run.later(function () {
  30. self._showToast(type, message);
  31. }, HIDE_ANIMATION_LENGTH);
  32. } else {
  33. this._showToast(type, message);
  34. }
  35. },
  36. // Public api
  37. info: function(message) {
  38. this._setToast('info', message);
  39. },
  40. success: function(message) {
  41. this._setToast('success', message);
  42. },
  43. warning: function(message) {
  44. this._setToast('warning', message);
  45. },
  46. error: function(message) {
  47. this._setToast('error', message);
  48. }
  49. });