logintoboggan.permissions.js 3.0 KB

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