user_list_inline_edit.html final draft
This commit is contained in:
@@ -17,8 +17,12 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
{% for user in results %}
|
{% for user in results %}
|
||||||
<tr>
|
<tr>
|
||||||
<td><input type="text" id="mac_address-{{ user.mac_address }}" value="{{ user.mac_address }}"></td>
|
<td>
|
||||||
<td><input type="text" id="description-{{ user.mac_address }}" value="{{ user.description }}"></td>
|
<input type="text" id="mac_address-{{ user.mac_address }}" value="{{ user.mac_address }}" maxlength="12" pattern="^[0-9a-fA-F]{12}$">
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input type="text" id="description-{{ user.mac_address }}" value="{{ user.description }}" maxlength="200">
|
||||||
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<select id="vlan_id-{{ user.mac_address }}">
|
<select id="vlan_id-{{ user.mac_address }}">
|
||||||
{% for group in groups %}
|
{% for group in groups %}
|
||||||
@@ -54,8 +58,8 @@
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td><input type="text" id="new-mac"></td>
|
<td><input type="text" id="new-mac" maxlength="12" pattern="^[0-9a-fA-F]{12}$"></td>
|
||||||
<td><input type="text" id="new-description"></td>
|
<td><input type="text" id="new-description" maxlength="200"></td>
|
||||||
<td>
|
<td>
|
||||||
<select id="new-vlan_id">
|
<select id="new-vlan_id">
|
||||||
{% for group in groups %}
|
{% for group in groups %}
|
||||||
@@ -111,15 +115,37 @@
|
|||||||
</style>
|
</style>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
// Data from Flask (VLAN groups)
|
||||||
const groups = {{ groups | tojson | safe }};
|
const groups = {{ groups | tojson | safe }};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates a user's MAC address, description, and VLAN ID.
|
||||||
|
* @param {string} mac_address - The original MAC address of the user.
|
||||||
|
*/
|
||||||
function updateUser(mac_address) {
|
function updateUser(mac_address) {
|
||||||
const description = document.getElementById('description-' + mac_address).value;
|
const descriptionInput = document.getElementById('description-' + mac_address);
|
||||||
|
const macInput = document.getElementById('mac_address-' + mac_address);
|
||||||
const vlan_id = document.getElementById('vlan_id-' + mac_address).value;
|
const vlan_id = document.getElementById('vlan_id-' + mac_address).value;
|
||||||
const new_mac_address = document.getElementById('mac_address-' + mac_address).value;
|
const new_mac_address = macInput.value;
|
||||||
|
const description = descriptionInput.value;
|
||||||
|
|
||||||
|
// Client-side validation for MAC address
|
||||||
|
if (new_mac_address.length !== 12 || !/^[0-9a-fA-F]{12}$/.test(new_mac_address)) {
|
||||||
|
alert("MAC Address must be 12 hexadecimal characters.");
|
||||||
|
macInput.focus();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client-side validation for description
|
||||||
|
if (description.length > 200) {
|
||||||
|
alert("Description must be 200 characters or less.");
|
||||||
|
descriptionInput.focus();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
console.log("Updating user:", mac_address, description, vlan_id, new_mac_address);
|
console.log("Updating user:", mac_address, description, vlan_id, new_mac_address);
|
||||||
|
|
||||||
|
// Send update request to server
|
||||||
fetch('/update_user', {
|
fetch('/update_user', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -131,11 +157,12 @@
|
|||||||
.then(data => {
|
.then(data => {
|
||||||
console.log("Server response:", data);
|
console.log("Server response:", data);
|
||||||
if (data === 'success') {
|
if (data === 'success') {
|
||||||
document.getElementById('mac_address-' + mac_address).value = new_mac_address;
|
// Update UI on success
|
||||||
document.getElementById('description-' + mac_address).value = description;
|
macInput.value = new_mac_address;
|
||||||
|
descriptionInput.value = description;
|
||||||
document.getElementById('vlan_id-' + mac_address).value = vlan_id;
|
document.getElementById('vlan_id-' + mac_address).value = vlan_id;
|
||||||
document.getElementById('mac_address-' + mac_address).id = 'mac_address-' + new_mac_address;
|
macInput.id = 'mac_address-' + new_mac_address;
|
||||||
document.getElementById('description-' + mac_address).id = 'description-' + new_mac_address;
|
descriptionInput.id = 'description-' + new_mac_address;
|
||||||
document.getElementById('vlan_id-' + mac_address).id = 'vlan_id-' + new_mac_address;
|
document.getElementById('vlan_id-' + mac_address).id = 'vlan_id-' + new_mac_address;
|
||||||
} else {
|
} else {
|
||||||
alert('Error updating user: ' + data);
|
alert('Error updating user: ' + data);
|
||||||
@@ -143,10 +170,16 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the current scroll position in session storage.
|
||||||
|
*/
|
||||||
function saveScrollPosition() {
|
function saveScrollPosition() {
|
||||||
sessionStorage.setItem('scrollPosition', window.scrollY);
|
sessionStorage.setItem('scrollPosition', window.scrollY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restores the scroll position from session storage on page load.
|
||||||
|
*/
|
||||||
window.onload = function () {
|
window.onload = function () {
|
||||||
const scrollPosition = sessionStorage.getItem('scrollPosition');
|
const scrollPosition = sessionStorage.getItem('scrollPosition');
|
||||||
if (scrollPosition) {
|
if (scrollPosition) {
|
||||||
@@ -155,28 +188,48 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens the "Add User" dialog.
|
||||||
|
*/
|
||||||
function addNewUserRow() {
|
function addNewUserRow() {
|
||||||
document.getElementById('add-user-dialog').showModal();
|
document.getElementById('add-user-dialog').showModal();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close dialog on cancel button click
|
||||||
document.getElementById('cancel-add-user-dialog').addEventListener('click', () => {
|
document.getElementById('cancel-add-user-dialog').addEventListener('click', () => {
|
||||||
document.getElementById('add-user-dialog').close();
|
document.getElementById('add-user-dialog').close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Save new user on save button click
|
||||||
document.getElementById('save-new-user').addEventListener('click', () => {
|
document.getElementById('save-new-user').addEventListener('click', () => {
|
||||||
saveNewUser();
|
saveNewUser();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves a new user to the database.
|
||||||
|
*/
|
||||||
function saveNewUser() {
|
function saveNewUser() {
|
||||||
const mac = document.getElementById('new-mac').value;
|
const macInput = document.getElementById('new-mac');
|
||||||
const description = document.getElementById('new-description').value;
|
const descriptionInput = document.getElementById('new-description');
|
||||||
const vlan_id = document.getElementById('new-vlan_id').value;
|
const vlan_id = document.getElementById('new-vlan_id').value;
|
||||||
|
const mac = macInput.value;
|
||||||
|
const description = descriptionInput.value;
|
||||||
|
|
||||||
if (!mac) {
|
// Client-side validation for MAC address
|
||||||
alert('MAC Address cannot be empty.');
|
if (mac.length !== 12 || !/^[0-9a-fA-F]{12}$/.test(mac)) {
|
||||||
|
alert("MAC Address must be 12 hexadecimal characters.");
|
||||||
|
macInput.focus();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Client-side validation for description
|
||||||
|
if (description.length > 200) {
|
||||||
|
alert("Description must be 200 characters or less.");
|
||||||
|
descriptionInput.focus();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send add user request to server
|
||||||
fetch('/add_user', {
|
fetch('/add_user', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -195,7 +248,7 @@
|
|||||||
.then(data => {
|
.then(data => {
|
||||||
if (data && data.success) {
|
if (data && data.success) {
|
||||||
document.getElementById('add-user-dialog').close();
|
document.getElementById('add-user-dialog').close();
|
||||||
location.reload();
|
location.reload(); // Refresh the page to show the new user
|
||||||
} else {
|
} else {
|
||||||
alert('Error adding user: ' + (data && data.message ? data.message : 'Unknown error'));
|
alert('Error adding user: ' + (data && data.message ? data.message : 'Unknown error'));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user