flash-message.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import Ember from 'ember';
  2. import ENV from '../config/environment';
  3. var HIDE_ANIMATION_LENGTH = 200;
  4. export default Ember.Controller.extend({
  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. showFlash: function(type, message) {
  22. var flashId = this.incrementProperty('id');
  23. this.set('type', type);
  24. this.set('message', message);
  25. this.set('isVisible', true);
  26. var self = this;
  27. Ember.run.later(function () {
  28. if (self.get('id') === flashId) {
  29. self.set('isVisible', false);
  30. }
  31. }, ENV.APP.FLASH_MIN_DISPLAY_TIME);
  32. },
  33. actions: {
  34. setFlash: function(type, message) {
  35. var self = this;
  36. if (this.get('isVisible')) {
  37. this.set('isVisible', false);
  38. Ember.run.later(function () {
  39. self.showFlash(type, message);
  40. }, HIDE_ANIMATION_LENGTH);
  41. } else {
  42. this.showFlash(type, message);
  43. }
  44. }
  45. }
  46. });