upend/webui/src/components/AttributeView.svelte

109 lines
2.6 KiB
Svelte
Raw Normal View History

2021-11-11 23:37:42 +01:00
<script lang="ts">
2021-11-30 22:59:34 +01:00
import { createEventDispatcher } from "svelte";
2021-12-21 20:02:47 +01:00
import UpLink from "./display/UpLink.svelte";
import { Component, UNTYPED, UpType, Widget } from "../lib/types";
2021-11-11 23:37:42 +01:00
import Table from "./widgets/Table.svelte";
2021-11-30 22:59:34 +01:00
import TableComponent from "./widgets/Table.svelte"; // silence false svelte(reactive-component) warnings
import type { UpEntry } from "upend";
import Icon from "./utils/Icon.svelte";
import IconButton from "./utils/IconButton.svelte";
2021-11-11 23:37:42 +01:00
export let entries: UpEntry[];
2021-11-11 23:37:42 +01:00
export let type: UpType | undefined = undefined;
export let title: String | undefined = undefined;
export let editable = false;
export let reverse = false;
let currentWidget: string | undefined;
2021-11-11 23:37:42 +01:00
let availableWidgets: Widget[] = [];
$: {
availableWidgets = [
{
2021-11-11 23:37:42 +01:00
name: "table",
icon: "table",
components: [
{
2021-11-30 22:59:34 +01:00
component: TableComponent,
2021-11-11 23:37:42 +01:00
},
],
},
];
if (type?.widgetInfo) {
availableWidgets = [type.widgetInfo, ...availableWidgets];
}
currentWidget = availableWidgets[0].name;
2021-11-11 23:37:42 +01:00
}
let components: Component[] = [];
$: {
components = availableWidgets.find(
(w) => w.name === currentWidget
)!.components;
}
</script>
<section class="attribute-view labelborder">
2021-11-11 23:37:42 +01:00
<header>
<h3>
{#if type && type !== UNTYPED}
2021-11-11 23:37:42 +01:00
<UpLink to={{ entity: type.address }}>
{#if type.icon}
2021-12-02 20:49:59 +01:00
<div class="icon">
<Icon name={type.icon} />
2021-12-02 20:49:59 +01:00
</div>
2021-11-11 23:37:42 +01:00
{/if}
{type.name || "???"}
</UpLink>
{:else}
{title || "???"}
{/if}
</h3>
{#if currentWidget && (availableWidgets.length > 1 || editable)}
2021-11-11 23:37:42 +01:00
<div class="views">
{#each availableWidgets as widget (widget.name)}
<IconButton
2021-11-11 23:37:42 +01:00
name={widget.icon || "question-diamond"}
active={widget.name === currentWidget}
--active-color="var(--foreground)"
2021-11-11 23:37:42 +01:00
on:click={() => (currentWidget = widget.name)}
/>
{/each}
</div>
{/if}
</header>
2021-11-30 22:59:34 +01:00
{#if !reverse}
{#each components as component}
<svelte:component
this={component.component}
{...component.props || {}}
{entries}
2021-11-30 22:59:34 +01:00
{editable}
on:change
2021-11-30 22:59:34 +01:00
/>
{/each}
{:else}
<Table columns="entity, attribute" {entries} />
2021-11-30 22:59:34 +01:00
{/if}
2021-11-11 23:37:42 +01:00
</section>
<style scoped lang="scss">
@use "./util";
2021-11-11 23:37:42 +01:00
.icon {
display: inline-block;
font-size: 1.25em;
margin-top: -0.3em;
position: relative;
bottom: -2px;
}
2021-11-11 23:37:42 +01:00
.views {
display: flex;
right: 1ex;
font-size: 18px;
2021-11-11 23:37:42 +01:00
}
</style>