From ff179360ccc967615ad178d91f81022d645f8e0f Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 5 Nov 2023 01:27:12 +0100 Subject: [PATCH] Added confirmation dialog to delete forms --- resources/assets/js/forms.js | 65 +++++++++++++++++++ resources/lang/de_DE/default.po | 3 + resources/lang/en_US/default.po | 3 + resources/views/admin/locations/index.twig | 2 +- resources/views/layouts/app.twig | 2 +- resources/views/macros/form.twig | 14 ++++ resources/views/pages/design.twig | 18 +++-- resources/views/pages/news/news.twig | 2 +- resources/views/pages/questions/overview.twig | 2 +- resources/views/pages/settings/sessions.twig | 5 +- 10 files changed, 102 insertions(+), 14 deletions(-) diff --git a/resources/assets/js/forms.js b/resources/assets/js/forms.js index 3acfd3c6..e98dddd6 100644 --- a/resources/assets/js/forms.js +++ b/resources/assets/js/forms.js @@ -275,6 +275,71 @@ ready(() => { document.querySelectorAll('.modal').forEach((element) => new bootstrap.Modal(element)); }); +/** + * Show confirmation modal before submitting form + * + * Uses the buttons data attributes to show in the modal: + * - data-confirm_title: Optional title of the modal + * - data-confirm_submit: Body of the modal + * + * The class, title and content of the requesting button gets copied for confirmation + * + */ +ready(() => { + document.querySelectorAll('[data-confirm_submit_title], [data-confirm_submit_text]').forEach((element) => { + let modalOpen = false; + let oldType = element.type; + if (element.type !== 'submit') { + return; + } + + element.type = 'button'; + element.addEventListener('click', (event) => { + if (modalOpen) { + return; + } + event.preventDefault(); + + document.getElementById('confirmation-modal')?.remove(); + document.body.insertAdjacentHTML( + 'beforeend', + ` + + ` + ); + + let modal = document.getElementById('confirmation-modal'); + modal.addEventListener('hide.bs.modal', () => { + modalOpen = false; + }); + modal.querySelector('[data-submit]').addEventListener('click', (event) => { + element.type = oldType; + element.click(); + }); + + modalOpen = true; + let bootstrapModal = new bootstrap.Modal(modal); + bootstrapModal.show(); + }); + }); +}); + /** * Show oauth buttons on welcome title click */ diff --git a/resources/lang/de_DE/default.po b/resources/lang/de_DE/default.po index 54113d24..6134dd18 100644 --- a/resources/lang/de_DE/default.po +++ b/resources/lang/de_DE/default.po @@ -1989,3 +1989,6 @@ msgstr "Was möchtest Du machen?" msgid "registration.register" msgstr "Registrieren" + +msgid "confirmation.delete" +msgstr "Möchtest du es wirklich löschen?" diff --git a/resources/lang/en_US/default.po b/resources/lang/en_US/default.po index 8213d122..3ec07cef 100644 --- a/resources/lang/en_US/default.po +++ b/resources/lang/en_US/default.po @@ -644,3 +644,6 @@ msgstr "Please enter a lastname in your settings!" msgid "mobile.required.hint" msgstr "Please enter a mobile number in your settings!" + +msgid "confirmation.delete" +msgstr "Do you really want to delete it?" diff --git a/resources/views/admin/locations/index.twig b/resources/views/admin/locations/index.twig index 2b7e937e..1df7cad4 100644 --- a/resources/views/admin/locations/index.twig +++ b/resources/views/admin/locations/index.twig @@ -57,7 +57,7 @@
{{ csrf() }} {{ f.hidden('id', location.id) }} - {{ f.button(m.icon('trash'), {'title': __('form.delete'), 'name': 'delete', 'type': 'submit', 'btn_type': 'danger', 'size': 'sm'}) }} + {{ f.delete(null, {'size': 'sm','confirm_title': location.name|e}) }}
diff --git a/resources/views/layouts/app.twig b/resources/views/layouts/app.twig index fa07f01e..b074b73d 100644 --- a/resources/views/layouts/app.twig +++ b/resources/views/layouts/app.twig @@ -23,7 +23,7 @@ {% endblock %} - + {% block body %} {% block header %} diff --git a/resources/views/macros/form.twig b/resources/views/macros/form.twig index f13ca267..f6a60deb 100644 --- a/resources/views/macros/form.twig +++ b/resources/views/macros/form.twig @@ -261,6 +261,8 @@ Renders a button. Must be a Bootstrap icon class without prefix, such as "info" or "check". @param {string} [opt.icon_right] - Optional icon to be added after the button label. Must be a Bootstrap icon class without prefix, such as "info" or "check". +@param {string} [opt.confirm_title] - Optional value for the confirmation title. +@param {string} [opt.confirm_text] - Optional value for the confirmation text. #} {% macro button(label, opt) %}