flash-message.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import Ember from 'ember';
  2. export default Ember.Controller.extend({
  3. VISIBLE_FOR: 4500,
  4. HIDE_ANIMATION_LENGTH: 200,
  5. id: null,
  6. type: null,
  7. message: null,
  8. isVisible: false,
  9. isInfo: function() {
  10. return this.get('type') === 'info';
  11. }.property('type'),
  12. isSuccess: function() {
  13. return this.get('type') === 'success';
  14. }.property('type'),
  15. isWarning: function() {
  16. return this.get('type') === 'warning';
  17. }.property('type'),
  18. isError: function() {
  19. return this.get('type') === 'error';
  20. }.property('type'),
  21. actions: {
  22. setFlash: function(type, message) {
  23. var self = this;
  24. if (this.get('isVisible')) {
  25. this.set('isVisible', false);
  26. Ember.run.later(function () {
  27. self.send('showFlash', type, message);
  28. }, this.get('HIDE_ANIMATION_LENGTH'));
  29. } else {
  30. this.send('showFlash', type, message);
  31. }
  32. },
  33. showFlash: function(type, message) {
  34. var flashId = this.incrementProperty('id');
  35. this.set('type', type);
  36. this.set('message', message);
  37. this.set('isVisible', true);
  38. var self = this;
  39. Ember.run.later(function () {
  40. if (self.get('id') === flashId) {
  41. self.set('isVisible', false);
  42. }
  43. }, this.get('VISIBLE_FOR'));
  44. }
  45. }
  46. });