UNPKG

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