forked from jupyterlab/jupyterlab
/
widget.ts
236 lines (200 loc) · 5.24 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ActivityMonitor
} from '@jupyterlab/coreutils';
import {
ABCWidgetFactory, DocumentRegistry, IDocumentWidget, DocumentWidget
} from '@jupyterlab/docregistry';
import {
PromiseDelegate
} from '@phosphor/coreutils';
import {
DataGrid
} from '@phosphor/datagrid';
import {
Message
} from '@phosphor/messaging';
import {
PanelLayout, Widget
} from '@phosphor/widgets';
import {
CSVDelimiter
} from './toolbar';
import {
DSVModel
} from './model';
/**
* The class name added to a CSV viewer.
*/
const CSV_CLASS = 'jp-CSVViewer';
/**
* The class name added to a CSV viewer datagrid.
*/
const CSV_GRID_CLASS = 'jp-CSVViewer-grid';
/**
* 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._grid = new DataGrid();
this._grid.addClass(CSV_GRID_CLASS);
this._grid.headerVisibility = 'all';
layout.addWidget(this._grid);
this._context.ready.then(() => {
this._updateGrid();
this._revealed.resolve(undefined);
// Throttle the rendering rate of the widget.
this._monitor = new ActivityMonitor({
signal: context.model.contentChanged,
timeout: RENDER_TIMEOUT
});
this._monitor.activityStopped.connect(this._updateGrid, this);
});
}
/**
* The CSV widget's context.
*/
get context(): DocumentRegistry.Context {
return this._context;
}
/**
* A promise that resolves when the csv viewer is ready to be revealed.
*/
get revealed() {
return this._revealed.promise;
}
/**
* The delimiter for the file.
*/
get delimiter(): string {
return this._delimiter;
}
set delimiter(value: string) {
if (value === this._delimiter) {
return;
}
this._delimiter = value;
this._updateGrid();
}
/**
* Dispose of the resources used by the widget.
*/
dispose(): void {
if (this._monitor) {
this._monitor.dispose();
}
super.dispose();
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
this.node.tabIndex = -1;
this.node.focus();
}
/**
* Create the model for the grid.
*/
private _updateGrid(): void {
let data: string = this._context.model.toString();
let delimiter = this._delimiter;
let oldModel = this._grid.model as DSVModel;
this._grid.model = new DSVModel({ data, delimiter });
if (oldModel) {
oldModel.dispose();
}
}
private _context: DocumentRegistry.Context;
private _grid: DataGrid;
private _monitor: ActivityMonitor<any, any> | null = null;
private _delimiter = ',';
private _revealed = new PromiseDelegate<void>();
}
/**
* 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 document widget for CSV content widgets.
*/
export
class CSVDocumentWidget extends DocumentWidget<CSVViewer> {
constructor(options: CSVDocumentWidget.IOptions) {
let {content, context, delimiter, reveal, ...other} = options;
content = content || Private.createContent(context);
reveal = Promise.all([reveal, content.revealed]);
super({content, context, reveal, ...other});
if (delimiter) {
content.delimiter = delimiter;
}
const csvDelimiter = new CSVDelimiter({ selected: content.delimiter });
this.toolbar.addItem('delimiter', csvDelimiter);
csvDelimiter.delimiterChanged.connect((sender: CSVDelimiter, delimiter: string) => { content.delimiter = delimiter; });
}
}
export
namespace CSVDocumentWidget {
// TODO: In TypeScript 2.8, we can make just the content property optional
// using something like https://stackoverflow.com/a/46941824, instead of
// inheriting from this IOptionsOptionalContent.
export
interface IOptions extends DocumentWidget.IOptionsOptionalContent<CSVViewer> {
delimiter?: string;
}
}
namespace Private {
export
function createContent(context: DocumentRegistry.IContext<DocumentRegistry.IModel>) {
return new CSVViewer({ context });
}
}
/**
* A widget factory for CSV widgets.
*/
export
class CSVViewerFactory extends ABCWidgetFactory<IDocumentWidget<CSVViewer>> {
/**
* Create a new widget given a context.
*/
protected createNewWidget(context: DocumentRegistry.Context): IDocumentWidget<CSVViewer> {
return new CSVDocumentWidget({ context });
}
}
/**
* A widget factory for TSV widgets.
*/
export
class TSVViewerFactory extends ABCWidgetFactory<IDocumentWidget<CSVViewer>> {
/**
* Create a new widget given a context.
*/
protected createNewWidget(context: DocumentRegistry.Context): IDocumentWidget<CSVViewer> {
const delimiter = '\t';
return new CSVDocumentWidget({context, delimiter});
}
}