From c855d5099928fc62582fdc881101557d478e9a53 Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Sat, 1 Feb 2025 13:07:46 +1100 Subject: [PATCH] docs + sorting #983 --- docs/PLUGINS_DEV.md | 2 +- front/js/ui_components.js | 51 +++++++++++++++++++------ front/php/templates/language/ca_ca.json | 0 front/php/templates/language/en_us.json | 2 +- 4 files changed, 41 insertions(+), 14 deletions(-) mode change 100644 => 100755 front/php/templates/language/ca_ca.json diff --git a/docs/PLUGINS_DEV.md b/docs/PLUGINS_DEV.md index 6d948cc4..12316abe 100755 --- a/docs/PLUGINS_DEV.md +++ b/docs/PLUGINS_DEV.md @@ -578,7 +578,7 @@ Each element may also have associated events (e.g., running a scan or triggering ##### Supported settings `function` values -You can have any `"function": "my_custom_name"` custom name, however, the ones listed below have a specific functionality attached to them. If you use a custom name, then the setting is mostly used as an input parameter for the `params` section. +You can have any `"function": "my_custom_name"` custom name, however, the ones listed below have a specific functionality attached to them. | Setting | Description | | ------- | ----------- | diff --git a/front/js/ui_components.js b/front/js/ui_components.js index 12db34f6..78fe1f25 100755 --- a/front/js/ui_components.js +++ b/front/js/ui_components.js @@ -231,38 +231,65 @@ function copyToClipboard(buttonElement) { // Simple Sortable Table columns // ----------------------------------------------------------------------------- +// Function to handle column sorting when a user clicks on a table header function sortColumn(element) { - var th = $(element).closest('th'); - var table = th.closest('table'); - var columnIndex = th.index(); - var ascending = !th.data('asc'); + var th = $(element).closest('th'); // Get the clicked table header + var table = th.closest('table'); // Find the closest table + var columnIndex = th.index(); // Get the index of the column + var ascending = !th.data('asc'); // Toggle sorting order sortTable(table, columnIndex, ascending); - th.data('asc', ascending); + th.data('asc', ascending); // Store sorting order } +// Function to sort the table based on the selected column function sortTable(table, columnIndex, ascending) { - var tbody = table.find('tbody'); - var rows = tbody.find('tr').toArray().sort(comparer(columnIndex)); + var tbody = table.find('tbody'); // Get the table body + var rows = tbody.find('tr').toArray().sort(comparer(columnIndex)); // Convert rows to an array and sort if (!ascending) { - rows = rows.reverse(); + rows = rows.reverse(); // Reverse order if descending } for (var i = 0; i < rows.length; i++) { - tbody.append(rows[i]); + tbody.append(rows[i]); // Append sorted rows back to the table } } +// Function to compare values in the selected column function comparer(index) { - return function(a, b) { + return function (a, b) { var valA = getCellValue(a, index); var valB = getCellValue(b, index); - return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.localeCompare(valB); + + // Check if both values are valid IP addresses, and sort numerically if so + if (isIPAddress(valA) && isIPAddress(valB)) { + return ipToNum(valA) - ipToNum(valB); + } + + // If both values are numbers, sort numerically + if ($.isNumeric(valA) && $.isNumeric(valB)) { + return valA - valB; + } + + // Otherwise, sort as text + return valA.localeCompare(valB); }; } +// Function to get the text value from a table cell function getCellValue(row, index) { - return $(row).children('td').eq(index).text(); + return $(row).children('td').eq(index).text().trim(); // Get text from the specified column and trim spaces } +// Function to check if a string is a valid IPv4 address +function isIPAddress(value) { + return /^\d{1,3}(\.\d{1,3}){3}$/.test(value); // Regular expression to match IPv4 format +} + +// Function to convert an IP address to a numeric value for sorting +function ipToNum(ip) { + return ip.split('.').reduce((acc, octet) => (acc << 8) + parseInt(octet, 10), 0); +} + + // ----------------------------------------------------------------------------- // handling events // ----------------------------------------------------------------------------- diff --git a/front/php/templates/language/ca_ca.json b/front/php/templates/language/ca_ca.json old mode 100644 new mode 100755 diff --git a/front/php/templates/language/en_us.json b/front/php/templates/language/en_us.json index 42cfb001..b5b660e0 100755 --- a/front/php/templates/language/en_us.json +++ b/front/php/templates/language/en_us.json @@ -48,7 +48,7 @@ "BackDevices_DBTools_ImportCSVError": "The CSV file could not be imported. Make sure the format is correct.", "BackDevices_DBTools_ImportCSVMissing": "The CSV file could not be found under /config/devices.csv.", "BackDevices_DBTools_Purge": "The oldest backups were deleted", - "BackDevices_DBTools_UpdDev": "Device updated successfully", + "BackDevices_DBTools_UpdDev": "Device updated successfully. Main devices list may need some time to reload if a scan is in progress.", "BackDevices_DBTools_UpdDevError": "Error updating device", "BackDevices_DBTools_Upgrade": "Database upgraded successfully", "BackDevices_DBTools_UpgradeError": "Database upgrade failed",