2021-04-08 16:57:24 +02:00
|
|
|
<script>
|
|
|
|
import { createEventDispatcher } from "svelte"
|
|
|
|
import "@spectrum-css/table/dist/index-vars.css"
|
|
|
|
import CellRenderer from "./CellRenderer.svelte"
|
|
|
|
import SelectEditRenderer from "./SelectEditRenderer.svelte"
|
|
|
|
|
|
|
|
export let data = []
|
|
|
|
export let schema = {}
|
|
|
|
export let showAutoColumns = false
|
2021-04-13 15:14:14 +02:00
|
|
|
export let rowCount = 0
|
2021-04-08 16:57:24 +02:00
|
|
|
export let quiet = true
|
|
|
|
export let loading = false
|
|
|
|
export let allowSelectRows = true
|
|
|
|
export let allowEditRows = true
|
|
|
|
export let allowEditColumns = true
|
|
|
|
export let selectedRows = []
|
2021-04-12 15:45:41 +02:00
|
|
|
export let editColumnTitle = "Edit"
|
2021-04-09 15:21:27 +02:00
|
|
|
export let customRenderers = []
|
2021-04-08 16:57:24 +02:00
|
|
|
|
|
|
|
const dispatch = createEventDispatcher()
|
|
|
|
|
|
|
|
// Config
|
|
|
|
const rowHeight = 55
|
|
|
|
const headerHeight = 36
|
|
|
|
const rowPreload = 5
|
|
|
|
|
|
|
|
// Sorting state
|
|
|
|
let sortColumn
|
|
|
|
let sortOrder
|
|
|
|
|
|
|
|
// Table state
|
2021-04-13 15:14:14 +02:00
|
|
|
let height = 0
|
2021-04-08 16:57:24 +02:00
|
|
|
let loaded = false
|
|
|
|
$: if (!loading) loaded = true
|
|
|
|
$: rows = data ?? []
|
2021-04-13 15:14:14 +02:00
|
|
|
$: visibleRowCount = getVisibleRowCount(loaded, height, rows.length, rowCount)
|
|
|
|
$: contentStyle = getContentStyle(visibleRowCount, rowCount)
|
2021-04-08 16:57:24 +02:00
|
|
|
$: sortedRows = sortRows(rows, sortColumn, sortOrder)
|
|
|
|
$: fields = getFields(schema, showAutoColumns)
|
2021-04-12 15:45:41 +02:00
|
|
|
$: showEditColumn = allowEditRows || allowSelectRows
|
2021-04-08 16:57:24 +02:00
|
|
|
|
|
|
|
// Scrolling state
|
|
|
|
let timeout
|
|
|
|
let nextScrollTop = 0
|
|
|
|
let scrollTop = 0
|
|
|
|
$: firstVisibleRow = calculateFirstVisibleRow(scrollTop)
|
|
|
|
$: lastVisibleRow = calculateLastVisibleRow(
|
|
|
|
firstVisibleRow,
|
|
|
|
visibleRowCount,
|
|
|
|
rows.length
|
|
|
|
)
|
|
|
|
|
2021-04-13 16:10:27 +02:00
|
|
|
// Reset state when data chanegs
|
|
|
|
$: data.length, reset()
|
|
|
|
const reset = () => {
|
|
|
|
nextScrollTop = 0
|
|
|
|
scrollTop = 0
|
|
|
|
clearTimeout(timeout)
|
|
|
|
timeout = null
|
|
|
|
}
|
|
|
|
|
2021-04-13 15:14:14 +02:00
|
|
|
const getVisibleRowCount = (loaded, height, allRows, rowCount) => {
|
|
|
|
if (!loaded) {
|
|
|
|
return rowCount || 0
|
|
|
|
}
|
|
|
|
if (rowCount) {
|
|
|
|
return Math.min(allRows, rowCount)
|
|
|
|
}
|
|
|
|
return Math.min(allRows, Math.ceil(height / rowHeight))
|
|
|
|
}
|
|
|
|
|
|
|
|
const getContentStyle = (visibleRows, rowCount) => {
|
|
|
|
if (!rowCount) {
|
2021-04-08 16:57:24 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return `height: ${headerHeight - 1 + visibleRows * (rowHeight + 1)}px;`
|
|
|
|
}
|
|
|
|
|
|
|
|
const sortRows = (rows, sortColumn, sortOrder) => {
|
|
|
|
if (!sortColumn || !sortOrder) {
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
return rows.slice().sort((a, b) => {
|
|
|
|
const colA = a[sortColumn]
|
|
|
|
const colB = b[sortColumn]
|
|
|
|
if (sortOrder === "Descending") {
|
|
|
|
return colA > colB ? -1 : 1
|
|
|
|
} else {
|
|
|
|
return colA > colB ? 1 : -1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-13 15:44:30 +02:00
|
|
|
const sortBy = fieldSchema => {
|
|
|
|
if (fieldSchema.sortable === false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (fieldSchema.name === sortColumn) {
|
2021-04-08 16:57:24 +02:00
|
|
|
sortOrder = sortOrder === "Descending" ? "Ascending" : "Descending"
|
|
|
|
} else {
|
2021-04-13 15:44:30 +02:00
|
|
|
sortColumn = fieldSchema.name
|
2021-04-08 16:57:24 +02:00
|
|
|
sortOrder = "Descending"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:14:24 +02:00
|
|
|
const getDisplayName = schema => {
|
|
|
|
let name = schema?.displayName
|
|
|
|
if (schema && name === undefined) {
|
|
|
|
name = schema.name
|
|
|
|
}
|
|
|
|
return name || ""
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:57:24 +02:00
|
|
|
const getFields = (schema, showAutoColumns) => {
|
|
|
|
let columns = []
|
|
|
|
let autoColumns = []
|
2021-04-09 15:48:26 +02:00
|
|
|
Object.entries(schema || {}).forEach(([field, fieldSchema]) => {
|
2021-04-12 17:14:24 +02:00
|
|
|
if (!field || !fieldSchema) {
|
|
|
|
return
|
|
|
|
}
|
2021-04-09 15:48:26 +02:00
|
|
|
schema[field].name = field
|
2021-04-08 16:57:24 +02:00
|
|
|
if (!fieldSchema?.autocolumn) {
|
2021-04-12 17:14:24 +02:00
|
|
|
columns.push(fieldSchema)
|
2021-04-08 16:57:24 +02:00
|
|
|
} else if (showAutoColumns) {
|
2021-04-12 17:14:24 +02:00
|
|
|
autoColumns.push(fieldSchema)
|
2021-04-08 16:57:24 +02:00
|
|
|
}
|
|
|
|
})
|
2021-04-09 15:21:27 +02:00
|
|
|
return columns
|
|
|
|
.sort((a, b) => {
|
2021-04-12 17:14:24 +02:00
|
|
|
const orderA = a.order || Number.MAX_SAFE_INTEGER
|
|
|
|
const orderB = b.order || Number.MAX_SAFE_INTEGER
|
|
|
|
const nameA = getDisplayName(a)
|
|
|
|
const nameB = getDisplayName(b)
|
|
|
|
if (orderA !== orderB) {
|
|
|
|
return orderA < orderB ? orderA : orderB
|
|
|
|
}
|
|
|
|
return nameA < nameB ? a : b
|
2021-04-09 15:21:27 +02:00
|
|
|
})
|
|
|
|
.concat(autoColumns)
|
2021-04-12 17:14:24 +02:00
|
|
|
.map(column => column.name)
|
2021-04-08 16:57:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const onScroll = event => {
|
|
|
|
nextScrollTop = event.target.scrollTop
|
|
|
|
if (timeout) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
scrollTop = nextScrollTop
|
|
|
|
timeout = null
|
|
|
|
}, 50)
|
|
|
|
}
|
|
|
|
|
|
|
|
const calculateFirstVisibleRow = scrollTop => {
|
|
|
|
return Math.max(Math.floor(scrollTop / (rowHeight + 1)) - rowPreload, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
const calculateLastVisibleRow = (firstRow, visibleRowCount, allRowCount) => {
|
2021-04-13 15:14:14 +02:00
|
|
|
if (visibleRowCount === 0) {
|
2021-04-13 15:44:30 +02:00
|
|
|
return -1
|
2021-04-13 15:14:14 +02:00
|
|
|
}
|
2021-04-08 16:57:24 +02:00
|
|
|
return Math.min(firstRow + visibleRowCount + 2 * rowPreload, allRowCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
const editColumn = (e, field) => {
|
|
|
|
e.stopPropagation()
|
|
|
|
dispatch("editcolumn", field)
|
|
|
|
}
|
|
|
|
|
|
|
|
const editRow = (e, row) => {
|
|
|
|
e.stopPropagation()
|
|
|
|
dispatch("editrow", row)
|
|
|
|
}
|
|
|
|
|
|
|
|
const toggleSelectRow = row => {
|
|
|
|
if (!allowSelectRows) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (selectedRows.includes(row)) {
|
|
|
|
selectedRows = selectedRows.filter(selectedRow => selectedRow !== row)
|
|
|
|
} else {
|
|
|
|
selectedRows = [...selectedRows, row]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2021-04-13 16:10:27 +02:00
|
|
|
<div class="wrapper" bind:offsetHeight={height}>
|
2021-04-13 15:14:14 +02:00
|
|
|
{#if !loaded}
|
|
|
|
<div class="loading" style={contentStyle} />
|
|
|
|
{:else}
|
2021-04-13 16:10:27 +02:00
|
|
|
<div
|
|
|
|
on:scroll={onScroll}
|
|
|
|
class:quiet
|
|
|
|
style={`--row-height: ${rowHeight}px; --header-height: ${headerHeight}px;`}
|
|
|
|
class="container">
|
|
|
|
<div style={contentStyle}>
|
|
|
|
<table class="spectrum-Table" class:spectrum-Table--quiet={quiet}>
|
2021-04-09 15:48:26 +02:00
|
|
|
{#if sortedRows?.length}
|
2021-04-13 16:10:27 +02:00
|
|
|
<thead class="spectrum-Table-head">
|
|
|
|
<tr>
|
|
|
|
{#if showEditColumn}
|
|
|
|
<th class="spectrum-Table-headCell">
|
|
|
|
<div class="spectrum-Table-headCell-content">
|
|
|
|
{editColumnTitle || ''}
|
|
|
|
</div>
|
|
|
|
</th>
|
2021-04-08 16:57:24 +02:00
|
|
|
{/if}
|
2021-04-13 16:10:27 +02:00
|
|
|
{#each fields as field}
|
|
|
|
<th
|
|
|
|
class="spectrum-Table-headCell"
|
|
|
|
class:is-sortable={schema[field].sortable !== false}
|
|
|
|
class:is-sorted-desc={sortColumn === field && sortOrder === 'Descending'}
|
|
|
|
class:is-sorted-asc={sortColumn === field && sortOrder === 'Ascending'}
|
|
|
|
on:click={() => sortBy(schema[field])}>
|
|
|
|
<div class="spectrum-Table-headCell-content">
|
|
|
|
<div class="title">{getDisplayName(schema[field])}</div>
|
|
|
|
{#if schema[field]?.autocolumn}
|
|
|
|
<svg
|
|
|
|
class="spectrum-Icon spectrum-Table-autoIcon"
|
|
|
|
focusable="false">
|
|
|
|
<use xlink:href="#spectrum-icon-18-MagicWand" />
|
|
|
|
</svg>
|
|
|
|
{/if}
|
|
|
|
{#if sortColumn === field}
|
|
|
|
<svg
|
|
|
|
class="spectrum-Icon spectrum-UIIcon-ArrowDown100 spectrum-Table-sortedIcon"
|
|
|
|
focusable="false"
|
|
|
|
aria-hidden="true">
|
|
|
|
<use xlink:href="#spectrum-css-icon-Arrow100" />
|
|
|
|
</svg>
|
|
|
|
{/if}
|
|
|
|
{#if allowEditColumns && schema[field]?.editable !== false}
|
|
|
|
<svg
|
|
|
|
class="spectrum-Icon spectrum-Table-editIcon"
|
|
|
|
focusable="false"
|
|
|
|
on:click={e => editColumn(e, field)}>
|
|
|
|
<use xlink:href="#spectrum-icon-18-Edit" />
|
|
|
|
</svg>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
</th>
|
|
|
|
{/each}
|
2021-04-09 15:48:26 +02:00
|
|
|
</tr>
|
2021-04-13 16:10:27 +02:00
|
|
|
</thead>
|
2021-04-09 15:48:26 +02:00
|
|
|
{/if}
|
2021-04-13 16:10:27 +02:00
|
|
|
<tbody class="spectrum-Table-body">
|
|
|
|
{#if sortedRows?.length}
|
|
|
|
{#each sortedRows as row, idx}
|
|
|
|
<tr
|
|
|
|
on:click={() => toggleSelectRow(row)}
|
|
|
|
class="spectrum-Table-row"
|
|
|
|
class:hidden={idx < firstVisibleRow || idx > lastVisibleRow}>
|
|
|
|
{#if idx >= firstVisibleRow && idx <= lastVisibleRow}
|
|
|
|
{#if showEditColumn}
|
|
|
|
<td
|
|
|
|
class="spectrum-Table-cell spectrum-Table-cell--divider">
|
|
|
|
<div class="spectrum-Table-cell-content">
|
|
|
|
<SelectEditRenderer
|
|
|
|
data={row}
|
|
|
|
selected={selectedRows.includes(row)}
|
|
|
|
onToggleSelection={() => toggleSelectRow(row)}
|
|
|
|
onEdit={e => editRow(e, row)}
|
|
|
|
{allowSelectRows}
|
|
|
|
{allowEditRows} />
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
{/if}
|
|
|
|
{#each fields as field}
|
|
|
|
<td class="spectrum-Table-cell">
|
|
|
|
<div class="spectrum-Table-cell-content">
|
|
|
|
<CellRenderer
|
|
|
|
{customRenderers}
|
|
|
|
{row}
|
|
|
|
schema={schema[field]}
|
|
|
|
value={row[field]}
|
|
|
|
on:clickrelationship>
|
|
|
|
<slot />
|
|
|
|
</CellRenderer>
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
{/each}
|
|
|
|
{/if}
|
|
|
|
</tr>
|
|
|
|
{/each}
|
|
|
|
{:else}
|
|
|
|
<div class="placeholder">
|
|
|
|
<svg
|
|
|
|
class="spectrum-Icon spectrum-Icon--sizeXXL"
|
|
|
|
focusable="false">
|
|
|
|
<use xlink:href="#spectrum-icon-18-Table" />
|
|
|
|
</svg>
|
|
|
|
<div>No rows found</div>
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2021-04-08 16:57:24 +02:00
|
|
|
</div>
|
2021-04-13 15:14:14 +02:00
|
|
|
{/if}
|
|
|
|
</div>
|
2021-04-08 16:57:24 +02:00
|
|
|
|
|
|
|
<style>
|
2021-04-13 16:10:27 +02:00
|
|
|
.wrapper {
|
2021-04-08 16:57:24 +02:00
|
|
|
background-color: var(--spectrum-global-color-gray-100);
|
2021-04-13 16:10:27 +02:00
|
|
|
overflow: hidden;
|
2021-04-08 16:57:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.container {
|
2021-04-13 16:10:27 +02:00
|
|
|
height: 100%;
|
2021-04-08 16:57:24 +02:00
|
|
|
position: relative;
|
|
|
|
overflow: auto;
|
|
|
|
border: 1px solid
|
|
|
|
var(--spectrum-table-border-color, var(--spectrum-alias-border-color-mid)) !important;
|
|
|
|
scrollbar-width: thin;
|
2021-04-09 14:08:37 +02:00
|
|
|
scrollbar-color: var(--spectrum-global-color-gray-400)
|
2021-04-08 16:57:24 +02:00
|
|
|
var(--spectrum-global-color-gray-100);
|
|
|
|
}
|
|
|
|
.container::-webkit-scrollbar {
|
2021-04-09 14:02:55 +02:00
|
|
|
width: 16px;
|
|
|
|
height: 16px;
|
2021-04-08 16:57:24 +02:00
|
|
|
}
|
|
|
|
.container::-webkit-scrollbar-track {
|
|
|
|
background: var(--spectrum-global-color-gray-100);
|
|
|
|
}
|
|
|
|
.container::-webkit-scrollbar-thumb {
|
2021-04-09 14:08:37 +02:00
|
|
|
background-color: var(--spectrum-global-color-gray-400);
|
2021-04-08 16:57:24 +02:00
|
|
|
border-radius: 20px;
|
|
|
|
border: 4px solid var(--spectrum-global-color-gray-100);
|
|
|
|
}
|
|
|
|
.container::-webkit-scrollbar-corner {
|
|
|
|
background: var(--spectrum-global-color-gray-100);
|
|
|
|
}
|
|
|
|
.container.quiet {
|
|
|
|
border: none !important;
|
|
|
|
}
|
|
|
|
table {
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.spectrum-Table-headCell .spectrum-Icon {
|
|
|
|
pointer-events: all;
|
|
|
|
margin-left: var(
|
|
|
|
--spectrum-table-header-sort-icon-gap,
|
|
|
|
var(--spectrum-global-dimension-size-125)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
.spectrum-Table-editIcon,
|
|
|
|
.spectrum-Table-autoIcon {
|
|
|
|
width: var(--spectrum-global-dimension-size-150);
|
|
|
|
height: var(--spectrum-global-dimension-size-150);
|
|
|
|
}
|
|
|
|
.spectrum-Table-editIcon {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
.spectrum-Table-headCell:hover .spectrum-Table-editIcon {
|
|
|
|
opacity: 1;
|
|
|
|
transition: opacity 0.2s ease;
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:07:22 +02:00
|
|
|
.container,
|
2021-04-08 16:57:24 +02:00
|
|
|
th {
|
|
|
|
border-bottom: 1px solid
|
|
|
|
var(--spectrum-table-border-color, var(--spectrum-alias-border-color-mid)) !important;
|
|
|
|
}
|
|
|
|
th {
|
|
|
|
vertical-align: middle;
|
|
|
|
height: var(--header-height);
|
|
|
|
position: sticky;
|
|
|
|
top: 0;
|
|
|
|
z-index: 2;
|
2021-04-13 16:10:27 +02:00
|
|
|
background-color: var(--spectrum-global-color-gray-100);
|
2021-04-08 16:57:24 +02:00
|
|
|
}
|
|
|
|
.spectrum-Table-headCell-content {
|
|
|
|
white-space: nowrap;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
justify-content: flex-start;
|
|
|
|
align-items: center;
|
|
|
|
user-select: none;
|
|
|
|
}
|
|
|
|
.spectrum-Table-headCell-content .title {
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:48:26 +02:00
|
|
|
.placeholder {
|
|
|
|
padding: 20px;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
color: var(
|
|
|
|
--spectrum-table-cell-text-color,
|
|
|
|
var(--spectrum-alias-text-color)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
.placeholder div {
|
|
|
|
margin-top: 10px;
|
|
|
|
font-size: var(
|
|
|
|
--spectrum-table-cell-text-size,
|
|
|
|
var(--spectrum-alias-font-size-default)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:57:24 +02:00
|
|
|
tbody {
|
|
|
|
z-index: 1;
|
|
|
|
}
|
|
|
|
tbody tr {
|
|
|
|
height: var(--row-height);
|
|
|
|
}
|
|
|
|
tbody tr.hidden {
|
|
|
|
height: calc(var(--row-height) + 1px);
|
|
|
|
}
|
|
|
|
tbody tr.offset {
|
|
|
|
background-color: red;
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
td {
|
|
|
|
padding-top: 0;
|
|
|
|
padding-bottom: 0;
|
|
|
|
border-bottom: none !important;
|
|
|
|
border-left: none !important;
|
|
|
|
border-right: none !important;
|
|
|
|
border-top: 1px solid
|
|
|
|
var(--spectrum-table-border-color, var(--spectrum-alias-border-color-mid)) !important;
|
|
|
|
}
|
|
|
|
tr:first-child td {
|
|
|
|
border-top: none !important;
|
|
|
|
}
|
2021-04-09 14:07:22 +02:00
|
|
|
.container:not(.quiet) td.spectrum-Table-cell--divider {
|
2021-04-08 16:57:24 +02:00
|
|
|
width: 1px;
|
|
|
|
border-right: 1px solid
|
|
|
|
var(--spectrum-table-border-color, var(--spectrum-alias-border-color-mid)) !important;
|
|
|
|
}
|
|
|
|
.spectrum-Table-cell-content {
|
|
|
|
height: var(--row-height);
|
|
|
|
white-space: nowrap;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
justify-content: flex-start;
|
|
|
|
align-items: center;
|
|
|
|
gap: 4px;
|
|
|
|
}
|
|
|
|
</style>
|