templates/bundles/SyliusAdminBundle/Script/_confirmation_modals.html.twig line 1

Open in your IDE?
  1. <script>
  2. /**
  3.  * Global confirmation modal handler
  4.  */
  5. (function() {
  6.     'use strict';
  7.     // Initialize confirmation modals when DOM is ready
  8.     document.addEventListener('DOMContentLoaded', function() {
  9.         initializeConfirmationModals();
  10.     });
  11.     function initializeConfirmationModals() {
  12.         // Handle buttons with data-confirm attribute
  13.         document.querySelectorAll('[data-confirm]').forEach(function(button) {
  14.             button.addEventListener('click', function(e) {
  15.                 e.preventDefault();
  16.                 showConfirmationModal(
  17.                     this.getAttribute('data-confirm') || 'Are you sure?',
  18.                     function() {
  19.                         // If it's a form submit
  20.                         if (button.type === 'submit' && button.closest('form')) {
  21.                             button.closest('form').submit();
  22.                         }
  23.                         // If it's a link
  24.                         else if (button.tagName === 'A' && button.href) {
  25.                             window.location.href = button.href;
  26.                         }
  27.                         // If it's a button with form action
  28.                         else if (button.closest('form')) {
  29.                             button.closest('form').submit();
  30.                         }
  31.                     }
  32.                 );
  33.             });
  34.         });
  35.     }
  36.     function showConfirmationModal(message, onConfirm) {
  37.         // Create modal HTML
  38.         const modalHtml = `
  39.             <div class="ui small basic modal confirmation-modal">
  40.                 <div class="ui icon header">
  41.                     <i class="warning sign icon"></i>
  42.                     {{ 'sylius.ui.confirm_your_action'|trans }}
  43.                 </div>
  44.                 <div class="content">
  45.                     <p>${message}</p>
  46.                 </div>
  47.                 <div class="actions">
  48.                     <div class="ui red basic cancel inverted button">
  49.                         <i class="remove icon"></i>
  50.                         {{ 'sylius.ui.no_label'|trans }}
  51.                     </div>
  52.                     <div class="ui green ok inverted button confirm-action-btn">
  53.                         <i class="checkmark icon"></i>
  54.                         {{ 'sylius.ui.yes_label'|trans }}
  55.                     </div>
  56.                 </div>
  57.             </div>
  58.         `;
  59.         // Add modal to body
  60.         document.body.insertAdjacentHTML('beforeend', modalHtml);
  61.         const modal = document.querySelector('.confirmation-modal');
  62.         // Handle confirmation
  63.         modal.querySelector('.confirm-action-btn').addEventListener('click', function() {
  64.             onConfirm();
  65.             $(modal).modal('hide');
  66.             // Remove modal from DOM after hiding
  67.             setTimeout(function() {
  68.                 modal.remove();
  69.             }, 300);
  70.         });
  71.         // Show modal
  72.         $(modal).modal('show');
  73.         // Remove modal from DOM when hidden
  74.         $(modal).on('hidden.bs.modal', function() {
  75.             modal.remove();
  76.         });
  77.     }
  78.     // Make functions globally available
  79.     window.ConfirmationModals = {
  80.         show: showConfirmationModal,
  81.         init: initializeConfirmationModals
  82.     };
  83. })();
  84. </script>