diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7c439ae --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2024 Ferdinand Kuhl + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/form-stamp.js b/form-stamp.js new file mode 100644 index 0000000..592e1c4 --- /dev/null +++ b/form-stamp.js @@ -0,0 +1,44 @@ +/* jshint esversion: 6 */ +const defaults = { + nameToReplace: 'idx', + removeSelector: '.remove' +} + +export function formStamp(element, options = {}) +{ + const { + nameToReplace, + removeSelector + } = { ...defaults, ...element.dataset, ...options }; + const container = element.parentElement; + const template = element.querySelector('template'); + const itemSelector = '.' + template.content.firstElementChild.className.replace(/ /g, '.'); + const initialItemCount = container.querySelectorAll(itemSelector).length; + let created = 0; + const removeRow = (element) => () => { + const rowToDelete = element.closest(itemSelector); + // 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()) + } + rowToDelete.remove(); + } + + element.addEventListener('click', () => { + const clone = template.content.cloneNode(true); + clone.querySelectorAll('input, select, textarea').forEach(child => { + child.name = child.name.replace(nameToReplace, initialItemCount + created); + }); + clone.querySelectorAll(removeSelector).forEach(child => { + child.onclick = removeRow(child); + }); + element.before(clone); + created++; + }); + container.querySelectorAll(removeSelector).forEach(child => { + child.onclick = removeRow(child); + }); +} +export default formStamp; diff --git a/package.json b/package.json new file mode 100644 index 0000000..a1fb509 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "@digicomp/form-stamp", + "version": "0.0.1", + "description": "duplicates an entry form field group", + "main": "form-stamp.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git@digital-competence.de:npm/form-stamp.git" + }, + "keywords": [ + "form" + ], + "author": "Ferdinand Kuhl", + "license": "MIT", + "dependencies": { + }, + "engines": { + + } +}