UNPKG

20.5 kBTypeScriptView Raw
1import { IIterator } from '@lumino/algorithm';
2import { IObservableDisposable } from '@lumino/disposable';
3import { ConflatableMessage, IMessageHandler, Message } from '@lumino/messaging';
4import { ISignal } from '@lumino/signaling';
5import { Layout } from './layout';
6import { Title } from './title';
7/**
8 * The base class of the lumino widget hierarchy.
9 *
10 * #### Notes
11 * This class will typically be subclassed in order to create a useful
12 * widget. However, it can be used directly to host externally created
13 * content.
14 */
15export declare class Widget implements IMessageHandler, IObservableDisposable {
16 /**
17 * Construct a new widget.
18 *
19 * @param options - The options for initializing the widget.
20 */
21 constructor(options?: Widget.IOptions);
22 /**
23 * Dispose of the widget and its descendant widgets.
24 *
25 * #### Notes
26 * It is unsafe to use the widget after it has been disposed.
27 *
28 * All calls made to this method after the first are a no-op.
29 */
30 dispose(): void;
31 /**
32 * A signal emitted when the widget is disposed.
33 */
34 readonly disposed: ISignal<this, void>;
35 /**
36 * Get the DOM node owned by the widget.
37 */
38 readonly node: HTMLElement;
39 /**
40 * Test whether the widget has been disposed.
41 */
42 readonly isDisposed: boolean;
43 /**
44 * Test whether the widget's node is attached to the DOM.
45 */
46 readonly isAttached: boolean;
47 /**
48 * Test whether the widget is explicitly hidden.
49 */
50 readonly isHidden: boolean;
51 /**
52 * Test whether the widget is visible.
53 *
54 * #### Notes
55 * A widget is visible when it is attached to the DOM, is not
56 * explicitly hidden, and has no explicitly hidden ancestors.
57 */
58 readonly isVisible: boolean;
59 /**
60 * The title object for the widget.
61 *
62 * #### Notes
63 * The title object is used by some container widgets when displaying
64 * the widget alongside some title, such as a tab panel or side bar.
65 *
66 * Since not all widgets will use the title, it is created on demand.
67 *
68 * The `owner` property of the title is set to this widget.
69 */
70 readonly title: Title<Widget>;
71 /**
72 * Get the id of the widget's DOM node.
73 */
74 /**
75 * Set the id of the widget's DOM node.
76 */
77 id: string;
78 /**
79 * The dataset for the widget's DOM node.
80 */
81 readonly dataset: DOMStringMap;
82 /**
83 * Get the method for hiding the widget.
84 */
85 /**
86 * Set the method for hiding the widget.
87 */
88 hiddenMode: Widget.HiddenMode;
89 /**
90 * Get the parent of the widget.
91 */
92 /**
93 * Set the parent of the widget.
94 *
95 * #### Notes
96 * Children are typically added to a widget by using a layout, which
97 * means user code will not normally set the parent widget directly.
98 *
99 * The widget will be automatically removed from its old parent.
100 *
101 * This is a no-op if there is no effective parent change.
102 */
103 parent: Widget | null;
104 /**
105 * Get the layout for the widget.
106 */
107 /**
108 * Set the layout for the widget.
109 *
110 * #### Notes
111 * The layout is single-use only. It cannot be changed after the
112 * first assignment.
113 *
114 * The layout is disposed automatically when the widget is disposed.
115 */
116 layout: Layout | null;
117 /**
118 * Create an iterator over the widget's children.
119 *
120 * @returns A new iterator over the children of the widget.
121 *
122 * #### Notes
123 * The widget must have a populated layout in order to have children.
124 *
125 * If a layout is not installed, the returned iterator will be empty.
126 */
127 children(): IIterator<Widget>;
128 /**
129 * Test whether a widget is a descendant of this widget.
130 *
131 * @param widget - The descendant widget of interest.
132 *
133 * @returns `true` if the widget is a descendant, `false` otherwise.
134 */
135 contains(widget: Widget): boolean;
136 /**
137 * Test whether the widget's DOM node has the given class name.
138 *
139 * @param name - The class name of interest.
140 *
141 * @returns `true` if the node has the class, `false` otherwise.
142 */
143 hasClass(name: string): boolean;
144 /**
145 * Add a class name to the widget's DOM node.
146 *
147 * @param name - The class name to add to the node.
148 *
149 * #### Notes
150 * If the class name is already added to the node, this is a no-op.
151 *
152 * The class name must not contain whitespace.
153 */
154 addClass(name: string): void;
155 /**
156 * Remove a class name from the widget's DOM node.
157 *
158 * @param name - The class name to remove from the node.
159 *
160 * #### Notes
161 * If the class name is not yet added to the node, this is a no-op.
162 *
163 * The class name must not contain whitespace.
164 */
165 removeClass(name: string): void;
166 /**
167 * Toggle a class name on the widget's DOM node.
168 *
169 * @param name - The class name to toggle on the node.
170 *
171 * @param force - Whether to force add the class (`true`) or force
172 * remove the class (`false`). If not provided, the presence of
173 * the class will be toggled from its current state.
174 *
175 * @returns `true` if the class is now present, `false` otherwise.
176 *
177 * #### Notes
178 * The class name must not contain whitespace.
179 */
180 toggleClass(name: string, force?: boolean): boolean;
181 /**
182 * Post an `'update-request'` message to the widget.
183 *
184 * #### Notes
185 * This is a simple convenience method for posting the message.
186 */
187 update(): void;
188 /**
189 * Post a `'fit-request'` message to the widget.
190 *
191 * #### Notes
192 * This is a simple convenience method for posting the message.
193 */
194 fit(): void;
195 /**
196 * Post an `'activate-request'` message to the widget.
197 *
198 * #### Notes
199 * This is a simple convenience method for posting the message.
200 */
201 activate(): void;
202 /**
203 * Send a `'close-request'` message to the widget.
204 *
205 * #### Notes
206 * This is a simple convenience method for sending the message.
207 */
208 close(): void;
209 /**
210 * Show the widget and make it visible to its parent widget.
211 *
212 * #### Notes
213 * This causes the [[isHidden]] property to be `false`.
214 *
215 * If the widget is not explicitly hidden, this is a no-op.
216 */
217 show(): void;
218 /**
219 * Hide the widget and make it hidden to its parent widget.
220 *
221 * #### Notes
222 * This causes the [[isHidden]] property to be `true`.
223 *
224 * If the widget is explicitly hidden, this is a no-op.
225 */
226 hide(): void;
227 /**
228 * Show or hide the widget according to a boolean value.
229 *
230 * @param hidden - `true` to hide the widget, or `false` to show it.
231 *
232 * #### Notes
233 * This is a convenience method for `hide()` and `show()`.
234 */
235 setHidden(hidden: boolean): void;
236 /**
237 * Test whether the given widget flag is set.
238 *
239 * #### Notes
240 * This will not typically be called directly by user code.
241 */
242 testFlag(flag: Widget.Flag): boolean;
243 /**
244 * Set the given widget flag.
245 *
246 * #### Notes
247 * This will not typically be called directly by user code.
248 */
249 setFlag(flag: Widget.Flag): void;
250 /**
251 * Clear the given widget flag.
252 *
253 * #### Notes
254 * This will not typically be called directly by user code.
255 */
256 clearFlag(flag: Widget.Flag): void;
257 /**
258 * Process a message sent to the widget.
259 *
260 * @param msg - The message sent to the widget.
261 *
262 * #### Notes
263 * Subclasses may reimplement this method as needed.
264 */
265 processMessage(msg: Message): void;
266 /**
267 * Invoke the message processing routine of the widget's layout.
268 *
269 * @param msg - The message to dispatch to the layout.
270 *
271 * #### Notes
272 * This is a no-op if the widget does not have a layout.
273 *
274 * This will not typically be called directly by user code.
275 */
276 protected notifyLayout(msg: Message): void;
277 /**
278 * A message handler invoked on a `'close-request'` message.
279 *
280 * #### Notes
281 * The default implementation unparents or detaches the widget.
282 */
283 protected onCloseRequest(msg: Message): void;
284 /**
285 * A message handler invoked on a `'resize'` message.
286 *
287 * #### Notes
288 * The default implementation of this handler is a no-op.
289 */
290 protected onResize(msg: Widget.ResizeMessage): void;
291 /**
292 * A message handler invoked on an `'update-request'` message.
293 *
294 * #### Notes
295 * The default implementation of this handler is a no-op.
296 */
297 protected onUpdateRequest(msg: Message): void;
298 /**
299 * A message handler invoked on a `'fit-request'` message.
300 *
301 * #### Notes
302 * The default implementation of this handler is a no-op.
303 */
304 protected onFitRequest(msg: Message): void;
305 /**
306 * A message handler invoked on an `'activate-request'` message.
307 *
308 * #### Notes
309 * The default implementation of this handler is a no-op.
310 */
311 protected onActivateRequest(msg: Message): void;
312 /**
313 * A message handler invoked on a `'before-show'` message.
314 *
315 * #### Notes
316 * The default implementation of this handler is a no-op.
317 */
318 protected onBeforeShow(msg: Message): void;
319 /**
320 * A message handler invoked on an `'after-show'` message.
321 *
322 * #### Notes
323 * The default implementation of this handler is a no-op.
324 */
325 protected onAfterShow(msg: Message): void;
326 /**
327 * A message handler invoked on a `'before-hide'` message.
328 *
329 * #### Notes
330 * The default implementation of this handler is a no-op.
331 */
332 protected onBeforeHide(msg: Message): void;
333 /**
334 * A message handler invoked on an `'after-hide'` message.
335 *
336 * #### Notes
337 * The default implementation of this handler is a no-op.
338 */
339 protected onAfterHide(msg: Message): void;
340 /**
341 * A message handler invoked on a `'before-attach'` message.
342 *
343 * #### Notes
344 * The default implementation of this handler is a no-op.
345 */
346 protected onBeforeAttach(msg: Message): void;
347 /**
348 * A message handler invoked on an `'after-attach'` message.
349 *
350 * #### Notes
351 * The default implementation of this handler is a no-op.
352 */
353 protected onAfterAttach(msg: Message): void;
354 /**
355 * A message handler invoked on a `'before-detach'` message.
356 *
357 * #### Notes
358 * The default implementation of this handler is a no-op.
359 */
360 protected onBeforeDetach(msg: Message): void;
361 /**
362 * A message handler invoked on an `'after-detach'` message.
363 *
364 * #### Notes
365 * The default implementation of this handler is a no-op.
366 */
367 protected onAfterDetach(msg: Message): void;
368 /**
369 * A message handler invoked on a `'child-added'` message.
370 *
371 * #### Notes
372 * The default implementation of this handler is a no-op.
373 */
374 protected onChildAdded(msg: Widget.ChildMessage): void;
375 /**
376 * A message handler invoked on a `'child-removed'` message.
377 *
378 * #### Notes
379 * The default implementation of this handler is a no-op.
380 */
381 protected onChildRemoved(msg: Widget.ChildMessage): void;
382 private _flags;
383 private _layout;
384 private _parent;
385 private _disposed;
386 private _hiddenMode;
387}
388/**
389 * The namespace for the `Widget` class statics.
390 */
391export declare namespace Widget {
392 /**
393 * An options object for initializing a widget.
394 */
395 interface IOptions {
396 /**
397 * The optional node to use for the widget.
398 *
399 * If a node is provided, the widget will assume full ownership
400 * and control of the node, as if it had created the node itself.
401 *
402 * The default is a new `<div>`.
403 */
404 node?: HTMLElement;
405 /**
406 * The optional element tag, used for constructing the widget's node.
407 *
408 * If a pre-constructed node is provided via the `node` arg, this
409 * value is ignored.
410 */
411 tag?: keyof HTMLElementTagNameMap;
412 }
413 /**
414 * The method for hiding the widget.
415 *
416 * The default is Display.
417 *
418 * Using `Scale` will often increase performance as most browsers will not
419 * trigger style computation for the `transform` action. This should be used
420 * sparingly and tested, since increasing the number of composition layers
421 * may slow things down.
422 *
423 * To ensure the transformation does not trigger style recomputation, you
424 * may need to set the widget CSS style `will-change: transform`. This
425 * should be used only when needed as it may overwhelm the browser with a
426 * high number of layers. See
427 * https://developer.mozilla.org/en-US/docs/Web/CSS/will-change
428 */
429 enum HiddenMode {
430 /**
431 * Set a `lm-mod-hidden` CSS class to hide the widget using `display:none`
432 * CSS from the standard Lumino CSS.
433 */
434 Display = 0,
435 /**
436 * Hide the widget by setting the `transform` to `'scale(0)'`.
437 */
438 Scale = 1
439 }
440 /**
441 * An enum of widget bit flags.
442 */
443 enum Flag {
444 /**
445 * The widget has been disposed.
446 */
447 IsDisposed = 1,
448 /**
449 * The widget is attached to the DOM.
450 */
451 IsAttached = 2,
452 /**
453 * The widget is hidden.
454 */
455 IsHidden = 4,
456 /**
457 * The widget is visible.
458 */
459 IsVisible = 8,
460 /**
461 * A layout cannot be set on the widget.
462 */
463 DisallowLayout = 16
464 }
465 /**
466 * A collection of stateless messages related to widgets.
467 */
468 namespace Msg {
469 /**
470 * A singleton `'before-show'` message.
471 *
472 * #### Notes
473 * This message is sent to a widget before it becomes visible.
474 *
475 * This message is **not** sent when the widget is being attached.
476 */
477 const BeforeShow: Message;
478 /**
479 * A singleton `'after-show'` message.
480 *
481 * #### Notes
482 * This message is sent to a widget after it becomes visible.
483 *
484 * This message is **not** sent when the widget is being attached.
485 */
486 const AfterShow: Message;
487 /**
488 * A singleton `'before-hide'` message.
489 *
490 * #### Notes
491 * This message is sent to a widget before it becomes not-visible.
492 *
493 * This message is **not** sent when the widget is being detached.
494 */
495 const BeforeHide: Message;
496 /**
497 * A singleton `'after-hide'` message.
498 *
499 * #### Notes
500 * This message is sent to a widget after it becomes not-visible.
501 *
502 * This message is **not** sent when the widget is being detached.
503 */
504 const AfterHide: Message;
505 /**
506 * A singleton `'before-attach'` message.
507 *
508 * #### Notes
509 * This message is sent to a widget before it is attached.
510 */
511 const BeforeAttach: Message;
512 /**
513 * A singleton `'after-attach'` message.
514 *
515 * #### Notes
516 * This message is sent to a widget after it is attached.
517 */
518 const AfterAttach: Message;
519 /**
520 * A singleton `'before-detach'` message.
521 *
522 * #### Notes
523 * This message is sent to a widget before it is detached.
524 */
525 const BeforeDetach: Message;
526 /**
527 * A singleton `'after-detach'` message.
528 *
529 * #### Notes
530 * This message is sent to a widget after it is detached.
531 */
532 const AfterDetach: Message;
533 /**
534 * A singleton `'parent-changed'` message.
535 *
536 * #### Notes
537 * This message is sent to a widget when its parent has changed.
538 */
539 const ParentChanged: Message;
540 /**
541 * A singleton conflatable `'update-request'` message.
542 *
543 * #### Notes
544 * This message can be dispatched to supporting widgets in order to
545 * update their content based on the current widget state. Not all
546 * widgets will respond to messages of this type.
547 *
548 * For widgets with a layout, this message will inform the layout to
549 * update the position and size of its child widgets.
550 */
551 const UpdateRequest: ConflatableMessage;
552 /**
553 * A singleton conflatable `'fit-request'` message.
554 *
555 * #### Notes
556 * For widgets with a layout, this message will inform the layout to
557 * recalculate its size constraints to fit the space requirements of
558 * its child widgets, and to update their position and size. Not all
559 * layouts will respond to messages of this type.
560 */
561 const FitRequest: ConflatableMessage;
562 /**
563 * A singleton conflatable `'activate-request'` message.
564 *
565 * #### Notes
566 * This message should be dispatched to a widget when it should
567 * perform the actions necessary to activate the widget, which
568 * may include focusing its node or descendant node.
569 */
570 const ActivateRequest: ConflatableMessage;
571 /**
572 * A singleton conflatable `'close-request'` message.
573 *
574 * #### Notes
575 * This message should be dispatched to a widget when it should close
576 * and remove itself from the widget hierarchy.
577 */
578 const CloseRequest: ConflatableMessage;
579 }
580 /**
581 * A message class for child related messages.
582 */
583 class ChildMessage extends Message {
584 /**
585 * Construct a new child message.
586 *
587 * @param type - The message type.
588 *
589 * @param child - The child widget for the message.
590 */
591 constructor(type: string, child: Widget);
592 /**
593 * The child widget for the message.
594 */
595 readonly child: Widget;
596 }
597 /**
598 * A message class for `'resize'` messages.
599 */
600 class ResizeMessage extends Message {
601 /**
602 * Construct a new resize message.
603 *
604 * @param width - The **offset width** of the widget, or `-1` if
605 * the width is not known.
606 *
607 * @param height - The **offset height** of the widget, or `-1` if
608 * the height is not known.
609 */
610 constructor(width: number, height: number);
611 /**
612 * The offset width of the widget.
613 *
614 * #### Notes
615 * This will be `-1` if the width is unknown.
616 */
617 readonly width: number;
618 /**
619 * The offset height of the widget.
620 *
621 * #### Notes
622 * This will be `-1` if the height is unknown.
623 */
624 readonly height: number;
625 }
626 /**
627 * The namespace for the `ResizeMessage` class statics.
628 */
629 namespace ResizeMessage {
630 /**
631 * A singleton `'resize'` message with an unknown size.
632 */
633 const UnknownSize: ResizeMessage;
634 }
635 /**
636 * Attach a widget to a host DOM node.
637 *
638 * @param widget - The widget of interest.
639 *
640 * @param host - The DOM node to use as the widget's host.
641 *
642 * @param ref - The child of `host` to use as the reference element.
643 * If this is provided, the widget will be inserted before this
644 * node in the host. The default is `null`, which will cause the
645 * widget to be added as the last child of the host.
646 *
647 * #### Notes
648 * This will throw an error if the widget is not a root widget, if
649 * the widget is already attached, or if the host is not attached
650 * to the DOM.
651 */
652 function attach(widget: Widget, host: HTMLElement, ref?: HTMLElement | null): void;
653 /**
654 * Detach the widget from its host DOM node.
655 *
656 * @param widget - The widget of interest.
657 *
658 * #### Notes
659 * This will throw an error if the widget is not a root widget,
660 * or if the widget is not attached to the DOM.
661 */
662 function detach(widget: Widget): void;
663}
664//# sourceMappingURL=widget.d.ts.map
\No newline at end of file