/
browser.ts
305 lines (258 loc) · 7.21 KB
/
browser.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { showErrorMessage, Toolbar, ToolbarButton } from '@jupyterlab/apputils';
import { DocumentManager } from '@jupyterlab/docmanager';
import { Contents, ServerConnection } from '@jupyterlab/services';
import { IIterator } from '@phosphor/algorithm';
import { PanelLayout, Widget } from '@phosphor/widgets';
import { BreadCrumbs } from './crumbs';
import { DirListing } from './listing';
import { FileBrowserModel } from './model';
import { Uploader } from './upload';
/**
* The class name added to file browsers.
*/
const FILE_BROWSER_CLASS = 'jp-FileBrowser';
/**
* The class name added to the filebrowser crumbs node.
*/
const CRUMBS_CLASS = 'jp-FileBrowser-crumbs';
/**
* The class name added to the filebrowser toolbar node.
*/
const TOOLBAR_CLASS = 'jp-FileBrowser-toolbar';
/**
* The class name added to the filebrowser listing node.
*/
const LISTING_CLASS = 'jp-FileBrowser-listing';
/**
* A widget which hosts a file browser.
*
* The widget uses the Jupyter Contents API to retrieve contents,
* and presents itself as a flat list of files and directories with
* breadcrumbs.
*/
export class FileBrowser extends Widget {
/**
* Construct a new file browser.
*
* @param model - The file browser view model.
*/
constructor(options: FileBrowser.IOptions) {
super();
this.addClass(FILE_BROWSER_CLASS);
this.id = options.id;
const model = (this.model = options.model);
const renderer = options.renderer;
model.connectionFailure.connect(this._onConnectionFailure, this);
this._manager = model.manager;
this._crumbs = new BreadCrumbs({ model });
this.toolbar = new Toolbar<Widget>();
this._directoryPending = false;
let newFolder = new ToolbarButton({
iconClassName: 'jp-NewFolderIcon',
onClick: () => {
this.createNewDirectory();
},
tooltip: 'New Folder'
});
let uploader = new Uploader({ model });
let refresher = new ToolbarButton({
iconClassName: 'jp-RefreshIcon',
onClick: () => {
void model.refresh();
},
tooltip: 'Refresh File List'
});
this.toolbar.addItem('newFolder', newFolder);
this.toolbar.addItem('upload', uploader);
this.toolbar.addItem('refresher', refresher);
this._listing = new DirListing({ model, renderer });
this._crumbs.addClass(CRUMBS_CLASS);
this.toolbar.addClass(TOOLBAR_CLASS);
this._listing.addClass(LISTING_CLASS);
let layout = new PanelLayout();
layout.addWidget(this.toolbar);
layout.addWidget(this._crumbs);
layout.addWidget(this._listing);
this.layout = layout;
void model.restore(this.id);
}
/**
* The model used by the file browser.
*/
readonly model: FileBrowserModel;
/**
* The toolbar used by the file browser.
*/
readonly toolbar: Toolbar<Widget>;
/**
* Create an iterator over the listing's selected items.
*
* @returns A new iterator over the listing's selected items.
*/
selectedItems(): IIterator<Contents.IModel> {
return this._listing.selectedItems();
}
/**
* Rename the first currently selected item.
*
* @returns A promise that resolves with the new name of the item.
*/
rename(): Promise<string> {
return this._listing.rename();
}
/**
* Cut the selected items.
*/
cut(): void {
this._listing.cut();
}
/**
* Copy the selected items.
*/
copy(): void {
this._listing.copy();
}
/**
* Paste the items from the clipboard.
*
* @returns A promise that resolves when the operation is complete.
*/
paste(): Promise<void> {
return this._listing.paste();
}
/**
* Create a new directory
*/
createNewDirectory(): void {
if (this._directoryPending === true) {
return;
}
this._directoryPending = true;
// TODO: We should provide a hook into when the
// directory is done being created. This probably
// means storing a pendingDirectory promise and
// returning that if there is already a directory
// request.
void this._manager
.newUntitled({
path: this.model.path,
type: 'directory'
})
.then(async model => {
await this._listing.selectItemByName(model.name);
this._directoryPending = false;
})
.catch(err => {
this._directoryPending = false;
});
}
/**
* Delete the currently selected item(s).
*
* @returns A promise that resolves when the operation is complete.
*/
delete(): Promise<void> {
return this._listing.delete();
}
/**
* Duplicate the currently selected item(s).
*
* @returns A promise that resolves when the operation is complete.
*/
duplicate(): Promise<void> {
return this._listing.duplicate();
}
/**
* Download the currently selected item(s).
*/
download(): Promise<void> {
return this._listing.download();
}
/**
* Shut down kernels on the applicable currently selected items.
*
* @returns A promise that resolves when the operation is complete.
*/
shutdownKernels(): Promise<void> {
return this._listing.shutdownKernels();
}
/**
* Select next item.
*/
selectNext(): void {
this._listing.selectNext();
}
/**
* Select previous item.
*/
selectPrevious(): void {
this._listing.selectPrevious();
}
/**
* Find a model given a click.
*
* @param event - The mouse event.
*
* @returns The model for the selected file.
*/
modelForClick(event: MouseEvent): Contents.IModel | undefined {
return this._listing.modelForClick(event);
}
/**
* Handle a connection lost signal from the model.
*/
private _onConnectionFailure(sender: FileBrowserModel, args: Error): void {
if (this._showingError) {
return;
}
this._showingError = true;
let title = 'Server Connection Error';
let networkMsg =
'A connection to the Jupyter server could not be established.\n' +
'JupyterLab will continue trying to reconnect.\n' +
'Check your network connection or Jupyter server configuration.\n';
// Check for a fetch error.
if (args instanceof ServerConnection.NetworkError) {
args.message = networkMsg;
} else if (args instanceof ServerConnection.ResponseError) {
if (args.response.status === 404) {
title = 'Directory not found';
args.message = `Directory not found: "${this.model.path}"`;
}
}
void showErrorMessage(title, args).then(() => {
this._showingError = false;
});
}
private _crumbs: BreadCrumbs;
private _listing: DirListing;
private _manager: DocumentManager;
private _showingError = false;
private _directoryPending: boolean;
}
/**
* The namespace for the `FileBrowser` class statics.
*/
export namespace FileBrowser {
/**
* An options object for initializing a file browser widget.
*/
export interface IOptions {
/**
* The widget/DOM id of the file browser.
*/
id: string;
/**
* A file browser model instance.
*/
model: FileBrowserModel;
/**
* An optional renderer for the directory listing area.
*
* The default is a shared instance of `DirListing.Renderer`.
*/
renderer?: DirListing.IRenderer;
}
}