Files
wp-plugin-contactform/assets/admin.js
T

18 lines
3.1 KiB
JavaScript
Raw Normal View History

(() => {
const app=document.querySelector('.hgcf-admin');if(!app)return;
const panels=[...app.querySelectorAll('.hgcf-admin-panel')];const tabs=[...app.querySelectorAll('.hgcf-admin-tabs a')];
function selectTab(id){if(!panels.some(p=>p.id===id))id='hgcf-general';panels.forEach(p=>p.hidden=p.id!==id);tabs.forEach(t=>{const active=t.hash==='#'+id;t.classList.toggle('is-active',active);if(active)t.setAttribute('aria-current','page');else t.removeAttribute('aria-current');});}
tabs.forEach(tab=>tab.addEventListener('click',()=>selectTab(tab.hash.slice(1))));selectTab(location.hash.slice(1));window.addEventListener('hashchange',()=>selectTab(location.hash.slice(1)));
let sequence=0;const unique=prefix=>prefix+Date.now().toString(36)+(++sequence).toString(36);
function initCard(card){
const update=()=>{const state=key=>card.querySelector(`[data-hgcf-state="${key}"]`).value;card.querySelector('.hgcf-flow-services').hidden=state('service')==='hidden';card.querySelector('.hgcf-flow-times').hidden=state('time')==='hidden';const enabled=card.querySelector('.hgcf-workflow-enabled').checked;card.querySelector('.hgcf-workflow-badge').textContent=enabled?'Actief':'Uitgeschakeld';};
card.querySelectorAll('[data-hgcf-state],.hgcf-workflow-enabled').forEach(input=>input.addEventListener('change',update));
const name=card.querySelector('input[name*="[label]"]');name.addEventListener('input',()=>card.querySelector('.hgcf-workflow-name').textContent=name.value||'Workflow');
card.querySelector('.hgcf-remove-workflow').addEventListener('click',()=>{if(confirm('Deze workflow verwijderen? De wijziging wordt pas actief na opslaan.'))card.remove();});
const initRow=row=>row.querySelector('.hgcf-remove-service').addEventListener('click',()=>row.remove());card.querySelectorAll('.hgcf-service-row').forEach(initRow);
card.querySelector('.hgcf-add-service').addEventListener('click',()=>{const list=card.querySelector('.hgcf-service-list');if(list.children.length>=50){alert('Maximaal 50 keuzemogelijkheden per workflow.');return;}const index=Number(card.dataset.nextService);card.dataset.nextService=index+1;const template=card.querySelector('.hgcf-service-template');const wrapper=document.createElement('div');wrapper.innerHTML=template.innerHTML.replaceAll('__SERVICE__',String(index)).replaceAll('__SERVICE_ID__',unique('service_'));const row=wrapper.firstElementChild;list.append(row);initRow(row);row.querySelector('input:not([type="hidden"])').focus();});
update();
}
const list=document.getElementById('hgcf-workflow-list');list.querySelectorAll('.hgcf-workflow').forEach(initCard);
document.getElementById('hgcf-add-workflow').addEventListener('click',()=>{if(list.children.length>=20){alert('Maximaal 20 workflows.');return;}const index=Number(list.dataset.nextIndex);list.dataset.nextIndex=index+1;const wrapper=document.createElement('div');wrapper.innerHTML=document.getElementById('hgcf-workflow-template').innerHTML.replaceAll('__INDEX__',String(index)).replaceAll('__WORKFLOW_ID__',unique('workflow_'));const card=wrapper.firstElementChild;list.append(card);initCard(card);card.querySelector('input[name*="[label]"]').focus();});
})();