2024-06-05 02:27:56 +02:00
|
|
|
/* jshint esversion: 6 */
|
|
|
|
const defaults = {
|
|
|
|
nameToReplace: 'idx',
|
2024-11-08 16:10:25 +01:00
|
|
|
removeSelector: '.remove',
|
|
|
|
minimalId: 'auto'
|
2024-06-05 02:27:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function formStamp(element, options = {})
|
|
|
|
{
|
|
|
|
const {
|
|
|
|
nameToReplace,
|
2024-11-08 16:10:25 +01:00
|
|
|
removeSelector,
|
|
|
|
minimalId
|
2024-06-05 02:27:56 +02:00
|
|
|
} = { ...defaults, ...element.dataset, ...options };
|
|
|
|
const container = element.parentElement;
|
|
|
|
const template = element.querySelector('template');
|
|
|
|
const itemSelector = '.' + template.content.firstElementChild.className.replace(/ /g, '.');
|
2024-11-08 16:10:25 +01:00
|
|
|
const startWithId = minimalId === 'auto' ? container.querySelectorAll(itemSelector).length : parseInt(minimalId);
|
2024-06-05 02:27:56 +02:00
|
|
|
let created = 0;
|
2024-10-16 23:12:11 +02:00
|
|
|
const afterDomManipulation = () => {
|
|
|
|
element.dispatchEvent(new CustomEvent('afterStampDomManipulation'));
|
|
|
|
}
|
2024-10-09 12:23:38 +02:00
|
|
|
const removeRow = (removeItem) => () => {
|
|
|
|
const rowToDelete = removeItem.closest(itemSelector);
|
2024-06-05 02:27:56 +02:00
|
|
|
// Flow renders identity fields directly at the form tag, so we need to look
|
|
|
|
if (rowToDelete.dataset.stampRemoveWithMe) {
|
|
|
|
container.closest('form')
|
|
|
|
.querySelectorAll(rowToDelete.dataset.stampRemoveWithMe)
|
|
|
|
.forEach(child => child.remove())
|
|
|
|
}
|
2024-10-09 12:23:38 +02:00
|
|
|
if (element.dispatchEvent(new CustomEvent(
|
|
|
|
'beforeStampRemoval',
|
|
|
|
{
|
|
|
|
cancelable: true,
|
|
|
|
detail: {
|
|
|
|
toBeRemoved: rowToDelete
|
|
|
|
}
|
|
|
|
}
|
|
|
|
))) {
|
|
|
|
rowToDelete.remove();
|
2024-10-16 23:12:11 +02:00
|
|
|
afterDomManipulation();
|
2024-10-09 12:23:38 +02:00
|
|
|
}
|
2024-06-05 02:27:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
element.addEventListener('click', () => {
|
|
|
|
const clone = template.content.cloneNode(true);
|
|
|
|
clone.querySelectorAll('input, select, textarea').forEach(child => {
|
2024-11-08 16:10:25 +01:00
|
|
|
child.name = child.name.replace(nameToReplace, startWithId + created);
|
2024-06-05 02:27:56 +02:00
|
|
|
});
|
|
|
|
clone.querySelectorAll(removeSelector).forEach(child => {
|
|
|
|
child.onclick = removeRow(child);
|
|
|
|
});
|
2024-09-28 17:21:03 +02:00
|
|
|
if (element.dispatchEvent(new CustomEvent(
|
|
|
|
'beforeStampInsert',
|
|
|
|
{
|
|
|
|
detail: {
|
2024-11-08 16:10:25 +01:00
|
|
|
i: startWithId + created,
|
2024-09-28 17:21:03 +02:00
|
|
|
node: clone
|
|
|
|
},
|
|
|
|
cancelable: true
|
|
|
|
}
|
|
|
|
))) {
|
|
|
|
element.before(clone);
|
2024-10-16 23:12:11 +02:00
|
|
|
afterDomManipulation();
|
2024-09-28 17:21:03 +02:00
|
|
|
created++;
|
|
|
|
}
|
2024-06-05 02:27:56 +02:00
|
|
|
});
|
|
|
|
container.querySelectorAll(removeSelector).forEach(child => {
|
|
|
|
child.onclick = removeRow(child);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
export default formStamp;
|