Refactorized the action listener of the tc buttons in the webinterface

This commit is contained in:
Kai Lauterbach 2023-05-20 10:57:50 +02:00
parent ec30fa512b
commit 83c2d74a79
2 changed files with 62 additions and 137 deletions

View file

@ -1,21 +1,18 @@
var links = document.querySelectorAll('[id^="tc_on"]'); function handleRequest(id, url) {
links.forEach(function(link) { var button = document.getElementById(id);
link.addEventListener('click', function(event) {
event.preventDefault();
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://{{IP_ADDRESS}}/?tc=true', true); xhr.open('GET', url, true);
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
var button = document.getElementById("tc_on");
if (xhr.status === 200) { if (xhr.status === 200) {
button.classList.remove("pure-button-primary"); button.classList.remove("pure-button-primary");
button.classList.add("success"); button.classList.add("success");
button.innerHTML = "Enabled!"; button.innerHTML = (id === 'tc_on') ? "Enabled!" : "Disabled!";
button.style.backgroundColor = "green"; button.style.backgroundColor = "green";
setTimeout(function () { setTimeout(function () {
button.classList.remove("success"); button.classList.remove("success");
button.classList.add("pure-button-primary"); button.classList.add("pure-button-primary");
button.innerHTML = "ON"; button.innerHTML = (id === 'tc_on') ? "ON" : "OFF";
button.style.backgroundColor = ""; button.style.backgroundColor = "";
}, 2000); }, 2000);
console.log('Data successfully sent to server!'); console.log('Data successfully sent to server!');
@ -27,7 +24,7 @@ button.style.backgroundColor = "";
setTimeout(function () { setTimeout(function () {
button.classList.remove("error"); button.classList.remove("error");
button.classList.add("pure-button-primary"); button.classList.add("pure-button-primary");
button.innerHTML = "ON"; button.innerHTML = (id === 'tc_on') ? "ON" : "OFF";
button.style.backgroundColor = ""; button.style.backgroundColor = "";
}, 2000); }, 2000);
showToast('Error while sending data to server.', 'error'); showToast('Error while sending data to server.', 'error');
@ -36,52 +33,18 @@ console.log('Error while sending data to server.');
} }
}; };
xhr.send(); xhr.send();
document.getElementById('tc_on').classList.add('pure-button-primary'); }
document.getElementById('tc_off').classList.remove('pure-button-primary'); function addButtonClickListener(id, url) {
}); var link = document.getElementById(id);
}); link.addEventListener('click', function (event) {
var links = document.querySelectorAll('[id^="tc_off"]');
links.forEach(function(link) {
link.addEventListener('click', function(event) {
event.preventDefault(); event.preventDefault();
var xhr = new XMLHttpRequest(); handleRequest(id, url);
xhr.open('GET', 'http://{{IP_ADDRESS}}/?tc=false', true); document.getElementById(id).classList.add('pure-button-primary');
xhr.onreadystatechange = function () { document.getElementById(id === 'tc_on' ? 'tc_off' : 'tc_on').classList.remove('pure-button-primary');
if (xhr.readyState === 4) {
var button = document.getElementById("tc_off");
if (xhr.status === 200) {
button.classList.remove("pure-button-primary");
button.classList.add("success");
button.innerHTML = "Disabled!";
button.style.backgroundColor = "green";
setTimeout(function () {
button.classList.remove("success");
button.classList.add("pure-button-primary");
button.innerHTML = "OFF";
button.style.backgroundColor = "";
}, 2000);
console.log('Data successfully sent to server!');
} else {
button.classList.remove("pure-button-primary");
button.classList.add("error");
button.innerHTML = "Error!";
button.style.backgroundColor = "red";
setTimeout(function () {
button.classList.remove("error");
button.classList.add("pure-button-primary");
button.innerHTML = "OFF";
button.style.backgroundColor = "";
}, 2000);
showToast('Error while sending data to server.', 'error');
console.log('Error while sending data to server.');
}
}
};
xhr.send();
document.getElementById('tc_off').classList.add('pure-button-primary');
document.getElementById('tc_on').classList.remove('pure-button-primary');
});
}); });
}
addButtonClickListener('tc_on', 'http://{{IP_ADDRESS}}/?tc=true');
addButtonClickListener('tc_off', 'http://{{IP_ADDRESS}}/?tc=false');
let timeoutId; let timeoutId;
function sendSliderValue(x) { function sendSliderValue(x) {
x = x - 1; x = x - 1;

View file

@ -1,93 +1,55 @@
var links = document.querySelectorAll('[id^="tc_on"]'); function handleRequest(id, url) {
links.forEach(function(link) { var button = document.getElementById(id);
link.addEventListener('click', function(event) {
event.preventDefault();
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://{{IP_ADDRESS}}/?tc=true', true);
xhr.onreadystatechange = function () { var xhr = new XMLHttpRequest();
if (xhr.readyState === 4) { xhr.open('GET', url, true);
var button = document.getElementById("tc_on");
if (xhr.status === 200) { xhr.onreadystatechange = function () {
button.classList.remove("pure-button-primary"); if (xhr.readyState === 4) {
button.classList.add("success"); if (xhr.status === 200) {
button.innerHTML = "Enabled!"; button.classList.remove("pure-button-primary");
button.style.backgroundColor = "green"; button.classList.add("success");
setTimeout(function () { button.innerHTML = (id === 'tc_on') ? "Enabled!" : "Disabled!";
button.classList.remove("success"); button.style.backgroundColor = "green";
button.classList.add("pure-button-primary"); setTimeout(function () {
button.innerHTML = "ON"; button.classList.remove("success");
button.style.backgroundColor = ""; button.classList.add("pure-button-primary");
}, 2000); button.innerHTML = (id === 'tc_on') ? "ON" : "OFF";
console.log('Data successfully sent to server!');
} else {
button.classList.remove("pure-button-primary");
button.classList.add("error");
button.innerHTML = "Error!";
button.style.backgroundColor = ""; button.style.backgroundColor = "";
setTimeout(function () { }, 2000);
button.classList.remove("error"); console.log('Data successfully sent to server!');
button.classList.add("pure-button-primary"); } else {
button.innerHTML = "ON"; button.classList.remove("pure-button-primary");
button.style.backgroundColor = ""; button.classList.add("error");
}, 2000); button.innerHTML = "Error!";
showToast('Error while sending data to server.', 'error'); button.style.backgroundColor = "";
console.log('Error while sending data to server.'); setTimeout(function () {
} button.classList.remove("error");
button.classList.add("pure-button-primary");
button.innerHTML = (id === 'tc_on') ? "ON" : "OFF";
button.style.backgroundColor = "";
}, 2000);
showToast('Error while sending data to server.', 'error');
console.log('Error while sending data to server.');
} }
}; }
};
xhr.send(); xhr.send();
//console.log('tc=true call'); }
document.getElementById('tc_on').classList.add('pure-button-primary');
document.getElementById('tc_off').classList.remove('pure-button-primary'); function addButtonClickListener(id, url) {
}); var link = document.getElementById(id);
}); link.addEventListener('click', function (event) {
var links = document.querySelectorAll('[id^="tc_off"]');
links.forEach(function(link) {
link.addEventListener('click', function(event) {
event.preventDefault(); event.preventDefault();
var xhr = new XMLHttpRequest(); handleRequest(id, url);
xhr.open('GET', 'http://{{IP_ADDRESS}}/?tc=false', true); document.getElementById(id).classList.add('pure-button-primary');
document.getElementById(id === 'tc_on' ? 'tc_off' : 'tc_on').classList.remove('pure-button-primary');
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var button = document.getElementById("tc_off");
if (xhr.status === 200) {
button.classList.remove("pure-button-primary");
button.classList.add("success");
button.innerHTML = "Disabled!";
button.style.backgroundColor = "green";
setTimeout(function () {
button.classList.remove("success");
button.classList.add("pure-button-primary");
button.innerHTML = "OFF";
button.style.backgroundColor = "";
}, 2000);
console.log('Data successfully sent to server!');
} else {
button.classList.remove("pure-button-primary");
button.classList.add("error");
button.innerHTML = "Error!";
button.style.backgroundColor = "red";
setTimeout(function () {
button.classList.remove("error");
button.classList.add("pure-button-primary");
button.innerHTML = "OFF";
button.style.backgroundColor = "";
}, 2000);
showToast('Error while sending data to server.', 'error');
console.log('Error while sending data to server.');
}
}
};
xhr.send();
//console.log('tc=false call');
document.getElementById('tc_off').classList.add('pure-button-primary');
document.getElementById('tc_on').classList.remove('pure-button-primary');
}); });
}); }
addButtonClickListener('tc_on', 'http://{{IP_ADDRESS}}/?tc=true');
addButtonClickListener('tc_off', 'http://{{IP_ADDRESS}}/?tc=false');
let timeoutId; let timeoutId;
function sendSliderValue(x) { function sendSliderValue(x) {