-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
mainareawidget.ts
278 lines (246 loc) · 7.4 KB
/
mainareawidget.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { Message, MessageLoop } from '@phosphor/messaging';
import { BoxLayout, Widget } from '@phosphor/widgets';
import { Spinner } from './spinner';
import { Toolbar } from './toolbar';
import { DOMUtils } from './domutils';
import { Printing } from './printing';
/**
* A widget meant to be contained in the JupyterLab main area.
*
* #### Notes
* Mirrors all of the `title` attributes of the content.
* This widget is `closable` by default.
* This widget is automatically disposed when closed.
* This widget ensures its own focus when activated.
*/
export class MainAreaWidget<T extends Widget = Widget> extends Widget
implements Printing.IPrintable {
/**
* Construct a new main area widget.
*
* @param options - The options for initializing the widget.
*/
constructor(options: MainAreaWidget.IOptions<T>) {
super(options);
this.addClass('jp-MainAreaWidget');
this.id = DOMUtils.createDomID();
const content = (this._content = options.content);
const toolbar = (this._toolbar = options.toolbar || new Toolbar());
const spinner = this._spinner;
const layout = (this.layout = new BoxLayout({ spacing: 0 }));
layout.direction = 'top-to-bottom';
BoxLayout.setStretch(toolbar, 0);
BoxLayout.setStretch(content, 1);
layout.addWidget(toolbar);
layout.addWidget(content);
if (!content.id) {
content.id = DOMUtils.createDomID();
}
content.node.tabIndex = -1;
this._updateTitle();
content.title.changed.connect(this._updateTitle, this);
this.title.closable = true;
this.title.changed.connect(this._updateContentTitle, this);
if (options.reveal) {
this.node.appendChild(spinner.node);
this._revealed = options.reveal
.then(() => {
if (content.isDisposed) {
this.dispose();
return;
}
content.disposed.connect(() => this.dispose());
const active = document.activeElement === spinner.node;
this.node.removeChild(spinner.node);
spinner.dispose();
this._isRevealed = true;
if (active) {
this._focusContent();
}
})
.catch(e => {
// Show a revealed promise error.
const error = new Widget();
// Show the error to the user.
const pre = document.createElement('pre');
pre.textContent = String(e);
error.node.appendChild(pre);
BoxLayout.setStretch(error, 1);
this.node.removeChild(spinner.node);
spinner.dispose();
content.dispose();
this._content = null;
toolbar.dispose();
this._toolbar = null;
layout.addWidget(error);
this._isRevealed = true;
throw error;
});
} else {
// Handle no reveal promise.
spinner.dispose();
content.disposed.connect(() => this.dispose());
this._isRevealed = true;
this._revealed = Promise.resolve(undefined);
}
}
/**
* Print method. Defered to content.
*/
[Printing.symbol](): Printing.OptionalAsyncThunk {
return Printing.getPrintFunction(this._content);
}
/**
* The content hosted by the widget.
*/
get content(): T {
return this._content;
}
/**
* The toolbar hosted by the widget.
*/
get toolbar(): Toolbar {
return this._toolbar;
}
/**
* Whether the content widget or an error is revealed.
*/
get isRevealed(): boolean {
return this._isRevealed;
}
/**
* A promise that resolves when the widget is revealed.
*/
get revealed(): Promise<void> {
return this._revealed;
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
if (this._isRevealed) {
if (this._content) {
this._focusContent();
}
} else {
this._spinner.node.focus();
}
}
/**
* Handle `'close-request'` messages.
*/
protected onCloseRequest(msg: Message): void {
this.dispose();
}
/**
* Handle `'update-request'` messages by forwarding them to the content.
*/
protected onUpdateRequest(msg: Message): void {
MessageLoop.sendMessage(this._content, msg);
}
/**
* Update the title based on the attributes of the child widget.
*/
private _updateTitle(): void {
if (this._changeGuard) {
return;
}
this._changeGuard = true;
const content = this.content;
this.title.label = content.title.label;
this.title.mnemonic = content.title.mnemonic;
this.title.iconClass = content.title.iconClass;
this.title.iconLabel = content.title.iconLabel;
this.title.iconRenderer = content.title.iconRenderer;
this.title.caption = content.title.caption;
this.title.className = content.title.className;
this.title.dataset = content.title.dataset;
this._changeGuard = false;
}
/**
* Update the content title based on attributes of the main widget.
*/
private _updateContentTitle(): void {
if (this._changeGuard) {
return;
}
this._changeGuard = true;
const content = this.content;
content.title.label = this.title.label;
content.title.mnemonic = this.title.mnemonic;
content.title.iconClass = this.title.iconClass;
content.title.iconLabel = this.title.iconLabel;
content.title.iconRenderer = this.title.iconRenderer;
content.title.caption = this.title.caption;
content.title.className = this.title.className;
content.title.dataset = this.title.dataset;
this._changeGuard = false;
}
/**
* Give focus to the content.
*/
private _focusContent(): void {
// Focus the content node if we aren't already focused on it or a
// descendent.
if (!this.content.node.contains(document.activeElement)) {
this.content.node.focus();
}
// Activate the content asynchronously (which may change the focus).
this.content.activate();
}
private _content: T;
private _toolbar: Toolbar;
private _changeGuard = false;
private _spinner = new Spinner();
private _isRevealed = false;
private _revealed: Promise<void>;
}
/**
* The namespace for the `MainAreaWidget` class statics.
*/
export namespace MainAreaWidget {
/**
* An options object for creating a main area widget.
*/
export interface IOptions<T extends Widget = Widget> extends Widget.IOptions {
/**
* The child widget to wrap.
*/
content: T;
/**
* The toolbar to use for the widget. Defaults to an empty toolbar.
*/
toolbar?: Toolbar;
/**
* An optional promise for when the content is ready to be revealed.
*/
reveal?: Promise<any>;
}
/**
* An options object for main area widget subclasses providing their own
* default content.
*
* #### Notes
* This makes it easier to have a subclass that provides its own default
* content. This can go away once we upgrade to TypeScript 2.8 and have an
* easy way to make a single property optional, ala
* https://stackoverflow.com/a/46941824
*/
export interface IOptionsOptionalContent<T extends Widget = Widget>
extends Widget.IOptions {
/**
* The child widget to wrap.
*/
content?: T;
/**
* The toolbar to use for the widget. Defaults to an empty toolbar.
*/
toolbar?: Toolbar;
/**
* An optional promise for when the content is ready to be revealed.
*/
reveal?: Promise<any>;
}
}