Files
resman/backend/public/js/modals.js
T
2026-04-24 09:57:25 +02:00

311 lines
7.3 KiB
JavaScript

/* =========================
RESOURCE MODAL
========================= */
function toInputDate(value){
if(!value) return ""
return String(value).slice(0, 10)
}
function formatDisplayDate(value){
if(!value) return ""
const date = new Date(value)
if(Number.isNaN(date.getTime())){
return String(value)
}
return date.toLocaleDateString("de-DE")
}
function formatSubdomainName(subdomain, domainName){
if(!subdomain) return domainName || ""
if(!domainName) return subdomain
return subdomain.endsWith("." + domainName) || subdomain === domainName
? subdomain
: subdomain + "." + domainName
}
window.openCreate = function(){
document.getElementById("modalTitle").innerText = "Create Resource"
document.getElementById("resource_id").value = ""
document.querySelectorAll("#resourceModal input, #resourceModal textarea")
.forEach(e => e.value = "")
document.getElementById("status").value = "aktiv"
openModal("resourceModal")
}
window.openEdit = function(resource){
document.getElementById("modalTitle").innerText="Edit Resource"
document.getElementById("resource_id").value=resource.id
Object.keys(resource).forEach(k=>{
const el=document.getElementById(k)
if(el){
if(el.type === "date"){
el.value = toInputDate(resource[k])
}else{
el.value = resource[k] || ""
}
}
})
document.getElementById("resourceModal").style.display="block"
}
window.closeModal = function(){
closeModalById("resourceModal")
}
window.openServerDetail = async function(resource){
const mappings = await api(API+"/domainmap")
const subs = await api(API+"/subdomains")
let html = `
<b>Name:</b> ${resource.name}<br>
<b>Produkt:</b> ${resource.produkt || ""}<br>
<b>Provider:</b> ${resource.provider || ""}<br>
<b>Provider Name:</b> ${resource.providername || ""}
<br><br>
<b>System</b><br>
CPU: ${resource.cpu || ""}<br>
RAM: ${resource.ram || ""}<br>
Disk: ${resource.disk || ""}<br>
OS: ${resource.os || ""}
<br><br>
<b>IPs</b><br>
`
// IPs
if(Array.isArray(resource.ips)){
resource.ips.forEach(ip=>{
html += `<div class="ip">${ip.type || ""} ${ip.ip}</div>`
})
}
// Domains
html += `<br><b>Domains</b><br>`
const domains = mappings.filter(m => m.resource_id == resource.id)
domains.forEach(d=>{
html += `<div class="ip">🌐 ${d.domain_name}</div>`
// Subdomains dazu
subs.forEach(s => {
if(s.domain_name === d.domain_name){
html += `
<div class="subdomain-detail">
${formatSubdomainName(s.subdomain, s.domain_name)}
</div>
`
}
})
})
// Zusatzinfos
html += `
<br>
<b>Bestelldatum:</b> ${formatDisplayDate(resource.bestelldatum)}<br>
<b>Kündbar ab:</b> ${formatDisplayDate(resource.kuendbar_ab)}<br>
<b>Kündigungsdatum:</b> ${formatDisplayDate(resource.kuendigungsdatum)}
<br><br>
<b>Bemerkung</b><br>
${resource.bemerkung || ""}
`
document.getElementById("serverDetailContent").innerHTML = html
document.getElementById("serverDetailModal").style.display="block"
}
window.closeServerDetail = function(){
document.getElementById("serverDetailModal").style.display="none"
}
/* =========================
DOMAIN MODAL
========================= */
window.openDomainCreate = function(){
document.getElementById("domainModalTitle").innerText = "Create Domain"
document.getElementById("domainModalMeta").innerText = "Neue Domain anlegen"
document.getElementById("domain_id").value = ""
document.getElementById("domain_name").value = ""
document.getElementById("domain_provider").value = ""
document.getElementById("domain_ip").value = ""
document.getElementById("domain_cost").value = ""
document.getElementById("domain_notes").value = ""
document.getElementById("domainSaveBtn").innerText = "Create"
openModal("domainModal")
document.getElementById("domain_name").focus()
}
window.openDomainEdit = function(d){
document.getElementById("domainModalTitle").innerText="Edit Domain"
document.getElementById("domainModalMeta").innerText=d.domain_name || ""
document.getElementById("domain_id").value=d.id
document.getElementById("domain_name").value=d.domain_name || ""
document.getElementById("domain_provider").value=d.provider || ""
document.getElementById("domain_ip").value=d.ip_address || ""
document.getElementById("domain_cost").value=d.yearly_cost || ""
document.getElementById("domain_notes").value=d.notes || ""
document.getElementById("domainSaveBtn").innerText = "Update"
document.getElementById("domainModal").style.display="block"
document.getElementById("domain_name").focus()
}
window.closeDomainModal = function(){
closeModalById("domainModal")
}
/* =========================
SUBDOMAIN MODAL
========================= */
window.openSubCreate = function(domainId){
const domains = window.domainList || []
const domain = domains.find(d => d.id == domainId)
const domainName = domain?.domain_name || "Unbekannte Domain"
document.getElementById("sub_id").value = "" // RESET !!
document.getElementById("sub_domain_id").value = domainId
document.getElementById("sub_name").value = ""
document.getElementById("sub_ip").value = domain?.ip_address || ""
document.querySelector("#subdomainModal h3").innerText = "Subdomain erstellen"
document.getElementById("subdomainModalMeta").innerText = "Fuer " + domainName
document.getElementById("subdomainSaveBtn").innerText = "Create"
document.getElementById("subdomainModal").style.display="block"
document.getElementById("sub_name").focus()
}
window.closeSubModal = function(){
closeModalById("subdomainModal")
}
window.openSubEdit = function(s){
const fullName = s.subdomain + "." + s.domain_name
document.getElementById("sub_id").value = s.id
document.getElementById("sub_domain_id").value = s.domain_id
document.getElementById("sub_name").value = s.subdomain
document.getElementById("sub_ip").value = s.ip_address
document.querySelector("#subdomainModal h3").innerText = "Subdomain bearbeiten"
document.getElementById("subdomainModalMeta").innerText = fullName
document.getElementById("subdomainSaveBtn").innerText = "Update"
document.getElementById("subdomainModal").style.display="block"
document.getElementById("sub_name").focus()
}
/* =========================
IP MODAL
========================= */
window.openIPManager = function(resourceId){
document.getElementById("ip_resource_id").value = resourceId
openModal("ipModal")
loadIPs(resourceId)
}
window.closeIPModal = function(){
closeModalById("ipModal")
}
/*========================
Neu Global Modal
=========================*/
window.openModal = function(id){
document.getElementById(id).style.display = "block"
}
window.closeModalById = function(id){
const el = document.getElementById(id)
if(el){
el.style.display = "none"
}
}
document.addEventListener("keydown", function(e){
if(e.key === "Escape"){
document.querySelectorAll(".modal").forEach(m => {
m.style.display = "none"
})
}
})
window.addEventListener("click", function(e){
document.querySelectorAll(".modal").forEach(m => {
if(e.target === m){
m.style.display = "none"
}
})
})