user.permissions.es6.js 3.3 KB

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