cms/drupal/modules/user/user.permissions.js
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 (function ($) {
       
     2 
       
     3 /**
       
     4  * Shows checked and disabled checkboxes for inherited permissions.
       
     5  */
       
     6 Drupal.behaviors.permissions = {
       
     7   attach: function (context) {
       
     8     var self = this;
       
     9     $('table#permissions').once('permissions', function () {
       
    10       // On a site with many roles and permissions, this behavior initially has
       
    11       // to perform thousands of DOM manipulations to inject checkboxes and hide
       
    12       // them. By detaching the table from the DOM, all operations can be
       
    13       // performed without triggering internal layout and re-rendering processes
       
    14       // in the browser.
       
    15       var $table = $(this);
       
    16       if ($table.prev().length) {
       
    17         var $ancestor = $table.prev(), method = 'after';
       
    18       }
       
    19       else {
       
    20         var $ancestor = $table.parent(), method = 'append';
       
    21       }
       
    22       $table.detach();
       
    23 
       
    24       // Create dummy checkboxes. We use dummy checkboxes instead of reusing
       
    25       // the existing checkboxes here because new checkboxes don't alter the
       
    26       // submitted form. If we'd automatically check existing checkboxes, the
       
    27       // permission table would be polluted with redundant entries. This
       
    28       // is deliberate, but desirable when we automatically check them.
       
    29       var $dummy = $('<input type="checkbox" class="dummy-checkbox" disabled="disabled" checked="checked" />')
       
    30         .attr('title', Drupal.t("This permission is inherited from the authenticated user role."))
       
    31         .hide();
       
    32 
       
    33       $('input[type=checkbox]', this).not('.rid-2, .rid-1').addClass('real-checkbox').each(function () {
       
    34         $dummy.clone().insertAfter(this);
       
    35       });
       
    36 
       
    37       // Initialize the authenticated user checkbox.
       
    38       $('input[type=checkbox].rid-2', this)
       
    39         .bind('click.permissions', self.toggle)
       
    40         // .triggerHandler() cannot be used here, as it only affects the first
       
    41         // element.
       
    42         .each(self.toggle);
       
    43 
       
    44       // Re-insert the table into the DOM.
       
    45       $ancestor[method]($table);
       
    46     });
       
    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 
       
    69 })(jQuery);