annotate core/modules/user/user.permissions.es6.js @ 19:fa3358dc1485 tip

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