Spaces:
Running
Running
Download script.js from ffcc9393/dns-dynamo-dashboard: direct link, hf CLI and curl.
- Browser
- Download file 5.89 kB
-
https://huggingface.co/spaces/ffcc9393/dns-dynamo-dashboard/resolve/main/script.js
- Command line
-
hf download hf://spaces/ffcc9393/dns-dynamo-dashboard/script.js
-
curl -L -o script.js https://huggingface.co/spaces/ffcc9393/dns-dynamo-dashboard/resolve/main/script.js
5.89 kB
| document.addEventListener('DOMContentLoaded', function() { | |
| // Initialize modals | |
| const recordEditor = document.querySelector('custom-record-editor'); | |
| // Add record button | |
| document.querySelectorAll('.add-record-btn').forEach(btn => { | |
| btn.addEventListener('click', () => { | |
| recordEditor.setAttribute('visible', 'true'); | |
| recordEditor.setAttribute('mode', 'add'); | |
| feather.replace(); | |
| }); | |
| }); | |
| // Edit buttons | |
| document.querySelectorAll('.edit-btn').forEach(btn => { | |
| btn.addEventListener('click', (e) => { | |
| const row = e.target.closest('tr'); | |
| const recordData = { | |
| type: row.querySelector('.record-type').textContent, | |
| name: row.querySelector('.record-name').textContent, | |
| content: row.querySelector('.record-content').textContent, | |
| ttl: row.querySelector('.record-ttl').textContent, | |
| priority: row.querySelector('.record-priority')?.textContent || '' | |
| }; | |
| recordEditor.setAttribute('visible', 'true'); | |
| recordEditor.setAttribute('mode', 'edit'); | |
| populateEditForm(recordData); | |
| feather.replace(); | |
| }); | |
| }); | |
| // Delete buttons with confirmation | |
| document.querySelectorAll('.delete-btn').forEach(btn => { | |
| btn.addEventListener('click', async (e) => { | |
| if (!confirm('Are you sure you want to delete this record?')) return; | |
| const row = e.target.closest('tr'); | |
| try { | |
| // Simulate API call | |
| await new Promise(resolve => setTimeout(resolve, 500)); | |
| row.remove(); | |
| showToast('Record deleted successfully'); | |
| } catch (error) { | |
| showToast('Error deleting record', 'error'); | |
| } | |
| }); | |
| }); | |
| // Handle form submission | |
| const recordForm = document.getElementById('record-form'); | |
| if (recordForm) { | |
| recordForm.addEventListener('submit', async function(e) { | |
| e.preventDefault(); | |
| const formData = new FormData(recordForm); | |
| const recordData = Object.fromEntries(formData.entries()); | |
| try { | |
| // Simulate API call | |
| await new Promise(resolve => setTimeout(resolve, 800)); | |
| recordEditor.setAttribute('visible', 'false'); | |
| showToast('Record saved successfully'); | |
| // Refresh the table or update the specific row | |
| if (recordEditor.getAttribute('mode') === 'add') { | |
| addRecordToTable(recordData); | |
| } | |
| } catch (error) { | |
| showToast('Error saving record', 'error'); | |
| } | |
| }); | |
| } | |
| // Helper functions | |
| function populateEditForm(data) { | |
| const shadow = recordEditor.shadowRoot; | |
| shadow.getElementById('record-type').value = data.type; | |
| shadow.getElementById('record-name').value = data.name; | |
| shadow.getElementById('record-content').value = data.content; | |
| shadow.getElementById('record-ttl').value = data.ttl; | |
| if (data.priority && ['MX', 'SRV'].includes(data.type)) { | |
| shadow.getElementById('record-priority').value = data.priority; | |
| shadow.getElementById('priority-field').classList.remove('hidden'); | |
| } | |
| } | |
| function addRecordToTable(data) { | |
| const tableBody = document.querySelector('tbody'); | |
| const newRow = document.createElement('tr'); | |
| newRow.innerHTML = ` | |
| <td class="px-6 py-4 whitespace-nowrap"> | |
| <span class="record-type px-2 inline-flex text-xs leading-5 font-semibold rounded-full ${getTypeBadgeClass(data.type)}">${data.type}</span> | |
| </td> | |
| <td class="record-name px-6 py-4 whitespace-nowrap text-sm text-gray-900">${data.name}</td> | |
| <td class="record-content px-6 py-4 whitespace-nowrap text-sm text-gray-900">${data.content}</td> | |
| <td class="record-ttl px-6 py-4 whitespace-nowrap text-sm text-gray-900">${data.ttl}</td> | |
| <td class="record-priority px-6 py-4 whitespace-nowrap text-sm text-gray-900">${data.priority || '-'}</td> | |
| <td class="px-6 py-4 whitespace-nowrap text-sm font-medium flex gap-2"> | |
| <button class="edit-btn text-primary-600 hover:text-primary-900"> | |
| <i data-feather="edit-2"></i> | |
| </button> | |
| <button class="delete-btn text-red-600 hover:text-red-900"> | |
| <i data-feather="trash-2"></i> | |
| </button> | |
| </td> | |
| `; | |
| tableBody.appendChild(newRow); | |
| feather.replace(); | |
| } | |
| function getTypeBadgeClass(type) { | |
| const classes = { | |
| 'A': 'bg-green-100 text-green-800', | |
| 'AAAA': 'bg-emerald-100 text-emerald-800', | |
| 'CNAME': 'bg-purple-100 text-purple-800', | |
| 'MX': 'bg-blue-100 text-blue-800', | |
| 'TXT': 'bg-yellow-100 text-yellow-800', | |
| 'NS': 'bg-red-100 text-red-800', | |
| 'SRV': 'bg-lime-100 text-lime-800' | |
| }; | |
| return classes[type] || 'bg-gray-100 text-gray-800'; | |
| } | |
| function showToast(message, type = 'success') { | |
| const toast = document.createElement('div'); | |
| toast.className = `fixed top-4 right-4 z-50 px-4 py-2 rounded-md shadow-md text-white ${ | |
| type === 'success' ? 'bg-green-500' : 'bg-red-500' | |
| }`; | |
| toast.textContent = message; | |
| document.body.appendChild(toast); | |
| setTimeout(() => { | |
| toast.classList.add('opacity-0', 'transition-opacity', 'duration-300'); | |
| setTimeout(() => toast.remove(), 300); | |
| }, 3000); | |
| } | |
| }); | |