You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.2 KiB

1 year ago
  1. /**
  2. * Suggests users in a multisite environment.
  3. *
  4. * For input fields where the admin can select a user based on email or
  5. * username, this script shows an autocompletion menu for these inputs. Should
  6. * only be used in a multisite environment. Only users in the currently active
  7. * site are shown.
  8. *
  9. * @since 3.4.0
  10. * @output wp-admin/js/user-suggest.js
  11. */
  12. /* global ajaxurl, current_site_id, isRtl */
  13. (function( $ ) {
  14. var id = ( typeof current_site_id !== 'undefined' ) ? '&site_id=' + current_site_id : '';
  15. $( function() {
  16. var position = { offset: '0, -1' };
  17. if ( typeof isRtl !== 'undefined' && isRtl ) {
  18. position.my = 'right top';
  19. position.at = 'right bottom';
  20. }
  21. /**
  22. * Adds an autocomplete function to input fields marked with the class
  23. * 'wp-suggest-user'.
  24. *
  25. * A minimum of two characters is required to trigger the suggestions. The
  26. * autocompletion menu is shown at the left bottom of the input field. On
  27. * RTL installations, it is shown at the right top. Adds the class 'open' to
  28. * the input field when the autocompletion menu is shown.
  29. *
  30. * Does a backend call to retrieve the users.
  31. *
  32. * Optional data-attributes:
  33. * - data-autocomplete-type (add, search)
  34. * The action that is going to be performed: search for existing users
  35. * or add a new one. Default: add
  36. * - data-autocomplete-field (user_login, user_email)
  37. * The field that is returned as the value for the suggestion.
  38. * Default: user_login
  39. *
  40. * @see wp-admin/includes/admin-actions.php:wp_ajax_autocomplete_user()
  41. */
  42. $( '.wp-suggest-user' ).each( function(){
  43. var $this = $( this ),
  44. autocompleteType = ( typeof $this.data( 'autocompleteType' ) !== 'undefined' ) ? $this.data( 'autocompleteType' ) : 'add',
  45. autocompleteField = ( typeof $this.data( 'autocompleteField' ) !== 'undefined' ) ? $this.data( 'autocompleteField' ) : 'user_login';
  46. $this.autocomplete({
  47. source: ajaxurl + '?action=autocomplete-user&autocomplete_type=' + autocompleteType + '&autocomplete_field=' + autocompleteField + id,
  48. delay: 500,
  49. minLength: 2,
  50. position: position,
  51. open: function() {
  52. $( this ).addClass( 'open' );
  53. },
  54. close: function() {
  55. $( this ).removeClass( 'open' );
  56. }
  57. });
  58. });
  59. });
  60. })( jQuery );