UNPKG

13.5 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 `'click'` event for a dialog button.
90 *
91 * @param event - The DOM event sent to the widget
92 */
93 protected _evtClick(event: MouseEvent): void;
94 /**
95 * Handle the `'keydown'` event for the widget.
96 *
97 * @param event - The DOM event sent to the widget
98 */
99 protected _evtKeydown(event: KeyboardEvent): void;
100 /**
101 * Handle the `'focus'` event for the widget.
102 *
103 * @param event - The DOM event sent to the widget
104 */
105 protected _evtFocus(event: FocusEvent): void;
106 /**
107 * Handle the `'mousedown'` event for the widget.
108 *
109 * @param event - The DOM event sent to the widget
110 */
111 protected _evtMouseDown(event: MouseEvent): void;
112 /**
113 * Resolve a button item.
114 */
115 private _resolve;
116 private _ready;
117 private _buttonNodes;
118 private _buttons;
119 private _checkboxNode;
120 private _original;
121 private _first;
122 private _primary;
123 private _promise;
124 private _defaultButton;
125 private _host;
126 private _hasClose;
127 private _body;
128 private _lastMouseDownInDialog;
129 private _focusNodeSelector;
130 private _bodyWidget;
131}
132/**
133 * The namespace for Dialog class statics.
134 */
135export declare namespace Dialog {
136 /**
137 * Translator object.
138 */
139 let translator: ITranslator;
140 /**
141 * The body input types.
142 */
143 type Body<T> = IBodyWidget<T> | React.ReactElement<any> | string;
144 /**
145 * The header input types.
146 */
147 type Header = React.ReactElement<any> | string;
148 /**
149 * A widget used as a dialog body.
150 */
151 interface IBodyWidget<T = string> extends Widget {
152 /**
153 * Get the serialized value of the widget.
154 */
155 getValue?(): T;
156 }
157 /**
158 * The options used to make a button item.
159 */
160 interface IButton {
161 /**
162 * The aria label for the button.
163 */
164 ariaLabel: string;
165 /**
166 * The label for the button.
167 */
168 label: string;
169 /**
170 * The icon class for the button.
171 */
172 iconClass: string;
173 /**
174 * The icon label for the button.
175 */
176 iconLabel: string;
177 /**
178 * The caption for the button.
179 */
180 caption: string;
181 /**
182 * The extra class name for the button.
183 */
184 className: string;
185 /**
186 * The dialog action to perform when the button is clicked.
187 */
188 accept: boolean;
189 /**
190 * The additional dialog actions to perform when the button is clicked.
191 */
192 actions: Array<string>;
193 /**
194 * The button display type.
195 */
196 displayType: 'default' | 'warn';
197 }
198 /**
199 * The options used to make a checkbox item.
200 */
201 interface ICheckbox {
202 /**
203 * The label for the checkbox.
204 */
205 label: string;
206 /**
207 * The caption for the checkbox.
208 */
209 caption: string;
210 /**
211 * The initial checkbox state.
212 */
213 checked: boolean;
214 /**
215 * The extra class name for the checkbox.
216 */
217 className: string;
218 }
219 /**
220 * Error object interface
221 */
222 interface IError {
223 /**
224 * Error message
225 */
226 message: string | React.ReactElement<any>;
227 }
228 /**
229 * The options used to create a dialog.
230 */
231 interface IOptions<T> {
232 /**
233 * The top level text for the dialog. Defaults to an empty string.
234 */
235 title: Header;
236 /**
237 * The main body element for the dialog or a message to display.
238 * Defaults to an empty string.
239 *
240 * #### Notes
241 * If a widget is given as the body, it will be disposed after the
242 * dialog is resolved. If the widget has a `getValue()` method,
243 * the method will be called prior to disposal and the value
244 * will be provided as part of the dialog result.
245 * A string argument will be used as raw `textContent`.
246 * All `input` and `select` nodes will be wrapped and styled.
247 */
248 body: Body<T>;
249 /**
250 * The host element for the dialog. Defaults to `document.body`.
251 */
252 host: HTMLElement;
253 /**
254 * The buttons to display. Defaults to cancel and accept buttons.
255 */
256 buttons: ReadonlyArray<IButton>;
257 /**
258 * The checkbox to display in the footer. Defaults no checkbox.
259 */
260 checkbox: Partial<ICheckbox> | null;
261 /**
262 * The index of the default button. Defaults to the last button.
263 */
264 defaultButton: number;
265 /**
266 * A selector for the primary element that should take focus in the dialog.
267 * Defaults to an empty string, causing the [[defaultButton]] to take
268 * focus.
269 */
270 focusNodeSelector: string;
271 /**
272 * When "false", disallows user from dismissing the dialog by clicking outside it
273 * or pressing escape. Defaults to "true", which renders a close button.
274 */
275 hasClose: boolean;
276 /**
277 * An optional renderer for dialog items. Defaults to a shared
278 * default renderer.
279 */
280 renderer: IRenderer;
281 }
282 /**
283 * A dialog renderer.
284 */
285 interface IRenderer {
286 /**
287 * Create the header of the dialog.
288 *
289 * @param title - The title of the dialog.
290 *
291 * @returns A widget for the dialog header.
292 */
293 createHeader<T>(title: Header, reject: () => void, options: Partial<Dialog.IOptions<T>>): Widget;
294 /**
295 * Create the body of the dialog.
296 *
297 * @param body - The input value for the body.
298 *
299 * @returns A widget for the body.
300 */
301 createBody(body: Body<any>): Widget;
302 /**
303 * Create the footer of the dialog.
304 *
305 * @param buttons - The button nodes to add to the footer.
306 * @param checkbox - The checkbox node to add to the footer.
307 *
308 * @returns A widget for the footer.
309 */
310 createFooter(buttons: ReadonlyArray<HTMLElement>, checkbox: HTMLElement | null): Widget;
311 /**
312 * Create a button node for the dialog.
313 *
314 * @param button - The button data.
315 *
316 * @returns A node for the button.
317 */
318 createButtonNode(button: IButton): HTMLElement;
319 /**
320 * Create a checkbox node for the dialog.
321 *
322 * @param checkbox - The checkbox data.
323 *
324 * @returns A node for the checkbox.
325 */
326 createCheckboxNode(checkbox: ICheckbox): HTMLElement;
327 }
328 /**
329 * The result of a dialog.
330 */
331 interface IResult<T> {
332 /**
333 * The button that was pressed.
334 */
335 button: IButton;
336 /**
337 * State of the dialog checkbox.
338 *
339 * #### Notes
340 * It will be null if no checkbox is defined for the dialog.
341 */
342 isChecked: boolean | null;
343 /**
344 * The value retrieved from `.getValue()` if given on the widget.
345 */
346 value: T | null;
347 }
348 /**
349 * Create a button item.
350 */
351 function createButton(value: Partial<IButton>): Readonly<IButton>;
352 /**
353 * Create a reject button.
354 */
355 function cancelButton(options?: Partial<IButton>): Readonly<IButton>;
356 /**
357 * Create an accept button.
358 */
359 function okButton(options?: Partial<IButton>): Readonly<IButton>;
360 /**
361 * Create a warn button.
362 */
363 function warnButton(options?: Partial<IButton>): Readonly<IButton>;
364 /**
365 * Disposes all dialog instances.
366 *
367 * #### Notes
368 * This function should only be used in tests or cases where application state
369 * may be discarded.
370 */
371 function flush(): void;
372 /**
373 * The default implementation of a dialog renderer.
374 */
375 class Renderer {
376 /**
377 * Create the header of the dialog.
378 *
379 * @param title - The title of the dialog.
380 *
381 * @returns A widget for the dialog header.
382 */
383 createHeader<T>(title: Header, reject?: () => void, options?: Partial<Dialog.IOptions<T>>): Widget;
384 /**
385 * Create the body of the dialog.
386 *
387 * @param value - The input value for the body.
388 *
389 * @returns A widget for the body.
390 */
391 createBody(value: Body<any>): Widget;
392 /**
393 * Create the footer of the dialog.
394 *
395 * @param buttons - The buttons nodes to add to the footer.
396 * @param checkbox - The checkbox node to add to the footer.
397 *
398 * @returns A widget for the footer.
399 */
400 createFooter(buttons: ReadonlyArray<HTMLElement>, checkbox: HTMLElement | null): Widget;
401 /**
402 * Create a button node for the dialog.
403 *
404 * @param button - The button data.
405 *
406 * @returns A node for the button.
407 */
408 createButtonNode(button: IButton): HTMLElement;
409 /**
410 * Create a checkbox node for the dialog.
411 *
412 * @param checkbox - The checkbox data.
413 *
414 * @returns A node for the checkbox.
415 */
416 createCheckboxNode(checkbox: ICheckbox): HTMLElement;
417 /**
418 * Create the class name for the button.
419 *
420 * @param data - The data to use for the class name.
421 *
422 * @returns The full class name for the button.
423 */
424 createItemClass(data: IButton): string;
425 /**
426 * Render an icon element for a dialog item.
427 *
428 * @param data - The data to use for rendering the icon.
429 *
430 * @returns An HTML element representing the icon.
431 */
432 renderIcon(data: IButton): HTMLElement;
433 /**
434 * Create the class name for the button icon.
435 *
436 * @param data - The data to use for the class name.
437 *
438 * @returns The full class name for the item icon.
439 */
440 createIconClass(data: IButton): string;
441 /**
442 * Render the label element for a button.
443 *
444 * @param data - The data to use for rendering the label.
445 *
446 * @returns An HTML element representing the item label.
447 */
448 renderLabel(data: IButton): HTMLElement;
449 }
450 /**
451 * The default renderer instance.
452 */
453 const defaultRenderer: Renderer;
454 /**
455 * The dialog widget tracker.
456 */
457 const tracker: WidgetTracker<Dialog<any>>;
458}
459
\No newline at end of file