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