user.permissions.js 2.7 KB

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