@@ -79,8 +79,6 @@ export class FileBrowser extends SidePanel {
79
79
this . _trans . __ ( 'file browser' )
80
80
) ;
81
81
82
- this . _directoryPending = false ;
83
-
84
82
// File browser widgets container
85
83
this . mainPanel = new Panel ( ) ;
86
84
this . mainPanel . addClass ( FILE_BROWSER_PANEL_CLASS ) ;
@@ -263,63 +261,52 @@ export class FileBrowser extends SidePanel {
263
261
return this . listing . paste ( ) ;
264
262
}
265
263
266
- /**
267
- * Create a new directory
268
- */
269
- createNewDirectory ( ) : void {
270
- if ( this . _directoryPending === true ) {
271
- return ;
264
+ private _createNew (
265
+ future : Private . FutureNewDirectoryItem ,
266
+ createOptions : Contents . ICreateOptions
267
+ ) : Promise < Contents . IModel > {
268
+ if ( future . pending === true ) {
269
+ return future . promise ;
272
270
}
273
- this . _directoryPending = true ;
274
- // TODO: We should provide a hook into when the
275
- // directory is done being created. This probably
276
- // means storing a pendingDirectory promise and
277
- // returning that if there is already a directory
278
- // request.
279
- void this . _manager
280
- . newUntitled ( {
281
- path : this . model . path ,
282
- type : 'directory'
283
- } )
284
- . then ( async model => {
285
- await this . listing . selectItemByName ( model . name ) ;
271
+ const newItem = {
272
+ pending : true ,
273
+ promise : this . _manager . newUntitled ( createOptions ) . then ( async model => {
274
+ await this . listing . selectItemByName ( model . name , true ) ;
286
275
await this . rename ( ) ;
287
- this . _directoryPending = false ;
276
+ return model ;
288
277
} )
278
+ } ;
279
+ Object . assign ( future , newItem ) ;
280
+ const { promise } = newItem ;
281
+ promise
289
282
. catch ( err => {
290
283
void showErrorMessage ( this . _trans . __ ( 'Error' ) , err ) ;
291
- this . _directoryPending = false ;
284
+ } )
285
+ . finally ( ( ) => {
286
+ future . pending = false ;
292
287
} ) ;
288
+ return promise ;
289
+ }
290
+
291
+ /**
292
+ * Create a new directory
293
+ */
294
+ createNewDirectory ( ) : Promise < Contents . IModel > {
295
+ return this . _createNew ( this . _futureNewDirectory , {
296
+ path : this . model . path ,
297
+ type : 'directory'
298
+ } ) ;
293
299
}
294
300
295
301
/**
296
302
* Create a new file
297
303
*/
298
- createNewFile ( options : FileBrowser . IFileOptions ) : void {
299
- if ( this . _filePending === true ) {
300
- return ;
301
- }
302
- this . _filePending = true ;
303
- // TODO: We should provide a hook into when the
304
- // file is done being created. This probably
305
- // means storing a pendingFile promise and
306
- // returning that if there is already a file
307
- // request.
308
- void this . _manager
309
- . newUntitled ( {
310
- path : this . model . path ,
311
- type : 'file' ,
312
- ext : options . ext
313
- } )
314
- . then ( async model => {
315
- await this . listing . selectItemByName ( model . name ) ;
316
- await this . rename ( ) ;
317
- this . _filePending = false ;
318
- } )
319
- . catch ( err => {
320
- void showErrorMessage ( this . _trans . __ ( 'Error' ) , err ) ;
321
- this . _filePending = false ;
322
- } ) ;
304
+ createNewFile ( options : FileBrowser . IFileOptions ) : Promise < Contents . IModel > {
305
+ return this . _createNew ( this . _futureNewFile , {
306
+ path : this . model . path ,
307
+ type : 'file' ,
308
+ ext : options . ext
309
+ } ) ;
323
310
}
324
311
325
312
/**
@@ -347,6 +334,15 @@ export class FileBrowser extends SidePanel {
347
334
return this . listing . download ( ) ;
348
335
}
349
336
337
+ /**
338
+ * cd ..
339
+ *
340
+ * Go up one level in the directory tree.
341
+ */
342
+ async goUp ( ) {
343
+ return this . listing . goUp ( ) ;
344
+ }
345
+
350
346
/**
351
347
* Shut down kernels on the applicable currently selected items.
352
348
*
@@ -420,8 +416,14 @@ export class FileBrowser extends SidePanel {
420
416
421
417
private _filenameSearcher : ReactWidget ;
422
418
private _manager : IDocumentManager ;
423
- private _directoryPending : boolean ;
424
- private _filePending : boolean ;
419
+ private _futureNewDirectory : Private . FutureNewDirectoryItem = {
420
+ pending : false ,
421
+ promise : null
422
+ } ;
423
+ private _futureNewFile : Private . FutureNewDirectoryItem = {
424
+ pending : false ,
425
+ promise : null
426
+ } ;
425
427
private _navigateToCurrentDirectory : boolean ;
426
428
private _showLastModifiedColumn : boolean = true ;
427
429
private _useFuzzyFilter : boolean = true ;
@@ -480,3 +482,21 @@ export namespace FileBrowser {
480
482
ext : string ;
481
483
}
482
484
}
485
+
486
+ namespace Private {
487
+ /**
488
+ * A special type with the following properties:
489
+ * - When `pending` is `true`, the `promise` field points to a promise for a
490
+ * new directory or file.
491
+ * - When `pending` is `false`, the `promise` field has no guarantee.
492
+ */
493
+ export type FutureNewDirectoryItem =
494
+ | {
495
+ pending : true ;
496
+ promise : Promise < Contents . IModel > ;
497
+ }
498
+ | {
499
+ pending : false ;
500
+ promise : any ;
501
+ } ;
502
+ }
0 commit comments