UNPKG

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