notification.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const { Emitter } = require('event-kit');
  2. const _ = require('underscore-plus');
  3. // Public: A notification to the user containing a message and type.
  4. module.exports = class Notification {
  5. constructor(type, message, options = {}) {
  6. this.type = type;
  7. this.message = message;
  8. this.options = options;
  9. this.emitter = new Emitter();
  10. this.timestamp = new Date();
  11. this.dismissed = true;
  12. if (this.isDismissable()) this.dismissed = false;
  13. this.displayed = false;
  14. this.validate();
  15. }
  16. validate() {
  17. if (typeof this.message !== 'string') {
  18. throw new Error(
  19. `Notification must be created with string message: ${this.message}`
  20. );
  21. }
  22. if (!_.isObject(this.options) || Array.isArray(this.options)) {
  23. throw new Error(
  24. `Notification must be created with an options object: ${this.options}`
  25. );
  26. }
  27. }
  28. /*
  29. Section: Event Subscription
  30. */
  31. // Public: Invoke the given callback when the notification is dismissed.
  32. //
  33. // * `callback` {Function} to be called when the notification is dismissed.
  34. //
  35. // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
  36. onDidDismiss(callback) {
  37. return this.emitter.on('did-dismiss', callback);
  38. }
  39. // Public: Invoke the given callback when the notification is displayed.
  40. //
  41. // * `callback` {Function} to be called when the notification is displayed.
  42. //
  43. // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
  44. onDidDisplay(callback) {
  45. return this.emitter.on('did-display', callback);
  46. }
  47. getOptions() {
  48. return this.options;
  49. }
  50. /*
  51. Section: Methods
  52. */
  53. // Public: Returns the {String} type.
  54. getType() {
  55. return this.type;
  56. }
  57. // Public: Returns the {String} message.
  58. getMessage() {
  59. return this.message;
  60. }
  61. getTimestamp() {
  62. return this.timestamp;
  63. }
  64. getDetail() {
  65. return this.options.detail;
  66. }
  67. isEqual(other) {
  68. return (
  69. this.getMessage() === other.getMessage() &&
  70. this.getType() === other.getType() &&
  71. this.getDetail() === other.getDetail()
  72. );
  73. }
  74. // Extended: Dismisses the notification, removing it from the UI. Calling this
  75. // programmatically will call all callbacks added via `onDidDismiss`.
  76. dismiss() {
  77. if (!this.isDismissable() || this.isDismissed()) return;
  78. this.dismissed = true;
  79. this.emitter.emit('did-dismiss', this);
  80. }
  81. isDismissed() {
  82. return this.dismissed;
  83. }
  84. isDismissable() {
  85. return !!this.options.dismissable;
  86. }
  87. wasDisplayed() {
  88. return this.displayed;
  89. }
  90. setDisplayed(displayed) {
  91. this.displayed = displayed;
  92. this.emitter.emit('did-display', this);
  93. }
  94. getIcon() {
  95. if (this.options.icon != null) return this.options.icon;
  96. switch (this.type) {
  97. case 'fatal':
  98. return 'bug';
  99. case 'error':
  100. return 'flame';
  101. case 'warning':
  102. return 'alert';
  103. case 'info':
  104. return 'info';
  105. case 'success':
  106. return 'check';
  107. }
  108. }
  109. };