3 * User permission page behaviors.
6 (function ($, Drupal) {
11 * Shows checked and disabled checkboxes for inherited permissions.
13 * @type {Drupal~behavior}
15 * @prop {Drupal~behaviorAttach} attach
16 * Attaches functionality to the permissions table.
18 Drupal.behaviors.permissions = {
19 attach: function (context) {
21 $('table#permissions').once('permissions').each(function () {
22 // On a site with many roles and permissions, this behavior initially
23 // has to perform thousands of DOM manipulations to inject checkboxes
24 // and hide them. By detaching the table from the DOM, all operations
25 // can be performed without triggering internal layout and re-rendering
26 // processes in the browser.
30 if ($table.prev().length) {
31 $ancestor = $table.prev();
35 $ancestor = $table.parent();
40 // Create dummy checkboxes. We use dummy checkboxes instead of reusing
41 // the existing checkboxes here because new checkboxes don't alter the
42 // submitted form. If we'd automatically check existing checkboxes, the
43 // permission table would be polluted with redundant entries. This
44 // is deliberate, but desirable when we automatically check them.
45 var $dummy = $('<input type="checkbox" class="dummy-checkbox js-dummy-checkbox" disabled="disabled" checked="checked" />')
46 .attr('title', Drupal.t('This permission is inherited from the authenticated user role.'))
50 .find('input[type="checkbox"]')
51 .not('.js-rid-anonymous, .js-rid-authenticated')
52 .addClass('real-checkbox js-real-checkbox')
55 // Initialize the authenticated user checkbox.
56 $table.find('input[type=checkbox].js-rid-authenticated')
57 .on('click.permissions', self.toggle)
58 // .triggerHandler() cannot be used here, as it only affects the first
62 // Re-insert the table into the DOM.
63 $ancestor[method]($table);
68 * Toggles all dummy checkboxes based on the checkboxes' state.
70 * If the "authenticated user" checkbox is checked, the checked and disabled
71 * checkboxes are shown, the real checkboxes otherwise.
74 var authCheckbox = this;
75 var $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' : '');
82 $row.find('.js-dummy-checkbox').each(function () {
83 this.style.display = (authCheckbox.checked ? '' : 'none');