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.3KB

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