1 |
|
2 |
|
3 | import { showErrorMessage } from '@jupyterlab/apputils';
|
4 | import { PathExt } from '@jupyterlab/coreutils';
|
5 | import { ServerConnection } from '@jupyterlab/services';
|
6 | import { nullTranslator } from '@jupyterlab/translation';
|
7 | import { SidePanel } from '@jupyterlab/ui-components';
|
8 | import { Panel } from '@lumino/widgets';
|
9 | import { BreadCrumbs } from './crumbs';
|
10 | import { DirListing } from './listing';
|
11 |
|
12 |
|
13 |
|
14 | const FILE_BROWSER_CLASS = 'jp-FileBrowser';
|
15 |
|
16 |
|
17 |
|
18 | const FILE_BROWSER_PANEL_CLASS = 'jp-FileBrowser-Panel';
|
19 |
|
20 |
|
21 |
|
22 | const CRUMBS_CLASS = 'jp-FileBrowser-crumbs';
|
23 |
|
24 |
|
25 |
|
26 | const TOOLBAR_CLASS = 'jp-FileBrowser-toolbar';
|
27 |
|
28 |
|
29 |
|
30 | const LISTING_CLASS = 'jp-FileBrowser-listing';
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | export class FileBrowser extends SidePanel {
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 | constructor(options) {
|
45 | var _a;
|
46 | super({ content: new Panel(), translator: options.translator });
|
47 | this._directoryPending = null;
|
48 | this._filePending = null;
|
49 | this._showLastModifiedColumn = true;
|
50 | this._showFileSizeColumn = false;
|
51 | this._showHiddenFiles = false;
|
52 | this._showFileCheckboxes = false;
|
53 | this._sortNotebooksFirst = false;
|
54 | this.addClass(FILE_BROWSER_CLASS);
|
55 | this.toolbar.addClass(TOOLBAR_CLASS);
|
56 | this.id = options.id;
|
57 | const translator = (this.translator = (_a = options.translator) !== null && _a !== void 0 ? _a : nullTranslator);
|
58 | const model = (this.model = options.model);
|
59 | const renderer = options.renderer;
|
60 | model.connectionFailure.connect(this._onConnectionFailure, this);
|
61 | this._manager = model.manager;
|
62 | this.toolbar.node.setAttribute('aria-label', this._trans.__('file browser'));
|
63 |
|
64 | this.mainPanel = new Panel();
|
65 | this.mainPanel.addClass(FILE_BROWSER_PANEL_CLASS);
|
66 | this.mainPanel.title.label = this._trans.__('File Browser');
|
67 | this.crumbs = new BreadCrumbs({ model, translator });
|
68 | this.crumbs.addClass(CRUMBS_CLASS);
|
69 | this.listing = this.createDirListing({
|
70 | model,
|
71 | renderer,
|
72 | translator
|
73 | });
|
74 | this.listing.addClass(LISTING_CLASS);
|
75 | this.mainPanel.addWidget(this.crumbs);
|
76 | this.mainPanel.addWidget(this.listing);
|
77 | this.addWidget(this.mainPanel);
|
78 | if (options.restore !== false) {
|
79 | void model.restore(this.id);
|
80 | }
|
81 | }
|
82 | |
83 |
|
84 |
|
85 | get navigateToCurrentDirectory() {
|
86 | return this._navigateToCurrentDirectory;
|
87 | }
|
88 | set navigateToCurrentDirectory(value) {
|
89 | this._navigateToCurrentDirectory = value;
|
90 | }
|
91 | |
92 |
|
93 |
|
94 | get showLastModifiedColumn() {
|
95 | return this._showLastModifiedColumn;
|
96 | }
|
97 | set showLastModifiedColumn(value) {
|
98 | if (this.listing.setColumnVisibility) {
|
99 | this.listing.setColumnVisibility('last_modified', value);
|
100 | this._showLastModifiedColumn = value;
|
101 | }
|
102 | else {
|
103 | console.warn('Listing does not support toggling column visibility');
|
104 | }
|
105 | }
|
106 | |
107 |
|
108 |
|
109 | get showFullPath() {
|
110 | return this.crumbs.fullPath;
|
111 | }
|
112 | set showFullPath(value) {
|
113 | this.crumbs.fullPath = value;
|
114 | }
|
115 | |
116 |
|
117 |
|
118 | get showFileSizeColumn() {
|
119 | return this._showFileSizeColumn;
|
120 | }
|
121 | set showFileSizeColumn(value) {
|
122 | if (this.listing.setColumnVisibility) {
|
123 | this.listing.setColumnVisibility('file_size', value);
|
124 | this._showFileSizeColumn = value;
|
125 | }
|
126 | else {
|
127 | console.warn('Listing does not support toggling column visibility');
|
128 | }
|
129 | }
|
130 | |
131 |
|
132 |
|
133 | get showHiddenFiles() {
|
134 | return this._showHiddenFiles;
|
135 | }
|
136 | set showHiddenFiles(value) {
|
137 | this.model.showHiddenFiles(value);
|
138 | this._showHiddenFiles = value;
|
139 | }
|
140 | |
141 |
|
142 |
|
143 | get showFileCheckboxes() {
|
144 | return this._showFileCheckboxes;
|
145 | }
|
146 | set showFileCheckboxes(value) {
|
147 | if (this.listing.setColumnVisibility) {
|
148 | this.listing.setColumnVisibility('is_selected', value);
|
149 | this._showFileCheckboxes = value;
|
150 | }
|
151 | else {
|
152 | console.warn('Listing does not support toggling column visibility');
|
153 | }
|
154 | }
|
155 | |
156 |
|
157 |
|
158 | get sortNotebooksFirst() {
|
159 | return this._sortNotebooksFirst;
|
160 | }
|
161 | set sortNotebooksFirst(value) {
|
162 | if (this.listing.setNotebooksFirstSorting) {
|
163 | this.listing.setNotebooksFirstSorting(value);
|
164 | this._sortNotebooksFirst = value;
|
165 | }
|
166 | else {
|
167 | console.warn('Listing does not support sorting notebooks first');
|
168 | }
|
169 | }
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 | selectedItems() {
|
176 | return this.listing.selectedItems();
|
177 | }
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 | async selectItemByName(name) {
|
184 | await this.listing.selectItemByName(name);
|
185 | }
|
186 | clearSelectedItems() {
|
187 | this.listing.clearSelectedItems();
|
188 | }
|
189 | |
190 |
|
191 |
|
192 |
|
193 |
|
194 | rename() {
|
195 | return this.listing.rename();
|
196 | }
|
197 | |
198 |
|
199 |
|
200 | cut() {
|
201 | this.listing.cut();
|
202 | }
|
203 | |
204 |
|
205 |
|
206 | copy() {
|
207 | this.listing.copy();
|
208 | }
|
209 | |
210 |
|
211 |
|
212 |
|
213 |
|
214 | paste() {
|
215 | return this.listing.paste();
|
216 | }
|
217 | async _createNew(options) {
|
218 |
|
219 | if (options.path) {
|
220 | const localPath = this._manager.services.contents.localPath(options.path);
|
221 | options.path = this._toDrivePath(this.model.driveName, localPath);
|
222 | }
|
223 | try {
|
224 | const model = await this._manager.newUntitled(options);
|
225 | await this.listing.selectItemByName(model.name, true);
|
226 | await this.rename();
|
227 | return model;
|
228 | }
|
229 | catch (err) {
|
230 | void showErrorMessage(this._trans.__('Error'), err);
|
231 | throw err;
|
232 | }
|
233 | }
|
234 | |
235 |
|
236 |
|
237 | async createNewDirectory() {
|
238 | if (this._directoryPending) {
|
239 | return this._directoryPending;
|
240 | }
|
241 | this._directoryPending = this._createNew({
|
242 | path: this.model.path,
|
243 | type: 'directory'
|
244 | });
|
245 | try {
|
246 | return await this._directoryPending;
|
247 | }
|
248 | finally {
|
249 | this._directoryPending = null;
|
250 | }
|
251 | }
|
252 | |
253 |
|
254 |
|
255 | async createNewFile(options) {
|
256 | if (this._filePending) {
|
257 | return this._filePending;
|
258 | }
|
259 | this._filePending = this._createNew({
|
260 | path: this.model.path,
|
261 | type: 'file',
|
262 | ext: options.ext
|
263 | });
|
264 | try {
|
265 | return await this._filePending;
|
266 | }
|
267 | finally {
|
268 | this._filePending = null;
|
269 | }
|
270 | }
|
271 | |
272 |
|
273 |
|
274 |
|
275 |
|
276 | delete() {
|
277 | return this.listing.delete();
|
278 | }
|
279 | |
280 |
|
281 |
|
282 |
|
283 |
|
284 | duplicate() {
|
285 | return this.listing.duplicate();
|
286 | }
|
287 | |
288 |
|
289 |
|
290 | download() {
|
291 | return this.listing.download();
|
292 | }
|
293 | |
294 |
|
295 |
|
296 |
|
297 |
|
298 | async goUp() {
|
299 | return this.listing.goUp();
|
300 | }
|
301 | |
302 |
|
303 |
|
304 |
|
305 |
|
306 | shutdownKernels() {
|
307 | return this.listing.shutdownKernels();
|
308 | }
|
309 | |
310 |
|
311 |
|
312 | selectNext() {
|
313 | this.listing.selectNext();
|
314 | }
|
315 | |
316 |
|
317 |
|
318 | selectPrevious() {
|
319 | this.listing.selectPrevious();
|
320 | }
|
321 | |
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | modelForClick(event) {
|
329 | return this.listing.modelForClick(event);
|
330 | }
|
331 | |
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | createDirListing(options) {
|
339 | return new DirListing(options);
|
340 | }
|
341 | |
342 |
|
343 |
|
344 | _onConnectionFailure(sender, args) {
|
345 | if (args instanceof ServerConnection.ResponseError &&
|
346 | args.response.status === 404) {
|
347 | const title = this._trans.__('Directory not found');
|
348 | args.message = this._trans.__('Directory not found: "%1"', this.model.path);
|
349 | void showErrorMessage(title, args);
|
350 | }
|
351 | }
|
352 | |
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | _toDrivePath(driveName, localPath) {
|
362 | if (driveName === '') {
|
363 | return localPath;
|
364 | }
|
365 | else {
|
366 | return `${driveName}:${PathExt.removeSlash(localPath)}`;
|
367 | }
|
368 | }
|
369 | }
|
370 |
|
\ | No newline at end of file |