2020-03-10 14:53:23 +01:00
|
|
|
<script>
|
2021-09-29 11:33:55 +02:00
|
|
|
import { fade } from "svelte/transition"
|
2021-09-23 17:08:47 +02:00
|
|
|
import { tables } from "stores/backend"
|
2020-10-02 13:00:17 +02:00
|
|
|
import CreateRowButton from "./buttons/CreateRowButton.svelte"
|
|
|
|
import CreateColumnButton from "./buttons/CreateColumnButton.svelte"
|
|
|
|
import CreateViewButton from "./buttons/CreateViewButton.svelte"
|
2021-11-01 22:15:46 +01:00
|
|
|
import ExistingRelationshipButton from "./buttons/ExistingRelationshipButton.svelte"
|
2020-10-03 22:06:58 +02:00
|
|
|
import ExportButton from "./buttons/ExportButton.svelte"
|
2021-11-12 19:26:57 +01:00
|
|
|
import ImportButton from "./buttons/ImportButton.svelte"
|
2020-12-04 09:27:42 +01:00
|
|
|
import EditRolesButton from "./buttons/EditRolesButton.svelte"
|
2021-02-10 19:18:31 +01:00
|
|
|
import ManageAccessButton from "./buttons/ManageAccessButton.svelte"
|
2021-02-16 14:56:40 +01:00
|
|
|
import HideAutocolumnButton from "./buttons/HideAutocolumnButton.svelte"
|
2021-09-29 10:05:04 +02:00
|
|
|
import TableFilterButton from "./buttons/TableFilterButton.svelte"
|
2020-10-01 08:19:38 +02:00
|
|
|
import Table from "./Table.svelte"
|
2020-12-04 09:27:42 +01:00
|
|
|
import { TableNames } from "constants"
|
2020-12-07 20:05:39 +01:00
|
|
|
import CreateEditRow from "./modals/CreateEditRow.svelte"
|
2022-04-07 16:30:55 +02:00
|
|
|
import { Pagination, Heading, Body, Layout } from "@budibase/bbui"
|
2022-01-26 18:43:48 +01:00
|
|
|
import { fetchData } from "@budibase/frontend-core"
|
|
|
|
import { API } from "api"
|
2020-03-10 17:06:30 +01:00
|
|
|
|
2021-09-23 18:04:12 +02:00
|
|
|
let hideAutocolumns = true
|
2022-01-26 18:43:48 +01:00
|
|
|
|
2021-03-23 12:16:54 +01:00
|
|
|
$: isUsersTable = $tables.selected?._id === TableNames.USERS
|
2021-06-21 14:41:58 +02:00
|
|
|
$: type = $tables.selected?.type
|
2021-07-07 18:21:56 +02:00
|
|
|
$: isInternal = type !== "external"
|
2021-10-18 17:03:13 +02:00
|
|
|
$: schema = $tables.selected?.schema
|
|
|
|
$: enrichedSchema = enrichSchema($tables.selected?.schema)
|
|
|
|
$: id = $tables.selected?._id
|
2022-01-26 18:43:48 +01:00
|
|
|
$: fetch = createFetch(id)
|
2022-04-07 16:30:55 +02:00
|
|
|
$: hasCols = checkHasCols(schema)
|
|
|
|
$: hasRows = !!$fetch.data?.length
|
2021-10-13 12:39:36 +02:00
|
|
|
|
2021-10-18 17:03:13 +02:00
|
|
|
const enrichSchema = schema => {
|
|
|
|
let tempSchema = { ...schema }
|
|
|
|
tempSchema._id = {
|
2021-10-13 12:39:36 +02:00
|
|
|
type: "internal",
|
|
|
|
editable: false,
|
|
|
|
displayName: "ID",
|
|
|
|
autocolumn: true,
|
|
|
|
}
|
|
|
|
if (isInternal) {
|
2021-10-18 17:03:13 +02:00
|
|
|
tempSchema._rev = {
|
2021-10-13 12:39:36 +02:00
|
|
|
type: "internal",
|
|
|
|
editable: false,
|
|
|
|
displayName: "Revision",
|
|
|
|
autocolumn: true,
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 08:19:38 +02:00
|
|
|
|
2021-10-18 17:03:13 +02:00
|
|
|
return tempSchema
|
|
|
|
}
|
2022-04-07 16:30:55 +02:00
|
|
|
|
|
|
|
const checkHasCols = schema => {
|
|
|
|
if (!schema || Object.keys(schema).length === 0) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
let fields = Object.values(schema)
|
|
|
|
for (let field of fields) {
|
|
|
|
if (!field.autocolumn) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-28 16:29:15 +02:00
|
|
|
// Fetches new data whenever the table changes
|
2022-01-26 18:43:48 +01:00
|
|
|
const createFetch = tableId => {
|
|
|
|
return fetchData({
|
|
|
|
API,
|
|
|
|
datasource: {
|
|
|
|
tableId,
|
|
|
|
type: "table",
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
schema,
|
|
|
|
limit: 10,
|
|
|
|
paginate: true,
|
|
|
|
},
|
2021-09-23 18:04:12 +02:00
|
|
|
})
|
|
|
|
}
|
2021-04-13 16:10:49 +02:00
|
|
|
|
2021-09-23 17:08:47 +02:00
|
|
|
// Fetch data whenever sorting option changes
|
|
|
|
const onSort = e => {
|
2022-01-26 18:43:48 +01:00
|
|
|
fetch.update({
|
2021-09-23 17:08:47 +02:00
|
|
|
sortColumn: e.detail.column,
|
|
|
|
sortOrder: e.detail.order,
|
2021-06-15 14:32:11 +02:00
|
|
|
})
|
2020-03-16 19:54:48 +01:00
|
|
|
}
|
2021-09-29 11:17:12 +02:00
|
|
|
|
|
|
|
// Fetch data whenever filters change
|
|
|
|
const onFilter = e => {
|
2022-01-26 18:43:48 +01:00
|
|
|
fetch.update({
|
|
|
|
filter: e.detail,
|
2021-09-29 11:17:12 +02:00
|
|
|
})
|
|
|
|
}
|
2021-09-29 13:07:35 +02:00
|
|
|
|
2021-09-29 13:29:26 +02:00
|
|
|
// Fetch data whenever schema changes
|
2021-09-29 13:07:35 +02:00
|
|
|
const onUpdateColumns = () => {
|
2022-01-26 18:43:48 +01:00
|
|
|
fetch.refresh()
|
2021-09-29 13:07:35 +02:00
|
|
|
}
|
2021-09-30 14:49:09 +02:00
|
|
|
|
|
|
|
// Fetch data whenever rows are modified. Unfortunately we have to lose
|
|
|
|
// our pagination place, as our bookmarks will have shifted.
|
|
|
|
const onUpdateRows = () => {
|
2022-01-28 16:17:21 +01:00
|
|
|
fetch.refresh()
|
2021-09-30 14:49:09 +02:00
|
|
|
}
|
2020-03-10 14:53:23 +01:00
|
|
|
</script>
|
|
|
|
|
2021-09-23 17:08:47 +02:00
|
|
|
<div>
|
|
|
|
<Table
|
2021-09-29 11:17:12 +02:00
|
|
|
title={$tables.selected?.name}
|
2021-10-18 17:03:13 +02:00
|
|
|
schema={enrichedSchema}
|
2021-09-23 17:08:47 +02:00
|
|
|
{type}
|
2021-09-29 11:17:12 +02:00
|
|
|
tableId={id}
|
2022-01-26 18:43:48 +01:00
|
|
|
data={$fetch.rows}
|
2021-09-23 17:08:47 +02:00
|
|
|
bind:hideAutocolumns
|
2022-02-18 15:12:29 +01:00
|
|
|
loading={!$fetch.loaded}
|
2021-09-23 17:08:47 +02:00
|
|
|
on:sort={onSort}
|
|
|
|
allowEditing
|
|
|
|
disableSorting
|
2021-09-29 13:07:35 +02:00
|
|
|
on:updatecolumns={onUpdateColumns}
|
2021-09-30 14:49:09 +02:00
|
|
|
on:updaterows={onUpdateRows}
|
2022-04-07 16:30:55 +02:00
|
|
|
customPlaceholder
|
2021-09-23 17:08:47 +02:00
|
|
|
>
|
2022-04-07 16:30:55 +02:00
|
|
|
<div class="buttons">
|
|
|
|
<div class="left-buttons">
|
|
|
|
<CreateColumnButton highlighted on:updatecolumns={onUpdateColumns} />
|
|
|
|
{#if !isUsersTable}
|
|
|
|
<CreateRowButton
|
|
|
|
on:updaterows={onUpdateRows}
|
|
|
|
title={"Create row"}
|
|
|
|
modalContentComponent={CreateEditRow}
|
|
|
|
disabled={!hasCols}
|
|
|
|
highlighted={hasCols && !hasRows}
|
|
|
|
/>
|
|
|
|
{/if}
|
|
|
|
{#if isInternal}
|
|
|
|
<CreateViewButton disabled={!hasCols || !hasRows} />
|
|
|
|
{/if}
|
|
|
|
</div>
|
2022-04-07 16:32:46 +02:00
|
|
|
<div class="right-buttons">
|
|
|
|
<ManageAccessButton resourceId={$tables.selected?._id} />
|
|
|
|
{#if isUsersTable}
|
|
|
|
<EditRolesButton />
|
|
|
|
{/if}
|
|
|
|
{#if !isInternal}
|
|
|
|
<ExistingRelationshipButton
|
|
|
|
table={$tables.selected}
|
|
|
|
on:updatecolumns={onUpdateColumns}
|
|
|
|
/>
|
|
|
|
{/if}
|
|
|
|
<HideAutocolumnButton bind:hideAutocolumns />
|
|
|
|
<ImportButton
|
|
|
|
tableId={$tables.selected?._id}
|
|
|
|
on:updaterows={onUpdateRows}
|
2021-11-01 22:15:46 +01:00
|
|
|
/>
|
2022-04-07 16:39:14 +02:00
|
|
|
<ExportButton
|
|
|
|
disabled={!hasRows || !hasCols}
|
|
|
|
view={$tables.selected?._id}
|
|
|
|
/>
|
2022-04-07 16:32:46 +02:00
|
|
|
{#key id}
|
2022-04-07 16:39:14 +02:00
|
|
|
<TableFilterButton
|
|
|
|
{schema}
|
|
|
|
on:change={onFilter}
|
|
|
|
disabled={!hasCols || !hasRows}
|
|
|
|
/>
|
2022-04-07 16:32:46 +02:00
|
|
|
{/key}
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-04-07 16:30:55 +02:00
|
|
|
<div slot="placeholder">
|
|
|
|
<Layout gap="S">
|
|
|
|
<Heading>Let's create some columns!</Heading>
|
|
|
|
<Body>
|
|
|
|
Start building out your table structure<br />
|
|
|
|
by adding some columns
|
|
|
|
</Body>
|
|
|
|
</Layout>
|
|
|
|
</div>
|
2021-09-23 17:08:47 +02:00
|
|
|
</Table>
|
2021-09-29 11:33:55 +02:00
|
|
|
{#key id}
|
|
|
|
<div in:fade={{ delay: 200, duration: 100 }}>
|
|
|
|
<div class="pagination">
|
|
|
|
<Pagination
|
2022-01-26 18:43:48 +01:00
|
|
|
page={$fetch.pageNumber + 1}
|
|
|
|
hasPrevPage={$fetch.hasPrevPage}
|
|
|
|
hasNextPage={$fetch.hasNextPage}
|
|
|
|
goToPrevPage={$fetch.loading ? null : fetch.prevPage}
|
|
|
|
goToNextPage={$fetch.loading ? null : fetch.nextPage}
|
2021-09-29 11:33:55 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{/key}
|
2021-09-23 17:08:47 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.pagination {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
justify-content: flex-end;
|
|
|
|
align-items: center;
|
|
|
|
margin-top: var(--spacing-xl);
|
|
|
|
}
|
2022-04-07 16:32:46 +02:00
|
|
|
.buttons {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
justify-content: space-between;
|
|
|
|
align-items: center;
|
|
|
|
}
|
2021-09-23 17:08:47 +02:00
|
|
|
</style>
|