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-24 05:05:33 +02:00
|
|
|
import { revertIcon } from "./icons";
|
2021-05-24 10:26:01 +02:00
|
|
|
import { isEqual as isEqualLodash, cloneDeep } from "lodash-es";
|
2021-06-21 21:35:20 +02:00
|
|
|
import { touchDeviceKey } from "components/contextKeys";
|
|
|
|
import { getContext } from "svelte";
|
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-22 01:38:28 +02:00
|
|
|
$: className = !modified ? "opacity-0" : "";
|
2021-04-12 06:18:30 +02:00
|
|
|
|
2021-06-21 21:35:20 +02:00
|
|
|
const isTouchDevice = getContext<boolean>(touchDeviceKey);
|
|
|
|
|
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 20:24:15 +02:00
|
|
|
<WithDropdownMenu
|
|
|
|
disabled={!modified}
|
|
|
|
let:createDropdown
|
|
|
|
let:activateDropdown
|
|
|
|
let:menuId
|
|
|
|
>
|
|
|
|
<Badge
|
2021-06-21 20:48:33 +02:00
|
|
|
class={`p-1 ${className}`}
|
2021-06-21 20:24:15 +02:00
|
|
|
on:mount={(event) => createDropdown(event.detail.span)}
|
|
|
|
on:click={activateDropdown}
|
|
|
|
>
|
2021-06-24 05:05:33 +02:00
|
|
|
{@html revertIcon}
|
2021-06-21 19:11:10 +02:00
|
|
|
</Badge>
|
|
|
|
|
|
|
|
<DropdownMenu id={menuId}>
|
2021-06-21 20:24:15 +02:00
|
|
|
<DropdownItem
|
2021-06-21 21:35:20 +02:00
|
|
|
class={`spinner ${isTouchDevice ? "spin-always" : ""}`}
|
2021-06-21 20:24:15 +02:00
|
|
|
on:click={() => {
|
|
|
|
revert();
|
|
|
|
// Otherwise the menu won't close when the item is clicked
|
|
|
|
// TODO: investigate why this is necessary
|
|
|
|
activateDropdown();
|
|
|
|
}}
|
|
|
|
>
|
2021-06-21 19:11:10 +02:00
|
|
|
{tr.deckConfigRevertButtonTooltip()}<Badge>{@html revertIcon}</Badge>
|
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenu>
|
|
|
|
</WithDropdownMenu>
|
2021-06-21 20:48:33 +02:00
|
|
|
|
|
|
|
<style lang="scss">
|
2021-06-21 21:35:20 +02:00
|
|
|
:global(.spinner:hover .badge, .spinner.spin-always .badge) {
|
2021-06-21 20:48:33 +02:00
|
|
|
animation: spin-animation 1s infinite;
|
|
|
|
animation-timing-function: linear;
|
|
|
|
}
|
|
|
|
|
|
|
|
@keyframes -global-spin-animation {
|
|
|
|
0% {
|
|
|
|
transform: rotate(360deg);
|
|
|
|
}
|
|
|
|
100% {
|
|
|
|
transform: rotate(0deg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|