/ src / notification-manager.js
notification-manager.js
  1  const { Emitter } = require('event-kit');
  2  const Notification = require('../src/notification');
  3  
  4  // Public: A notification manager used to create {Notification}s to be shown
  5  // to the user.
  6  //
  7  // An instance of this class is always available as the `atom.notifications`
  8  // global.
  9  module.exports = class NotificationManager {
 10    constructor() {
 11      this.notifications = [];
 12      this.emitter = new Emitter();
 13    }
 14  
 15    /*
 16    Section: Events
 17    */
 18  
 19    // Public: Invoke the given callback after a notification has been added.
 20    //
 21    // * `callback` {Function} to be called after the notification is added.
 22    //   * `notification` The {Notification} that was added.
 23    //
 24    // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
 25    onDidAddNotification(callback) {
 26      return this.emitter.on('did-add-notification', callback);
 27    }
 28  
 29    // Public: Invoke the given callback after the notifications have been cleared.
 30    //
 31    // * `callback` {Function} to be called after the notifications are cleared.
 32    //
 33    // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
 34    onDidClearNotifications(callback) {
 35      return this.emitter.on('did-clear-notifications', callback);
 36    }
 37  
 38    /*
 39    Section: Adding Notifications
 40    */
 41  
 42    // Public: Add a success notification.
 43    //
 44    // * `message` A {String} message
 45    // * `options` (optional) An options {Object} with the following keys:
 46    //    * `buttons` (optional) An {Array} of {Object} where each {Object} has
 47    //      the following options:
 48    //      * `className` (optional) {String} a class name to add to the button's
 49    //        default class name (`btn btn-success`).
 50    //      * `onDidClick` (optional) {Function} callback to call when the button
 51    //        has been clicked. The context will be set to the
 52    //        {NotificationElement} instance.
 53    //      * `text` {String} inner text for the button
 54    //    * `description` (optional) A Markdown {String} containing a longer
 55    //      description about the notification. By default, this **will not**
 56    //      preserve newlines and whitespace when it is rendered.
 57    //    * `detail` (optional) A plain-text {String} containing additional
 58    //      details about the notification. By default, this **will** preserve
 59    //      newlines and whitespace when it is rendered.
 60    //    * `dismissable` (optional) A {Boolean} indicating whether this
 61    //      notification can be dismissed by the user. Defaults to `false`.
 62    //    * `icon` (optional) A {String} name of an icon from Octicons to display
 63    //      in the notification header. Defaults to `'check'`.
 64    //
 65    // Returns the {Notification} that was added.
 66    addSuccess(message, options) {
 67      return this.addNotification(new Notification('success', message, options));
 68    }
 69  
 70    // Public: Add an informational notification.
 71    //
 72    // * `message` A {String} message
 73    // * `options` (optional) An options {Object} with the following keys:
 74    //    * `buttons` (optional) An {Array} of {Object} where each {Object} has
 75    //      the following options:
 76    //      * `className` (optional) {String} a class name to add to the button's
 77    //        default class name (`btn btn-info`).
 78    //      * `onDidClick` (optional) {Function} callback to call when the button
 79    //        has been clicked. The context will be set to the
 80    //        {NotificationElement} instance.
 81    //      * `text` {String} inner text for the button
 82    //    * `description` (optional) A Markdown {String} containing a longer
 83    //      description about the notification. By default, this **will not**
 84    //      preserve newlines and whitespace when it is rendered.
 85    //    * `detail` (optional) A plain-text {String} containing additional
 86    //      details about the notification. By default, this **will** preserve
 87    //      newlines and whitespace when it is rendered.
 88    //    * `dismissable` (optional) A {Boolean} indicating whether this
 89    //      notification can be dismissed by the user. Defaults to `false`.
 90    //    * `icon` (optional) A {String} name of an icon from Octicons to display
 91    //      in the notification header. Defaults to `'info'`.
 92    //
 93    // Returns the {Notification} that was added.
 94    addInfo(message, options) {
 95      return this.addNotification(new Notification('info', message, options));
 96    }
 97  
 98    // Public: Add a warning notification.
 99    //
100    // * `message` A {String} message
101    // * `options` (optional) An options {Object} with the following keys:
102    //    * `buttons` (optional) An {Array} of {Object} where each {Object} has
103    //      the following options:
104    //      * `className` (optional) {String} a class name to add to the button's
105    //        default class name (`btn btn-warning`).
106    //      * `onDidClick` (optional) {Function} callback to call when the button
107    //        has been clicked. The context will be set to the
108    //        {NotificationElement} instance.
109    //      * `text` {String} inner text for the button
110    //    * `description` (optional) A Markdown {String} containing a longer
111    //      description about the notification. By default, this **will not**
112    //      preserve newlines and whitespace when it is rendered.
113    //    * `detail` (optional) A plain-text {String} containing additional
114    //      details about the notification. By default, this **will** preserve
115    //      newlines and whitespace when it is rendered.
116    //    * `dismissable` (optional) A {Boolean} indicating whether this
117    //      notification can be dismissed by the user. Defaults to `false`.
118    //    * `icon` (optional) A {String} name of an icon from Octicons to display
119    //      in the notification header. Defaults to `'alert'`.
120    //
121    // Returns the {Notification} that was added.
122    addWarning(message, options) {
123      return this.addNotification(new Notification('warning', message, options));
124    }
125  
126    // Public: Add an error notification.
127    //
128    // * `message` A {String} message
129    // * `options` (optional) An options {Object} with the following keys:
130    //    * `buttons` (optional) An {Array} of {Object} where each {Object} has
131    //      the following options:
132    //      * `className` (optional) {String} a class name to add to the button's
133    //        default class name (`btn btn-error`).
134    //      * `onDidClick` (optional) {Function} callback to call when the button
135    //        has been clicked. The context will be set to the
136    //        {NotificationElement} instance.
137    //      * `text` {String} inner text for the button
138    //    * `description` (optional) A Markdown {String} containing a longer
139    //      description about the notification. By default, this **will not**
140    //      preserve newlines and whitespace when it is rendered.
141    //    * `detail` (optional) A plain-text {String} containing additional
142    //      details about the notification. By default, this **will** preserve
143    //      newlines and whitespace when it is rendered.
144    //    * `dismissable` (optional) A {Boolean} indicating whether this
145    //      notification can be dismissed by the user. Defaults to `false`.
146    //    * `icon` (optional) A {String} name of an icon from Octicons to display
147    //      in the notification header. Defaults to `'flame'`.
148    //    * `stack` (optional) A preformatted {String} with stack trace
149    //      information describing the location of the error.
150    //
151    // Returns the {Notification} that was added.
152    addError(message, options) {
153      return this.addNotification(new Notification('error', message, options));
154    }
155  
156    // Public: Add a fatal error notification.
157    //
158    // * `message` A {String} message
159    // * `options` (optional) An options {Object} with the following keys:
160    //    * `buttons` (optional) An {Array} of {Object} where each {Object} has
161    //      the following options:
162    //      * `className` (optional) {String} a class name to add to the button's
163    //        default class name (`btn btn-error`).
164    //      * `onDidClick` (optional) {Function} callback to call when the button
165    //        has been clicked. The context will be set to the
166    //        {NotificationElement} instance.
167    //      * `text` {String} inner text for the button
168    //    * `description` (optional) A Markdown {String} containing a longer
169    //      description about the notification. By default, this **will not**
170    //      preserve newlines and whitespace when it is rendered.
171    //    * `detail` (optional) A plain-text {String} containing additional
172    //      details about the notification. By default, this **will** preserve
173    //      newlines and whitespace when it is rendered.
174    //    * `dismissable` (optional) A {Boolean} indicating whether this
175    //      notification can be dismissed by the user. Defaults to `false`.
176    //    * `icon` (optional) A {String} name of an icon from Octicons to display
177    //      in the notification header. Defaults to `'bug'`.
178    //    * `stack` (optional) A preformatted {String} with stack trace
179    //      information describing the location of the error.
180    //
181    // Returns the {Notification} that was added.
182    addFatalError(message, options) {
183      return this.addNotification(new Notification('fatal', message, options));
184    }
185  
186    add(type, message, options) {
187      return this.addNotification(new Notification(type, message, options));
188    }
189  
190    addNotification(notification) {
191      this.notifications.push(notification);
192      this.emitter.emit('did-add-notification', notification);
193      return notification;
194    }
195  
196    /*
197    Section: Getting Notifications
198    */
199  
200    // Public: Get all the notifications.
201    //
202    // Returns an {Array} of {Notification}s.
203    getNotifications() {
204      return this.notifications.slice();
205    }
206  
207    /*
208    Section: Managing Notifications
209    */
210  
211    // Public: Clear all the notifications.
212    clear() {
213      this.notifications = [];
214      this.emitter.emit('did-clear-notifications');
215    }
216  };