/
opendialog.ts
216 lines (199 loc) · 5.53 KB
/
opendialog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { toArray } from '@phosphor/algorithm';
import { PanelLayout, Widget } from '@phosphor/widgets';
import { PathExt } from '@jupyterlab/coreutils';
import { Dialog } from '@jupyterlab/apputils';
import { IDocumentManager } from '@jupyterlab/docmanager';
import { Contents } from '@jupyterlab/services';
import { IIconRegistry } from '@jupyterlab/ui-components';
import { FileBrowser } from './browser';
import { FilterFileBrowserModel } from './model';
import { IFileBrowserFactory } from './tokens';
/**
* The class name added to open file dialog
*/
const OPEN_DIALOG_CLASS = 'jp-Open-Dialog';
/**
* Namespace for file dialog
*/
export namespace FileDialog {
/**
* Options for the open directory dialog
*/
export interface IDirectoryOptions
extends Partial<
Pick<
Dialog.IOptions<Promise<Contents.IModel[]>>,
Exclude<
keyof Dialog.IOptions<Promise<Contents.IModel[]>>,
'body' | 'buttons' | 'defaultButton'
>
>
> {
/**
* An icon registry instance.
*/
iconRegistry: IIconRegistry;
/**
* Document manager
*/
manager: IDocumentManager;
}
/**
* Options for the open file dialog
*/
export interface IFileOptions extends IDirectoryOptions {
/**
* Filter function on file browser item model
*/
filter?: (value: Contents.IModel) => boolean;
}
/**
* Create and show a open files dialog.
*
* Note: if nothing is selected when `getValue` will return the browser
* model current path.
*
* @param options - The dialog setup options.
*
* @returns A promise that resolves with whether the dialog was accepted.
*/
export function getOpenFiles(
options: IFileOptions
): Promise<Dialog.IResult<Contents.IModel[]>> {
let dialogOptions: Partial<Dialog.IOptions<Contents.IModel[]>> = {
title: options.title,
buttons: [
Dialog.cancelButton(),
Dialog.okButton({
label: 'Select'
})
],
focusNodeSelector: options.focusNodeSelector,
host: options.host,
renderer: options.renderer,
body: new OpenDialog(
options.iconRegistry,
options.manager,
options.filter
)
};
let dialog = new Dialog(dialogOptions);
return dialog.launch();
}
/**
* Create and show a open directory dialog.
*
* Note: if nothing is selected when `getValue` will return the browser
* model current path.
*
* @param options - The dialog setup options.
*
* @returns A promise that resolves with whether the dialog was accepted.
*/
export function getExistingDirectory(
options: IDirectoryOptions
): Promise<Dialog.IResult<Contents.IModel[]>> {
return getOpenFiles({
...options,
filter: model => false
});
}
}
/**
* Open dialog widget
*/
class OpenDialog extends Widget
implements Dialog.IBodyWidget<Contents.IModel[]> {
constructor(
iconRegistry: IIconRegistry,
manager: IDocumentManager,
filter?: (value: Contents.IModel) => boolean
) {
super();
this.addClass(OPEN_DIALOG_CLASS);
this._browser = Private.createFilteredFileBrowser(
'filtered-file-browser-dialog',
iconRegistry,
manager,
filter
);
// Build the sub widgets
let layout = new PanelLayout();
layout.addWidget(this._browser);
// Set Widget content
this.layout = layout;
}
/**
* Get the selected items.
*/
getValue(): Contents.IModel[] {
const selection = toArray(this._browser.selectedItems());
if (selection.length === 0) {
// Return current path
return [
{
path: this._browser.model.path,
name: PathExt.basename(this._browser.model.path),
type: 'directory',
content: undefined,
writable: false,
created: 'unknown',
last_modified: 'unknown',
mimetype: 'text/plain',
format: 'text'
}
];
} else {
return selection;
}
}
private _browser: FileBrowser;
}
namespace Private {
/**
* Create a new file browser instance.
*
* @param id - The widget/DOM id of the file browser.
*
* @param iconRegistry - An icon registry instance.
*
* @param manager - A document manager instance.
*
* @param filter - function to filter file browser item.
*
* @param options - The optional file browser configuration object.
*
* #### Notes
* The ID parameter is used to set the widget ID. It is also used as part of
* the unique key necessary to store the file browser's restoration data in
* the state database if that functionality is enabled.
*
* If, after the file browser has been generated by the factory, the ID of the
* resulting widget is changed by client code, the restoration functionality
* will not be disrupted as long as there are no ID collisions, i.e., as long
* as the initial ID passed into the factory is used for only one file browser
* instance.
*/
export const createFilteredFileBrowser = (
id: string,
iconRegistry: IIconRegistry,
manager: IDocumentManager,
filter?: (value: Contents.IModel) => boolean,
options: IFileBrowserFactory.IOptions = {}
) => {
const model = new FilterFileBrowserModel({
iconRegistry,
manager,
filter,
driveName: options.driveName,
refreshInterval: options.refreshInterval
});
const widget = new FileBrowser({
id,
model
});
return widget;
};
}