This repository has been archived by the owner on Nov 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathwidget.ts
1055 lines (963 loc) · 26.2 KB
/
widget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2017, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import {
IIterator, empty
} from '@phosphor/algorithm';
import {
IObservableDisposable
} from '@phosphor/disposable';
import {
ConflatableMessage, IMessageHandler, Message, MessageLoop
} from '@phosphor/messaging';
import {
AttachedProperty
} from '@phosphor/properties';
import {
ISignal, Signal
} from '@phosphor/signaling';
import {
Layout
} from './layout';
import {
Title
} from './title';
/**
* The base class of the Phosphor widget hierarchy.
*
* #### Notes
* This class will typically be subclassed in order to create a useful
* widget. However, it can be used directly to host externally created
* content.
*/
export
class Widget implements IMessageHandler, IObservableDisposable {
/**
* Construct a new widget.
*
* @param options - The options for initializing the widget.
*/
constructor(options: Widget.IOptions = {}) {
this.node = Private.createNode(options);
this.addClass('p-Widget');
}
/**
* Dispose of the widget and its descendant widgets.
*
* #### Notes
* It is unsafe to use the widget after it has been disposed.
*
* All calls made to this method after the first are a no-op.
*/
dispose(): void {
// Do nothing if the widget is already disposed.
if (this.isDisposed) {
return;
}
// Set the disposed flag and emit the disposed signal.
this.setFlag(Widget.Flag.IsDisposed);
this._disposed.emit(undefined);
// Remove or detach the widget if necessary.
if (this.parent) {
this.parent = null;
} else if (this.isAttached) {
Widget.detach(this);
}
// Dispose of the widget layout.
if (this._layout) {
this._layout.dispose();
this._layout = null;
}
// Clear the extra data associated with the widget.
Signal.clearData(this);
MessageLoop.clearData(this);
AttachedProperty.clearData(this);
}
/**
* A signal emitted when the widget is disposed.
*/
get disposed(): ISignal<this, void> {
return this._disposed;
}
/**
* Get the DOM node owned by the widget.
*/
readonly node: HTMLElement;
/**
* Test whether the widget has been disposed.
*/
get isDisposed(): boolean {
return this.testFlag(Widget.Flag.IsDisposed);
}
/**
* Test whether the widget's node is attached to the DOM.
*/
get isAttached(): boolean {
return this.testFlag(Widget.Flag.IsAttached);
}
/**
* Test whether the widget is explicitly hidden.
*/
get isHidden(): boolean {
return this.testFlag(Widget.Flag.IsHidden);
}
/**
* Test whether the widget is visible.
*
* #### Notes
* A widget is visible when it is attached to the DOM, is not
* explicitly hidden, and has no explicitly hidden ancestors.
*/
get isVisible(): boolean {
return this.testFlag(Widget.Flag.IsVisible);
}
/**
* The title object for the widget.
*
* #### Notes
* The title object is used by some container widgets when displaying
* the widget alongside some title, such as a tab panel or side bar.
*
* Since not all widgets will use the title, it is created on demand.
*
* The `owner` property of the title is set to this widget.
*/
get title(): Title<Widget> {
return Private.titleProperty.get(this);
}
/**
* Get the id of the widget's DOM node.
*/
get id(): string {
return this.node.id;
}
/**
* Set the id of the widget's DOM node.
*/
set id(value: string) {
this.node.id = value;
}
/**
* The dataset for the widget's DOM node.
*/
get dataset(): DOMStringMap {
return this.node.dataset;
}
/**
* Get the parent of the widget.
*/
get parent(): Widget | null {
return this._parent;
}
/**
* Set the parent of the widget.
*
* #### Notes
* Children are typically added to a widget by using a layout, which
* means user code will not normally set the parent widget directly.
*
* The widget will be automatically removed from its old parent.
*
* This is a no-op if there is no effective parent change.
*/
set parent(value: Widget | null) {
if (this._parent === value) {
return;
}
if (value && this.contains(value)) {
throw new Error('Invalid parent widget.');
}
if (this._parent && !this._parent.isDisposed) {
let msg = new Widget.ChildMessage('child-removed', this);
MessageLoop.sendMessage(this._parent, msg);
}
this._parent = value;
if (this._parent && !this._parent.isDisposed) {
let msg = new Widget.ChildMessage('child-added', this);
MessageLoop.sendMessage(this._parent, msg);
}
if (!this.isDisposed) {
MessageLoop.sendMessage(this, Widget.Msg.ParentChanged);
}
}
/**
* Get the layout for the widget.
*/
get layout(): Layout | null {
return this._layout;
}
/**
* Set the layout for the widget.
*
* #### Notes
* The layout is single-use only. It cannot be changed after the
* first assignment.
*
* The layout is disposed automatically when the widget is disposed.
*/
set layout(value: Layout | null) {
if (this._layout === value) {
return;
}
if (this.testFlag(Widget.Flag.DisallowLayout)) {
throw new Error('Cannot set widget layout.');
}
if (this._layout) {
throw new Error('Cannot change widget layout.');
}
if (value!.parent) {
throw new Error('Cannot change layout parent.');
}
this._layout = value;
value!.parent = this;
}
/**
* Create an iterator over the widget's children.
*
* @returns A new iterator over the children of the widget.
*
* #### Notes
* The widget must have a populated layout in order to have children.
*
* If a layout is not installed, the returned iterator will be empty.
*/
children(): IIterator<Widget> {
return this._layout ? this._layout.iter() : empty<Widget>();
}
/**
* Test whether a widget is a descendant of this widget.
*
* @param widget - The descendant widget of interest.
*
* @returns `true` if the widget is a descendant, `false` otherwise.
*/
contains(widget: Widget): boolean {
for (let value: Widget | null = widget; value; value = value._parent) {
if (value === this) {
return true;
}
}
return false;
}
/**
* Test whether the widget's DOM node has the given class name.
*
* @param name - The class name of interest.
*
* @returns `true` if the node has the class, `false` otherwise.
*/
hasClass(name: string): boolean {
return this.node.classList.contains(name);
}
/**
* Add a class name to the widget's DOM node.
*
* @param name - The class name to add to the node.
*
* #### Notes
* If the class name is already added to the node, this is a no-op.
*
* The class name must not contain whitespace.
*/
addClass(name: string): void {
this.node.classList.add(name);
}
/**
* Remove a class name from the widget's DOM node.
*
* @param name - The class name to remove from the node.
*
* #### Notes
* If the class name is not yet added to the node, this is a no-op.
*
* The class name must not contain whitespace.
*/
removeClass(name: string): void {
this.node.classList.remove(name);
}
/**
* Toggle a class name on the widget's DOM node.
*
* @param name - The class name to toggle on the node.
*
* @param force - Whether to force add the class (`true`) or force
* remove the class (`false`). If not provided, the presence of
* the class will be toggled from its current state.
*
* @returns `true` if the class is now present, `false` otherwise.
*
* #### Notes
* The class name must not contain whitespace.
*/
toggleClass(name: string, force?: boolean): boolean {
if (force === true) {
this.node.classList.add(name);
return true;
}
if (force === false) {
this.node.classList.remove(name);
return false;
}
return this.node.classList.toggle(name);
}
/**
* Post an `'update-request'` message to the widget.
*
* #### Notes
* This is a simple convenience method for posting the message.
*/
update(): void {
MessageLoop.postMessage(this, Widget.Msg.UpdateRequest);
}
/**
* Post a `'fit-request'` message to the widget.
*
* #### Notes
* This is a simple convenience method for posting the message.
*/
fit(): void {
MessageLoop.postMessage(this, Widget.Msg.FitRequest);
}
/**
* Post an `'activate-request'` message to the widget.
*
* #### Notes
* This is a simple convenience method for posting the message.
*/
activate(): void {
MessageLoop.postMessage(this, Widget.Msg.ActivateRequest);
}
/**
* Send a `'close-request'` message to the widget.
*
* #### Notes
* This is a simple convenience method for sending the message.
*/
close(): void {
MessageLoop.sendMessage(this, Widget.Msg.CloseRequest);
}
/**
* Show the widget and make it visible to its parent widget.
*
* #### Notes
* This causes the [[isHidden]] property to be `false`.
*
* If the widget is not explicitly hidden, this is a no-op.
*/
show(): void {
if (!this.testFlag(Widget.Flag.IsHidden)) {
return;
}
if (this.isAttached && (!this.parent || this.parent.isVisible)) {
MessageLoop.sendMessage(this, Widget.Msg.BeforeShow);
}
this.clearFlag(Widget.Flag.IsHidden);
this.removeClass('p-mod-hidden');
if (this.isAttached && (!this.parent || this.parent.isVisible)) {
MessageLoop.sendMessage(this, Widget.Msg.AfterShow);
}
if (this.parent) {
let msg = new Widget.ChildMessage('child-shown', this);
MessageLoop.sendMessage(this.parent, msg);
}
}
/**
* Hide the widget and make it hidden to its parent widget.
*
* #### Notes
* This causes the [[isHidden]] property to be `true`.
*
* If the widget is explicitly hidden, this is a no-op.
*/
hide(): void {
if (this.testFlag(Widget.Flag.IsHidden)) {
return;
}
if (this.isAttached && (!this.parent || this.parent.isVisible)) {
MessageLoop.sendMessage(this, Widget.Msg.BeforeHide);
}
this.setFlag(Widget.Flag.IsHidden);
this.addClass('p-mod-hidden');
if (this.isAttached && (!this.parent || this.parent.isVisible)) {
MessageLoop.sendMessage(this, Widget.Msg.AfterHide);
}
if (this.parent) {
let msg = new Widget.ChildMessage('child-hidden', this);
MessageLoop.sendMessage(this.parent, msg);
}
}
/**
* Show or hide the widget according to a boolean value.
*
* @param hidden - `true` to hide the widget, or `false` to show it.
*
* #### Notes
* This is a convenience method for `hide()` and `show()`.
*/
setHidden(hidden: boolean): void {
if (hidden) {
this.hide();
} else {
this.show();
}
}
/**
* Test whether the given widget flag is set.
*
* #### Notes
* This will not typically be called directly by user code.
*/
testFlag(flag: Widget.Flag): boolean {
return (this._flags & flag) !== 0;
}
/**
* Set the given widget flag.
*
* #### Notes
* This will not typically be called directly by user code.
*/
setFlag(flag: Widget.Flag): void {
this._flags |= flag;
}
/**
* Clear the given widget flag.
*
* #### Notes
* This will not typically be called directly by user code.
*/
clearFlag(flag: Widget.Flag): void {
this._flags &= ~flag;
}
/**
* Process a message sent to the widget.
*
* @param msg - The message sent to the widget.
*
* #### Notes
* Subclasses may reimplement this method as needed.
*/
processMessage(msg: Message): void {
switch (msg.type) {
case 'resize':
this.notifyLayout(msg);
this.onResize(msg as Widget.ResizeMessage);
break;
case 'update-request':
this.notifyLayout(msg);
this.onUpdateRequest(msg);
break;
case 'fit-request':
this.notifyLayout(msg);
this.onFitRequest(msg);
break;
case 'before-show':
this.notifyLayout(msg);
this.onBeforeShow(msg);
break;
case 'after-show':
this.setFlag(Widget.Flag.IsVisible);
this.notifyLayout(msg);
this.onAfterShow(msg);
break;
case 'before-hide':
this.notifyLayout(msg);
this.onBeforeHide(msg);
break;
case 'after-hide':
this.clearFlag(Widget.Flag.IsVisible);
this.notifyLayout(msg);
this.onAfterHide(msg);
break;
case 'before-attach':
this.notifyLayout(msg);
this.onBeforeAttach(msg);
break;
case 'after-attach':
if (!this.isHidden && (!this.parent || this.parent.isVisible)) {
this.setFlag(Widget.Flag.IsVisible);
}
this.setFlag(Widget.Flag.IsAttached);
this.notifyLayout(msg);
this.onAfterAttach(msg);
break;
case 'before-detach':
this.notifyLayout(msg);
this.onBeforeDetach(msg);
break;
case 'after-detach':
this.clearFlag(Widget.Flag.IsVisible);
this.clearFlag(Widget.Flag.IsAttached);
this.notifyLayout(msg);
this.onAfterDetach(msg);
break;
case 'activate-request':
this.notifyLayout(msg);
this.onActivateRequest(msg);
break;
case 'close-request':
this.notifyLayout(msg);
this.onCloseRequest(msg);
break;
case 'child-added':
this.notifyLayout(msg);
this.onChildAdded(msg as Widget.ChildMessage);
break;
case 'child-removed':
this.notifyLayout(msg);
this.onChildRemoved(msg as Widget.ChildMessage);
break;
default:
this.notifyLayout(msg);
break;
}
}
/**
* Invoke the message processing routine of the widget's layout.
*
* @param msg - The message to dispatch to the layout.
*
* #### Notes
* This is a no-op if the widget does not have a layout.
*
* This will not typically be called directly by user code.
*/
protected notifyLayout(msg: Message): void {
if (this._layout) {
this._layout.processParentMessage(msg);
}
}
/**
* A message handler invoked on a `'close-request'` message.
*
* #### Notes
* The default implementation unparents or detaches the widget.
*/
protected onCloseRequest(msg: Message): void {
if (this.parent) {
this.parent = null;
} else if (this.isAttached) {
Widget.detach(this);
}
}
/**
* A message handler invoked on a `'resize'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onResize(msg: Widget.ResizeMessage): void { }
/**
* A message handler invoked on an `'update-request'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onUpdateRequest(msg: Message): void { }
/**
* A message handler invoked on a `'fit-request'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onFitRequest(msg: Message): void { }
/**
* A message handler invoked on an `'activate-request'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onActivateRequest(msg: Message): void { }
/**
* A message handler invoked on a `'before-show'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onBeforeShow(msg: Message): void { }
/**
* A message handler invoked on an `'after-show'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onAfterShow(msg: Message): void { }
/**
* A message handler invoked on a `'before-hide'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onBeforeHide(msg: Message): void { }
/**
* A message handler invoked on an `'after-hide'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onAfterHide(msg: Message): void { }
/**
* A message handler invoked on a `'before-attach'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onBeforeAttach(msg: Message): void { }
/**
* A message handler invoked on an `'after-attach'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onAfterAttach(msg: Message): void { }
/**
* A message handler invoked on a `'before-detach'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onBeforeDetach(msg: Message): void { }
/**
* A message handler invoked on an `'after-detach'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onAfterDetach(msg: Message): void { }
/**
* A message handler invoked on a `'child-added'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onChildAdded(msg: Widget.ChildMessage): void { }
/**
* A message handler invoked on a `'child-removed'` message.
*
* #### Notes
* The default implementation of this handler is a no-op.
*/
protected onChildRemoved(msg: Widget.ChildMessage): void { }
private _flags = 0;
private _layout: Layout | null = null;
private _parent: Widget | null = null;
private _disposed = new Signal<this, void>(this);
}
/**
* The namespace for the `Widget` class statics.
*/
export
namespace Widget {
/**
* An options object for initializing a widget.
*/
export
interface IOptions {
/**
* The optional node to use for the widget.
*
* If a node is provided, the widget will assume full ownership
* and control of the node, as if it had created the node itself.
*
* The default is a new `<div>`.
*/
node?: HTMLElement;
}
/**
* An enum of widget bit flags.
*/
export
enum Flag {
/**
* The widget has been disposed.
*/
IsDisposed = 0x1,
/**
* The widget is attached to the DOM.
*/
IsAttached = 0x2,
/**
* The widget is hidden.
*/
IsHidden = 0x4,
/**
* The widget is visible.
*/
IsVisible = 0x8,
/**
* A layout cannot be set on the widget.
*/
DisallowLayout = 0x10
}
/**
* A collection of stateless messages related to widgets.
*/
export
namespace Msg {
/**
* A singleton `'before-show'` message.
*
* #### Notes
* This message is sent to a widget before it becomes visible.
*
* This message is **not** sent when the widget is being attached.
*/
export
const BeforeShow = new Message('before-show');
/**
* A singleton `'after-show'` message.
*
* #### Notes
* This message is sent to a widget after it becomes visible.
*
* This message is **not** sent when the widget is being attached.
*/
export
const AfterShow = new Message('after-show');
/**
* A singleton `'before-hide'` message.
*
* #### Notes
* This message is sent to a widget before it becomes not-visible.
*
* This message is **not** sent when the widget is being detached.
*/
export
const BeforeHide = new Message('before-hide');
/**
* A singleton `'after-hide'` message.
*
* #### Notes
* This message is sent to a widget after it becomes not-visible.
*
* This message is **not** sent when the widget is being detached.
*/
export
const AfterHide = new Message('after-hide');
/**
* A singleton `'before-attach'` message.
*
* #### Notes
* This message is sent to a widget before it is attached.
*/
export
const BeforeAttach = new Message('before-attach');
/**
* A singleton `'after-attach'` message.
*
* #### Notes
* This message is sent to a widget after it is attached.
*/
export
const AfterAttach = new Message('after-attach');
/**
* A singleton `'before-detach'` message.
*
* #### Notes
* This message is sent to a widget before it is detached.
*/
export
const BeforeDetach = new Message('before-detach');
/**
* A singleton `'after-detach'` message.
*
* #### Notes
* This message is sent to a widget after it is detached.
*/
export
const AfterDetach = new Message('after-detach');
/**
* A singleton `'parent-changed'` message.
*
* #### Notes
* This message is sent to a widget when its parent has changed.
*/
export
const ParentChanged = new Message('parent-changed');
/**
* A singleton conflatable `'update-request'` message.
*
* #### Notes
* This message can be dispatched to supporting widgets in order to
* update their content based on the current widget state. Not all
* widgets will respond to messages of this type.
*
* For widgets with a layout, this message will inform the layout to
* update the position and size of its child widgets.
*/
export
const UpdateRequest = new ConflatableMessage('update-request');
/**
* A singleton conflatable `'fit-request'` message.
*
* #### Notes
* For widgets with a layout, this message will inform the layout to
* recalculate its size constraints to fit the space requirements of
* its child widgets, and to update their position and size. Not all
* layouts will respond to messages of this type.
*/
export
const FitRequest = new ConflatableMessage('fit-request');
/**
* A singleton conflatable `'activate-request'` message.
*
* #### Notes
* This message should be dispatched to a widget when it should
* perform the actions necessary to activate the widget, which
* may include focusing its node or descendant node.
*/
export
const ActivateRequest = new ConflatableMessage('activate-request');
/**
* A singleton conflatable `'close-request'` message.
*
* #### Notes
* This message should be dispatched to a widget when it should close
* and remove itself from the widget hierarchy.
*/
export
const CloseRequest = new ConflatableMessage('close-request');
}
/**
* A message class for child related messages.
*/
export
class ChildMessage extends Message {
/**
* Construct a new child message.
*
* @param type - The message type.
*
* @param child - The child widget for the message.
*/
constructor(type: string, child: Widget) {
super(type);
this.child = child;
}
/**
* The child widget for the message.
*/
readonly child: Widget;
}
/**
* A message class for `'resize'` messages.
*/
export
class ResizeMessage extends Message {
/**
* Construct a new resize message.
*
* @param width - The **offset width** of the widget, or `-1` if
* the width is not known.
*
* @param height - The **offset height** of the widget, or `-1` if
* the height is not known.
*/
constructor(width: number, height: number) {
super('resize');
this.width = width;
this.height = height;
}
/**
* The offset width of the widget.
*
* #### Notes
* This will be `-1` if the width is unknown.
*/
readonly width: number;
/**
* The offset height of the widget.
*
* #### Notes
* This will be `-1` if the height is unknown.
*/
readonly height: number;
}
/**
* The namespace for the `ResizeMessage` class statics.
*/
export
namespace ResizeMessage {
/**
* A singleton `'resize'` message with an unknown size.
*/
export
const UnknownSize = new ResizeMessage(-1, -1);
}
/**
* Attach a widget to a host DOM node.
*
* @param widget - The widget of interest.
*
* @param host - The DOM node to use as the widget's host.
*
* @param ref - The child of `host` to use as the reference element.
* If this is provided, the widget will be inserted before this
* node in the host. The default is `null`, which will cause the
* widget to be added as the last child of the host.
*
* #### Notes
* This will throw an error if the widget is not a root widget, if
* the widget is already attached, or if the host is not attached
* to the DOM.
*/
export
function attach(widget: Widget, host: HTMLElement, ref: HTMLElement | null = null): void {
if (widget.parent) {
throw new Error('Cannot attach a child widget.');
}
if (widget.isAttached || document.body.contains(widget.node)) {