templates/bundles/EasyAdminBundle/default/includes/_body_javascript.html.twig line 1

Open in your IDE?
  1. {% block body_javascript %}
  2.     <script type="text/javascript">
  3.         $(function() {
  4.             const toggles = document.querySelectorAll('.checkbox-switch input[type="checkbox"]');
  5.             for (i = 0; i < toggles.length; i++) {
  6.                 toggles[i].addEventListener('change', function () {
  7.                     const toggle = this;
  8.                     const newValue = this.checked;
  9.                     const oldValue = !newValue;
  10.                     const propertyName = this.closest('.checkbox-switch').dataset.propertyname;
  11.                     const toggleUrl = "{{ path('easyadmin', { action: 'edit', entity: _entity_config.name, view: 'list' })|raw }}"
  12.                         + "&id=" + this.closest('tr').dataset.id
  13.                         + "&property=" + propertyName
  14.                         + "&newValue=" + newValue.toString();
  15.                     let toggleRequest = $.ajax({ type: "GET", url: toggleUrl, data: {} });
  16.                     toggleRequest.done(function(result) {});
  17.                     toggleRequest.fail(function() {
  18.                         // in case of error, restore the original value and disable the toggle
  19.                         toggle.checked = oldValue;
  20.                         toggle.disabled = true;
  21.                         toggle.closest('.checkbox-switch').classList.add('disabled');
  22.                     });
  23.                 });
  24.             }
  25.             $('.action-delete').on('click', function(e) {
  26.                 e.preventDefault();
  27.                 const id = $(this).parents('tr').first().data('id');
  28.                 $('#modal-delete').modal({ backdrop: true, keyboard: true })
  29.                     .off('click', '#modal-delete-button')
  30.                     .on('click', '#modal-delete-button', function () {
  31.                         let deleteForm = $('#delete-form');
  32.                         deleteForm.attr('action', deleteForm.attr('action').replace('__id__', id));
  33.                         deleteForm.trigger('submit');
  34.                     });
  35.             });
  36.             {% if _has_filters %}
  37.             // HTML5 specifies that a <script> tag inserted with innerHTML should not execute
  38.             // https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML#Security_considerations
  39.             // That's why we can't use just 'innerHTML'. See https://stackoverflow.com/a/47614491/2804294
  40.             var setInnerHTML = function(element, htmlContent) {
  41.                 element.innerHTML = htmlContent;
  42.                 Array.from(element.querySelectorAll('script')).forEach( oldScript => {
  43.                     const newScript = document.createElement('script');
  44.                     Array.from(oldScript.attributes)
  45.                         .forEach(attr => newScript.setAttribute(attr.name, attr.value));
  46.                     newScript.appendChild(document.createTextNode(oldScript.innerHTML));
  47.                     oldScript.parentNode.replaceChild(newScript, oldScript);
  48.                 });
  49.             };
  50.             document.querySelector('.action-filters-button').addEventListener('click', function(event) {
  51.                 let filterButton = event.currentTarget;
  52.                 let filterModal = document.querySelector(filterButton.dataset.modal);
  53.                 let filterModalBody = filterModal.querySelector('.modal-body');
  54.                 $(filterModal).modal({ backdrop: true, keyboard: true });
  55.                 filterModalBody.innerHTML = '<div class="fa-3x px-3 py-3 text-muted text-center"><i class="fas fa-circle-notch fa-spin"></i></div>';
  56.                 $.get(filterButton.getAttribute('href'), function (response) {
  57.                     setInnerHTML(filterModalBody, response);
  58.                 });
  59.                 event.preventDefault();
  60.                 event.stopPropagation();
  61.             });
  62.             {% endif %}
  63.             {% if _has_batch_actions %}
  64.             $(document).ready(function () {
  65.                 const $content = $('.panel');
  66.                 let $input = $content.find(':hidden#batch_form_ids');
  67.                 let ids = $input.val() ? $input.val().split(',') : [];
  68.                 showBatchAcions(ids, $content);
  69.             });
  70.             //const titleContent = $('.panel-heading > .panel-title').html();
  71.             $(document).on('click', '.deselect-batch-button', function () {
  72.                 $(this).closest('.panel').find(':checkbox.form-batch-checkbox-all').prop('checked', false).trigger('change');
  73.             });
  74.             $(document).on('change', '.form-batch-checkbox-all', function () {
  75.                 $(this).closest('.panel').find(':checkbox.form-batch-checkbox').prop('checked', $(this).prop('checked')).trigger('change');
  76.             });
  77.             $(document).on('change', '.form-batch-checkbox', function () {
  78.                 const $content = $(this).closest('.panel');
  79.                 let $input = $content.find(':hidden#batch_form_ids');
  80.                 let ids = $input.val() ? $input.val().split(',') : [];
  81.                 const id = $(this).val();
  82.                 if ($(this).prop('checked')) {
  83.                     if (-1 === ids.indexOf(id)) {
  84.                         ids.push(id);
  85.                     }
  86.                 } else {
  87.                     ids = ids.filter(function(value) { return value !== id });
  88.                     $content.find(':checkbox.form-batch-checkbox-all').prop('checked', false);
  89.                 }
  90.                 showBatchAcions(ids, $content);
  91.                 $input.val(ids.join(','));
  92.                 //$content.find('.panel-heading > .panel-title').html(0 == ids.length ? titleContent : ' ');
  93.             });
  94.             let showBatchAcions = function (ids, $content) {
  95.                 if (0 === ids.length) {
  96.                     $content.find('.panel-control').show();
  97.                     $content.find('.batch-actions').hide();
  98.                     $content.find('table').removeClass('table-batch');
  99.                 } else {
  100.                     $content.find('.batch-actions').show();
  101.                     $content.find('.panel-control').hide();
  102.                     $content.find('table').addClass('table-batch');
  103.                 }
  104.             };
  105.             $('button[name="batch_form[name]"][value="captureMail"]').on('click', function (event) {
  106.                 event.preventDefault();
  107.                 event.stopPropagation();
  108.                 const $content = $('.panel');
  109.                 let $input = $content.find(':hidden#batch_form_ids');
  110.                 let ids = $input.val() ? $input.val().split(',') : [];
  111.                 let emails = [];
  112.                 for (const idsKey in ids)
  113.                 {
  114.                     emails.push($('table tbody tr[data-id=' + ids[idsKey] + ']').find('td.string:eq(1) > span').attr('title'));
  115.                 }
  116.                 $('.toast').text('(' + ids.length + ') Correos Copiados').toast('show');
  117.                 copyTextToClipboard(emails.join(' '));
  118.                 $('.deselect-batch-button').click();
  119.             });
  120.             let modalTitle = $('#batch-action-confirmation-title');
  121.             const titleContentWithPlaceholders = modalTitle.text();
  122.             $('button[name="batch_form[name]"].batch-action-requires-confirmation').on('click', function (event) {
  123.                 event.preventDefault();
  124.                 event.stopPropagation();
  125.                 let $button = $(this);
  126.                 const actionName = $button.text();
  127.                 const numberOfSelectedItems = $('input[type="checkbox"].form-batch-checkbox:checked').length;
  128.                 modalTitle.text(titleContentWithPlaceholders
  129.                     .replace('%action_name%', actionName)
  130.                     .replace('%num_items%', numberOfSelectedItems));
  131.                 $('#modal-batch-action').modal({ backdrop : true, keyboard : true })
  132.                     .off('click', '#modal-batch-action-button')
  133.                     .on('click', '#modal-batch-action-button', function () {
  134.                         //let body_mail = $('textarea[name="batch_form[body_mail]"]').clone().hide();
  135.                         //$('form[name="batch_form"]').append(body_mail);
  136.                         $button.unbind('click');
  137.                         $button.trigger('click');
  138.                         modalTitle.text(titleContentWithPlaceholders);
  139.                     });
  140.             });
  141.             {% endif %}
  142.         });
  143.     </script>
  144.     {% if 'search' == app.request.get('action') %}
  145.         <script type="text/javascript">
  146.             const _search_query = "{{ app.request.get('query')|default('')|e('js') }}";
  147.             // the original query is prepended to allow matching exact phrases in addition to single words
  148.             $('#main').find('table tbody td:not(.actions)').highlight($.merge([_search_query], _search_query.split(' ')));
  149.         </script>
  150.     {% endif %}
  151. {% endblock %}