@jupyterlab/filebrowser 4.0.0-alpha.2 → 4.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,257 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { Dialog, setToolbar, ToolbarButton } from '@jupyterlab/apputils';
5
+ import { PathExt } from '@jupyterlab/coreutils';
6
+ import { IDocumentManager } from '@jupyterlab/docmanager';
7
+ import { Contents } from '@jupyterlab/services';
8
+ import { ITranslator, nullTranslator } from '@jupyterlab/translation';
9
+ import { IScore, newFolderIcon, refreshIcon } from '@jupyterlab/ui-components';
10
+ import { PanelLayout, Widget } from '@lumino/widgets';
11
+ import { FileBrowser } from './browser';
12
+ import { FilterFileBrowserModel } from './model';
13
+ import { IFileBrowserFactory } from './tokens';
14
+
15
+ /**
16
+ * The class name added to open file dialog
17
+ */
18
+ const OPEN_DIALOG_CLASS = 'jp-Open-Dialog';
19
+
20
+ /**
21
+ * Namespace for file dialog
22
+ */
23
+ export namespace FileDialog {
24
+ /**
25
+ * Options for the open directory dialog
26
+ */
27
+ export interface IDirectoryOptions
28
+ extends Partial<
29
+ Pick<
30
+ Dialog.IOptions<Promise<Contents.IModel[]>>,
31
+ Exclude<
32
+ keyof Dialog.IOptions<Promise<Contents.IModel[]>>,
33
+ 'body' | 'buttons' | 'defaultButton'
34
+ >
35
+ >
36
+ > {
37
+ /**
38
+ * Document manager
39
+ */
40
+ manager: IDocumentManager;
41
+
42
+ /**
43
+ * The application language translator.
44
+ */
45
+ translator?: ITranslator;
46
+ }
47
+
48
+ /**
49
+ * Options for the open file dialog
50
+ */
51
+ export interface IFileOptions extends IDirectoryOptions {
52
+ /**
53
+ * Filter function on file browser item model
54
+ */
55
+ filter?: (value: Contents.IModel) => Partial<IScore> | null;
56
+
57
+ /**
58
+ * The application language translator.
59
+ */
60
+ translator?: ITranslator;
61
+ }
62
+
63
+ /**
64
+ * Create and show a open files dialog.
65
+ *
66
+ * Note: if nothing is selected when `getValue` will return the browser
67
+ * model current path.
68
+ *
69
+ * @param options - The dialog setup options.
70
+ *
71
+ * @returns A promise that resolves with whether the dialog was accepted.
72
+ */
73
+ export function getOpenFiles(
74
+ options: IFileOptions
75
+ ): Promise<Dialog.IResult<Contents.IModel[]>> {
76
+ const translator = options.translator || nullTranslator;
77
+ const trans = translator.load('jupyterlab');
78
+ const dialogOptions: Partial<Dialog.IOptions<Contents.IModel[]>> = {
79
+ title: options.title,
80
+ buttons: [
81
+ Dialog.cancelButton(),
82
+ Dialog.okButton({
83
+ label: trans.__('Select')
84
+ })
85
+ ],
86
+ focusNodeSelector: options.focusNodeSelector,
87
+ host: options.host,
88
+ renderer: options.renderer,
89
+ body: new OpenDialog(options.manager, options.filter, translator)
90
+ };
91
+ const dialog = new Dialog(dialogOptions);
92
+ return dialog.launch();
93
+ }
94
+
95
+ /**
96
+ * Create and show a open directory dialog.
97
+ *
98
+ * Note: if nothing is selected when `getValue` will return the browser
99
+ * model current path.
100
+ *
101
+ * @param options - The dialog setup options.
102
+ *
103
+ * @returns A promise that resolves with whether the dialog was accepted.
104
+ */
105
+ export function getExistingDirectory(
106
+ options: IDirectoryOptions
107
+ ): Promise<Dialog.IResult<Contents.IModel[]>> {
108
+ return getOpenFiles({
109
+ ...options,
110
+ filter: model => {
111
+ return model.type === 'directory' ? {} : null;
112
+ }
113
+ });
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Open dialog widget
119
+ */
120
+ class OpenDialog
121
+ extends Widget
122
+ implements Dialog.IBodyWidget<Contents.IModel[]>
123
+ {
124
+ constructor(
125
+ manager: IDocumentManager,
126
+ filter?: (value: Contents.IModel) => Partial<IScore> | null,
127
+ translator?: ITranslator,
128
+ filterDirectories?: boolean
129
+ ) {
130
+ super();
131
+ translator = translator ?? nullTranslator;
132
+ const trans = translator.load('jupyterlab');
133
+ this.addClass(OPEN_DIALOG_CLASS);
134
+
135
+ this._browser = Private.createFilteredFileBrowser(
136
+ 'filtered-file-browser-dialog',
137
+ manager,
138
+ filter,
139
+ {},
140
+ translator,
141
+ filterDirectories
142
+ );
143
+
144
+ // Add toolbar items
145
+ setToolbar(this._browser, (browser: FileBrowser) => [
146
+ {
147
+ name: 'new-folder',
148
+ widget: new ToolbarButton({
149
+ icon: newFolderIcon,
150
+ onClick: () => {
151
+ browser.createNewDirectory();
152
+ },
153
+ tooltip: trans.__('New Folder')
154
+ })
155
+ },
156
+ {
157
+ name: 'refresher',
158
+ widget: new ToolbarButton({
159
+ icon: refreshIcon,
160
+ onClick: () => {
161
+ browser.model.refresh().catch(reason => {
162
+ console.error(
163
+ 'Failed to refresh file browser in open dialog.',
164
+ reason
165
+ );
166
+ });
167
+ },
168
+ tooltip: trans.__('Refresh File List')
169
+ })
170
+ }
171
+ ]);
172
+
173
+ // Build the sub widgets
174
+ const layout = new PanelLayout();
175
+ layout.addWidget(this._browser);
176
+
177
+ // Set Widget content
178
+ this.layout = layout;
179
+ }
180
+
181
+ /**
182
+ * Get the selected items.
183
+ */
184
+ getValue(): Contents.IModel[] {
185
+ const selection = Array.from(this._browser.selectedItems());
186
+ if (selection.length === 0) {
187
+ // Return current path
188
+ return [
189
+ {
190
+ path: this._browser.model.path,
191
+ name: PathExt.basename(this._browser.model.path),
192
+ type: 'directory',
193
+ content: undefined,
194
+ writable: false,
195
+ created: 'unknown',
196
+ last_modified: 'unknown',
197
+ mimetype: 'text/plain',
198
+ format: 'text'
199
+ }
200
+ ];
201
+ } else {
202
+ return selection;
203
+ }
204
+ }
205
+
206
+ private _browser: FileBrowser;
207
+ }
208
+
209
+ namespace Private {
210
+ /**
211
+ * Create a new file browser instance.
212
+ *
213
+ * @param id - The widget/DOM id of the file browser.
214
+ *
215
+ * @param manager - A document manager instance.
216
+ *
217
+ * @param filter - function to filter file browser item.
218
+ *
219
+ * @param options - The optional file browser configuration object.
220
+ *
221
+ * #### Notes
222
+ * The ID parameter is used to set the widget ID. It is also used as part of
223
+ * the unique key necessary to store the file browser's restoration data in
224
+ * the state database if that functionality is enabled.
225
+ *
226
+ * If, after the file browser has been generated by the factory, the ID of the
227
+ * resulting widget is changed by client code, the restoration functionality
228
+ * will not be disrupted as long as there are no ID collisions, i.e., as long
229
+ * as the initial ID passed into the factory is used for only one file browser
230
+ * instance.
231
+ */
232
+ export const createFilteredFileBrowser = (
233
+ id: string,
234
+ manager: IDocumentManager,
235
+ filter?: (value: Contents.IModel) => Partial<IScore> | null,
236
+ options: IFileBrowserFactory.IOptions = {},
237
+ translator?: ITranslator,
238
+ filterDirectories?: boolean
239
+ ): FileBrowser => {
240
+ translator = translator || nullTranslator;
241
+ const model = new FilterFileBrowserModel({
242
+ manager,
243
+ filter,
244
+ translator,
245
+ driveName: options.driveName,
246
+ refreshInterval: options.refreshInterval,
247
+ filterDirectories
248
+ });
249
+ const widget = new FileBrowser({
250
+ id,
251
+ model,
252
+ translator
253
+ });
254
+
255
+ return widget;
256
+ };
257
+ }
package/src/tokens.ts ADDED
@@ -0,0 +1,122 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import type { WidgetTracker } from '@jupyterlab/apputils';
5
+ import type { IStateDB } from '@jupyterlab/statedb';
6
+ import { Token } from '@lumino/coreutils';
7
+ import type { FileBrowser } from './browser';
8
+
9
+ /**
10
+ * The file browser factory token.
11
+ */
12
+ export const IFileBrowserFactory = new Token<IFileBrowserFactory>(
13
+ '@jupyterlab/filebrowser:IFileBrowserFactory'
14
+ );
15
+
16
+ /**
17
+ * The default file browser token.
18
+ */
19
+ export const IDefaultFileBrowser = new Token<IDefaultFileBrowser>(
20
+ '@jupyterlab/filebrowser:IDefaultFileBrowser'
21
+ );
22
+
23
+ /**
24
+ * Default file browser type.
25
+ */
26
+ export type IDefaultFileBrowser = FileBrowser;
27
+
28
+ /**
29
+ * The file browser factory interface.
30
+ */
31
+ export interface IFileBrowserFactory {
32
+ /**
33
+ * Create a new file browser instance.
34
+ *
35
+ * @param id - The widget/DOM id of the file browser.
36
+ *
37
+ * @param options - The optional file browser configuration object.
38
+ *
39
+ * #### Notes
40
+ * The ID parameter is used to set the widget ID. It is also used as part of
41
+ * the unique key necessary to store the file browser's restoration data in
42
+ * the state database if that functionality is enabled.
43
+ *
44
+ * If, after the file browser has been generated by the factory, the ID of the
45
+ * resulting widget is changed by client code, the restoration functionality
46
+ * will not be disrupted as long as there are no ID collisions, i.e., as long
47
+ * as the initial ID passed into the factory is used for only one file browser
48
+ * instance.
49
+ */
50
+ createFileBrowser(
51
+ id: string,
52
+ options?: IFileBrowserFactory.IOptions
53
+ ): FileBrowser;
54
+
55
+ /**
56
+ * The widget tracker used by the factory to track file browsers.
57
+ */
58
+ readonly tracker: WidgetTracker<FileBrowser>;
59
+ }
60
+
61
+ /**
62
+ * A namespace for file browser factory interfaces.
63
+ */
64
+ export namespace IFileBrowserFactory {
65
+ /**
66
+ * The options for creating a file browser using a file browser factory.
67
+ *
68
+ * #### Notes
69
+ * In future versions of JupyterLab, some of these options may disappear,
70
+ * which is a backward-incompatible API change and will necessitate a new
71
+ * version release. This is because in future versions, there will likely be
72
+ * an application-wide notion of a singleton command registry and a singleton
73
+ * state database.
74
+ */
75
+ export interface IOptions {
76
+ /**
77
+ * Whether a file browser automatically loads its initial path.
78
+ *
79
+ * #### Notes
80
+ * The default is `true`.
81
+ */
82
+ auto?: boolean;
83
+
84
+ /**
85
+ * An optional `Contents.IDrive` name for the model.
86
+ * If given, the model will prepend `driveName:` to
87
+ * all paths used in file operations.
88
+ */
89
+ driveName?: string;
90
+
91
+ /**
92
+ * The time interval for browser refreshing, in ms.
93
+ */
94
+ refreshInterval?: number;
95
+
96
+ /**
97
+ * Whether a file browser automatically restores state when instantiated.
98
+ * The default is `true`.
99
+ *
100
+ * #### Notes
101
+ * The file browser model will need to be restored before for the file
102
+ * browser to start saving its state.
103
+ */
104
+ restore?: boolean;
105
+
106
+ /**
107
+ * The state database to use for saving file browser state and restoring it.
108
+ *
109
+ * #### Notes
110
+ * Unless the value `null` is set for this option, the application state
111
+ * database will be automatically passed in and used for state restoration.
112
+ */
113
+ state?: IStateDB | null;
114
+ }
115
+ }
116
+
117
+ /**
118
+ * The token that indicates the default file browser commands are loaded.
119
+ */
120
+ export const IFileBrowserCommands = new Token<void>(
121
+ '@jupyterlab/filebrowser:IFileBrowserCommands'
122
+ );
package/src/upload.ts ADDED
@@ -0,0 +1,119 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { showErrorMessage } from '@jupyterlab/apputils';
5
+ import {
6
+ ITranslator,
7
+ nullTranslator,
8
+ TranslationBundle
9
+ } from '@jupyterlab/translation';
10
+ import { fileUploadIcon, ToolbarButton } from '@jupyterlab/ui-components';
11
+ import { FileBrowserModel } from './model';
12
+
13
+ /**
14
+ * A widget which provides an upload button.
15
+ */
16
+ export class Uploader extends ToolbarButton {
17
+ /**
18
+ * Construct a new file browser buttons widget.
19
+ */
20
+ constructor(options: Uploader.IOptions) {
21
+ super({
22
+ icon: fileUploadIcon,
23
+ label: options.label,
24
+ onClick: () => {
25
+ this._input.click();
26
+ },
27
+ tooltip: Private.translateToolTip(options.translator)
28
+ });
29
+ this.fileBrowserModel = options.model;
30
+ this.translator = options.translator || nullTranslator;
31
+ this._trans = this.translator.load('jupyterlab');
32
+ this._input.onclick = this._onInputClicked;
33
+ this._input.onchange = this._onInputChanged;
34
+ this.addClass('jp-id-upload');
35
+ }
36
+
37
+ /**
38
+ * The underlying file browser fileBrowserModel for the widget.
39
+ *
40
+ * This cannot be named model as that conflicts with the model property of VDomRenderer.
41
+ */
42
+ readonly fileBrowserModel: FileBrowserModel;
43
+
44
+ /**
45
+ * The 'change' handler for the input field.
46
+ */
47
+ private _onInputChanged = () => {
48
+ const files = Array.prototype.slice.call(this._input.files) as File[];
49
+ const pending = files.map(file => this.fileBrowserModel.upload(file));
50
+ void Promise.all(pending).catch(error => {
51
+ void showErrorMessage(
52
+ this._trans._p('showErrorMessage', 'Upload Error'),
53
+ error
54
+ );
55
+ });
56
+ };
57
+
58
+ /**
59
+ * The 'click' handler for the input field.
60
+ */
61
+ private _onInputClicked = () => {
62
+ // In order to allow repeated uploads of the same file (with delete in between),
63
+ // we need to clear the input value to trigger a change event.
64
+ this._input.value = '';
65
+ };
66
+
67
+ protected translator: ITranslator;
68
+ private _trans: TranslationBundle;
69
+ private _input = Private.createUploadInput();
70
+ }
71
+
72
+ /**
73
+ * The namespace for Uploader class statics.
74
+ */
75
+ export namespace Uploader {
76
+ /**
77
+ * The options used to create an uploader.
78
+ */
79
+ export interface IOptions {
80
+ /**
81
+ * A file browser fileBrowserModel instance.
82
+ */
83
+ model: FileBrowserModel;
84
+
85
+ /**
86
+ * The language translator.
87
+ */
88
+ translator?: ITranslator;
89
+
90
+ /**
91
+ * An optional label.
92
+ */
93
+ label?: string;
94
+ }
95
+ }
96
+
97
+ /**
98
+ * The namespace for module private data.
99
+ */
100
+ namespace Private {
101
+ /**
102
+ * Create the upload input node for a file buttons widget.
103
+ */
104
+ export function createUploadInput(): HTMLInputElement {
105
+ const input = document.createElement('input');
106
+ input.type = 'file';
107
+ input.multiple = true;
108
+ return input;
109
+ }
110
+
111
+ /**
112
+ * Translate upload tooltip.
113
+ */
114
+ export function translateToolTip(translator?: ITranslator): string {
115
+ translator = translator || nullTranslator;
116
+ const trans = translator.load('jupyterlab');
117
+ return trans.__('Upload Files');
118
+ }
119
+ }