Commit 04/08/2025

This commit is contained in:
2025-08-04 13:21:06 +07:00
parent 38882a2b72
commit 8290418100
7 changed files with 379 additions and 47 deletions

View File

@@ -0,0 +1,184 @@
$(document).ready(function() {
// Your initialization code here
console.log('setting.js is ready!');
$('#dropdownM1 button').text('');
$('#dropdownM2 button').text('');
$('#dropdownM3 button').text('');
$('#dropdownM4 button').text('');
$('#dropdownM5 button').text('');
$('#dropdownM6 button').text('');
$('#dropdownM7 button').text('');
$('#dropdownM8 button').text('');
$('#dropdownM1 .dropdown-menu').empty();
$('#dropdownM2 .dropdown-menu').empty();
$('#dropdownM3 .dropdown-menu').empty();
$('#dropdownM4 .dropdown-menu').empty();
$('#dropdownM5 .dropdown-menu').empty();
$('#dropdownM6 .dropdown-menu').empty();
$('#dropdownM7 .dropdown-menu').empty();
$('#dropdownM8 .dropdown-menu').empty();
// Connect to WebSocket at "<current html file>/ws"
const path = window.location.pathname;
const ws = new WebSocket('ws://' + window.location.host + path + '/ws');
ws.onopen = function() {
console.log('WebSocket connection opened');
$('#indicatorDisconnected').addClass('visually-hidden');
$('#indicatorConnected').removeClass('visually-hidden');
sendCommand({ command: "getConfig" });
};
ws.onmessage = function(event) {
console.log('WebSocket message received:', event.data);
let msg = {};
try {
msg = JSON.parse(event.data);
} catch (e) {
return;
}
if (msg.reply === "getConfig" && msg.data !== undefined ) {
const configData = msg.data;
console.log('Config Data:', configData);
$('#zelloUsername').val(configData.ZelloUsername || '');
$('#zelloPassword').val(configData.ZelloPassword || '');
$('#zelloChannel').val(configData.ZelloChannel || '');
if ("community" === configData.ZelloServer) {
$('#zellocommunity').prop('checked', true);
$('#zelloWorkNetworkName').val('').prop('disabled', true);
$('#zelloEnterpriseServerDomain').val('').prop('disabled', true);
} else if ("work" === configData.ZelloServer) {
$('#zellowork').prop('checked', true);
$('#zelloWorkNetworkName').val(configData.ZelloWorkNetworkName || '').prop('disabled', false);
$('#zelloEnterpriseServerDomain').val('').prop('disabled', true);
} else if ("enterprise" === configData.ZelloServer) {
$('#zelloenterprise').prop('checked', true);
$('#zelloWorkNetworkName').val('').prop('disabled', true);
$('#zelloEnterpriseServerDomain').val(configData.ZelloEnterpriseServerDomain || '').prop('disabled', false);
}
for (let i = 1; i <= 8; i++) {
const dropdownMenu = $(`#dropdownM${i} .dropdown-menu`);
const dropdownButton = $(`#dropdownM${i} button`);
dropdownMenu.empty();
const messages = configData[`MessageList`] || [];
messages.forEach((msg, idx) => {
const item = $('<a class="dropdown-item" href="#"></a>').text(msg);
item.on('click', function() {
dropdownButton.text(msg);
});
dropdownMenu.append(item);
});
// Set button text to selected message if present
if (configData[`M${i}`]) {
dropdownButton.text(configData[`M${i}`]);
} else {
dropdownButton.text('');
}
}
}
};
ws.onclose = function() {
console.log('WebSocket connection closed');
$('#indicatorDisconnected').removeClass('visually-hidden');
$('#indicatorConnected').addClass('visually-hidden');
};
ws.onerror = function(error) {
console.error('WebSocket error:', error);
};
$('#zellocommunity').on('click', function() {
$('#zelloWorkNetworkName').val('').prop('disabled', true);
$('#zelloEnterpriseServerDomain').val('').prop('disabled', true);
});
$('#zellowork').on('click', function() {
$('#zelloWorkNetworkName').prop('disabled', false);
$('#zelloEnterpriseServerDomain').val('').prop('disabled', true);
});
$('#zelloenterprise').on('click', function() {
$('#zelloWorkNetworkName').val('').prop('disabled', true);
$('#zelloEnterpriseServerDomain').prop('disabled', false);
});
$('#btnApplyZello').on('click', function() {
if (ws.readyState === WebSocket.OPEN) {
let data = {
command: "setZelloConfig",
ZelloUsername: $('#zelloUsername').val(),
ZelloPassword: $('#zelloPassword').val(),
ZelloChannel: $('#zelloChannel').val(),
ZelloServer: $('#zellocommunity').is(':checked') ? 'community' :
$('#zellowork').is(':checked') ? 'work' :
$('#zelloenterprise').is(':checked') ? 'enterprise' : ''
};
if ($('#zellowork').is(':checked')) {
data.ZelloWorkNetworkName = $('#zelloWorkNetworkName').val();
}
if ($('#zelloenterprise').is(':checked')) {
data.ZelloEnterpriseServerDomain = $('#zelloEnterpriseServerDomain').val();
}
sendCommand(data);
} else {
console.warn('WebSocket is not open.');
}
});
$('#btnApplyMessage').on('click', function() {
if (ws.readyState === WebSocket.OPEN) {
let data = {
command: "setMessageConfig",
M1: $('#dropdownM1 button').text(),
M2: $('#dropdownM2 button').text(),
M3: $('#dropdownM3 button').text(),
M4: $('#dropdownM4 button').text(),
M5: $('#dropdownM5 button').text(),
M6: $('#dropdownM6 button').text(),
M7: $('#dropdownM7 button').text(),
M8: $('#dropdownM8 button').text()
};
sendCommand(data);
} else {
console.warn('WebSocket is not open.');
}
});
$('#btnUploadContent').on('click', function() {
const fileInput = document.getElementById('chosenFile');
if (!fileInput || !fileInput.files.length) {
alert('Please select a file to upload.');
return;
}
const file = fileInput.files[0];
const formData = new FormData();
formData.append('file', file);
fetch(window.location.pathname + '/upload', {
method: 'POST',
body: formData
})
.then(response => {
if (response.ok) {
alert('File uploaded successfully.');
} else {
alert('File upload failed.');
}
})
.catch(error => {
console.error('Upload error:', error);
alert('An error occurred during upload.');
});
});
function sendCommand(cmd) {
if (ws.readyState === WebSocket.OPEN) {
ws.send(cmd);
} else {
console.error('WebSocket is not open. Unable to send command:', JSON.stringify(cmd));
}
}
});