user.permissions.es6.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @file
  3. * User permission page behaviors.
  4. */
  5. (function ($, Drupal) {
  6. /**
  7. * Shows checked and disabled checkboxes for inherited permissions.
  8. *
  9. * @type {Drupal~behavior}
  10. *
  11. * @prop {Drupal~behaviorAttach} attach
  12. * Attaches functionality to the permissions table.
  13. */
  14. Drupal.behaviors.permissions = {
  15. attach(context) {
  16. const self = this;
  17. $('table#permissions').once('permissions').each(function () {
  18. // On a site with many roles and permissions, this behavior initially
  19. // has to perform thousands of DOM manipulations to inject checkboxes
  20. // and hide them. By detaching the table from the DOM, all operations
  21. // can be performed without triggering internal layout and re-rendering
  22. // processes in the browser.
  23. const $table = $(this);
  24. let $ancestor;
  25. let method;
  26. if ($table.prev().length) {
  27. $ancestor = $table.prev();
  28. method = 'after';
  29. }
  30. else {
  31. $ancestor = $table.parent();
  32. method = 'append';
  33. }
  34. $table.detach();
  35. // Create dummy checkboxes. We use dummy checkboxes instead of reusing
  36. // the existing checkboxes here because new checkboxes don't alter the
  37. // submitted form. If we'd automatically check existing checkboxes, the
  38. // permission table would be polluted with redundant entries. This
  39. // is deliberate, but desirable when we automatically check them.
  40. const $dummy = $('<input type="checkbox" class="dummy-checkbox js-dummy-checkbox" disabled="disabled" checked="checked" />')
  41. .attr('title', Drupal.t('This permission is inherited from the authenticated user role.'))
  42. .hide();
  43. $table
  44. .find('input[type="checkbox"]')
  45. .not('.js-rid-anonymous, .js-rid-authenticated')
  46. .addClass('real-checkbox js-real-checkbox')
  47. .after($dummy);
  48. // Initialize the authenticated user checkbox.
  49. $table.find('input[type=checkbox].js-rid-authenticated')
  50. .on('click.permissions', self.toggle)
  51. // .triggerHandler() cannot be used here, as it only affects the first
  52. // element.
  53. .each(self.toggle);
  54. // Re-insert the table into the DOM.
  55. $ancestor[method]($table);
  56. });
  57. },
  58. /**
  59. * Toggles all dummy checkboxes based on the checkboxes' state.
  60. *
  61. * If the "authenticated user" checkbox is checked, the checked and disabled
  62. * checkboxes are shown, the real checkboxes otherwise.
  63. */
  64. toggle() {
  65. const authCheckbox = this;
  66. const $row = $(this).closest('tr');
  67. // jQuery performs too many layout calculations for .hide() and .show(),
  68. // leading to a major page rendering lag on sites with many roles and
  69. // permissions. Therefore, we toggle visibility directly.
  70. $row.find('.js-real-checkbox').each(function () {
  71. this.style.display = (authCheckbox.checked ? 'none' : '');
  72. });
  73. $row.find('.js-dummy-checkbox').each(function () {
  74. this.style.display = (authCheckbox.checked ? '' : 'none');
  75. });
  76. },
  77. };
  78. }(jQuery, Drupal));