UNPKG

13.8 kBTypeScriptView Raw
1import { ITranslator } from '@jupyterlab/translation';
2import { Message } from '@lumino/messaging';
3import { Widget } from '@lumino/widgets';
4import * as React from 'react';
5import { WidgetTracker } from './widgettracker';
6/**
7 * Create and show a dialog.
8 *
9 * @param options - The dialog setup options.
10 *
11 * @returns A promise that resolves with whether the dialog was accepted.
12 */
13export declare function showDialog<T>(options?: Partial<Dialog.IOptions<T>>): Promise<Dialog.IResult<T>>;
14/**
15 * Show an error message dialog.
16 *
17 * @param title - The title of the dialog box.
18 *
19 * @param error - the error to show in the dialog body (either a string
20 * or an object with a string `message` property).
21 */
22export declare function showErrorMessage(title: string, error: string | Dialog.IError, buttons?: ReadonlyArray<Dialog.IButton>): Promise<void>;
23/**
24 * A modal dialog widget.
25 */
26export declare class Dialog<T> extends Widget {
27 /**
28 * Create a dialog panel instance.
29 *
30 * @param options - The dialog setup options.
31 */
32 constructor(options?: Partial<Dialog.IOptions<T>>);
33 /**
34 * A promise that resolves when the Dialog first rendering is done.
35 */
36 get ready(): Promise<void>;
37 /**
38 * Dispose of the resources used by the dialog.
39 */
40 dispose(): void;
41 /**
42 * Launch the dialog as a modal window.
43 *
44 * @returns a promise that resolves with the result of the dialog.
45 */
46 launch(): Promise<Dialog.IResult<T>>;
47 /**
48 * Resolve the current dialog.
49 *
50 * @param index - An optional index to the button to resolve.
51 *
52 * #### Notes
53 * Will default to the defaultIndex.
54 * Will resolve the current `show()` with the button value.
55 * Will be a no-op if the dialog is not shown.
56 */
57 resolve(index?: number): void;
58 /**
59 * Reject the current dialog with a default reject value.
60 *
61 * #### Notes
62 * Will be a no-op if the dialog is not shown.
63 */
64 reject(): void;
65 /**
66 * Handle the DOM events for the directory listing.
67 *
68 * @param event - The DOM event sent to the widget.
69 *
70 * #### Notes
71 * This method implements the DOM `EventListener` interface and is
72 * called in response to events on the panel's DOM node. It should
73 * not be called directly by user code.
74 */
75 handleEvent(event: Event): void;
76 /**
77 * A message handler invoked on an `'after-attach'` message.
78 */
79 protected onAfterAttach(msg: Message): void;
80 /**
81 * A message handler invoked on an `'after-detach'` message.
82 */
83 protected onAfterDetach(msg: Message): void;
84 /**
85 * A message handler invoked on a `'close-request'` message.
86 */
87 protected onCloseRequest(msg: Message): void;
88 /**
89 * Handle the `'input'` event for dialog's children.
90 *
91 * @param event - The DOM event sent to the widget
92 */
93 protected _evtInput(_event: InputEvent): void;
94 /**
95 * Handle the `'click'` event for a dialog button.
96 *
97 * @param event - The DOM event sent to the widget
98 */
99 protected _evtClick(event: MouseEvent): void;
100 /**
101 * Handle the `'keydown'` event for the widget.
102 *
103 * @param event - The DOM event sent to the widget
104 */
105 protected _evtKeydown(event: KeyboardEvent): void;
106 /**
107 * Handle the `'focus'` event for the widget.
108 *
109 * @param event - The DOM event sent to the widget
110 */
111 protected _evtFocus(event: FocusEvent): void;
112 /**
113 * Handle the `'mousedown'` event for the widget.
114 *
115 * @param event - The DOM event sent to the widget
116 */
117 protected _evtMouseDown(event: MouseEvent): void;
118 /**
119 * Resolve a button item.
120 */
121 private _resolve;
122 private _hasValidationErrors;
123 private _ready;
124 private _buttonNodes;
125 private _buttons;
126 private _checkboxNode;
127 private _original;
128 private _first;
129 private _primary;
130 private _promise;
131 private _defaultButton;
132 private _host;
133 private _hasClose;
134 private _body;
135 private _lastMouseDownInDialog;
136 private _focusNodeSelector;
137 private _bodyWidget;
138}
139/**
140 * The namespace for Dialog class statics.
141 */
142export declare namespace Dialog {
143 /**
144 * Translator object.
145 */
146 let translator: ITranslator;
147 /**
148 * The body input types.
149 */
150 type Body<T> = IBodyWidget<T> | React.ReactElement<any> | string;
151 /**
152 * The header input types.
153 */
154 type Header = React.ReactElement<any> | string;
155 /**
156 * A widget used as a dialog body.
157 */
158 interface IBodyWidget<T = string> extends Widget {
159 /**
160 * Get the serialized value of the widget.
161 */
162 getValue?(): T;
163 }
164 /**
165 * The options used to make a button item.
166 */
167 interface IButton {
168 /**
169 * The aria label for the button.
170 */
171 ariaLabel: string;
172 /**
173 * The label for the button.
174 */
175 label: string;
176 /**
177 * The icon class for the button.
178 */
179 iconClass: string;
180 /**
181 * The icon label for the button.
182 */
183 iconLabel: string;
184 /**
185 * The caption for the button.
186 */
187 caption: string;
188 /**
189 * The extra class name for the button.
190 */
191 className: string;
192 /**
193 * The dialog action to perform when the button is clicked.
194 */
195 accept: boolean;
196 /**
197 * The additional dialog actions to perform when the button is clicked.
198 */
199 actions: Array<string>;
200 /**
201 * The button display type.
202 */
203 displayType: 'default' | 'warn';
204 }
205 /**
206 * The options used to make a checkbox item.
207 */
208 interface ICheckbox {
209 /**
210 * The label for the checkbox.
211 */
212 label: string;
213 /**
214 * The caption for the checkbox.
215 */
216 caption: string;
217 /**
218 * The initial checkbox state.
219 */
220 checked: boolean;
221 /**
222 * The extra class name for the checkbox.
223 */
224 className: string;
225 }
226 /**
227 * Error object interface
228 */
229 interface IError {
230 /**
231 * Error message
232 */
233 message: string | React.ReactElement<any>;
234 }
235 /**
236 * The options used to create a dialog.
237 */
238 interface IOptions<T> {
239 /**
240 * The top level text for the dialog. Defaults to an empty string.
241 */
242 title: Header;
243 /**
244 * The main body element for the dialog or a message to display.
245 * Defaults to an empty string.
246 *
247 * #### Notes
248 * If a widget is given as the body, it will be disposed after the
249 * dialog is resolved. If the widget has a `getValue()` method,
250 * the method will be called prior to disposal and the value
251 * will be provided as part of the dialog result.
252 * A string argument will be used as raw `textContent`.
253 * All `input` and `select` nodes will be wrapped and styled.
254 */
255 body: Body<T>;
256 /**
257 * The host element for the dialog. Defaults to `document.body`.
258 */
259 host: HTMLElement;
260 /**
261 * The buttons to display. Defaults to cancel and accept buttons.
262 */
263 buttons: ReadonlyArray<IButton>;
264 /**
265 * The checkbox to display in the footer. Defaults no checkbox.
266 */
267 checkbox: Partial<ICheckbox> | null;
268 /**
269 * The index of the default button. Defaults to the last button.
270 */
271 defaultButton: number;
272 /**
273 * A selector for the primary element that should take focus in the dialog.
274 * Defaults to an empty string, causing the [[defaultButton]] to take
275 * focus.
276 */
277 focusNodeSelector: string;
278 /**
279 * When "false", disallows user from dismissing the dialog by clicking outside it
280 * or pressing escape. Defaults to "true", which renders a close button.
281 */
282 hasClose: boolean;
283 /**
284 * An optional renderer for dialog items. Defaults to a shared
285 * default renderer.
286 */
287 renderer: IRenderer;
288 }
289 /**
290 * A dialog renderer.
291 */
292 interface IRenderer {
293 /**
294 * Create the header of the dialog.
295 *
296 * @param title - The title of the dialog.
297 *
298 * @returns A widget for the dialog header.
299 */
300 createHeader<T>(title: Header, reject: () => void, options: Partial<Dialog.IOptions<T>>): Widget;
301 /**
302 * Create the body of the dialog.
303 *
304 * @param body - The input value for the body.
305 *
306 * @returns A widget for the body.
307 */
308 createBody(body: Body<any>): Widget;
309 /**
310 * Create the footer of the dialog.
311 *
312 * @param buttons - The button nodes to add to the footer.
313 * @param checkbox - The checkbox node to add to the footer.
314 *
315 * @returns A widget for the footer.
316 */
317 createFooter(buttons: ReadonlyArray<HTMLElement>, checkbox: HTMLElement | null): Widget;
318 /**
319 * Create a button node for the dialog.
320 *
321 * @param button - The button data.
322 *
323 * @returns A node for the button.
324 */
325 createButtonNode(button: IButton): HTMLButtonElement;
326 /**
327 * Create a checkbox node for the dialog.
328 *
329 * @param checkbox - The checkbox data.
330 *
331 * @returns A node for the checkbox.
332 */
333 createCheckboxNode(checkbox: ICheckbox): HTMLElement;
334 }
335 /**
336 * The result of a dialog.
337 */
338 interface IResult<T> {
339 /**
340 * The button that was pressed.
341 */
342 button: IButton;
343 /**
344 * State of the dialog checkbox.
345 *
346 * #### Notes
347 * It will be null if no checkbox is defined for the dialog.
348 */
349 isChecked: boolean | null;
350 /**
351 * The value retrieved from `.getValue()` if given on the widget.
352 */
353 value: T | null;
354 }
355 /**
356 * Create a button item.
357 */
358 function createButton(value: Partial<IButton>): Readonly<IButton>;
359 /**
360 * Create a reject button.
361 */
362 function cancelButton(options?: Partial<IButton>): Readonly<IButton>;
363 /**
364 * Create an accept button.
365 */
366 function okButton(options?: Partial<IButton>): Readonly<IButton>;
367 /**
368 * Create a warn button.
369 */
370 function warnButton(options?: Partial<IButton>): Readonly<IButton>;
371 /**
372 * Disposes all dialog instances.
373 *
374 * #### Notes
375 * This function should only be used in tests or cases where application state
376 * may be discarded.
377 */
378 function flush(): void;
379 /**
380 * The default implementation of a dialog renderer.
381 */
382 class Renderer {
383 /**
384 * Create the header of the dialog.
385 *
386 * @param title - The title of the dialog.
387 *
388 * @returns A widget for the dialog header.
389 */
390 createHeader<T>(title: Header, reject?: () => void, options?: Partial<Dialog.IOptions<T>>): Widget;
391 /**
392 * Create the body of the dialog.
393 *
394 * @param value - The input value for the body.
395 *
396 * @returns A widget for the body.
397 */
398 createBody(value: Body<any>): Widget;
399 /**
400 * Create the footer of the dialog.
401 *
402 * @param buttons - The buttons nodes to add to the footer.
403 * @param checkbox - The checkbox node to add to the footer.
404 *
405 * @returns A widget for the footer.
406 */
407 createFooter(buttons: ReadonlyArray<HTMLElement>, checkbox: HTMLElement | null): Widget;
408 /**
409 * Create a button node for the dialog.
410 *
411 * @param button - The button data.
412 *
413 * @returns A node for the button.
414 */
415 createButtonNode(button: IButton): HTMLButtonElement;
416 /**
417 * Create a checkbox node for the dialog.
418 *
419 * @param checkbox - The checkbox data.
420 *
421 * @returns A node for the checkbox.
422 */
423 createCheckboxNode(checkbox: ICheckbox): HTMLElement;
424 /**
425 * Create the class name for the button.
426 *
427 * @param data - The data to use for the class name.
428 *
429 * @returns The full class name for the button.
430 */
431 createItemClass(data: IButton): string;
432 /**
433 * Render an icon element for a dialog item.
434 *
435 * @param data - The data to use for rendering the icon.
436 *
437 * @returns An HTML element representing the icon.
438 */
439 renderIcon(data: IButton): HTMLElement;
440 /**
441 * Create the class name for the button icon.
442 *
443 * @param data - The data to use for the class name.
444 *
445 * @returns The full class name for the item icon.
446 */
447 createIconClass(data: IButton): string;
448 /**
449 * Render the label element for a button.
450 *
451 * @param data - The data to use for rendering the label.
452 *
453 * @returns An HTML element representing the item label.
454 */
455 renderLabel(data: IButton): HTMLElement;
456 }
457 /**
458 * The default renderer instance.
459 */
460 const defaultRenderer: Renderer;
461 /**
462 * The dialog widget tracker.
463 */
464 const tracker: WidgetTracker<Dialog<any>>;
465}
466
\No newline at end of file