Files
IoTManagerWeb/src/widgets/Toggle.svelte

42 lines
1.2 KiB
Svelte
Raw Normal View History

2021-10-19 21:59:48 +08:00
<script>
2022-08-14 17:13:49 +02:00
import App from "../App.svelte";
2021-10-19 21:59:48 +08:00
export let widget;
export let toggleState = false;
export let wsPush = (ws, topic, status) => {};
2022-08-15 19:31:01 +02:00
$: widget.status, setValue();
2022-08-15 19:31:01 +02:00
function setValue() {
2022-08-15 19:31:01 +02:00
if (widget.status == "1") {
toggleState = true;
2022-08-15 19:31:01 +02:00
} else if (widget.status == "0") {
toggleState = false;
2022-08-15 19:31:01 +02:00
}
}
function changeValue() {
widget.sent = true;
if (toggleState) {
2022-08-15 19:31:01 +02:00
widget.status = "1";
} else {
widget.status = "0";
}
}
2021-10-19 21:59:48 +08:00
</script>
2021-12-28 23:55:14 +01:00
<div class="crd-itm-psn">
2022-02-11 00:22:41 +01:00
<div class="w-2/3">
2021-10-19 21:59:48 +08:00
<!-- svelte-ignore a11y-label-has-associated-control -->
2021-12-28 23:55:14 +01:00
<label class="wgt-dscr-stl">{!widget.descr ? "" : widget.descr}</label>
2021-10-19 21:59:48 +08:00
</div>
2022-02-11 00:22:41 +01:00
<div class="flex justify-end w-1/3">
2021-10-25 16:50:31 +07:00
<label for={widget.topic} class="items-center cursor-pointer">
<div class="relative">
<input bind:checked={toggleState} on:change={() => (changeValue(), wsPush(widget.ws, widget.topic, widget.status))} id={widget.topic} type="checkbox" class="sr-only" />
2021-10-27 05:11:28 +07:00
<div class="block bg-gray-600 w-10 h-6 rounded-full" />
<div class="dot {widget.sent ? 'bg-red-400' : 'bg-white'} absolute left-1 top-1 w-4 h-4 rounded-full transition" />
2021-10-25 16:50:31 +07:00
</div>
</label>
2021-10-19 21:59:48 +08:00
</div>
</div>