-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (66 loc) · 3 KB
/
script.js
File metadata and controls
73 lines (66 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
let allClientsData = []; // Store the full data globally
$(document).ready(function () {
// FETCHING DATA FROM JSON FILE
$.getJSON("clients.json", function (data) {
allClientsData = data.sort(function(a, b) {
return a.NAME.localeCompare(b.NAME);
});
// DO NOT call renderTable(allClientsData) here
// The table will remain hidden until a search is performed.
}).fail(function() {
$('#clientTableBody').html('<tr><td colspan="1" style="text-align: center; color: var(--primary-color);">Error loading client data. Please check clients.json.</td></tr>');
// Hide the initial message if there's an error
$('#initialMessage').hide();
});
// Set focus on the search bar when the page loads
$('#clientSearch').focus();
// Search functionality
$('#clientSearch').on('keyup', function() {
const searchTerm = $(this).val().toLowerCase().trim(); // Trim whitespace
const $initialMessage = $('#initialMessage');
const $noResults = $('#noResults');
const $clientTable = $('#clientTable');
if (searchTerm.length > 0) {
$initialMessage.hide(); // Hide the initial message once typing starts
const filteredClients = allClientsData.filter(client => {
return client.NAME.toLowerCase().includes(searchTerm);
});
renderTable(filteredClients);
$clientTable.show(); // Show the table only when there's a search term
} else {
// If search bar is empty, hide the table and show initial message
$clientTable.hide();
$noResults.hide(); // Hide no results as well
$initialMessage.show(); // Show initial message again
}
});
// Register the Service Worker (moved here from index.html)
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
});
function renderTable(clientsToRender) {
let clientRows = '';
const $tableBody = $('#clientTableBody');
const $noResults = $('#noResults');
if (clientsToRender.length === 0) {
$noResults.show(); // Show no results message
$tableBody.empty(); // Clear existing rows
} else {
$noResults.hide(); // Hide no results message
$.each(clientsToRender, function (key, value) {
clientRows += '<tr>';
clientRows += '<td><a href="#" onclick="location.href=\'rustdesk://' + value.ID + '\'">' + value.NAME + '</a></td>';
clientRows += '</tr>';
});
$tableBody.html(clientRows); // Use .html() to replace content
}
}