7aa79bbfa6e825f2cf27bcf5ca49edd03fe54d12
[yaffs-website] / web / core / modules / user / user.permissions.es6.js
1 /**
2  * @file
3  * User permission page behaviors.
4  */
5
6 (function ($, Drupal) {
7   /**
8    * Shows checked and disabled checkboxes for inherited permissions.
9    *
10    * @type {Drupal~behavior}
11    *
12    * @prop {Drupal~behaviorAttach} attach
13    *   Attaches functionality to the permissions table.
14    */
15   Drupal.behaviors.permissions = {
16     attach(context) {
17       const self = this;
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);
25         let $ancestor;
26         let method;
27         if ($table.prev().length) {
28           $ancestor = $table.prev();
29           method = 'after';
30         }
31         else {
32           $ancestor = $table.parent();
33           method = 'append';
34         }
35         $table.detach();
36
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.'))
44           .hide();
45
46         $table
47           .find('input[type="checkbox"]')
48           .not('.js-rid-anonymous, .js-rid-authenticated')
49           .addClass('real-checkbox js-real-checkbox')
50           .after($dummy);
51
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
56           // element.
57           .each(self.toggle);
58
59         // Re-insert the table into the DOM.
60         $ancestor[method]($table);
61       });
62     },
63
64     /**
65      * Toggles all dummy checkboxes based on the checkboxes' state.
66      *
67      * If the "authenticated user" checkbox is checked, the checked and disabled
68      * checkboxes are shown, the real checkboxes otherwise.
69      */
70     toggle() {
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' : '');
78       });
79       $row.find('.js-dummy-checkbox').each(function () {
80         this.style.display = (authCheckbox.checked ? '' : 'none');
81       });
82     },
83   };
84 }(jQuery, Drupal));