2021-04-12 06:18:30 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
|
|
|
<script lang="ts">
|
2021-04-26 15:34:33 +02:00
|
|
|
import * as tr from "lib/i18n";
|
2021-06-21 19:11:10 +02:00
|
|
|
import WithDropdownMenu from "components/WithDropdownMenu.svelte";
|
|
|
|
import DropdownMenu from "components/DropdownMenu.svelte";
|
|
|
|
import DropdownItem from "components/DropdownItem.svelte";
|
2021-06-12 00:18:50 +02:00
|
|
|
import Badge from "./Badge.svelte";
|
2021-06-21 19:11:10 +02:00
|
|
|
import { gearIcon, revertIcon } from "./icons";
|
2021-05-24 10:26:01 +02:00
|
|
|
import { isEqual as isEqualLodash, cloneDeep } from "lodash-es";
|
2021-04-26 15:34:33 +02:00
|
|
|
|
2021-06-12 00:18:50 +02:00
|
|
|
type T = unknown;
|
2021-04-26 15:34:33 +02:00
|
|
|
|
2021-06-12 00:18:50 +02:00
|
|
|
export let value: T;
|
|
|
|
export let defaultValue: T;
|
2021-04-12 06:18:30 +02:00
|
|
|
|
2021-06-12 00:18:50 +02:00
|
|
|
function isEqual(a: T, b: T): boolean {
|
2021-05-24 10:26:01 +02:00
|
|
|
if (typeof a === "number" && typeof b === "number") {
|
|
|
|
// round to .01 precision before comparing,
|
|
|
|
// so the values coming out of the UI match
|
|
|
|
// the originals
|
2021-06-12 00:18:50 +02:00
|
|
|
a = Math.round(a * 100) / 100;
|
|
|
|
b = Math.round(b * 100) / 100;
|
2021-05-24 10:26:01 +02:00
|
|
|
}
|
2021-06-12 00:18:50 +02:00
|
|
|
|
|
|
|
return isEqualLodash(a, b);
|
2021-05-24 10:26:01 +02:00
|
|
|
}
|
|
|
|
|
2021-04-12 06:18:30 +02:00
|
|
|
let modified: boolean;
|
2021-04-25 11:05:19 +02:00
|
|
|
$: modified = !isEqual(value, defaultValue);
|
2021-06-21 19:11:10 +02:00
|
|
|
$: className = !modified ? "opacity-25" : "";
|
2021-04-12 06:18:30 +02:00
|
|
|
|
|
|
|
function revert(): void {
|
2021-04-25 11:05:19 +02:00
|
|
|
value = cloneDeep(defaultValue);
|
2021-04-12 06:18:30 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2021-06-21 19:11:10 +02:00
|
|
|
<WithDropdownMenu let:createDropdown let:menuId>
|
|
|
|
<Badge class={className} on:mount={(event) => createDropdown(event.detail.span)}>
|
|
|
|
{@html gearIcon}
|
|
|
|
</Badge>
|
|
|
|
|
|
|
|
<DropdownMenu id={menuId}>
|
|
|
|
<DropdownItem on:click={revert}>
|
|
|
|
{tr.deckConfigRevertButtonTooltip()}<Badge>{@html revertIcon}</Badge>
|
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenu>
|
|
|
|
</WithDropdownMenu>
|