Refactor software version update logic into a reusable function; enhance UI for file uploads and table responsiveness

This commit is contained in:
“VeLiTi”
2025-12-16 16:18:24 +01:00
parent a9f623cf22
commit 3693b52886
7 changed files with 243 additions and 72 deletions

View File

@@ -104,7 +104,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$api_url = '/v2/products_software_versions/';
$result = ioServer($api_url, json_encode($data));
if ($result) {
if ($result !== 'NOK') {
$success = isset($_POST['delete']) ? 3 : (isset($_POST['rowID']) && $_POST['rowID'] != '' ? 2 : 1);
header('Location: ' . $url . '&success_msg=' . $success);
exit;
@@ -147,7 +147,14 @@ $view .= '<div class="content-block">
<label for="hw_version">HW Version</label>
<input id="hw_version" type="text" name="hw_version" placeholder="HW Version" value="' . htmlspecialchars($version['hw_version']) . '">
<label for="fileToUpload">Upload File</label>
<input type="file" name="fileToUpload" id="fileToUpload" onchange="updateFields()">
<div class="file-upload-wrapper">
<button type="button" class="file-upload-btn" onclick="document.getElementById(\'fileToUpload\').click()">
<i class="fas fa-cloud-upload-alt"></i>
<span id="uploadBtnText">Choose File</span>
</button>
<input type="file" name="fileToUpload" id="fileToUpload" style="display: none;" onchange="updateFields()" accept=".hex,.bin,.fw">
<span class="file-upload-info" id="fileUploadInfo">No file selected</span>
</div>
<label for="file_path">File Path</label>
<input id="file_path" type="text" name="file_path" placeholder="File Path" value="' . htmlspecialchars($version['file_path']) . '" readonly>
<label class="checkbox">
@@ -164,7 +171,10 @@ $view .= '<div class="content-block">
<script>
function updateFields() {
var fileInput = document.getElementById(\'fileToUpload\');
var uploadBtnText = document.getElementById(\'uploadBtnText\');
var fileUploadInfo = document.getElementById(\'fileUploadInfo\');
var file = fileInput.files[0];
if (file) {
var fileName = file.name;
var filePathInput = document.getElementById(\'file_path\');
@@ -175,8 +185,25 @@ function updateFields() {
var nameWithoutExt = fileName.replace(/\.[^/.]+$/, "");
versionInput.value = nameWithoutExt;
}
// Update button and info text
uploadBtnText.textContent = \'Change File\';
fileUploadInfo.textContent = fileName + \' (\' + formatFileSize(file.size) + \')\';
fileUploadInfo.style.color = \'#28a745\';
} else {
uploadBtnText.textContent = \'Choose File\';
fileUploadInfo.textContent = \'No file selected\';
fileUploadInfo.style.color = \'#6c757d\';
}
}
function formatFileSize(bytes) {
if (bytes === 0) return \'0 Bytes\';
const k = 1024;
const sizes = [\'Bytes\', \'KB\', \'MB\', \'GB\'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + \' \' + sizes[i];
}
</script>
';