Skip to content

Commit

Permalink
pikvm/pikvm#1405: Fixed behaviour on duplicating gpio leds
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevaev committed Oct 2, 2024
1 parent f4ba421 commit 8ce27dc
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions web/share/js/kvm/gpio.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"use strict";


import {tools, $, $$$} from "../tools.js";
import {tools, $, $$} from "../tools.js";
import {wm} from "../wm.js";


Expand All @@ -39,29 +39,26 @@ export function Gpio(__recorder) {
self.setState = function(state) {
if (state) {
for (let channel in state.inputs) {
let el = $(`gpio-led-${channel}`);
if (el) {
for (let el of $$(`gpio-led-${channel}`)) {
__setLedState(el, state.inputs[channel].state);
}
}
for (let channel in state.outputs) {
for (let type of ["switch", "button"]) {
let el = $(`gpio-${type}-${channel}`);
if (el) {
for (let el of $$(`gpio-${type}-${channel}`)) {
tools.el.setEnabled(el, state.outputs[channel].online && !state.outputs[channel].busy);
}
}
let el = $(`gpio-switch-${channel}`);
if (el) {
for (let el of $$(`gpio-switch-${channel}`)) {
el.checked = state.outputs[channel].state;
}
}
} else {
for (let el of $$$(".gpio-led")) {
for (let el of $$("gpio-led")) {
__setLedState(el, false);
}
for (let selector of [".gpio-switch", ".gpio-button"]) {
for (let el of $$$(selector)) {
for (let selector of ["gpio-switch", "gpio-button"]) {
for (let el of $$(selector)) {
tools.el.setEnabled(el, false);
}
}
Expand Down Expand Up @@ -103,13 +100,11 @@ export function Gpio(__recorder) {
$("gpio-menu").innerHTML = content;

for (let channel in model.scheme.outputs) {
let el = $(`gpio-switch-${channel}`);
if (el) {
tools.el.setOnClick(el, __createAction(el, __switchChannel));
for (let el of $$(`gpio-switch-${channel}`)) {
tools.el.setOnClick(el, tools.makeClosure(__switchChannel, el));
}
el = $(`gpio-button-${channel}`);
if (el) {
tools.el.setOnClick(el, __createAction(el, __pulseChannel));
for (let el of $$(`gpio-button-${channel}`)) {
tools.el.setOnClick(el, tools.makeClosure(__pulseChannel, el));
}
}

Expand All @@ -120,27 +115,33 @@ export function Gpio(__recorder) {
self.setState(__state);
};

var __createAction = function(el, action) {
return () => action(el);
};

var __createItem = function(item) {
if (item.type === "label") {
return item.text;
} else if (item.type === "input") {
return `
<img id="gpio-led-${item.channel}" class="gpio-led inline-lamp-big led-gray"
src="/share/svg/led-circle.svg" data-color="${item.color}" />
<img
class="gpio-led gpio-led-${item.channel} inline-lamp-big led-gray"
src="/share/svg/led-circle.svg"
data-color="${item.color}"
/>
`;
} else if (item.type === "output") {
let controls = [];
let confirm = (item.confirm ? "Are you sure you want to perform this action?" : "");
if (item.scheme["switch"]) {
let id = tools.makeId();
controls.push(`
<td><div class="switch-box">
<input disabled type="checkbox" id="gpio-switch-${item.channel}" class="gpio-switch"
data-channel="${item.channel}" data-confirm="${confirm}" />
<label for="gpio-switch-${item.channel}">
<input
disabled
type="checkbox"
id="gpio-switch-${id}"
class="gpio-switch gpio-switch-${item.channel}"
data-channel="${item.channel}"
data-confirm="${confirm}"
/>
<label for="gpio-switch-${id}">
<span class="switch-inner"></span>
<span class="switch"></span>
</label>
Expand All @@ -149,10 +150,14 @@ export function Gpio(__recorder) {
}
if (item.scheme.pulse.delay) {
controls.push(`
<td><button disabled id="gpio-button-${item.channel}" class="gpio-button"
${item.hide ? "data-force-hide-menu" : ""}
data-channel="${item.channel}" data-confirm="${confirm}">
${(item.hide ? "&bull; " : "") + item.text}
<td><button
disabled
class="gpio-button gpio-button-${item.channel}"
${item.hide ? "data-force-hide-menu" : ""}
data-channel="${item.channel}"
data-confirm="${confirm}"
>
${(item.hide ? "&bull; " : "") + item.text}
</button></td>
`);
}
Expand All @@ -176,7 +181,7 @@ export function Gpio(__recorder) {
var __switchChannel = function(el) {
let channel = el.getAttribute("data-channel");
let confirm = el.getAttribute("data-confirm");
let to = ($(`gpio-switch-${channel}`).checked ? "1" : "0");
let to = (el.checked ? "1" : "0");
if (to === "0" && el.hasAttribute("data-confirm-off")) {
confirm = el.getAttribute("data-confirm-off");
}
Expand Down

0 comments on commit 8ce27dc

Please sign in to comment.