|
|
|
@ -1,9 +1,9 @@ |
|
|
|
<script> |
|
|
|
import { onMount } from "svelte" |
|
|
|
import { get } from "svelte/store" |
|
|
|
import { goto } from "@roxi/routify" |
|
|
|
import { goto, params } from "@roxi/routify" |
|
|
|
import { BUDIBASE_INTERNAL_DB } from "constants" |
|
|
|
import { database, datasources, queries, tables } from "stores/backend" |
|
|
|
import { database, datasources, queries, tables, views } from "stores/backend" |
|
|
|
import EditDatasourcePopover from "./popovers/EditDatasourcePopover.svelte" |
|
|
|
import EditQueryPopover from "./popovers/EditQueryPopover.svelte" |
|
|
|
import NavItem from "components/common/NavItem.svelte" |
|
|
|
@ -11,16 +11,27 @@ |
|
|
|
import ICONS from "./icons" |
|
|
|
|
|
|
|
let openDataSources = [] |
|
|
|
$: enrichedDataSources = $datasources.list.map(datasource => ({ |
|
|
|
...datasource, |
|
|
|
open: |
|
|
|
openDataSources.includes(datasource._id) || |
|
|
|
containsActiveTable(datasource), |
|
|
|
selected: $datasources.selected === datasource._id, |
|
|
|
})) |
|
|
|
$: enrichedDataSources = $datasources.list.map(datasource => { |
|
|
|
const selected = $datasources.selected === datasource._id |
|
|
|
const open = openDataSources.includes(datasource._id) |
|
|
|
const containsSelected = containsActiveEntity(datasource) |
|
|
|
return { |
|
|
|
...datasource, |
|
|
|
selected, |
|
|
|
open: selected || open || containsSelected, |
|
|
|
} |
|
|
|
}) |
|
|
|
$: openDataSource = enrichedDataSources.find(x => x.open) |
|
|
|
$: { |
|
|
|
// Ensure the open data source is always included in the list of open |
|
|
|
// data sources |
|
|
|
if (openDataSource) { |
|
|
|
openNode(openDataSource) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function selectDatasource(datasource) { |
|
|
|
toggleNode(datasource) |
|
|
|
openNode(datasource) |
|
|
|
datasources.select(datasource._id) |
|
|
|
$goto(`./datasource/${datasource._id}`) |
|
|
|
} |
|
|
|
@ -30,12 +41,22 @@ |
|
|
|
$goto(`./datasource/${query.datasourceId}/${query._id}`) |
|
|
|
} |
|
|
|
|
|
|
|
function closeNode(datasource) { |
|
|
|
openDataSources = openDataSources.filter(id => datasource._id !== id) |
|
|
|
} |
|
|
|
|
|
|
|
function openNode(datasource) { |
|
|
|
if (!openDataSources.includes(datasource._id)) { |
|
|
|
openDataSources = [...openDataSources, datasource._id] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function toggleNode(datasource) { |
|
|
|
const isOpen = openDataSources.includes(datasource._id) |
|
|
|
if (isOpen) { |
|
|
|
openDataSources = openDataSources.filter(id => datasource._id !== id) |
|
|
|
closeNode(datasource) |
|
|
|
} else { |
|
|
|
openDataSources = [...openDataSources, datasource._id] |
|
|
|
openNode(datasource) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -44,16 +65,35 @@ |
|
|
|
queries.fetch() |
|
|
|
}) |
|
|
|
|
|
|
|
const containsActiveTable = datasource => { |
|
|
|
const activeTableId = get(tables).selected?._id |
|
|
|
const containsActiveEntity = datasource => { |
|
|
|
// If we're view a query then the data source ID is in the URL |
|
|
|
if ($params.selectedDatasource === datasource._id) { |
|
|
|
return true |
|
|
|
} |
|
|
|
|
|
|
|
// If there are no entities it can't contain anything |
|
|
|
if (!datasource.entities) { |
|
|
|
return false |
|
|
|
} |
|
|
|
let tableOptions = datasource.entities |
|
|
|
if (!Array.isArray(tableOptions)) { |
|
|
|
tableOptions = Object.values(tableOptions) |
|
|
|
|
|
|
|
// Get a list of table options |
|
|
|
let options = datasource.entities |
|
|
|
if (!Array.isArray(options)) { |
|
|
|
options = Object.values(options) |
|
|
|
} |
|
|
|
|
|
|
|
// Check for a matching table |
|
|
|
if ($params.selectedTable) { |
|
|
|
const selectedTable = get(tables).selected?._id |
|
|
|
return options.find(x => x._id === selectedTable) != null |
|
|
|
} |
|
|
|
return tableOptions.find(x => x._id === activeTableId) != null |
|
|
|
|
|
|
|
// Check for a matching view |
|
|
|
const selectedView = get(views).selected?.name |
|
|
|
const table = options.find(table => { |
|
|
|
return table.views?.[selectedView] != null |
|
|
|
}) |
|
|
|
return table != null |
|
|
|
} |
|
|
|
</script> |
|
|
|
|
|
|
|
|