2023-09-01 00:12:33 +08:00
|
|
|
import type {
|
|
|
|
DragEventHandler,
|
|
|
|
Draggable,
|
|
|
|
Droppable,
|
|
|
|
} from '@thisbeyond/solid-dnd'
|
|
|
|
import {
|
|
|
|
DragDropProvider,
|
|
|
|
DragDropSensors,
|
|
|
|
DragOverlay,
|
|
|
|
SortableProvider,
|
|
|
|
closestCenter,
|
|
|
|
createSortable,
|
|
|
|
useDragDropContext,
|
|
|
|
} from '@thisbeyond/solid-dnd'
|
|
|
|
import { For, createSignal } from 'solid-js'
|
2023-08-31 22:49:34 +08:00
|
|
|
import { AccessorKey } from '~/config/connection'
|
|
|
|
|
|
|
|
type ColumnVisibility = Partial<Record<AccessorKey, boolean>>
|
2023-09-01 00:12:33 +08:00
|
|
|
type ColumnOrder = AccessorKey[]
|
2023-08-31 22:49:34 +08:00
|
|
|
|
|
|
|
export default (props: {
|
2023-09-01 00:12:33 +08:00
|
|
|
order: ColumnOrder
|
|
|
|
visible: ColumnVisibility
|
|
|
|
onOrderChange: (value: ColumnOrder) => void
|
|
|
|
onVisibleChange: (value: ColumnVisibility) => void
|
2023-08-31 22:49:34 +08:00
|
|
|
}) => {
|
2023-09-01 00:12:33 +08:00
|
|
|
const [activeKey, setActiveKey] = createSignal<AccessorKey | null>(null)
|
|
|
|
const onDragStart = ({ draggable }: { draggable: Draggable }) =>
|
|
|
|
setActiveKey(draggable.id as AccessorKey)
|
|
|
|
const onDragEnd = ({
|
|
|
|
draggable,
|
|
|
|
droppable,
|
|
|
|
}: {
|
|
|
|
draggable: Draggable
|
|
|
|
droppable: Droppable
|
|
|
|
}) => {
|
|
|
|
if (draggable && droppable) {
|
|
|
|
const currentItems = props.order
|
|
|
|
const fromIndex = currentItems.indexOf(draggable.id as AccessorKey)
|
|
|
|
const toIndex = currentItems.indexOf(droppable.id as AccessorKey)
|
|
|
|
|
|
|
|
if (fromIndex !== toIndex) {
|
|
|
|
const updatedItems = currentItems.slice()
|
|
|
|
|
|
|
|
updatedItems.splice(toIndex, 0, ...updatedItems.splice(fromIndex, 1))
|
|
|
|
props.onOrderChange(updatedItems)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const FormRow = (p: { key: AccessorKey }) => {
|
|
|
|
const key = p.key
|
|
|
|
const sortable = createSortable(key)
|
|
|
|
const [state] = useDragDropContext()!
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
use:sortable
|
|
|
|
class="sortable"
|
|
|
|
classList={{
|
|
|
|
'opacity-25': sortable.isActiveDraggable,
|
|
|
|
'transition-transform': !!state.active.draggable,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div class="m-1 flex cursor-pointer justify-between p-1">
|
|
|
|
<span class="select-none">{key}</span>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
class="toggle"
|
|
|
|
checked={props.visible[key]}
|
|
|
|
onChange={(e) => {
|
|
|
|
props.onVisibleChange({
|
|
|
|
...props.visible,
|
|
|
|
[key]: e.target.checked,
|
|
|
|
})
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2023-08-31 22:49:34 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<input type="checkbox" id="connection-modal" class="modal-toggle" />
|
|
|
|
<div class="modal">
|
|
|
|
<div class="modal-box w-80">
|
2023-09-01 00:12:33 +08:00
|
|
|
<DragDropProvider
|
|
|
|
onDragStart={onDragStart}
|
|
|
|
onDragEnd={onDragEnd as DragEventHandler}
|
|
|
|
collisionDetector={closestCenter}
|
2023-08-31 22:49:34 +08:00
|
|
|
>
|
2023-09-01 00:12:33 +08:00
|
|
|
<DragDropSensors />
|
|
|
|
<div class="column self-stretch">
|
|
|
|
<SortableProvider ids={props.order}>
|
|
|
|
<For each={props.order}>{(key) => <FormRow key={key} />}</For>
|
|
|
|
</SortableProvider>
|
|
|
|
</div>
|
|
|
|
<DragOverlay>
|
|
|
|
<div class="sortable">{activeKey()}</div>
|
|
|
|
</DragOverlay>
|
|
|
|
</DragDropProvider>
|
2023-08-31 22:49:34 +08:00
|
|
|
</div>
|
|
|
|
<label class="modal-backdrop" htmlFor="connection-modal">
|
|
|
|
Close
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|