38 lines
1.4 KiB
JavaScript
38 lines
1.4 KiB
JavaScript
(function () {
|
|
function nextIndex(container) {
|
|
return container.querySelectorAll('[data-row]').length;
|
|
}
|
|
|
|
function addRow(target) {
|
|
const container = document.getElementById(target === 'actions' ? 'sodino-actions' : 'sodino-conditions');
|
|
const template = document.getElementById(target === 'actions' ? 'sodino-action-template' : 'sodino-condition-template');
|
|
if (!container || !template) {
|
|
return;
|
|
}
|
|
|
|
const index = nextIndex(container);
|
|
const wrapper = document.createElement('div');
|
|
wrapper.innerHTML = template.innerHTML.replaceAll('__INDEX__', String(index)).trim();
|
|
container.appendChild(wrapper.firstElementChild);
|
|
}
|
|
|
|
document.addEventListener('click', function (event) {
|
|
const addButton = event.target.closest('.sodino-add-row');
|
|
if (addButton) {
|
|
event.preventDefault();
|
|
addRow(addButton.dataset.target);
|
|
return;
|
|
}
|
|
|
|
const removeButton = event.target.closest('.sodino-remove-row');
|
|
if (removeButton) {
|
|
event.preventDefault();
|
|
const row = removeButton.closest('[data-row]');
|
|
const container = row ? row.parentElement : null;
|
|
if (container && container.querySelectorAll('[data-row]').length > 1) {
|
|
row.remove();
|
|
}
|
|
}
|
|
});
|
|
})();
|