forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.ts
212 lines (178 loc) · 4.62 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
Message
} from '@phosphor/messaging';
import {
PanelLayout
} from '@phosphor/widgets';
import {
Widget
} from '@phosphor/widgets';
import {
ActivityMonitor
} from '@jupyterlab/coreutils';
import {
ABCWidgetFactory, DocumentRegistry
} from '@jupyterlab/docregistry';
import {
CSVModel, CSVTable
} from './table';
import {
CSVToolbar
} from './toolbar';
/**
* The class name added to a CSV viewer.
*/
const CSV_CLASS = 'jp-CSVViewer';
/**
* The class name added to a CSV viewer warning.
*/
const CSV_WARNING_CLASS = 'jp-CSVViewer-warning';
/**
* The class name added to a CSV viewer toolbar.
*/
const CSV_VIEWER_CLASS = 'jp-CSVViewer-toolbar';
/**
* The timeout to wait for change activity to have ceased before rendering.
*/
const RENDER_TIMEOUT = 1000;
/**
* A viewer for CSV tables.
*/
export
class CSVViewer extends Widget {
/**
* Construct a new CSV viewer.
*/
constructor(options: CSVViewer.IOptions) {
super();
let context = this._context = options.context;
let layout = this.layout = new PanelLayout();
this.addClass(CSV_CLASS);
this.title.label = context.path.split('/').pop();
this._warning = new Widget();
this._warning.addClass(CSV_WARNING_CLASS);
this._model = new CSVModel({ content: context.model.toString() });
this._table = new CSVTable();
this._table.model = this._model;
this._model.maxExceeded.connect(this._onMaxExceeded, this);
this._toolbar = new CSVToolbar();
this._toolbar.delimiterChanged.connect(this._onDelimiterChanged, this);
this._toolbar.addClass(CSV_VIEWER_CLASS);
layout.addWidget(this._toolbar);
layout.addWidget(this._table);
layout.addWidget(this._warning);
context.pathChanged.connect(this._onPathChanged, this);
// Throttle the rendering rate of the widget.
this._monitor = new ActivityMonitor({
signal: context.model.contentChanged,
timeout: RENDER_TIMEOUT
});
this._monitor.activityStopped.connect(this._onContentChanged, this);
}
/**
* The CSV widget's context.
*/
get context(): DocumentRegistry.Context {
return this._context;
}
/**
* The CSV data model.
*/
get model(): CSVModel {
return this._model;
}
/**
* Dispose of the resources used by the widget.
*/
dispose(): void {
if (this._model === null) {
return;
}
let model = this._model;
let table = this._table;
let toolbar = this._toolbar;
let warning = this._warning;
let monitor = this._monitor;
this._model = null;
this._table = null;
this._toolbar = null;
this._warning = null;
this._monitor = null;
model.dispose();
table.dispose();
toolbar.dispose();
warning.dispose();
monitor.dispose();
super.dispose();
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
this.node.tabIndex = -1;
this.node.focus();
}
/**
* Handle a max exceeded in a csv widget.
*/
private _onMaxExceeded(sender: CSVModel, overflow: CSVModel.IOverflow): void {
let { available, maximum } = overflow;
let message = `Table is too long to render,
rendering ${maximum} of ${available} rows`;
this._warning.node.textContent = message;
}
/**
* Handle a change in delimiter.
*/
private _onDelimiterChanged(sender: CSVToolbar, delimiter: string): void {
this._table.model.delimiter = delimiter;
}
/**
* Handle a change in content.
*/
private _onContentChanged(): void {
this._table.model.content = this._context.model.toString();
}
/**
* Handle a change in path.
*/
private _onPathChanged(): void {
this.title.label = this._context.path.split('/').pop();
}
private _context: DocumentRegistry.Context = null;
private _model: CSVModel = null;
private _table: CSVTable = null;
private _toolbar: CSVToolbar = null;
private _warning: Widget = null;
private _monitor: ActivityMonitor<any, any> = null;
}
/**
* A namespace for `CSVViewer` statics.
*/
export
namespace CSVViewer {
/**
* Instantiation options for CSV widgets.
*/
export
interface IOptions {
/**
* The document context for the CSV being rendered by the widget.
*/
context: DocumentRegistry.Context;
}
}
/**
* A widget factory for CSV widgets.
*/
export
class CSVViewerFactory extends ABCWidgetFactory<CSVViewer, DocumentRegistry.IModel> {
/**
* Create a new widget given a context.
*/
protected createNewWidget(context: DocumentRegistry.Context): CSVViewer {
return new CSVViewer({ context });
}
}