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.

150 lines
6.5KB

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