dbgate/packages/web/src/appobj/AppFileAppObject.svelte

122 lines
3.1 KiB
Svelte
Raw Normal View History

2022-01-27 13:31:46 +00:00
<script lang="ts" context="module">
async function openTextFile(fileName, fileType, folderName, tabComponent, icon) {
const connProps: any = {};
let tooltip = undefined;
const resp = await apiCall('files/load', {
folder: 'app:' + folderName,
2022-01-29 10:30:10 +00:00
file: fileName + '.' + fileType,
2022-01-27 13:31:46 +00:00
format: 'text',
});
openNewTab(
{
title: fileName,
icon,
tabComponent,
tooltip,
props: {
2022-11-03 15:40:54 +00:00
savedFile: fileName + '.' + fileType,
2022-01-27 13:31:46 +00:00
savedFolder: 'app:' + folderName,
savedFormat: 'text',
appFolder: folderName,
...connProps,
},
},
{ editor: resp }
);
}
export const extractKey = data => data.fileName;
2022-11-03 15:40:54 +00:00
export const createMatcher =
({ fileName }) =>
filter =>
filterName(filter, fileName);
2022-01-27 13:31:46 +00:00
const APP_ICONS = {
2022-01-29 10:30:10 +00:00
'config.json': 'img json',
2022-01-27 13:31:46 +00:00
'command.sql': 'img app-command',
'query.sql': 'img app-query',
};
2022-01-27 16:01:58 +00:00
function getAppIcon(data) {
2022-01-27 13:31:46 +00:00
return APP_ICONS[data.fileType];
}
</script>
<script lang="ts">
import _ from 'lodash';
import { filterName } from 'dbgate-tools';
import { showModal } from '../modals/modalTools';
import openNewTab from '../utility/openNewTab';
import AppObjectCore from './AppObjectCore.svelte';
import InputTextModal from '../modals/InputTextModal.svelte';
import ConfirmModal from '../modals/ConfirmModal.svelte';
import { apiCall } from '../utility/api';
export let data;
const handleRename = () => {
showModal(InputTextModal, {
value: data.fileName,
label: 'New file name',
header: 'Rename file',
onConfirm: newFile => {
apiCall('apps/rename-file', {
file: data.fileName,
folder: data.folderName,
fileType: data.fileType,
newFile,
});
},
});
};
const handleDelete = () => {
showModal(ConfirmModal, {
message: `Really delete file ${data.fileName}?`,
onConfirm: () => {
apiCall('apps/delete-file', {
file: data.fileName,
folder: data.folderName,
fileType: data.fileType,
});
},
});
};
const handleClick = () => {
if (data.fileType.endsWith('.sql')) {
handleOpenSqlFile();
}
2022-01-28 16:36:40 +00:00
if (data.fileType.endsWith('.json')) {
handleOpenJsonFile();
}
2022-01-27 13:31:46 +00:00
};
const handleOpenSqlFile = () => {
openTextFile(data.fileName, data.fileType, data.folderName, 'QueryTab', 'img sql-file');
};
2022-01-28 16:36:40 +00:00
const handleOpenJsonFile = () => {
openTextFile(data.fileName, data.fileType, data.folderName, 'JsonEditorTab', 'img json');
2022-01-27 13:31:46 +00:00
};
function createMenu() {
return [
{ text: 'Delete', onClick: handleDelete },
{ text: 'Rename', onClick: handleRename },
data.fileType.endsWith('.sql') && { text: 'Open SQL', onClick: handleOpenSqlFile },
2022-01-28 16:36:40 +00:00
data.fileType.endsWith('.json') && { text: 'Open JSON', onClick: handleOpenJsonFile },
2022-01-27 16:01:58 +00:00
2022-01-27 13:31:46 +00:00
// data.fileType.endsWith('.yaml') && { text: 'Open YAML', onClick: handleOpenYamlFile },
];
}
</script>
<AppObjectCore
{...$$restProps}
{data}
title={data.fileLabel}
2022-01-27 16:01:58 +00:00
icon={getAppIcon(data)}
2022-01-27 13:31:46 +00:00
menu={createMenu}
on:click={handleClick}
/>