order_gizi/public/js/menu/action.js
2025-07-17 16:31:35 +07:00

68 lines
1.9 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
const formSetsContainer = document.getElementById('form-sets-container');
const btnTambahForm = document.getElementById('btnTambahForm');
// Auto-expand untuk textarea
function autoExpand(textarea) {
textarea.addEventListener('input', function () {
this.style.height = 'auto';
this.style.height = this.scrollHeight + 'px';
});
}
// Inisialisasi Tagify
function initTagifyAll() {
document.querySelectorAll('input.tags-input').forEach(input => {
if (!input._tagify) {
new Tagify(input);
}
});
}
// Inisialisasi awal
document.querySelectorAll('.auto-expand').forEach(autoExpand);
initTagifyAll();
// Tambah form-set
btnTambahForm.addEventListener('click', function () {
const lastFormSet = formSetsContainer.querySelector('.form-set:last-of-type');
const clone = lastFormSet.cloneNode(true);
// Bersihkan field
clone.querySelectorAll('input, textarea, select').forEach(el => {
if (el.type === 'file') {
el.value = null;
} else {
el.value = '';
}
if (el._tagify) el._tagify.destroy();
});
clone.querySelectorAll('.tagify').forEach(el => el.remove());
// Show tombol hapus
clone.querySelector('.btn-remove-form').classList.remove('d-none');
// Tambah ke container
formSetsContainer.appendChild(clone);
// Inisialisasi ulang
clone.querySelectorAll('.auto-expand').forEach(autoExpand);
initTagifyAll();
});
// Hapus form-set
formSetsContainer.addEventListener('click', function (e) {
if (e.target.closest('.btn-remove-form')) {
const formSets = formSetsContainer.querySelectorAll('.form-set');
if (formSets.length > 1) {
e.target.closest('.form-set').remove();
} else {
alert('Minimal satu form harus ada.');
}
}
});
});