forked from jupyterlab/jupyterlab
/
collapse.ts
176 lines (154 loc) · 4.11 KB
/
collapse.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { Message } from '@lumino/messaging';
import { ISignal, Signal } from '@lumino/signaling';
import { Widget, Panel, PanelLayout, Title } from '@lumino/widgets';
import { caretDownIcon, caretUpIcon } from '@jupyterlab/ui-components';
/**
* A panel that supports a collapsible header made from the widget's title.
* Clicking on the title expands or contracts the widget.
*/
export class Collapse<T extends Widget = Widget> extends Widget {
constructor(options: Collapse.IOptions<T>) {
super(options);
const { widget, collapsed = true } = options;
this.addClass('jp-Collapse');
this._header = new Widget();
this._header.addClass('jp-Collapse-header');
this._content = new Panel();
this._content.addClass('jp-Collapse-contents');
let layout = new PanelLayout();
this.layout = layout;
layout.addWidget(this._header);
layout.addWidget(this._content);
this.widget = widget;
this.collapsed = collapsed;
}
/**
* The widget inside the collapse panel.
*/
get widget(): T {
return this._widget;
}
set widget(widget: T) {
let oldWidget = this._widget;
if (oldWidget) {
oldWidget.title.changed.disconnect(this._onTitleChanged, this);
oldWidget.parent = null;
}
this._widget = widget;
widget.title.changed.connect(this._onTitleChanged, this);
this._onTitleChanged(widget.title);
this._content.addWidget(widget);
}
/**
* The collapsed state of the panel.
*/
get collapsed() {
return this._collapsed;
}
set collapsed(value: boolean) {
if (value === this._collapsed) {
return;
}
if (value) {
this._collapse();
} else {
this._uncollapse();
}
}
/**
* A signal for when the widget collapse state changes.
*/
get collapseChanged(): ISignal<Collapse, void> {
return this._collapseChanged;
}
/**
* Toggle the collapse state of the panel.
*/
toggle() {
this.collapsed = !this.collapsed;
}
/**
* Dispose the widget.
*/
dispose() {
if (this.isDisposed) {
return;
}
// Delete references we explicitly hold to other widgets.
this._header = null!;
this._widget = null!;
this._content = null!;
super.dispose();
}
/**
* Handle the DOM events for the Collapse widget.
*
* @param event - The DOM event sent to the panel.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the panel's DOM node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'click':
this._evtClick(event as MouseEvent);
break;
default:
break;
}
}
protected onAfterAttach(msg: Message) {
this._header.node.addEventListener('click', this);
}
protected onBeforeDetach(msg: Message) {
this._header.node.removeEventListener('click', this);
}
private _collapse() {
this._collapsed = true;
if (this._content) {
this._content.hide();
}
this._setHeader();
this._collapseChanged.emit(void 0);
}
private _uncollapse() {
this._collapsed = false;
if (this._content) {
this._content.show();
}
this._setHeader();
this._collapseChanged.emit(void 0);
}
private _evtClick(event: MouseEvent) {
this.toggle();
}
/**
* Handle the `changed` signal of a title object.
*/
private _onTitleChanged(sender: Title<Widget>): void {
this._setHeader();
}
private _setHeader(): void {
(this._collapsed ? caretUpIcon : caretDownIcon).element({
container: this._header.node,
label: this._widget.title.label,
elementPosition: 'right',
height: '28px'
});
}
private _collapseChanged = new Signal<this, void>(this);
private _collapsed: boolean;
private _content: Panel;
private _header: Widget;
private _widget: T;
}
export namespace Collapse {
export interface IOptions<T extends Widget = Widget> extends Widget.IOptions {
widget: T;
collapsed?: boolean;
}
}