UNPKG

10.6 kBTypeScriptView Raw
1import { ReadonlyJSONValue } from '@lumino/coreutils';
2import { IDisposable } from '@lumino/disposable';
3import { ISignal } from '@lumino/signaling';
4/**
5 * Notification manager
6 */
7export declare class NotificationManager implements IDisposable {
8 constructor();
9 /**
10 * Signal emitted whenever a notification changes.
11 */
12 get changed(): ISignal<NotificationManager, Notification.IChange>;
13 /**
14 * Total number of notifications.
15 */
16 get count(): number;
17 /**
18 * Whether the manager is disposed or not.
19 */
20 get isDisposed(): boolean;
21 /**
22 * The list of notifications.
23 */
24 get notifications(): Notification.INotification[];
25 /**
26 * Dismiss one notification (specified by its id) or all if no id provided.
27 *
28 * @param id Notification id
29 */
30 dismiss(id?: string): void;
31 /**
32 * Dispose the manager.
33 */
34 dispose(): void;
35 /**
36 * Test whether a notification exists or not.
37 *
38 * @param id Notification id
39 * @returns Notification status
40 */
41 has(id: string): boolean;
42 /**
43 * Add a new notification.
44 *
45 * This will trigger the `changed` signal with an `added` event.
46 *
47 * @param message Notification message
48 * @param type Notification type
49 * @param options Notification option
50 * @returns Notification unique id
51 */
52 notify<T extends ReadonlyJSONValue = ReadonlyJSONValue>(message: string, type: Notification.TypeOptions, options: Notification.IOptions<T>): string;
53 /**
54 * Update an existing notification.
55 *
56 * If the notification does not exists this won't do anything.
57 *
58 * Once updated the notification will be moved at the begin
59 * of the notification stack.
60 *
61 * @param args Update options
62 * @returns Whether the update was successful or not.
63 */
64 update<T extends ReadonlyJSONValue = ReadonlyJSONValue>(args: Notification.IUpdate<T>): boolean;
65 private _isDisposed;
66 private _changed;
67 private _queue;
68}
69/**
70 * Notification namespace
71 */
72export declare namespace Notification {
73 /**
74 * Enumeration of available action display type.
75 */
76 type ActionDisplayType = 'default' | 'accent' | 'warn' | 'link';
77 /**
78 * Interface describing an action linked to a notification.
79 */
80 interface IAction {
81 /**
82 * The action label.
83 *
84 * This should be a short description.
85 */
86 label: string;
87 /**
88 * Callback function to trigger
89 *
90 * ### Notes
91 * By default execution of the callback will close the toast
92 * and dismiss the notification. You can prevent this by calling
93 * `event.preventDefault()` in the callback.
94 */
95 callback: (event: MouseEvent) => void;
96 /**
97 * The action caption.
98 *
99 * This can be a longer description of the action.
100 */
101 caption?: string;
102 /**
103 * The action display type.
104 *
105 * This will be used to modify the action button style.
106 */
107 displayType?: ActionDisplayType;
108 }
109 /**
110 * Notification interface
111 */
112 interface INotification<T extends ReadonlyJSONValue = ReadonlyJSONValue> {
113 /**
114 * Notification unique identifier
115 */
116 id: string;
117 /**
118 * Notification message
119 *
120 * #### Notes
121 * The message will be truncated if longer than 140 characters.
122 */
123 message: string;
124 /**
125 * Notification creation date
126 */
127 createdAt: number;
128 /**
129 * Notification modification date
130 */
131 modifiedAt: number;
132 /**
133 * Notification type
134 */
135 type: TypeOptions;
136 /**
137 * Notification options
138 */
139 options: IOptions<T>;
140 }
141 /**
142 * Notification change interface
143 */
144 interface IChange {
145 /**
146 * Change type
147 */
148 type: 'added' | 'removed' | 'updated';
149 /**
150 * Notification that changed
151 */
152 notification: INotification;
153 }
154 /**
155 * Notification options
156 */
157 interface IOptions<T extends ReadonlyJSONValue> {
158 /**
159 * Autoclosing behavior - false (not closing automatically)
160 * or number (time in milliseconds before hiding the notification)
161 *
162 * Set to zero if you want the notification to be retained in the notification
163 * center but not displayed as toast. This is the default behavior.
164 */
165 autoClose?: number | false;
166 /**
167 * List of associated actions
168 */
169 actions?: Array<IAction>;
170 /**
171 * Data associated with a notification
172 */
173 data?: T;
174 /**
175 * Task progression
176 *
177 * ### Notes
178 * This should be a number between 0 (not started) and 1 (completed).
179 */
180 progress?: number;
181 }
182 /**
183 * Parameters for notification depending on a promise.
184 */
185 interface IPromiseOptions<Pending extends ReadonlyJSONValue, Success extends ReadonlyJSONValue = Pending, Error extends ReadonlyJSONValue = Pending> {
186 /**
187 * Promise pending message and options
188 *
189 * #### Notes
190 * The message will be truncated if longer than 140 characters.
191 */
192 pending: {
193 message: string;
194 options?: IOptions<Pending>;
195 };
196 /**
197 * Message when promise resolves and options
198 *
199 * The message factory receives as first argument the result
200 * of the promise and as second the success `options.data`.
201 *
202 * #### Notes
203 * The message will be truncated if longer than 140 characters.
204 */
205 success: {
206 message: (result: unknown, data?: Success) => string;
207 options?: IOptions<Success>;
208 };
209 /**
210 * Message when promise rejects and options
211 *
212 * The message factory receives as first argument the error
213 * of the promise and as second the error `options.data`.
214 *
215 * #### Notes
216 * The message will be truncated if longer than 140 characters.
217 */
218 error: {
219 message: (reason: unknown, data?: Error) => string;
220 options?: IOptions<Error>;
221 };
222 }
223 /**
224 * Type of notifications
225 */
226 type TypeOptions = 'info' | 'in-progress' | 'success' | 'warning' | 'error' | 'default';
227 /**
228 * Options for updating a notification
229 */
230 interface IUpdate<T extends ReadonlyJSONValue> extends IOptions<T> {
231 /**
232 * Notification unique id
233 */
234 id: string;
235 /**
236 * New notification message
237 */
238 message?: string;
239 /**
240 * New notification type
241 */
242 type?: TypeOptions;
243 }
244 /**
245 * The global notification manager.
246 */
247 const manager: NotificationManager;
248 /**
249 * Dismiss one notification (specified by its id) or all if no id provided
250 *
251 * @param id notification id
252 */
253 function dismiss(id?: string): void;
254 /**
255 * Helper function to emit a notification.
256 *
257 * #### Notes
258 * The message will be truncated if longer than 140 characters.
259 *
260 * @param message Notification message
261 * @param type Notification type
262 * @param options Options for the error notification
263 * @returns Notification unique id
264 */
265 function emit<T extends ReadonlyJSONValue = ReadonlyJSONValue>(message: string, type?: TypeOptions, options?: IOptions<T>): string;
266 /**
267 * Helper function to emit an error notification.
268 *
269 * #### Notes
270 * The message will be truncated if longer than 140 characters.
271 *
272 * @param message Notification message
273 * @param options Options for the error notification
274 * @returns Notification unique id
275 */
276 function error<T extends ReadonlyJSONValue = ReadonlyJSONValue>(message: string, options?: IOptions<T>): string;
277 /**
278 * Helper function to emit an info notification.
279 *
280 * #### Notes
281 * The message will be truncated if longer than 140 characters.
282 *
283 * @param message Notification message
284 * @param options Options for the info notification
285 * @returns Notification unique id
286 */
287 function info<T extends ReadonlyJSONValue = ReadonlyJSONValue>(message: string, options?: IOptions<T>): string;
288 /**
289 * Helper function to show an in-progress notification.
290 *
291 * #### Notes
292 * The message will be truncated if longer than 140 characters.
293 *
294 * @param promise Promise to wait for
295 * @param options Options for the in-progress notification
296 * @returns Notification unique id
297 */
298 function promise<Pending extends ReadonlyJSONValue = ReadonlyJSONValue, Success extends ReadonlyJSONValue = Pending, Error extends ReadonlyJSONValue = Pending>(promise: Promise<Success>, options: IPromiseOptions<Pending, Success, Error>): string;
299 /**
300 * Helper function to emit a success notification.
301 *
302 * #### Notes
303 * The message will be truncated if longer than 140 characters.
304 *
305 * @param message Notification message
306 * @param options Options for the success notification
307 * @returns Notification unique id
308 */
309 function success<T extends ReadonlyJSONValue = ReadonlyJSONValue>(message: string, options?: IOptions<T>): string;
310 /**
311 * Helper function to update a notification.
312 *
313 * If the notification does not exists, nothing will happen.
314 *
315 * Once updated the notification will be moved at the begin
316 * of the notification stack.
317 *
318 * #### Notes
319 * The message will be truncated if longer than 140 characters.
320 *
321 * @param args Update options
322 * @returns Whether the update was successful or not.
323 */
324 function update<T extends ReadonlyJSONValue = ReadonlyJSONValue>(args: IUpdate<T>): boolean;
325 /**
326 * Helper function to emit a warning notification.
327 *
328 * #### Notes
329 * The message will be truncated if longer than 140 characters.
330 *
331 * @param message Notification message
332 * @param options Options for the warning notification
333 * @returns Notification unique id
334 */
335 function warning<T extends ReadonlyJSONValue = ReadonlyJSONValue>(message: string, options?: IOptions<T>): string;
336}