upend/webui/src/components/BrowseColumn.svelte

170 lines
4 KiB
Svelte
Raw Normal View History

2022-02-09 20:59:21 +01:00
<script lang="ts">
import { createEventDispatcher, onMount, setContext, tick } from "svelte";
2022-02-09 20:59:21 +01:00
import { normUrl } from "../util/history";
import IconButton from "./utils/IconButton.svelte";
import { selected } from "./EntitySelect.svelte";
import type { BrowseContext } from "../util/browse";
import { writable } from "svelte/store";
import { useParams } from "svelte-navigator";
2022-02-09 20:59:21 +01:00
const dispatch = createEventDispatcher();
const params = useParams();
2022-02-09 20:59:21 +01:00
export let address: string | undefined = undefined;
export let index: number;
2022-02-09 20:59:21 +01:00
export let only: boolean;
export let background = "var(--background-lighter)";
export let forceDetail = false;
2022-02-09 20:59:21 +01:00
let detail = only || forceDetail;
2022-02-09 23:58:56 +01:00
let detailChanged = false;
$: if (!detailChanged) detail = only || forceDetail;
$: if (detailChanged) tick().then(() => dispatch("detail", detail));
2022-02-09 20:59:21 +01:00
let indexStore = writable(index);
$: $indexStore = index;
let addressesStore = writable([]);
$: $addressesStore = $params.addresses?.split(",") || [];
setContext("browse", {
index: indexStore,
addresses: addressesStore,
} as BrowseContext);
onMount(() => {
// Required to make detail mode detection work in Browse
dispatch("detail", detail);
});
$: if ($selected.length) {
detail = false;
}
2022-02-09 20:59:21 +01:00
function visit() {
window.open(normUrl(`/browse/${address}`), "_blank");
2022-02-09 20:59:21 +01:00
}
2023-03-07 19:32:33 +01:00
let width = 460;
if (window.innerWidth < 600) {
width = window.innerWidth - 6;
}
2023-03-07 19:32:33 +01:00
function drag(ev: MouseEvent) {
const startWidth = width;
const startX = ev.screenX;
function onMouseMove(ev: MouseEvent) {
width = startWidth + (ev.screenX - startX);
width = width < 300 ? 300 : width;
}
function onMouseUp(_: MouseEvent) {
window.removeEventListener("mousemove", onMouseMove);
window.removeEventListener("mouseup", onMouseUp);
}
window.addEventListener("mousemove", onMouseMove);
window.addEventListener("mouseup", onMouseUp);
}
2022-02-09 20:59:21 +01:00
</script>
<div
class="browse-column"
class:detail
style="--background-color: {background}"
>
2023-09-01 19:52:49 +02:00
<div class="view" style="--width: {width}px">
2023-03-07 19:32:33 +01:00
<header>
{#if address}
<IconButton name="link" on:click={() => visit()} disabled={only}>
Detach
</IconButton>
{/if}
{#if !forceDetail}
<IconButton
name={detail ? "zoom-out" : "zoom-in"}
on:click={() => {
detail = !detail;
detailChanged = true;
}}
active={detail}
>
Detail
</IconButton>
{:else}
<div class="noop"></div>
{/if}
{#if address}
<IconButton
name="intersect"
on:click={() => dispatch("combine", address)}
>
Combine
</IconButton>
{/if}
2023-03-07 19:32:33 +01:00
<IconButton
name="x-circle"
on:click={() => dispatch("close")}
disabled={only}
2023-04-23 13:14:36 +02:00
>
Close
</IconButton>
2023-03-07 19:32:33 +01:00
</header>
<slot {detail} />
2023-03-07 19:32:33 +01:00
</div>
<div class="resizeHandle" on:mousedown|preventDefault={drag} />
2022-02-09 20:59:21 +01:00
</div>
<style lang="scss">
2023-06-05 19:51:11 +02:00
.browse-column {
2023-03-07 19:32:33 +01:00
display: flex;
}
2023-06-05 19:51:11 +02:00
.browse-column.detail {
2023-03-07 21:46:53 +01:00
width: 100%;
2023-04-20 21:15:03 +02:00
2023-03-07 21:46:53 +01:00
.view {
@media screen and (min-width: 600px) {
min-width: 75vw;
max-width: min(75vw, 1920px);
margin-left: auto;
margin-right: auto;
}
2023-03-07 21:46:53 +01:00
}
}
2022-02-09 20:59:21 +01:00
.view {
2023-03-07 19:32:33 +01:00
min-width: var(--width);
max-width: var(--width);
2022-02-09 20:59:21 +01:00
display: flex;
flex-direction: column;
background: var(--background-color);
2022-02-09 20:59:21 +01:00
color: var(--foreground-lighter);
border: 1px solid var(--foreground-lightest);
border-radius: 0.5em;
padding: 1rem;
// transition: min-width 0.2s, max-width 0.2s;
// TODO - container has nowhere to scroll, breaking `detail` scroll
2022-02-09 20:59:21 +01:00
header {
font-size: 20px;
position: relative;
min-height: 1em;
display: flex;
justify-content: space-between;
flex: none;
}
}
2023-03-07 19:32:33 +01:00
.resizeHandle {
cursor: ew-resize;
height: 100%;
width: 0.5rem;
@media screen and (max-width: 600px) {
display: none;
}
2023-03-07 19:32:33 +01:00
}
2022-02-09 20:59:21 +01:00
</style>