2021-05-06 18:51:44 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
|
|
|
<script lang="typescript">
|
2021-05-06 19:26:50 +02:00
|
|
|
import { setContext } from "svelte";
|
|
|
|
import { writable } from "svelte/store";
|
|
|
|
import ButtonToolbarItem from "./ButtonToolbarItem.svelte";
|
|
|
|
import type { ButtonGroupRegistration } from "./buttons";
|
|
|
|
import { buttonToolbarKey } from "./contextKeys";
|
|
|
|
import type { Identifier } from "./identifier";
|
|
|
|
import { insert, add } from "./identifier";
|
|
|
|
import type { SvelteComponent } from "./registration";
|
|
|
|
import { makeInterface } from "./registration";
|
|
|
|
|
2021-05-06 18:51:44 +02:00
|
|
|
export let id: string | undefined = undefined;
|
2021-05-06 19:26:50 +02:00
|
|
|
let className: string = "";
|
2021-05-06 18:51:44 +02:00
|
|
|
export { className as class };
|
|
|
|
|
|
|
|
export let nowrap = false;
|
2021-05-06 19:26:50 +02:00
|
|
|
|
|
|
|
function makeRegistration(): ButtonGroupRegistration {
|
|
|
|
const detach = writable(false);
|
|
|
|
return { detach };
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:21:33 +02:00
|
|
|
const { registerComponent, dynamicItems, getDynamicInterface } =
|
|
|
|
makeInterface(makeRegistration);
|
2021-05-06 19:26:50 +02:00
|
|
|
|
|
|
|
setContext(buttonToolbarKey, registerComponent);
|
|
|
|
|
2021-05-06 20:39:52 +02:00
|
|
|
export let api: Record<string, unknown> | undefined = undefined;
|
2021-05-06 19:26:50 +02:00
|
|
|
let buttonToolbarRef: HTMLDivElement;
|
|
|
|
|
2021-05-06 20:39:52 +02:00
|
|
|
$: if (buttonToolbarRef && api) {
|
2021-05-26 01:21:33 +02:00
|
|
|
const { addComponent, updateRegistration } =
|
|
|
|
getDynamicInterface(buttonToolbarRef);
|
2021-05-06 19:26:50 +02:00
|
|
|
|
2021-05-06 20:29:55 +02:00
|
|
|
const insertGroup = (group: SvelteComponent, position: Identifier = 0) =>
|
|
|
|
addComponent(group, (added, parent) => insert(added, parent, position));
|
|
|
|
const appendGroup = (group: SvelteComponent, position: Identifier = -1) =>
|
|
|
|
addComponent(group, (added, parent) => add(added, parent, position));
|
2021-05-06 19:26:50 +02:00
|
|
|
|
|
|
|
const showGroup = (id: Identifier) =>
|
|
|
|
updateRegistration(({ detach }) => detach.set(false), id);
|
|
|
|
const hideGroup = (id: Identifier) =>
|
|
|
|
updateRegistration(({ detach }) => detach.set(true), id);
|
|
|
|
const toggleGroup = (id: Identifier) =>
|
|
|
|
updateRegistration(
|
|
|
|
({ detach }) => detach.update((old: boolean): boolean => !old),
|
|
|
|
id
|
|
|
|
);
|
|
|
|
|
|
|
|
Object.assign(api, {
|
|
|
|
insertGroup,
|
|
|
|
appendGroup,
|
|
|
|
showGroup,
|
|
|
|
hideGroup,
|
|
|
|
toggleGroup,
|
|
|
|
});
|
|
|
|
}
|
2021-05-06 18:51:44 +02:00
|
|
|
</script>
|
|
|
|
|
2021-05-06 19:26:50 +02:00
|
|
|
<div
|
|
|
|
bind:this={buttonToolbarRef}
|
|
|
|
{id}
|
|
|
|
class={`btn-toolbar ${className}`}
|
|
|
|
class:flex-nowrap={nowrap}
|
2021-05-26 01:21:33 +02:00
|
|
|
role="toolbar"
|
|
|
|
>
|
2021-05-06 18:51:44 +02:00
|
|
|
<slot />
|
2021-05-06 19:26:50 +02:00
|
|
|
{#each $dynamicItems as item}
|
|
|
|
<ButtonToolbarItem id={item[0].id} registration={item[1]}>
|
|
|
|
<svelte:component this={item[0].component} {...item[0].props} />
|
|
|
|
</ButtonToolbarItem>
|
|
|
|
{/each}
|
2021-05-06 18:51:44 +02:00
|
|
|
</div>
|