UNPKG

7.49 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { Dialog, setToolbar, ToolbarButton } from '@jupyterlab/apputils';
4import { PathExt } from '@jupyterlab/coreutils';
5import { nullTranslator } from '@jupyterlab/translation';
6import { newFolderIcon, refreshIcon } from '@jupyterlab/ui-components';
7import { PanelLayout, Widget } from '@lumino/widgets';
8import { FileBrowser } from './browser';
9import { FilterFileBrowserModel } from './model';
10import { PromiseDelegate } from '@lumino/coreutils';
11/**
12 * The class name added to open file dialog
13 */
14const OPEN_DIALOG_CLASS = 'jp-Open-Dialog';
15/**
16 * Namespace for file dialog
17 */
18export var FileDialog;
19(function (FileDialog) {
20 /**
21 * Create and show a open files dialog.
22 *
23 * Note: if nothing is selected when `getValue` will return the browser
24 * model current path.
25 *
26 * @param options - The dialog setup options.
27 *
28 * @returns A promise that resolves with whether the dialog was accepted.
29 */
30 async function getOpenFiles(options) {
31 const translator = options.translator || nullTranslator;
32 const trans = translator.load('jupyterlab');
33 const openDialog = new OpenDialog(options.manager, options.filter, translator, options.defaultPath);
34 const dialogOptions = {
35 title: options.title,
36 buttons: [
37 Dialog.cancelButton(),
38 Dialog.okButton({
39 label: trans.__('Select')
40 })
41 ],
42 focusNodeSelector: options.focusNodeSelector,
43 host: options.host,
44 renderer: options.renderer,
45 body: openDialog
46 };
47 await openDialog.ready;
48 const dialog = new Dialog(dialogOptions);
49 return dialog.launch();
50 }
51 FileDialog.getOpenFiles = getOpenFiles;
52 /**
53 * Create and show a open directory dialog.
54 *
55 * Note: if nothing is selected when `getValue` will return the browser
56 * model current path.
57 *
58 * @param options - The dialog setup options.
59 *
60 * @returns A promise that resolves with whether the dialog was accepted.
61 */
62 function getExistingDirectory(options) {
63 return getOpenFiles({
64 ...options,
65 filter: model => {
66 return model.type === 'directory' ? {} : null;
67 }
68 });
69 }
70 FileDialog.getExistingDirectory = getExistingDirectory;
71})(FileDialog || (FileDialog = {}));
72/**
73 * Open dialog widget
74 */
75class OpenDialog extends Widget {
76 constructor(manager, filter, translator, defaultPath, filterDirectories) {
77 super();
78 this._ready = new PromiseDelegate();
79 translator = translator !== null && translator !== void 0 ? translator : nullTranslator;
80 const trans = translator.load('jupyterlab');
81 this.addClass(OPEN_DIALOG_CLASS);
82 Private.createFilteredFileBrowser('filtered-file-browser-dialog', manager, filter, {}, translator, defaultPath, filterDirectories)
83 .then(browser => {
84 this._browser = browser;
85 // Add toolbar items
86 setToolbar(this._browser, (browser) => [
87 {
88 name: 'new-folder',
89 widget: new ToolbarButton({
90 icon: newFolderIcon,
91 onClick: () => {
92 void browser.createNewDirectory();
93 },
94 tooltip: trans.__('New Folder')
95 })
96 },
97 {
98 name: 'refresher',
99 widget: new ToolbarButton({
100 icon: refreshIcon,
101 onClick: () => {
102 browser.model.refresh().catch(reason => {
103 console.error('Failed to refresh file browser in open dialog.', reason);
104 });
105 },
106 tooltip: trans.__('Refresh File List')
107 })
108 }
109 ]);
110 // Build the sub widgets
111 const layout = new PanelLayout();
112 layout.addWidget(this._browser);
113 /**
114 * Dispose browser model when OpenDialog
115 * is disposed.
116 */
117 this.dispose = () => {
118 if (this.isDisposed) {
119 return;
120 }
121 this._browser.model.dispose();
122 super.dispose();
123 };
124 // Set Widget content
125 this.layout = layout;
126 this._ready.resolve();
127 })
128 .catch(reason => {
129 console.error('Error while creating file browser in open dialog', reason);
130 this._ready.reject(void 0);
131 });
132 }
133 /**
134 * Get the selected items.
135 */
136 getValue() {
137 const selection = Array.from(this._browser.selectedItems());
138 if (selection.length === 0) {
139 // Return current path
140 return [
141 {
142 path: this._browser.model.path,
143 name: PathExt.basename(this._browser.model.path),
144 type: 'directory',
145 content: undefined,
146 writable: false,
147 created: 'unknown',
148 last_modified: 'unknown',
149 mimetype: 'text/plain',
150 format: 'text'
151 }
152 ];
153 }
154 else {
155 return selection;
156 }
157 }
158 /**
159 * A promise that resolves when openDialog is successfully created.
160 */
161 get ready() {
162 return this._ready.promise;
163 }
164}
165var Private;
166(function (Private) {
167 /**
168 * Create a new file browser instance.
169 *
170 * @param id - The widget/DOM id of the file browser.
171 *
172 * @param manager - A document manager instance.
173 *
174 * @param filter - function to filter file browser item.
175 *
176 * @param options - The optional file browser configuration object.
177 *
178 * #### Notes
179 * The ID parameter is used to set the widget ID. It is also used as part of
180 * the unique key necessary to store the file browser's restoration data in
181 * the state database if that functionality is enabled.
182 *
183 * If, after the file browser has been generated by the factory, the ID of the
184 * resulting widget is changed by client code, the restoration functionality
185 * will not be disrupted as long as there are no ID collisions, i.e., as long
186 * as the initial ID passed into the factory is used for only one file browser
187 * instance.
188 */
189 Private.createFilteredFileBrowser = async (id, manager, filter, options = {}, translator, defaultPath, filterDirectories) => {
190 translator = translator || nullTranslator;
191 const model = new FilterFileBrowserModel({
192 manager,
193 filter,
194 translator,
195 driveName: options.driveName,
196 refreshInterval: options.refreshInterval,
197 filterDirectories
198 });
199 const widget = new FileBrowser({
200 id,
201 model,
202 translator
203 });
204 if (defaultPath) {
205 await widget.model.cd(defaultPath);
206 }
207 return widget;
208 };
209})(Private || (Private = {}));
210//# sourceMappingURL=opendialog.js.map
\No newline at end of file