UNPKG

10.2 kBJavaScriptView Raw
1/*
2 * Copyright (c) Jupyter Development Team.
3 * Distributed under the terms of the Modified BSD License.
4 */
5import { UUID } from '@lumino/coreutils';
6import { Signal } from '@lumino/signaling';
7/**
8 * Notification manager
9 */
10export class NotificationManager {
11 constructor() {
12 this._isDisposed = false;
13 this._queue = [];
14 this._changed = new Signal(this);
15 }
16 /**
17 * Signal emitted whenever a notification changes.
18 */
19 get changed() {
20 return this._changed;
21 }
22 /**
23 * Total number of notifications.
24 */
25 get count() {
26 return this._queue.length;
27 }
28 /**
29 * Whether the manager is disposed or not.
30 */
31 get isDisposed() {
32 return this._isDisposed;
33 }
34 /**
35 * The list of notifications.
36 */
37 get notifications() {
38 return this._queue.slice();
39 }
40 /**
41 * Dismiss one notification (specified by its id) or all if no id provided.
42 *
43 * @param id Notification id
44 */
45 dismiss(id) {
46 if (typeof id === 'undefined') {
47 const q = this._queue.slice();
48 this._queue.length = 0;
49 for (const notification of q) {
50 this._changed.emit({
51 type: 'removed',
52 notification
53 });
54 }
55 }
56 else {
57 const notificationIndex = this._queue.findIndex(n => n.id === id);
58 if (notificationIndex > -1) {
59 const notification = this._queue.splice(notificationIndex, 1)[0];
60 this._changed.emit({
61 type: 'removed',
62 notification
63 });
64 }
65 }
66 }
67 /**
68 * Dispose the manager.
69 */
70 dispose() {
71 if (this._isDisposed) {
72 return;
73 }
74 this._isDisposed = true;
75 Signal.clearData(this);
76 }
77 /**
78 * Test whether a notification exists or not.
79 *
80 * @param id Notification id
81 * @returns Notification status
82 */
83 has(id) {
84 return this._queue.findIndex(n => n.id === id) > -1;
85 }
86 /**
87 * Add a new notification.
88 *
89 * This will trigger the `changed` signal with an `added` event.
90 *
91 * @param message Notification message
92 * @param type Notification type
93 * @param options Notification option
94 * @returns Notification unique id
95 */
96 notify(message, type, options) {
97 const now = Date.now();
98 const { progress, ...othersOptions } = options;
99 const notification = Object.freeze({
100 id: UUID.uuid4(),
101 createdAt: now,
102 modifiedAt: now,
103 message,
104 type,
105 options: {
106 // By default notification will be silent
107 autoClose: 0,
108 progress: typeof progress === 'number'
109 ? Math.min(Math.max(0, progress), 1)
110 : progress,
111 ...othersOptions
112 }
113 });
114 this._queue.unshift(notification);
115 this._changed.emit({
116 type: 'added',
117 notification
118 });
119 return notification.id;
120 }
121 /**
122 * Update an existing notification.
123 *
124 * If the notification does not exists this won't do anything.
125 *
126 * Once updated the notification will be moved at the begin
127 * of the notification stack.
128 *
129 * @param args Update options
130 * @returns Whether the update was successful or not.
131 */
132 update(args) {
133 const { id, message, actions, autoClose, data, progress, type } = args;
134 const newProgress = typeof progress === 'number'
135 ? Math.min(Math.max(0, progress), 1)
136 : progress;
137 const notificationIndex = this._queue.findIndex(n => n.id === id);
138 if (notificationIndex > -1) {
139 const oldNotification = this._queue[notificationIndex];
140 // We need to create a new object as notification are frozen; i.e. cannot be edited
141 const notification = Object.freeze({
142 ...oldNotification,
143 message: message !== null && message !== void 0 ? message : oldNotification.message,
144 type: type !== null && type !== void 0 ? type : oldNotification.type,
145 options: {
146 actions: actions !== null && actions !== void 0 ? actions : oldNotification.options.actions,
147 autoClose: autoClose !== null && autoClose !== void 0 ? autoClose : oldNotification.options.autoClose,
148 data: data !== null && data !== void 0 ? data : oldNotification.options.data,
149 progress: newProgress !== null && newProgress !== void 0 ? newProgress : oldNotification.options.progress
150 },
151 modifiedAt: Date.now()
152 });
153 this._queue.splice(notificationIndex, 1);
154 this._queue.unshift(notification);
155 this._changed.emit({
156 type: 'updated',
157 notification
158 });
159 return true;
160 }
161 return false;
162 }
163}
164/**
165 * Notification namespace
166 */
167export var Notification;
168(function (Notification) {
169 /**
170 * The global notification manager.
171 */
172 Notification.manager = new NotificationManager();
173 /**
174 * Dismiss one notification (specified by its id) or all if no id provided
175 *
176 * @param id notification id
177 */
178 function dismiss(id) {
179 Notification.manager.dismiss(id);
180 }
181 Notification.dismiss = dismiss;
182 /**
183 * Helper function to emit a notification.
184 *
185 * #### Notes
186 * The message will be truncated if longer than 140 characters.
187 *
188 * @param message Notification message
189 * @param type Notification type
190 * @param options Options for the error notification
191 * @returns Notification unique id
192 */
193 function emit(message, type = 'default', options = {}) {
194 return Notification.manager.notify(message, type, options);
195 }
196 Notification.emit = emit;
197 /**
198 * Helper function to emit an error notification.
199 *
200 * #### Notes
201 * The message will be truncated if longer than 140 characters.
202 *
203 * @param message Notification message
204 * @param options Options for the error notification
205 * @returns Notification unique id
206 */
207 function error(message, options = {}) {
208 return Notification.manager.notify(message, 'error', options);
209 }
210 Notification.error = error;
211 /**
212 * Helper function to emit an info notification.
213 *
214 * #### Notes
215 * The message will be truncated if longer than 140 characters.
216 *
217 * @param message Notification message
218 * @param options Options for the info notification
219 * @returns Notification unique id
220 */
221 function info(message, options = {}) {
222 return Notification.manager.notify(message, 'info', options);
223 }
224 Notification.info = info;
225 /**
226 * Helper function to show an in-progress notification.
227 *
228 * #### Notes
229 * The message will be truncated if longer than 140 characters.
230 *
231 * @param promise Promise to wait for
232 * @param options Options for the in-progress notification
233 * @returns Notification unique id
234 */
235 function promise(promise, options) {
236 var _a;
237 const { pending, error, success } = options;
238 const id = Notification.manager.notify(pending.message, 'in-progress', (_a = pending.options) !== null && _a !== void 0 ? _a : {});
239 promise
240 .then(result => {
241 var _a, _b, _c;
242 Notification.manager.update({
243 id,
244 message: success.message(result, (_a = success.options) === null || _a === void 0 ? void 0 : _a.data),
245 type: 'success',
246 ...success.options,
247 data: (_c = (_b = success.options) === null || _b === void 0 ? void 0 : _b.data) !== null && _c !== void 0 ? _c : result
248 });
249 })
250 .catch(reason => {
251 var _a, _b, _c;
252 Notification.manager.update({
253 id,
254 message: error.message(reason, (_a = error.options) === null || _a === void 0 ? void 0 : _a.data),
255 type: 'error',
256 ...error.options,
257 data: (_c = (_b = error.options) === null || _b === void 0 ? void 0 : _b.data) !== null && _c !== void 0 ? _c : reason
258 });
259 });
260 return id;
261 }
262 Notification.promise = promise;
263 /**
264 * Helper function to emit a success notification.
265 *
266 * #### Notes
267 * The message will be truncated if longer than 140 characters.
268 *
269 * @param message Notification message
270 * @param options Options for the success notification
271 * @returns Notification unique id
272 */
273 function success(message, options = {}) {
274 return Notification.manager.notify(message, 'success', options);
275 }
276 Notification.success = success;
277 /**
278 * Helper function to update a notification.
279 *
280 * If the notification does not exists, nothing will happen.
281 *
282 * Once updated the notification will be moved at the begin
283 * of the notification stack.
284 *
285 * #### Notes
286 * The message will be truncated if longer than 140 characters.
287 *
288 * @param args Update options
289 * @returns Whether the update was successful or not.
290 */
291 function update(args) {
292 return Notification.manager.update(args);
293 }
294 Notification.update = update;
295 /**
296 * Helper function to emit a warning notification.
297 *
298 * #### Notes
299 * The message will be truncated if longer than 140 characters.
300 *
301 * @param message Notification message
302 * @param options Options for the warning notification
303 * @returns Notification unique id
304 */
305 function warning(message, options = {}) {
306 return Notification.manager.notify(message, 'warning', options);
307 }
308 Notification.warning = warning;
309})(Notification || (Notification = {}));
310//# sourceMappingURL=notification.js.map
\No newline at end of file