-
Notifications
You must be signed in to change notification settings - Fork 659
/
FileManagerModule.jsx
128 lines (115 loc) · 3.5 KB
/
FileManagerModule.jsx
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
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import gettext from 'sources/gettext';
import React from 'react';
import FileManager from './components/FileManager';
import { getBrowser } from '../../../../static/js/utils';
import pgAdmin from 'sources/pgadmin';
export default class FileManagerModule {
static instance;
static getInstance(...args) {
if(!FileManagerModule.instance) {
FileManagerModule.instance = new FileManagerModule(...args);
}
return FileManagerModule.instance;
}
constructor(pgAdmin) {
this.pgAdmin = pgAdmin;
}
init() {
if(this.initialized)
return;
this.initialized = true;
if(this.pgAdmin.server_mode == 'True') {
// Define the nodes on which the menus to be appear
this.pgAdmin.Browser.add_menus([{
name: 'storage_manager',
module: this,
applies: ['tools'],
callback: 'openStorageManager',
priority: 11,
label: gettext('Storage Manager...'),
enable: true,
}]);
}
}
openStorageManager(path) {
this.show({
dialog_type: 'storage_dialog',
supported_types: ['sql', 'csv', 'json', '*'],
dialog_title: gettext('Storage Manager'),
path: path,
});
}
showInternal(params, onOK, onCancel, modalObj) {
const modal = modalObj || pgAdmin.Browser.notifier;
let title = params.dialog_title;
if(!title) {
if(params.dialog_type == 'create_file') {
title = gettext('Save File');
} else if(params.dialog_type == 'select_file') {
title = gettext('Select File');
} else {
title = gettext('Storage Manager');
}
}
modal.showModal(title, (closeModal)=>{
return (
<FileManager
params={params}
closeModal={closeModal}
onCancel={onCancel}
onOK={onOK}
sharedStorages={this.pgAdmin.server_mode == 'True' ? this.pgAdmin.shared_storage: []}
restrictedSharedStorage={this.pgAdmin.server_mode == 'True' ? this.pgAdmin.restricted_shared_storage: []}
/>
);
}, {
isResizeable: true,
onClose: onCancel,
dialogWidth: 700, dialogHeight: 400
});
}
async showNative(params, onOK, onCancel) {
let res;
let options = {};
options['filters'] = params.supported_types?.map((v)=>(
v=='*' ? {name: 'All Files', extensions: ['*']} :
{name: `${v.toUpperCase()} File .${v}`, extensions:[v]}
));
if(params.dialog_type == 'create_file') {
res = await window.electronUI.showSaveDialog(options);
} else {
options['properties'] = ['openFile'];
if(params.dialog_type == 'select_folder') {
options['properties'] = ['openDirectory'];
}
res = await window.electronUI.showOpenDialog(options);
}
if(res.canceled) {
onCancel?.();
} else {
onOK?.(res.filePath ? res.filePath : res.filePaths[0]);
}
}
show(params, onOK, onCancel, modalObj) {
let {name: browser} = getBrowser();
if(browser == 'Electron') {
try {
this.showNative(params, onOK, onCancel);
} catch {
// Fall back to internal
this.showInternal(params, onOK, onCancel, modalObj);
}
} else {
// Fall back to internal
this.showInternal(params, onOK, onCancel, modalObj);
}
}
}