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