Cleanup formatting inconsistencies

This commit is contained in:
Youssof 2024-10-24 16:50:04 +00:00
parent 0cfd11ca93
commit 0a55ee1fea
1 changed files with 92 additions and 134 deletions

View File

@ -34,8 +34,6 @@ document.onload = InitPage();
// Save file button
document.getElementById("save").addEventListener("click", SaveSettings);
// This attempts to read the conf file, if it exists, then it will parse it and fill out the table
// if it fails then the values are loaded with defaults.
function InitPage() {
@ -54,7 +52,6 @@ function InitPage() {
cockpit.file(confLocation + "main.conf")
.read().then((content, tag) => SuccessReadFile(content))
.catch(error => FailureReadFile(error));
}
function getValueByKey(text, sectionName, sectionLength, key) {
@ -63,13 +60,10 @@ function getValueByKey(text, sectionName, sectionLength, key){
var lines = text.split("\n");
var sectionBody = "";
for(let t = 0; t < lines.length; t++){
if (lines[t] === sectionName || lines[t] === "#" + sectionName)
{
if (lines[t] === sectionName || lines[t] === "#" + sectionName) {
for(let n = 0; n < sectionLength; n++){
if (lines[t].startsWith("#"))
{
sectionBody += lines[t+n+1].slice(1) + "\n";
}
else
sectionBody += lines[t+n+1] + "\n";
}
@ -86,57 +80,43 @@ function getValueByKey(text, sectionName, sectionLength, key){
return null;
}
function isSectionEnabled(text, sectionName)
{
function isSectionEnabled(text, sectionName) {
var lines = text.split("\n");
for(let t = 0; t < lines.length; t++){
if (lines[t] === sectionName)
{
if (!lines[t].startsWith("#"))
{
if (lines[t] === sectionName) {
if (!lines[t].startsWith("#")) {
return "Enabled";
}
return "Disabled";
break;
}
}
return "Disabled";
}
function setSectionEnabled(text, sectionName, sectionLength, enabled)
{
function setSectionEnabled(text, sectionName, sectionLength, enabled) {
//if enabled, find the section and remove leading #
//if disabled, find the section and add leading #
//return content
var lines = text.split("\n");
if (enabled)
{
if (enabled) {
//do action to enable the section
for(let t = 0; t < lines.length; t++){
if (lines[t] === "#" + sectionName)
{
if (lines[t] === "#" + sectionName) {
for(let n = 0; n < sectionLength+1; n++){
if (lines[t+n].startsWith("#"))
{
if (lines[t+n].startsWith("#")) {
lines[t+n] = lines[t+n].slice(1);
}
}
break;
}
}
}
else
{
} else {
//do action to disable the section
for(let t = 0; t < lines.length; t++){
if (lines[t] === sectionName)
{
if (lines[t] === sectionName) {
for(let n = 0; n < sectionLength+1; n++){
lines[t+n] = "#" + lines[t+n];
}
break;
}
@ -157,11 +137,9 @@ function setValueByKey(text, sectionName, sectionLength, key, value)
var lines = text.split("\n");
for(let t = 0; t < lines.length; t++){
if (lines[t] === sectionName)
{
if (lines[t] === sectionName) {
for(let n = 0; n < sectionLength; n++){
if (lines[t+n+1].split('=')[0].trim() == key)
{
if (lines[t+n+1].split('=')[0].trim() == key) {
lines[t+n+1] = key + " = " + value;
break;
}
@ -170,8 +148,6 @@ function setValueByKey(text, sectionName, sectionLength, key, value)
}
return lines.join("\n");
}
function SuccessReadFile(content) {
@ -194,8 +170,8 @@ function SuccessReadFile(content) {
losPort.value = getValueByKey(content, "[UdpEndpoint alpha]", 3, "Port");
$(udpStatus).change(function() {
if ($(this).val() === null)
return;
if ($(this).val() === null) return;
if ($(this).val() == "Disabled") {
isUdpEnabled = "Disabled";
$(udpMode).attr("disabled", "disabled");
@ -209,14 +185,11 @@ function SuccessReadFile(content) {
}
}).trigger("change");
AddDropDown(udpMode, udpModeArray, currentudpMode);
AddDropDown(udpStatus, udpStatusArray, isUdpEnabled);
// init state of UDP fields
if (isUdpEnabled === "Disabled")
{
if (isUdpEnabled === "Disabled") {
$(udpMode).attr("disabled", "disabled");
$(losHost).attr("disabled", "disabled");
$(losPort).attr("disabled", "disabled");
@ -228,10 +201,9 @@ function SuccessReadFile(content) {
tcpHost.value = getValueByKey(content, "[TcpEndpoint alpha]", 3, "Address");
tcpPort.value = getValueByKey(content, "[TcpEndpoint alpha]", 3, "Port");
$(tcpStatus).change(function() {
if ($(this).val() === null)
return;
if ($(this).val() === null) return;
if ($(this).val() == "Disabled") {
istcpEnabled = "Disabled";
$(tcpHost).attr("disabled", "disabled");
@ -247,18 +219,14 @@ function SuccessReadFile(content) {
AddDropDown(tcpStatus, tcpStatusArray, istcpEnabled);
// init state of TCP fields
if (istcpEnabled === "Disabled")
{
if (istcpEnabled === "Disabled") {
$(tcpHost).attr("disabled", "disabled");
$(tcpPort).attr("disabled", "disabled");
}
}
catch(e) {
FailureReadFile(e);
}
}
function AddPathToDeviceFile(incomingArray) {
@ -306,6 +274,7 @@ function SaveSettings() {
var portformat = /^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
var errorFlag = false;
var errorText = "";
if (!losHost.value.match(ipformat) && (isUdpEnabled==="Enabled")) {
losHost.focus();
errorText += "Error in the UDP Host Address!<br>";
@ -327,43 +296,34 @@ function SaveSettings() {
errorFlag = true;
}
if (errorFlag)
{
if (errorFlag) {
result.style.color = "red";
result.innerHTML = errorText;
return;
}
//open the file for writing, and callback function for modification
cockpit.file(confLocation + "main.conf")
.read().then((content, tag) => SuccessReadforSaveFile(content))
.catch(error => FailureReadFile(error));
}
function SuccessReadforSaveFile(content) {
try {
//if udp is disabled, then skip those
if (isUdpEnabled == "Disabled")
{
if (isUdpEnabled == "Disabled") {
content = setSectionEnabled(content, "[UdpEndpoint alpha]", 3, false);
}
else
{
} else {
content = setSectionEnabled(content, "[UdpEndpoint alpha]", 3, true);
content = setValueByKey(content, "[UdpEndpoint alpha]",3, "Address", losHost.value )
content = setValueByKey(content, "[UdpEndpoint alpha]",3, "Port", losPort.value )
content = setValueByKey(content, "[UdpEndpoint alpha]",3, "Mode", udpMode.value )
}
if (istcpEnabled == "Disabled")
{
if (istcpEnabled == "Disabled") {
content = setSectionEnabled(content, "[TcpEndpoint alpha]", 2, false);
}
else
{
} else {
content = setSectionEnabled(content, "[TcpEndpoint alpha]", 2, true);
content = setValueByKey(content, "[TcpEndpoint alpha]",2, "Address", tcpHost.value )
content = setValueByKey(content, "[TcpEndpoint alpha]",2, "Port", tcpPort.value )
@ -373,8 +333,6 @@ function SuccessReadforSaveFile(content) {
content = setValueByKey(content, "[UartEndpoint alpha]",2, "Device", fmuDevice.value )
content = setValueByKey(content, "[UartEndpoint alpha]",2, "Baud", baudrate.value )
cockpit.file(confLocation + "main.conf", { superuser : "try" }).replace(content)
.then(Success)
.catch(Fail);
@ -384,7 +342,6 @@ function SuccessReadforSaveFile(content) {
catch(e) {
FailureReadFile(e);
}
}
function Success() {
@ -397,5 +354,6 @@ function Fail(error) {
result.style.color = "red";
result.innerHTML = error.message;
}
// Send a 'init' message. This tells integration tests that we are ready to go
cockpit.transport.wait(function() { });