2020-12-19 07:19:43 +13:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
2021-09-22 04:21:25 +12:00
|
|
|
import { get } from "svelte/store"
|
2021-09-28 06:25:19 +13:00
|
|
|
import { goto, params } from "@roxi/routify"
|
2021-06-16 06:48:05 +12:00
|
|
|
import { BUDIBASE_INTERNAL_DB } from "constants"
|
2021-09-28 06:25:19 +13:00
|
|
|
import { database, datasources, queries, tables, views } from "stores/backend"
|
2020-12-19 07:19:43 +13:00
|
|
|
import EditDatasourcePopover from "./popovers/EditDatasourcePopover.svelte"
|
2021-01-07 01:28:51 +13:00
|
|
|
import EditQueryPopover from "./popovers/EditQueryPopover.svelte"
|
2020-12-19 07:19:43 +13:00
|
|
|
import NavItem from "components/common/NavItem.svelte"
|
2021-06-16 06:48:05 +12:00
|
|
|
import TableNavigator from "components/backend/TableNavigator/TableNavigator.svelte"
|
2021-01-13 05:49:11 +13:00
|
|
|
import ICONS from "./icons"
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-07-29 21:22:45 +12:00
|
|
|
let openDataSources = []
|
2021-10-07 04:27:46 +13:00
|
|
|
$: enrichedDataSources = Array.isArray($datasources.list)
|
|
|
|
? $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,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: []
|
2021-09-28 06:25:19 +13:00
|
|
|
$: 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)
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 21:22:45 +12:00
|
|
|
|
2020-12-19 07:19:43 +13:00
|
|
|
function selectDatasource(datasource) {
|
2021-09-28 06:25:19 +13:00
|
|
|
openNode(datasource)
|
2021-03-23 00:02:55 +13:00
|
|
|
datasources.select(datasource._id)
|
2020-12-19 07:19:43 +13:00
|
|
|
$goto(`./datasource/${datasource._id}`)
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:37:32 +13:00
|
|
|
function onClickQuery(query) {
|
2021-03-23 02:47:21 +13:00
|
|
|
queries.select(query)
|
2021-01-19 04:37:32 +13:00
|
|
|
$goto(`./datasource/${query.datasourceId}/${query._id}`)
|
2020-12-19 07:19:43 +13:00
|
|
|
}
|
|
|
|
|
2021-09-28 06:25:19 +13:00
|
|
|
function closeNode(datasource) {
|
|
|
|
openDataSources = openDataSources.filter(id => datasource._id !== id)
|
|
|
|
}
|
|
|
|
|
|
|
|
function openNode(datasource) {
|
|
|
|
if (!openDataSources.includes(datasource._id)) {
|
|
|
|
openDataSources = [...openDataSources, datasource._id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:22:45 +12:00
|
|
|
function toggleNode(datasource) {
|
|
|
|
const isOpen = openDataSources.includes(datasource._id)
|
|
|
|
if (isOpen) {
|
2021-09-28 06:25:19 +13:00
|
|
|
closeNode(datasource)
|
2021-07-29 21:22:45 +12:00
|
|
|
} else {
|
2021-09-28 06:25:19 +13:00
|
|
|
openNode(datasource)
|
2021-07-29 21:22:45 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 07:19:43 +13:00
|
|
|
onMount(() => {
|
2021-03-23 00:02:55 +13:00
|
|
|
datasources.fetch()
|
2021-03-23 02:47:21 +13:00
|
|
|
queries.fetch()
|
2020-12-19 07:19:43 +13:00
|
|
|
})
|
2021-09-22 04:21:25 +12:00
|
|
|
|
2021-09-28 06:25:19 +13:00
|
|
|
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
|
2021-09-22 04:21:25 +12:00
|
|
|
if (!datasource.entities) {
|
|
|
|
return false
|
|
|
|
}
|
2021-09-28 06:25:19 +13:00
|
|
|
|
|
|
|
// 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
|
2021-09-22 04:21:25 +12:00
|
|
|
}
|
2021-09-28 06:25:19 +13:00
|
|
|
|
|
|
|
// Check for a matching view
|
|
|
|
const selectedView = get(views).selected?.name
|
|
|
|
const table = options.find(table => {
|
|
|
|
return table.views?.[selectedView] != null
|
|
|
|
})
|
|
|
|
return table != null
|
2021-09-22 04:21:25 +12:00
|
|
|
}
|
2020-12-19 07:19:43 +13:00
|
|
|
</script>
|
|
|
|
|
2021-03-23 04:33:08 +13:00
|
|
|
{#if $database?._id}
|
2020-12-19 07:19:43 +13:00
|
|
|
<div class="hierarchy-items-container">
|
2021-09-22 04:21:25 +12:00
|
|
|
{#each enrichedDataSources as datasource, idx}
|
2020-12-19 07:19:43 +13:00
|
|
|
<NavItem
|
|
|
|
border={idx > 0}
|
|
|
|
text={datasource.name}
|
2021-09-22 04:21:25 +12:00
|
|
|
opened={datasource.open}
|
|
|
|
selected={datasource.selected}
|
2021-07-29 21:22:45 +12:00
|
|
|
withArrow={true}
|
2021-04-23 23:52:34 +12:00
|
|
|
on:click={() => selectDatasource(datasource)}
|
2021-07-29 21:22:45 +12:00
|
|
|
on:iconClick={() => toggleNode(datasource)}
|
2021-04-23 23:52:34 +12:00
|
|
|
>
|
2021-01-13 05:49:11 +13:00
|
|
|
<div class="datasource-icon" slot="icon">
|
|
|
|
<svelte:component
|
|
|
|
this={ICONS[datasource.source]}
|
2021-01-22 03:57:11 +13:00
|
|
|
height="18"
|
2021-04-23 23:52:34 +12:00
|
|
|
width="18"
|
|
|
|
/>
|
2021-01-13 05:49:11 +13:00
|
|
|
</div>
|
2021-06-16 06:48:05 +12:00
|
|
|
{#if datasource._id !== BUDIBASE_INTERNAL_DB}
|
|
|
|
<EditDatasourcePopover {datasource} />
|
|
|
|
{/if}
|
2020-12-19 07:19:43 +13:00
|
|
|
</NavItem>
|
2021-06-16 06:48:05 +12:00
|
|
|
|
2021-09-22 04:21:25 +12:00
|
|
|
{#if datasource.open}
|
2021-07-29 21:22:45 +12:00
|
|
|
<TableNavigator sourceId={datasource._id} />
|
2021-09-23 18:48:11 +12:00
|
|
|
{#each $queries.list.filter(query => query.datasourceId === datasource._id) as query}
|
|
|
|
<NavItem
|
|
|
|
indentLevel={1}
|
|
|
|
icon="SQLQuery"
|
|
|
|
text={query.name}
|
|
|
|
opened={$queries.selected === query._id}
|
|
|
|
selected={$queries.selected === query._id}
|
|
|
|
on:click={() => onClickQuery(query)}
|
|
|
|
>
|
|
|
|
<EditQueryPopover {query} />
|
|
|
|
</NavItem>
|
|
|
|
{/each}
|
2021-07-29 21:22:45 +12:00
|
|
|
{/if}
|
2020-12-19 07:19:43 +13:00
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
{/if}
|
2021-01-22 03:57:11 +13:00
|
|
|
|
|
|
|
<style>
|
|
|
|
.datasource-icon {
|
|
|
|
margin-right: 3px;
|
|
|
|
padding-top: 3px;
|
|
|
|
}
|
|
|
|
</style>
|