-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
inspector.ts
498 lines (435 loc) · 10.8 KB
/
inspector.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
Toolbar,
ToolbarButton,
printSymbol,
printd
} from '@jupyterlab/apputils';
import { Token } from '@phosphor/coreutils';
import { DisposableDelegate, IDisposable } from '@phosphor/disposable';
import { ISignal } from '@phosphor/signaling';
import { PanelLayout, TabPanel, Widget } from '@phosphor/widgets';
/**
* The class name added to inspector panels.
*/
const PANEL_CLASS = 'jp-Inspector';
/**
* The class name added to inspector child item widgets.
*/
const ITEM_CLASS = 'jp-InspectorItem';
/**
* The class name added to inspector child item widgets' content.
*/
const CONTENT_CLASS = 'jp-InspectorItem-content';
/**
* The history clear button class name.
*/
const CLEAR_CLASS = 'jp-InspectorItem-clear';
/**
* The back button class name.
*/
const BACK_CLASS = 'jp-InspectorItem-back';
/**
* The forward button class name.
*/
const FORWARD_CLASS = 'jp-InspectorItem-forward';
/* tslint:disable */
/**
* The inspector panel token.
*/
export const IInspector = new Token<IInspector>(
'@jupyterlab/inspector:IInspector'
);
/* tslint:enable */
/**
* An interface for an inspector.
*/
export interface IInspector {
/**
* Create an inspector child item and return a disposable to remove it.
*
* @param item - The inspector child item being added to the inspector.
*
* @returns A disposable that removes the child item from the inspector.
*/
add(item: IInspector.IInspectorItem): IDisposable;
/**
* The source of events the inspector listens for.
*/
source: IInspector.IInspectable | null;
}
/**
* A namespace for inspector interfaces.
*/
export namespace IInspector {
/**
* The definition of an inspectable source.
*/
export interface IInspectable {
/**
* A signal emitted when the handler is disposed.
*/
disposed: ISignal<any, void>;
/**
* A signal emitted when inspector should clear all items with no history.
*/
ephemeralCleared: ISignal<any, void>;
/**
* A signal emitted when an inspector value is generated.
*/
inspected: ISignal<any, IInspectorUpdate>;
/**
* Indicates whether the inspectable source emits signals.
*
* #### Notes
* The use case for this attribute is to limit the API traffic when no
* inspector is visible.
*/
standby: boolean;
}
/**
* The definition of a child item of an inspector.
*/
export interface IInspectorItem {
/**
* The optional class name added to the inspector child widget.
*/
className?: string;
/**
* The display name of the inspector child.
*/
name: string;
/**
* The rank order of display priority for inspector updates. A lower rank
* denotes a higher display priority.
*/
rank: number;
/**
* A flag that indicates whether the inspector remembers history.
*
* The default value is `false`.
*/
remembers?: boolean;
/**
* The type of the inspector.
*/
type: string;
}
/**
* An update value for code inspectors.
*/
export interface IInspectorUpdate {
/**
* The content being sent to the inspector for display.
*/
content: Widget | null;
/**
* The type of the inspector being updated.
*/
type: string;
}
}
/**
* A panel which contains a set of inspectors.
*/
export class InspectorPanel extends TabPanel implements IInspector {
/**
* Construct an inspector.
*/
constructor() {
super();
this.addClass(PANEL_CLASS);
}
/**
* Print in iframe
*/
[printSymbol]() {
printd.bind(this)('.p-mod-hidden {display: none;}');
}
/**
* The source of events the inspector panel listens for.
*/
get source(): IInspector.IInspectable | null {
return this._source;
}
set source(source: IInspector.IInspectable | null) {
if (this._source === source) {
return;
}
// Disconnect old signal handler.
if (this._source) {
this._source.standby = true;
this._source.inspected.disconnect(this.onInspectorUpdate, this);
this._source.disposed.disconnect(this.onSourceDisposed, this);
}
// Clear the inspector child items (but maintain history) if necessary.
Object.keys(this._items).forEach(i => {
this._items[i].content = null;
});
this._source = source;
// Connect new signal handler.
if (this._source) {
this._source.standby = false;
this._source.inspected.connect(
this.onInspectorUpdate,
this
);
this._source.disposed.connect(
this.onSourceDisposed,
this
);
}
}
/**
* Create an inspector child item and return a disposable to remove it.
*
* @param item - The inspector child item being added to the inspector.
*
* @returns A disposable that removes the child item from the inspector.
*/
add(item: IInspector.IInspectorItem): IDisposable {
const widget = new InspectorItem();
widget.rank = item.rank;
widget.remembers = !!item.remembers;
widget.title.closable = false;
widget.title.label = item.name;
if (item.className) {
widget.addClass(item.className);
}
this._items[item.type] = widget;
this.addWidget(widget);
if (Object.keys(this._items).length < 2) {
this.tabBar.hide();
} else {
this.tabBar.show();
}
return new DisposableDelegate(() => {
if (widget.isDisposed || this.isDisposed) {
return;
}
widget.dispose();
delete this._items[item.type];
if (Object.keys(this._items).length < 2) {
this.tabBar.hide();
} else {
this.tabBar.show();
}
});
}
/**
* Dispose of the resources held by the widget.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this.source = null;
let items = this._items;
// Dispose the inspector child items.
Object.keys(items).forEach(i => {
items[i].dispose();
});
super.dispose();
}
/**
* Handle inspector update signals.
*/
protected onInspectorUpdate(
sender: any,
args: IInspector.IInspectorUpdate
): void {
let widget = this._items[args.type];
if (!widget) {
return;
}
// Update the content of the inspector widget.
widget.content = args.content;
let items = this._items;
// If any inspector with a higher rank has content, do not change focus.
if (args.content) {
for (let type in items) {
let inspector = this._items[type];
if (inspector.rank < widget.rank && inspector.content) {
return;
}
}
this.currentWidget = widget;
return;
}
// If the inspector was emptied, show the next best ranked inspector.
let lowest = Infinity;
let newWidget: Widget | null = null;
for (let type in items) {
let inspector = this._items[type];
if (inspector.rank < lowest && inspector.content) {
lowest = inspector.rank;
newWidget = inspector;
}
}
if (newWidget) {
this.currentWidget = newWidget;
}
}
/**
* Handle source disposed signals.
*/
protected onSourceDisposed(sender: any, args: void): void {
this.source = null;
}
private _items: { [type: string]: InspectorItem } = Object.create(null);
private _source: IInspector.IInspectable | null = null;
}
/**
* A code inspector child widget.
*/
class InspectorItem extends Widget {
/**
* Construct an inspector widget.
*/
constructor() {
super();
this.layout = new PanelLayout();
this.addClass(ITEM_CLASS);
this._toolbar = this._createToolbar();
(this.layout as PanelLayout).addWidget(this._toolbar);
}
/**
* The text of the inspector.
*/
get content(): Widget | null {
return this._content;
}
set content(newValue: Widget | null) {
if (newValue === this._content) {
return;
}
if (this._content) {
if (this._remembers) {
this._content.hide();
} else {
this._content.dispose();
}
}
this._content = newValue;
if (newValue) {
newValue.addClass(CONTENT_CLASS);
(this.layout as PanelLayout).addWidget(newValue);
if (this.remembers) {
this._history.push(newValue);
this._index++;
}
}
}
/**
* A flag that indicates whether the inspector remembers history.
*/
get remembers(): boolean {
return this._remembers;
}
set remembers(newValue: boolean) {
if (newValue === this._remembers) {
return;
}
this._remembers = newValue;
if (!newValue) {
this._clear();
}
this.update();
}
/**
* The display rank of the inspector.
*/
get rank(): number {
return this._rank;
}
set rank(newValue: number) {
this._rank = newValue;
}
/**
* Dispose of the resources held by the widget.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._history.forEach(widget => widget.dispose());
this._toolbar.dispose();
super.dispose();
}
/**
* Navigate back in history.
*/
private _back(): void {
if (this._history.length) {
this._navigateTo(Math.max(this._index - 1, 0));
}
}
/**
* Clear history.
*/
private _clear(): void {
this._history.forEach(widget => widget.dispose());
this._history = [];
this._index = -1;
}
/**
* Navigate forward in history.
*/
private _forward(): void {
if (this._history.length) {
this._navigateTo(Math.min(this._index + 1, this._history.length - 1));
}
}
/**
* Create a history toolbar.
*/
private _createToolbar(): Toolbar<Widget> {
let toolbar = new Toolbar();
if (!this._remembers) {
return toolbar;
}
let clear = new ToolbarButton({
iconClassName: CLEAR_CLASS + ' jp-Icon jp-Icon-16',
onClick: () => {
this._clear();
},
tooltip: 'Clear history.'
});
toolbar.addItem('clear', clear);
let back = new ToolbarButton({
iconClassName: BACK_CLASS + ' jp-Icon jp-Icon-16',
onClick: () => {
this._back();
},
tooltip: 'Navigate back in history.'
});
toolbar.addItem('back', back);
let forward = new ToolbarButton({
iconClassName: FORWARD_CLASS + ' jp-Icon jp-Icon-16',
onClick: () => {
this._forward();
},
tooltip: 'Navigate forward in history.'
});
toolbar.addItem('forward', forward);
return toolbar;
}
/**
* Navigate to a known index in history.
*/
private _navigateTo(index: number): void {
if (this._content) {
this._content.hide();
}
this._content = this._history[index];
this._index = index;
this._content.show();
}
private _content: Widget | null = null;
private _history: Widget[] = [];
private _index: number = -1;
private _rank: number = Infinity;
private _remembers: boolean = false;
private _toolbar: Toolbar<Widget>;
}