UNPKG

19 kBTypeScriptView Raw
1import { IIterator } from '@phosphor/algorithm';
2import { IObservableDisposable } from '@phosphor/disposable';
3import { ConflatableMessage, IMessageHandler, Message } from '@phosphor/messaging';
4import { ISignal } from '@phosphor/signaling';
5import { Layout } from './layout';
6import { Title } from './title';
7/**
8 * The base class of the Phosphor 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 parent of the widget.
84 */
85 /**
86 * Set the parent of the widget.
87 *
88 * #### Notes
89 * Children are typically added to a widget by using a layout, which
90 * means user code will not normally set the parent widget directly.
91 *
92 * The widget will be automatically removed from its old parent.
93 *
94 * This is a no-op if there is no effective parent change.
95 */
96 parent: Widget | null;
97 /**
98 * Get the layout for the widget.
99 */
100 /**
101 * Set the layout for the widget.
102 *
103 * #### Notes
104 * The layout is single-use only. It cannot be changed after the
105 * first assignment.
106 *
107 * The layout is disposed automatically when the widget is disposed.
108 */
109 layout: Layout | null;
110 /**
111 * Create an iterator over the widget's children.
112 *
113 * @returns A new iterator over the children of the widget.
114 *
115 * #### Notes
116 * The widget must have a populated layout in order to have children.
117 *
118 * If a layout is not installed, the returned iterator will be empty.
119 */
120 children(): IIterator<Widget>;
121 /**
122 * Test whether a widget is a descendant of this widget.
123 *
124 * @param widget - The descendant widget of interest.
125 *
126 * @returns `true` if the widget is a descendant, `false` otherwise.
127 */
128 contains(widget: Widget): boolean;
129 /**
130 * Test whether the widget's DOM node has the given class name.
131 *
132 * @param name - The class name of interest.
133 *
134 * @returns `true` if the node has the class, `false` otherwise.
135 */
136 hasClass(name: string): boolean;
137 /**
138 * Add a class name to the widget's DOM node.
139 *
140 * @param name - The class name to add to the node.
141 *
142 * #### Notes
143 * If the class name is already added to the node, this is a no-op.
144 *
145 * The class name must not contain whitespace.
146 */
147 addClass(name: string): void;
148 /**
149 * Remove a class name from the widget's DOM node.
150 *
151 * @param name - The class name to remove from the node.
152 *
153 * #### Notes
154 * If the class name is not yet added to the node, this is a no-op.
155 *
156 * The class name must not contain whitespace.
157 */
158 removeClass(name: string): void;
159 /**
160 * Toggle a class name on the widget's DOM node.
161 *
162 * @param name - The class name to toggle on the node.
163 *
164 * @param force - Whether to force add the class (`true`) or force
165 * remove the class (`false`). If not provided, the presence of
166 * the class will be toggled from its current state.
167 *
168 * @returns `true` if the class is now present, `false` otherwise.
169 *
170 * #### Notes
171 * The class name must not contain whitespace.
172 */
173 toggleClass(name: string, force?: boolean): boolean;
174 /**
175 * Post an `'update-request'` message to the widget.
176 *
177 * #### Notes
178 * This is a simple convenience method for posting the message.
179 */
180 update(): void;
181 /**
182 * Post a `'fit-request'` message to the widget.
183 *
184 * #### Notes
185 * This is a simple convenience method for posting the message.
186 */
187 fit(): void;
188 /**
189 * Post an `'activate-request'` message to the widget.
190 *
191 * #### Notes
192 * This is a simple convenience method for posting the message.
193 */
194 activate(): void;
195 /**
196 * Send a `'close-request'` message to the widget.
197 *
198 * #### Notes
199 * This is a simple convenience method for sending the message.
200 */
201 close(): void;
202 /**
203 * Show the widget and make it visible to its parent widget.
204 *
205 * #### Notes
206 * This causes the [[isHidden]] property to be `false`.
207 *
208 * If the widget is not explicitly hidden, this is a no-op.
209 */
210 show(): void;
211 /**
212 * Hide the widget and make it hidden to its parent widget.
213 *
214 * #### Notes
215 * This causes the [[isHidden]] property to be `true`.
216 *
217 * If the widget is explicitly hidden, this is a no-op.
218 */
219 hide(): void;
220 /**
221 * Show or hide the widget according to a boolean value.
222 *
223 * @param hidden - `true` to hide the widget, or `false` to show it.
224 *
225 * #### Notes
226 * This is a convenience method for `hide()` and `show()`.
227 */
228 setHidden(hidden: boolean): void;
229 /**
230 * Test whether the given widget flag is set.
231 *
232 * #### Notes
233 * This will not typically be called directly by user code.
234 */
235 testFlag(flag: Widget.Flag): boolean;
236 /**
237 * Set the given widget flag.
238 *
239 * #### Notes
240 * This will not typically be called directly by user code.
241 */
242 setFlag(flag: Widget.Flag): void;
243 /**
244 * Clear the given widget flag.
245 *
246 * #### Notes
247 * This will not typically be called directly by user code.
248 */
249 clearFlag(flag: Widget.Flag): void;
250 /**
251 * Process a message sent to the widget.
252 *
253 * @param msg - The message sent to the widget.
254 *
255 * #### Notes
256 * Subclasses may reimplement this method as needed.
257 */
258 processMessage(msg: Message): void;
259 /**
260 * Invoke the message processing routine of the widget's layout.
261 *
262 * @param msg - The message to dispatch to the layout.
263 *
264 * #### Notes
265 * This is a no-op if the widget does not have a layout.
266 *
267 * This will not typically be called directly by user code.
268 */
269 protected notifyLayout(msg: Message): void;
270 /**
271 * A message handler invoked on a `'close-request'` message.
272 *
273 * #### Notes
274 * The default implementation unparents or detaches the widget.
275 */
276 protected onCloseRequest(msg: Message): void;
277 /**
278 * A message handler invoked on a `'resize'` message.
279 *
280 * #### Notes
281 * The default implementation of this handler is a no-op.
282 */
283 protected onResize(msg: Widget.ResizeMessage): void;
284 /**
285 * A message handler invoked on an `'update-request'` message.
286 *
287 * #### Notes
288 * The default implementation of this handler is a no-op.
289 */
290 protected onUpdateRequest(msg: Message): void;
291 /**
292 * A message handler invoked on a `'fit-request'` message.
293 *
294 * #### Notes
295 * The default implementation of this handler is a no-op.
296 */
297 protected onFitRequest(msg: Message): void;
298 /**
299 * A message handler invoked on an `'activate-request'` message.
300 *
301 * #### Notes
302 * The default implementation of this handler is a no-op.
303 */
304 protected onActivateRequest(msg: Message): void;
305 /**
306 * A message handler invoked on a `'before-show'` message.
307 *
308 * #### Notes
309 * The default implementation of this handler is a no-op.
310 */
311 protected onBeforeShow(msg: Message): void;
312 /**
313 * A message handler invoked on an `'after-show'` message.
314 *
315 * #### Notes
316 * The default implementation of this handler is a no-op.
317 */
318 protected onAfterShow(msg: Message): void;
319 /**
320 * A message handler invoked on a `'before-hide'` message.
321 *
322 * #### Notes
323 * The default implementation of this handler is a no-op.
324 */
325 protected onBeforeHide(msg: Message): void;
326 /**
327 * A message handler invoked on an `'after-hide'` message.
328 *
329 * #### Notes
330 * The default implementation of this handler is a no-op.
331 */
332 protected onAfterHide(msg: Message): void;
333 /**
334 * A message handler invoked on a `'before-attach'` message.
335 *
336 * #### Notes
337 * The default implementation of this handler is a no-op.
338 */
339 protected onBeforeAttach(msg: Message): void;
340 /**
341 * A message handler invoked on an `'after-attach'` message.
342 *
343 * #### Notes
344 * The default implementation of this handler is a no-op.
345 */
346 protected onAfterAttach(msg: Message): void;
347 /**
348 * A message handler invoked on a `'before-detach'` message.
349 *
350 * #### Notes
351 * The default implementation of this handler is a no-op.
352 */
353 protected onBeforeDetach(msg: Message): void;
354 /**
355 * A message handler invoked on an `'after-detach'` message.
356 *
357 * #### Notes
358 * The default implementation of this handler is a no-op.
359 */
360 protected onAfterDetach(msg: Message): void;
361 /**
362 * A message handler invoked on a `'child-added'` message.
363 *
364 * #### Notes
365 * The default implementation of this handler is a no-op.
366 */
367 protected onChildAdded(msg: Widget.ChildMessage): void;
368 /**
369 * A message handler invoked on a `'child-removed'` message.
370 *
371 * #### Notes
372 * The default implementation of this handler is a no-op.
373 */
374 protected onChildRemoved(msg: Widget.ChildMessage): void;
375 private _flags;
376 private _layout;
377 private _parent;
378 private _disposed;
379}
380/**
381 * The namespace for the `Widget` class statics.
382 */
383export declare namespace Widget {
384 /**
385 * An options object for initializing a widget.
386 */
387 interface IOptions {
388 /**
389 * The optional node to use for the widget.
390 *
391 * If a node is provided, the widget will assume full ownership
392 * and control of the node, as if it had created the node itself.
393 *
394 * The default is a new `<div>`.
395 */
396 node?: HTMLElement;
397 }
398 /**
399 * An enum of widget bit flags.
400 */
401 enum Flag {
402 /**
403 * The widget has been disposed.
404 */
405 IsDisposed = 1,
406 /**
407 * The widget is attached to the DOM.
408 */
409 IsAttached = 2,
410 /**
411 * The widget is hidden.
412 */
413 IsHidden = 4,
414 /**
415 * The widget is visible.
416 */
417 IsVisible = 8,
418 /**
419 * A layout cannot be set on the widget.
420 */
421 DisallowLayout = 16
422 }
423 /**
424 * A collection of stateless messages related to widgets.
425 */
426 namespace Msg {
427 /**
428 * A singleton `'before-show'` message.
429 *
430 * #### Notes
431 * This message is sent to a widget before it becomes visible.
432 *
433 * This message is **not** sent when the widget is being attached.
434 */
435 const BeforeShow: Message;
436 /**
437 * A singleton `'after-show'` message.
438 *
439 * #### Notes
440 * This message is sent to a widget after it becomes visible.
441 *
442 * This message is **not** sent when the widget is being attached.
443 */
444 const AfterShow: Message;
445 /**
446 * A singleton `'before-hide'` message.
447 *
448 * #### Notes
449 * This message is sent to a widget before it becomes not-visible.
450 *
451 * This message is **not** sent when the widget is being detached.
452 */
453 const BeforeHide: Message;
454 /**
455 * A singleton `'after-hide'` message.
456 *
457 * #### Notes
458 * This message is sent to a widget after it becomes not-visible.
459 *
460 * This message is **not** sent when the widget is being detached.
461 */
462 const AfterHide: Message;
463 /**
464 * A singleton `'before-attach'` message.
465 *
466 * #### Notes
467 * This message is sent to a widget before it is attached.
468 */
469 const BeforeAttach: Message;
470 /**
471 * A singleton `'after-attach'` message.
472 *
473 * #### Notes
474 * This message is sent to a widget after it is attached.
475 */
476 const AfterAttach: Message;
477 /**
478 * A singleton `'before-detach'` message.
479 *
480 * #### Notes
481 * This message is sent to a widget before it is detached.
482 */
483 const BeforeDetach: Message;
484 /**
485 * A singleton `'after-detach'` message.
486 *
487 * #### Notes
488 * This message is sent to a widget after it is detached.
489 */
490 const AfterDetach: Message;
491 /**
492 * A singleton `'parent-changed'` message.
493 *
494 * #### Notes
495 * This message is sent to a widget when its parent has changed.
496 */
497 const ParentChanged: Message;
498 /**
499 * A singleton conflatable `'update-request'` message.
500 *
501 * #### Notes
502 * This message can be dispatched to supporting widgets in order to
503 * update their content based on the current widget state. Not all
504 * widgets will respond to messages of this type.
505 *
506 * For widgets with a layout, this message will inform the layout to
507 * update the position and size of its child widgets.
508 */
509 const UpdateRequest: ConflatableMessage;
510 /**
511 * A singleton conflatable `'fit-request'` message.
512 *
513 * #### Notes
514 * For widgets with a layout, this message will inform the layout to
515 * recalculate its size constraints to fit the space requirements of
516 * its child widgets, and to update their position and size. Not all
517 * layouts will respond to messages of this type.
518 */
519 const FitRequest: ConflatableMessage;
520 /**
521 * A singleton conflatable `'activate-request'` message.
522 *
523 * #### Notes
524 * This message should be dispatched to a widget when it should
525 * perform the actions necessary to activate the widget, which
526 * may include focusing its node or descendant node.
527 */
528 const ActivateRequest: ConflatableMessage;
529 /**
530 * A singleton conflatable `'close-request'` message.
531 *
532 * #### Notes
533 * This message should be dispatched to a widget when it should close
534 * and remove itself from the widget hierarchy.
535 */
536 const CloseRequest: ConflatableMessage;
537 }
538 /**
539 * A message class for child related messages.
540 */
541 class ChildMessage extends Message {
542 /**
543 * Construct a new child message.
544 *
545 * @param type - The message type.
546 *
547 * @param child - The child widget for the message.
548 */
549 constructor(type: string, child: Widget);
550 /**
551 * The child widget for the message.
552 */
553 readonly child: Widget;
554 }
555 /**
556 * A message class for `'resize'` messages.
557 */
558 class ResizeMessage extends Message {
559 /**
560 * Construct a new resize message.
561 *
562 * @param width - The **offset width** of the widget, or `-1` if
563 * the width is not known.
564 *
565 * @param height - The **offset height** of the widget, or `-1` if
566 * the height is not known.
567 */
568 constructor(width: number, height: number);
569 /**
570 * The offset width of the widget.
571 *
572 * #### Notes
573 * This will be `-1` if the width is unknown.
574 */
575 readonly width: number;
576 /**
577 * The offset height of the widget.
578 *
579 * #### Notes
580 * This will be `-1` if the height is unknown.
581 */
582 readonly height: number;
583 }
584 /**
585 * The namespace for the `ResizeMessage` class statics.
586 */
587 namespace ResizeMessage {
588 /**
589 * A singleton `'resize'` message with an unknown size.
590 */
591 const UnknownSize: ResizeMessage;
592 }
593 /**
594 * Attach a widget to a host DOM node.
595 *
596 * @param widget - The widget of interest.
597 *
598 * @param host - The DOM node to use as the widget's host.
599 *
600 * @param ref - The child of `host` to use as the reference element.
601 * If this is provided, the widget will be inserted before this
602 * node in the host. The default is `null`, which will cause the
603 * widget to be added as the last child of the host.
604 *
605 * #### Notes
606 * This will throw an error if the widget is not a root widget, if
607 * the widget is already attached, or if the host is not attached
608 * to the DOM.
609 */
610 function attach(widget: Widget, host: HTMLElement, ref?: HTMLElement | null): void;
611 /**
612 * Detach the widget from its host DOM node.
613 *
614 * @param widget - The widget of interest.
615 *
616 * #### Notes
617 * This will throw an error if the widget is not a root widget,
618 * or if the widget is not attached to the DOM.
619 */
620 function detach(widget: Widget): void;
621}