You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. $(document).ready(function() {
  2. lcCrudIndexToggle() ;
  3. lcCrudIndexBatchActions() ;
  4. }) ;
  5. function lcCrudIndexToggle() {
  6. const toggles = document.querySelectorAll('.custom-switch input[type="checkbox"]');
  7. for (i = 0; i < toggles.length; i++) {
  8. toggles[i].addEventListener('change', function () {
  9. const toggle = this;
  10. const newValue = this.checked;
  11. const oldValue = !newValue;
  12. const toggleUrl = this.closest('.custom-switch').dataset.url + "&newValue=" + newValue.toString();
  13. let toggleRequest = $.ajax({type: "POST", url: toggleUrl, data: {}, dataType: 'json'});
  14. toggleRequest.done(function (response) {
  15. Notification.add('success', 'La propriété a bien été mise à jour.');
  16. });
  17. toggleRequest.fail(function () {
  18. toggle.checked = oldValue;
  19. toggle.disabled = true;
  20. toggle.closest('.checkbox-switch').classList.add('disabled');
  21. Notification.add('error', 'Une erreur est survenue.');
  22. });
  23. });
  24. }
  25. }
  26. function lcCrudIndexBatchActions() {
  27. const titleContent = $('.content-header-title > .title').html();
  28. $(document).on('click', '.deselect-batch-button', function () {
  29. $(this).closest('.content').find(':checkbox.form-batch-checkbox-all').prop('checked', false).trigger('change');
  30. });
  31. $(document).on('change', '.form-batch-checkbox-all', function () {
  32. $(this).closest('.content').find(':checkbox.form-batch-checkbox').prop('checked', $(this).prop('checked')).trigger('change');
  33. });
  34. $(document).on('change', '.form-batch-checkbox', function () {
  35. const $content = $(this).closest('.content-wrapper');
  36. let $input = $content.find(':hidden#batch_form_entityIds');
  37. let ids = $input.val() ? $input.val().split(',') : [];
  38. const id = $(this).val();
  39. if ($(this).prop('checked')) {
  40. $(this).closest('tr').addClass('selected-row');
  41. if (-1 === ids.indexOf(id)) {
  42. ids.push(id);
  43. }
  44. } else {
  45. $(this).closest('tr').removeClass('selected-row');
  46. ids = ids.filter(function (value) {
  47. return value !== id
  48. });
  49. $content.find(':checkbox.form-batch-checkbox-all').prop('checked', false);
  50. }
  51. if (0 === ids.length) {
  52. $content.find('.global-actions').show();
  53. $content.find('.batch-actions').hide();
  54. $content.find('table').removeClass('table-batch');
  55. } else {
  56. $content.find('.batch-actions').show();
  57. $content.find('.global-actions').hide();
  58. $content.find('table').addClass('table-batch');
  59. }
  60. $input.val(ids.join(','));
  61. $content.find('.content-header-title > .title').html(0 === ids.length ? titleContent : '');
  62. });
  63. let modalTitle = $('#batch-action-confirmation-title');
  64. const titleContentWithPlaceholders = modalTitle.text();
  65. $('[data-action-batch]').on('click', function (event) {
  66. event.preventDefault();
  67. event.stopPropagation();
  68. let $actionElement = $(this);
  69. const actionName = $actionElement.attr('data-action-name');
  70. const selectedItems = $('input[type="checkbox"].form-batch-checkbox:checked');
  71. modalTitle.text(titleContentWithPlaceholders
  72. .replace('%action_name%', actionName)
  73. .replace('%num_items%', selectedItems.length));
  74. $('#modal-batch-action').modal({backdrop: true, keyboard: true})
  75. .off('click', '#modal-batch-action-button')
  76. .on('click', '#modal-batch-action-button', function () {
  77. $actionElement.unbind('click');
  78. $form = document.createElement('form');
  79. $form.setAttribute('action', $actionElement.attr('data-action-url'));
  80. $form.setAttribute('method', 'POST');
  81. $actionNameInput = document.createElement('input');
  82. $actionNameInput.setAttribute('type', 'hidden');
  83. $actionNameInput.setAttribute('name', 'batchActionName');
  84. $actionNameInput.setAttribute('value', $actionElement.attr('data-action-name'));
  85. $form.appendChild($actionNameInput);
  86. $entityFqcnInput = document.createElement('input');
  87. $entityFqcnInput.setAttribute('type', 'hidden');
  88. $entityFqcnInput.setAttribute('name', 'entityFqcn');
  89. $entityFqcnInput.setAttribute('value', $actionElement.attr('data-entity-fqcn'));
  90. $form.appendChild($entityFqcnInput);
  91. $actionUrlInput = document.createElement('input');
  92. $actionUrlInput.setAttribute('type', 'hidden');
  93. $actionUrlInput.setAttribute('name', 'batchActionUrl');
  94. $actionUrlInput.setAttribute('value', $actionElement.attr('data-action-url'));
  95. $form.appendChild($actionUrlInput);
  96. $csrfTokenInput = document.createElement('input');
  97. $csrfTokenInput.setAttribute('type', 'hidden');
  98. $csrfTokenInput.setAttribute('name', 'batchActionCsrfToken');
  99. $csrfTokenInput.setAttribute('value', $actionElement.attr('data-action-csrf-token'));
  100. $form.appendChild($csrfTokenInput);
  101. selectedItems.each((i, item) => {
  102. $entityIdInput = document.createElement('input');
  103. $entityIdInput.setAttribute('type', 'hidden');
  104. $entityIdInput.setAttribute('name', `batchActionEntityIds[${i}]`);
  105. $entityIdInput.setAttribute('value', item.value);
  106. $form.appendChild($entityIdInput);
  107. });
  108. document.body.appendChild($form);
  109. //modalTitle.text(titleContentWithPlaceholders);
  110. $form.submit();
  111. });
  112. });
  113. }