field_permissions.admin.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @file
  3. */
  4. (function ($) {
  5. Drupal.behaviors.fieldPermissionsSettings = {
  6. attach: function (context) {
  7. // For user fields, we want the "Create own value for field X" permission
  8. // row to only be displayed when it's meaningful (i.e., when the "Display
  9. // on user registration form" checkbox is checked).
  10. var $user_register_form_checkbox, $required_field_checkbox, $create_permission_row;
  11. $user_register_form_checkbox = $('.form-item-instance-settings-user-register-form .form-checkbox', context);
  12. if ($user_register_form_checkbox.length) {
  13. // The "Required field" checkbox can cause the user registration checkbox
  14. // to change, so we need it also.
  15. $required_field_checkbox = $('.form-item-instance-required .form-checkbox', context);
  16. if ($required_field_checkbox.length) {
  17. // Get the permissions table row corresponding to the "Create own value
  18. // for field X" permission. The theme_user_admin_permissions() function
  19. // does not give us a good way to directly detect which row contains
  20. // the create permissions, so we have rely on the fact that it will be
  21. // the first row.
  22. $create_permission_row = $('table#permissions tbody tr', context).filter(':first');
  23. new Drupal.fieldPermissions.HideCreatePermission($user_register_form_checkbox, $required_field_checkbox, $create_permission_row);
  24. }
  25. }
  26. // Show a warning if there's no available permissions matrix.
  27. $('#edit-field-field-permissions-permission-warning').toggle(!$('#permissions').length);
  28. $('[name="field[field_permissions][type]"]').bind('change', function(option) {
  29. $('#edit-field-field-permissions-permission-warning').toggle(!$('#permissions').length);
  30. });
  31. }
  32. };
  33. Drupal.fieldPermissions = {};
  34. /**
  35. * Constructor for the HideCreatePermission object.
  36. *
  37. * This object hides and shows the "Create own value for field X" permission
  38. * for user fields when it is appropriate to do so, depending on the state of
  39. * the "Display on user registration form" and "Required field" checkboxes.
  40. */
  41. Drupal.fieldPermissions.HideCreatePermission = function ($user_register_form_checkbox, $required_field_checkbox, $create_permission_row) {
  42. this.$user_register_form_checkbox = $user_register_form_checkbox;
  43. this.$create_permission_row = $create_permission_row;
  44. // Start off by making sure the create permission row has the correct
  45. // visibility.
  46. this.setCreatePermissionVisibility();
  47. // Set the row's visibility again whenever the user registration checkbox
  48. // changes, or when the required field checkbox (which controls it) changes.
  49. $user_register_form_checkbox.bind('change', $.proxy(this.setCreatePermissionVisibility, this));
  50. $required_field_checkbox.bind('change', $.proxy(this.setCreatePermissionVisibility, this));
  51. };
  52. /**
  53. * Set the correct visibility of the "Create own value for field X" permission.
  54. */
  55. Drupal.fieldPermissions.HideCreatePermission.prototype.setCreatePermissionVisibility = function () {
  56. // Granting permissions for "Create own value for field X" only makes sense
  57. // when the field is configured to appear on the user registration form, so
  58. // only show the row in the permissions table then.
  59. if (this.$user_register_form_checkbox.is(':checked')) {
  60. this.$create_permission_row.show();
  61. }
  62. else {
  63. this.$create_permission_row.hide();
  64. }
  65. };
  66. })(jQuery);