assets/js/wizard.min.js
(()=>{const e=document.getElementById("wizard-form"),t=document.getElementById("wizard-sites"),n=document.getElementById("wizard-site-template"),r=document.getElementById("wizard-message");if(!e)return;const i={installing:r?.dataset.installing??"Installing Brivacia...",installed:r?.dataset.installed??"Brivacia installed.",error:r?.dataset.error??"Unable to install Brivacia.",networkError:r?.dataset.networkError??"Network error."};function o(e,t=""){r&&(r.className=t,r.textContent=e||"")}function a(){return t?.querySelector(":scope > div")||null}function s(e){const t=a(),n=e.closest("fieldset");!t||!n||t.children.length<=1||(n.remove(),a()?.querySelectorAll(":scope > fieldset").forEach((e,t)=>{const n=e.querySelector('input[name$="[code]"]'),r=e.querySelector('input[name$="[domain]"]');n&&(n.name=`sites[${t}][code]`),r&&(r.name=`sites[${t}][domain]`)}))}function l(){const t=new FormData(e),n={};for(const[e,r]of t.entries())e.startsWith("sites[")||(n[e]=r);return e.querySelectorAll('input[type="checkbox"]').forEach(e=>{n[e.name]=e.checked}),n.sites=[],a()?.querySelectorAll(":scope > fieldset").forEach(e=>{const t=e.querySelector('input[name$="[code]"]')?.value.trim()||"",r=e.querySelector('input[name$="[domain]"]')?.value.trim()||"";""===t&&""===r||n.sites.push({code:t,domain:r})}),n}e.addEventListener("click",e=>{const t=e.target.closest("button");t&&("add-site"!==t.value?"remove-site"===t.value&&s(t):function(){const e=a();if(!e||!n)return;const t=document.createElement("div");t.innerHTML=n.innerHTML.replaceAll("__INDEX__",String(e.children.length)).trim(),t.firstElementChild&&e.appendChild(t.firstElementChild)}())}),e.addEventListener("submit",async e=>{e.preventDefault(),o(i.installing,"warning");try{const e=await fetch("/api/settings.php?action=install",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(l())}),t=await e.json();if(!e.ok||!t.ok)return void o(t.error?`${i.error} ${t.error}`:i.error,"danger");o(i.installed,"success"),setTimeout(()=>{document.getElementById("wizard-modal")?.close(),sessionStorage.setItem("brivacia.showPixelModal","1"),window.location.reload()},700)}catch{o(i.networkError,"danger")}})})();