| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
import { |
| 5 |
CommandToolbarButtonComponent, |
| 6 |
ReactWidget, |
| 7 |
} from '@jupyterlab/apputils'; |
| 8 |
|
| 9 |
import { FileBrowser } from '@jupyterlab/filebrowser'; |
| 10 |
|
| 11 |
import { ITranslator } from '@jupyterlab/translation'; |
| 12 |
|
| 13 |
import { CommandRegistry } from '@lumino/commands'; |
| 14 |
|
| 15 |
import { ISignal } from '@lumino/signaling'; |
| 16 |
|
| 17 |
import React from 'react'; |
| 18 |
|
| 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'); |
| 33 |
|
| 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> |
| 39 |
); |
| 40 |
placeholder.id = 'fileAction-placeholder'; |
| 41 |
this._widgets.set('placeholder', placeholder); |
| 42 |
|
| 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} |
| 49 |
commands={commands} |
| 50 |
id={`filebrowser:${action}`} |
| 51 |
args={{ toolbar: true }} |
| 52 |
icon={undefined} |
| 53 |
/> |
| 54 |
); |
| 55 |
widget.id = `fileAction-${action}`; |
| 56 |
widget.addClass('jp-ToolbarButton'); |
| 57 |
widget.addClass('jp-FileAction'); |
| 58 |
this._widgets.set(action, widget); |
| 59 |
}); |
| 60 |
|
| 61 |
selectionChanged.connect(this._onSelectionChanged, this); |
| 62 |
this._onSelectionChanged(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Return an iterator with all the action widgets. |
| 67 |
*/ |
| 68 |
get widgets(): IterableIterator<ReactWidget> { |
| 69 |
return this._widgets.values(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Triggered when the selection change in file browser. |
| 74 |
*/ |
| 75 |
private _onSelectionChanged = () => { |
| 76 |
const selectedItems = Array.from(this._browser.selectedItems()); |
| 77 |
const selection = selectedItems.length > 0; |
| 78 |
const oneFolder = selectedItems.some((item) => item.type === 'directory'); |
| 79 |
|
| 80 |
this._widgets.get('placeholder')?.setHidden(selection); |
| 81 |
this._widgets.get('delete')?.setHidden(!selection); |
| 82 |
this._widgets.get('duplicate')?.setHidden(!selection || oneFolder); |
| 83 |
this._widgets.get('download')?.setHidden(!selection || oneFolder); |
| 84 |
this._widgets.get('open')?.setHidden(!selection || oneFolder); |
| 85 |
this._widgets.get('rename')?.setHidden(selectedItems.length !== 1); |
| 86 |
}; |
| 87 |
|
| 88 |
private _browser: FileBrowser; |
| 89 |
private _widgets = new Map<string, ReactWidget>(); |
| 90 |
} |