4
4
import {
5
5
CommandToolbarButtonComponent ,
6
6
ReactWidget ,
7
- UseSignal ,
8
7
} from '@jupyterlab/apputils' ;
9
8
10
9
import { FileBrowser } from '@jupyterlab/filebrowser' ;
@@ -17,113 +16,74 @@ import { ISignal } from '@lumino/signaling';
17
16
18
17
import React from 'react' ;
19
18
20
- /**
21
- * A React component to display the list of command toolbar buttons.
22
- *
23
- */
24
- const Commands = ( {
25
- commands,
26
- browser,
27
- translator,
28
- } : {
29
- commands : CommandRegistry ;
30
- browser : FileBrowser ;
31
- translator : ITranslator ;
32
- } ) : JSX . Element => {
33
- const trans = translator . load ( 'notebook' ) ;
34
- const selection = Array . from ( browser . selectedItems ( ) ) ;
35
- const oneFolder = selection . some ( ( item ) => item . type === 'directory' ) ;
36
- const multipleFiles =
37
- selection . filter ( ( item ) => item . type === 'file' ) . length > 1 ;
38
- if ( selection . length === 0 ) {
39
- return < div > { trans . __ ( 'Select items to perform actions on them.' ) } </ div > ;
40
- } else {
41
- const buttons = [ 'delete' ] ;
42
- if ( ! oneFolder ) {
43
- buttons . unshift ( 'duplicate' ) ;
44
- if ( ! multipleFiles ) {
45
- buttons . unshift ( 'rename' ) ;
46
- }
47
- buttons . unshift ( 'download' ) ;
48
- buttons . unshift ( 'open' ) ;
49
- } else if ( selection . length === 1 ) {
50
- buttons . unshift ( 'rename' ) ;
51
- }
19
+ export class FilesActionButtons {
20
+ /**
21
+ * The constructor of FilesActionButtons.
22
+ * @param options
23
+ */
24
+ constructor ( options : {
25
+ commands : CommandRegistry ;
26
+ browser : FileBrowser ;
27
+ selectionChanged : ISignal < FileBrowser , void > ;
28
+ translator : ITranslator ;
29
+ } ) {
30
+ this . _browser = options . browser ;
31
+ const { commands, selectionChanged, translator } = options ;
32
+ const trans = translator . load ( 'notebook' ) ;
52
33
53
- return (
54
- < >
55
- { buttons . map ( ( action ) => (
56
- < CommandToolbarButtonComponent
57
- key = { action }
58
- commands = { commands }
59
- id = { `filebrowser:${ action } ` }
60
- args = { { toolbar : true } }
61
- icon = { undefined }
62
- />
63
- ) ) }
64
- </ >
34
+ // Placeholder, when no file is selected.
35
+ const placeholder = ReactWidget . create (
36
+ < div key = { 'placeholder' } >
37
+ { trans . __ ( 'Select items to perform actions on them.' ) }
38
+ </ div >
65
39
) ;
66
- }
67
- } ;
40
+ placeholder . id = 'fileAction-placeholder' ;
41
+ this . _widgets . set ( 'placeholder' , placeholder ) ;
68
42
69
- /**
70
- * A React component to display the file action buttons in the file browser toolbar.
71
- *
72
- * @param translator The Translation service
73
- */
74
- const FileActions = ( {
75
- commands,
76
- browser,
77
- selectionChanged,
78
- translator,
79
- } : {
80
- commands : CommandRegistry ;
81
- browser : FileBrowser ;
82
- selectionChanged : ISignal < FileBrowser , void > ;
83
- translator : ITranslator ;
84
- } ) : JSX . Element => {
85
- return (
86
- < UseSignal signal = { selectionChanged } shouldUpdate = { ( ) => true } >
87
- { ( ) : JSX . Element => (
88
- < Commands
43
+ // The action buttons.
44
+ const actions = [ 'open' , 'download' , 'rename' , 'duplicate' , 'delete' ] ;
45
+ actions . forEach ( ( action ) => {
46
+ const widget = ReactWidget . create (
47
+ < CommandToolbarButtonComponent
48
+ key = { action }
89
49
commands = { commands }
90
- browser = { browser }
91
- translator = { translator }
50
+ id = { `filebrowser:${ action } ` }
51
+ args = { { toolbar : true } }
52
+ icon = { undefined }
92
53
/>
93
- ) }
94
- </ UseSignal >
95
- ) ;
96
- } ;
54
+ ) ;
55
+ widget . id = `fileAction-${ action } ` ;
56
+ widget . addClass ( 'jp-FileAction' ) ;
57
+ this . _widgets . set ( action , widget ) ;
58
+ } ) ;
59
+
60
+ selectionChanged . connect ( this . _onSelectionChanged , this ) ;
61
+ this . _onSelectionChanged ( ) ;
62
+ }
97
63
98
- /**
99
- * A namespace for FileActionsComponent static functions.
100
- */
101
- export namespace FileActionsComponent {
102
64
/**
103
- * Create a new FileActionsComponent
104
- *
105
- * @param translator The translator
65
+ * Return an iterator with all the action widgets.
106
66
*/
107
- export const create = ( {
108
- commands,
109
- browser,
110
- selectionChanged,
111
- translator,
112
- } : {
113
- commands : CommandRegistry ;
114
- browser : FileBrowser ;
115
- selectionChanged : ISignal < FileBrowser , void > ;
116
- translator : ITranslator ;
117
- } ) : ReactWidget => {
118
- const widget = ReactWidget . create (
119
- < FileActions
120
- commands = { commands }
121
- browser = { browser }
122
- selectionChanged = { selectionChanged }
123
- translator = { translator }
124
- />
125
- ) ;
126
- widget . addClass ( 'jp-FileActions' ) ;
127
- return widget ;
67
+ get widgets ( ) : IterableIterator < ReactWidget > {
68
+ return this . _widgets . values ( ) ;
69
+ }
70
+
71
+ /**
72
+ * Triggered when the selection change in file browser.
73
+ */
74
+ private _onSelectionChanged = ( ) => {
75
+ const selectedItems = Array . from ( this . _browser . selectedItems ( ) ) ;
76
+ const selection = selectedItems . length > 0 ;
77
+ const oneFolder = selectedItems . some ( ( item ) => item . type === 'directory' ) ;
78
+
79
+ this . _widgets . get ( 'placeholder' ) ?. setHidden ( selection ) ;
80
+ this . _widgets . get ( 'delete' ) ?. setHidden ( ! selection ) ;
81
+ this . _widgets . get ( 'duplicate' ) ?. setHidden ( ! selection || oneFolder ) ;
82
+ this . _widgets . get ( 'download' ) ?. setHidden ( ! selection || oneFolder ) ;
83
+ this . _widgets . get ( 'open' ) ?. setHidden ( ! selection || oneFolder ) ;
84
+ this . _widgets . get ( 'rename' ) ?. setHidden ( selectedItems . length !== 1 ) ;
128
85
} ;
86
+
87
+ private _browser : FileBrowser ;
88
+ private _widgets = new Map < string , ReactWidget > ( ) ;
129
89
}
0 commit comments