/
inspector.ts
132 lines (111 loc) · 3.04 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { Printing } from '@jupyterlab/apputils';
import { Panel, PanelLayout, Widget } from '@phosphor/widgets';
import { IInspector } from './tokens';
/**
* The class name added to inspector panels.
*/
const PANEL_CLASS = 'jp-Inspector';
/**
* The class name added to inspector content.
*/
const CONTENT_CLASS = 'jp-Inspector-content';
/**
* The class name added to default inspector content.
*/
const DEFAULT_CONTENT_CLASS = 'jp-Inspector-default-content';
/**
* A panel which contains a set of inspectors.
*/
export class InspectorPanel extends Panel
implements IInspector, Printing.IPrintable {
/**
* Construct an inspector.
*/
constructor() {
super();
this.addClass(PANEL_CLASS);
(this.layout as PanelLayout).addWidget(this._content);
}
/**
* Print in iframe
*/
[Printing.symbol]() {
return () => Printing.printWidget(this);
}
/**
* 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);
}
// Reject a source that is already disposed.
if (source && source.isDisposed) {
source = null;
}
// Update source.
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);
}
}
/**
* Dispose of the resources held by the widget.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this.source = null;
super.dispose();
}
/**
* Handle inspector update signals.
*/
protected onInspectorUpdate(
sender: any,
args: IInspector.IInspectorUpdate
): void {
const { content } = args;
// Update the content of the inspector widget.
if (!content || content === this._content) {
return;
}
this._content.dispose();
this._content = content;
content.addClass(CONTENT_CLASS);
(this.layout as PanelLayout).addWidget(content);
}
/**
* Handle source disposed signals.
*/
protected onSourceDisposed(sender: any, args: void): void {
this.source = null;
}
private _content: Widget = Private.defaultContent();
private _source: IInspector.IInspectable | null = null;
}
namespace Private {
export function defaultContent() {
const defaultContent = new Widget();
defaultContent.node.innerHTML =
'<p>Click on a function to see documentation.</p>';
defaultContent.addClass(CONTENT_CLASS);
defaultContent.addClass(DEFAULT_CONTENT_CLASS);
return defaultContent;
}
}