UNPKG

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