forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.ts
494 lines (433 loc) · 11.9 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
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { showErrorMessage } from '@jupyterlab/apputils';
import { PathExt } from '@jupyterlab/coreutils';
import { IDocumentManager } from '@jupyterlab/docmanager';
import { Contents, ServerConnection } from '@jupyterlab/services';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { SidePanel } from '@jupyterlab/ui-components';
import { Panel } from '@lumino/widgets';
import { BreadCrumbs } from './crumbs';
import { DirListing } from './listing';
import { FilterFileBrowserModel } from './model';
/**
* The class name added to file browsers.
*/
const FILE_BROWSER_CLASS = 'jp-FileBrowser';
/**
* The class name added to file browser panel (gather filter, breadcrumbs and listing).
*/
const FILE_BROWSER_PANEL_CLASS = 'jp-FileBrowser-Panel';
/**
* 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 SidePanel {
/**
* Construct a new file browser.
*
* @param options - The file browser options.
*/
constructor(options: FileBrowser.IOptions) {
super({ content: new Panel(), translator: options.translator });
this.addClass(FILE_BROWSER_CLASS);
this.toolbar.addClass(TOOLBAR_CLASS);
this.id = options.id;
const translator = (this.translator = options.translator ?? nullTranslator);
const model = (this.model = options.model);
const renderer = options.renderer;
model.connectionFailure.connect(this._onConnectionFailure, this);
this._manager = model.manager;
this.toolbar.node.setAttribute(
'aria-label',
this._trans.__('file browser')
);
// File browser widgets container
this.mainPanel = new Panel();
this.mainPanel.addClass(FILE_BROWSER_PANEL_CLASS);
this.mainPanel.title.label = this._trans.__('File Browser');
this.crumbs = new BreadCrumbs({ model, translator });
this.crumbs.addClass(CRUMBS_CLASS);
this.listing = this.createDirListing({
model,
renderer,
translator
});
this.listing.addClass(LISTING_CLASS);
this.mainPanel.addWidget(this.crumbs);
this.mainPanel.addWidget(this.listing);
this.addWidget(this.mainPanel);
if (options.restore !== false) {
void model.restore(this.id);
}
}
/**
* The model used by the file browser.
*/
readonly model: FilterFileBrowserModel;
/**
* Whether to show active file in file browser
*/
get navigateToCurrentDirectory(): boolean {
return this._navigateToCurrentDirectory;
}
set navigateToCurrentDirectory(value: boolean) {
this._navigateToCurrentDirectory = value;
}
/**
* Whether to show the last modified column
*/
get showLastModifiedColumn(): boolean {
return this._showLastModifiedColumn;
}
set showLastModifiedColumn(value: boolean) {
if (this.listing.setColumnVisibility) {
this.listing.setColumnVisibility('last_modified', value);
this._showLastModifiedColumn = value;
} else {
console.warn('Listing does not support toggling column visibility');
}
}
/**
* Whether to show the full path in the breadcrumbs
*/
get showFullPath(): boolean {
return this.crumbs.fullPath;
}
set showFullPath(value: boolean) {
this.crumbs.fullPath = value;
}
/**
* Whether to show the file size column
*/
get showFileSizeColumn(): boolean {
return this._showFileSizeColumn;
}
set showFileSizeColumn(value: boolean) {
if (this.listing.setColumnVisibility) {
this.listing.setColumnVisibility('file_size', value);
this._showFileSizeColumn = value;
} else {
console.warn('Listing does not support toggling column visibility');
}
}
/**
* Whether to show hidden files
*/
get showHiddenFiles(): boolean {
return this._showHiddenFiles;
}
set showHiddenFiles(value: boolean) {
this.model.showHiddenFiles(value);
this._showHiddenFiles = value;
}
/**
* Whether to show checkboxes next to files and folders
*/
get showFileCheckboxes(): boolean {
return this._showFileCheckboxes;
}
set showFileCheckboxes(value: boolean) {
if (this.listing.setColumnVisibility) {
this.listing.setColumnVisibility('is_selected', value);
this._showFileCheckboxes = value;
} else {
console.warn('Listing does not support toggling column visibility');
}
}
/**
* Whether to sort notebooks above other files
*/
get sortNotebooksFirst(): boolean {
return this._sortNotebooksFirst;
}
set sortNotebooksFirst(value: boolean) {
if (this.listing.setNotebooksFirstSorting) {
this.listing.setNotebooksFirstSorting(value);
this._sortNotebooksFirst = value;
} else {
console.warn('Listing does not support sorting notebooks first');
}
}
/**
* Create an iterator over the listing's selected items.
*
* @returns A new iterator over the listing's selected items.
*/
selectedItems(): IterableIterator<Contents.IModel> {
return this.listing.selectedItems();
}
/**
* Select an item by name.
*
* @param name - The name of the item to select.
*/
async selectItemByName(name: string): Promise<void> {
await this.listing.selectItemByName(name);
}
clearSelectedItems(): void {
this.listing.clearSelectedItems();
}
/**
* 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();
}
private async _createNew(
options: Contents.ICreateOptions
): Promise<Contents.IModel> {
// normalize the path if the file is created from a custom drive
if (options.path) {
const localPath = this._manager.services.contents.localPath(options.path);
options.path = this._toDrivePath(this.model.driveName, localPath);
}
try {
const model = await this._manager.newUntitled(options);
await this.listing.selectItemByName(model.name, true);
await this.rename();
return model;
} catch (err) {
void showErrorMessage(this._trans.__('Error'), err);
throw err;
}
}
/**
* Create a new directory
*/
async createNewDirectory(): Promise<Contents.IModel> {
if (this._directoryPending) {
return this._directoryPending;
}
this._directoryPending = this._createNew({
path: this.model.path,
type: 'directory'
});
try {
return await this._directoryPending;
} finally {
this._directoryPending = null;
}
}
/**
* Create a new file
*/
async createNewFile(
options: FileBrowser.IFileOptions
): Promise<Contents.IModel> {
if (this._filePending) {
return this._filePending;
}
this._filePending = this._createNew({
path: this.model.path,
type: 'file',
ext: options.ext
});
try {
return await this._filePending;
} finally {
this._filePending = null;
}
}
/**
* 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();
}
/**
* cd ..
*
* Go up one level in the directory tree.
*/
async goUp() {
return this.listing.goUp();
}
/**
* 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);
}
/**
* Create the underlying DirListing instance.
*
* @param options - The DirListing constructor options.
*
* @returns The created DirListing instance.
*/
protected createDirListing(options: DirListing.IOptions): DirListing {
return new DirListing(options);
}
protected translator: ITranslator;
/**
* Handle a connection lost signal from the model.
*/
private _onConnectionFailure(
sender: FilterFileBrowserModel,
args: Error
): void {
if (
args instanceof ServerConnection.ResponseError &&
args.response.status === 404
) {
const title = this._trans.__('Directory not found');
args.message = this._trans.__(
'Directory not found: "%1"',
this.model.path
);
void showErrorMessage(title, args);
}
}
/**
* Given a drive name and a local path, return the full
* drive path which includes the drive name and the local path.
*
* @param driveName: the name of the drive
* @param localPath: the local path on the drive.
*
* @returns the full drive path
*/
private _toDrivePath(driveName: string, localPath: string): string {
if (driveName === '') {
return localPath;
} else {
return `${driveName}:${PathExt.removeSlash(localPath)}`;
}
}
protected listing: DirListing;
protected crumbs: BreadCrumbs;
protected mainPanel: Panel;
private _manager: IDocumentManager;
private _directoryPending: Promise<Contents.IModel> | null = null;
private _filePending: Promise<Contents.IModel> | null = null;
private _navigateToCurrentDirectory: boolean;
private _showLastModifiedColumn: boolean = true;
private _showFileSizeColumn: boolean = false;
private _showHiddenFiles: boolean = false;
private _showFileCheckboxes: boolean = false;
private _sortNotebooksFirst: boolean = false;
}
/**
* 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: FilterFileBrowserModel;
/**
* An optional renderer for the directory listing area.
*
* The default is a shared instance of `DirListing.Renderer`.
*/
renderer?: DirListing.IRenderer;
/**
* Whether a file browser automatically restores state when instantiated.
* The default is `true`.
*
* #### Notes
* The file browser model will need to be restored manually for the file
* browser to be able to save its state.
*/
restore?: boolean;
/**
* The application language translator.
*/
translator?: ITranslator;
}
/**
* An options object for creating a file.
*/
export interface IFileOptions {
/**
* The file extension.
*/
ext: string;
}
}