Projet

Général

Profil

Paste
Télécharger (3,44 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / field_permissions / field_permissions.admin.js @ 08475715

1
/**
2
 * @file
3
 */
4

    
5
(function ($) {
6

    
7
  Drupal.behaviors.fieldPermissionsSettings = {
8
    attach: function (context) {
9
      // For user fields, we want the "Create own value for field X" permission
10
      // row to only be displayed when it's meaningful (i.e., when the "Display
11
      // on user registration form" checkbox is checked).
12
      var $user_register_form_checkbox, $required_field_checkbox, $create_permission_row;
13
      $user_register_form_checkbox = $('.form-item-instance-settings-user-register-form .form-checkbox', context);
14
      if ($user_register_form_checkbox.length) {
15
        // The "Required field" checkbox can cause the user registration checkbox
16
        // to change, so we need it also.
17
        $required_field_checkbox = $('.form-item-instance-required .form-checkbox', context);
18
        if ($required_field_checkbox.length) {
19
          // Get the permissions table row corresponding to the "Create own value
20
          // for field X" permission. The theme_user_admin_permissions() function
21
          // does not give us a good way to directly detect which row contains
22
          // the create permissions, so we have rely on the fact that it will be
23
          // the first row.
24
          $create_permission_row = $('table#permissions tbody tr', context).filter(':first');
25
          new Drupal.fieldPermissions.HideCreatePermission($user_register_form_checkbox, $required_field_checkbox, $create_permission_row);
26
        }
27
      }
28

    
29
      // Show a warning if there's no available permissions matrix.
30
      $('#edit-field-field-permissions-permission-warning').toggle(!$('#permissions').length);
31

    
32
      $('[name="field[field_permissions][type]"]').bind('change', function(option) {
33
        $('#edit-field-field-permissions-permission-warning').toggle(!$('#permissions').length);
34
      });
35
    }
36
  };
37

    
38
  Drupal.fieldPermissions = {};
39

    
40
  /**
41
 * Constructor for the HideCreatePermission object.
42
 *
43
 * This object hides and shows the "Create own value for field X" permission
44
 * for user fields when it is appropriate to do so, depending on the state of
45
 * the "Display on user registration form" and "Required field" checkboxes.
46
 */
47
  Drupal.fieldPermissions.HideCreatePermission = function ($user_register_form_checkbox, $required_field_checkbox, $create_permission_row) {
48
    this.$user_register_form_checkbox = $user_register_form_checkbox;
49
    this.$create_permission_row = $create_permission_row;
50
    // Start off by making sure the create permission row has the correct
51
    // visibility.
52
    this.setCreatePermissionVisibility();
53
    // Set the row's visibility again whenever the user registration checkbox
54
    // changes, or when the required field checkbox (which controls it) changes.
55
    $user_register_form_checkbox.bind('change', $.proxy(this.setCreatePermissionVisibility, this));
56
    $required_field_checkbox.bind('change', $.proxy(this.setCreatePermissionVisibility, this));
57
  };
58

    
59
  /**
60
   * Set the correct visibility of the "Create own value for field X" permission.
61
   */
62
  Drupal.fieldPermissions.HideCreatePermission.prototype.setCreatePermissionVisibility = function () {
63
    // Granting permissions for "Create own value for field X" only makes sense
64
    // when the field is configured to appear on the user registration form, so
65
    // only show the row in the permissions table then.
66
    if (this.$user_register_form_checkbox.is(':checked')) {
67
      this.$create_permission_row.show();
68
    }
69
    else {
70
      this.$create_permission_row.hide();
71
    }
72
  };
73

    
74
})(jQuery);