ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

user-suggest.js (2301B)


      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 
     13 /* global ajaxurl, current_site_id, isRtl */
     14 
     15 (function( $ ) {
     16 	var id = ( typeof current_site_id !== 'undefined' ) ? '&site_id=' + current_site_id : '';
     17 	$( function() {
     18 		var position = { offset: '0, -1' };
     19 		if ( typeof isRtl !== 'undefined' && isRtl ) {
     20 			position.my = 'right top';
     21 			position.at = 'right bottom';
     22 		}
     23 
     24 		/**
     25 		 * Adds an autocomplete function to input fields marked with the class
     26 		 * 'wp-suggest-user'.
     27 		 *
     28 		 * A minimum of two characters is required to trigger the suggestions. The
     29 		 * autocompletion menu is shown at the left bottom of the input field. On
     30 		 * RTL installations, it is shown at the right top. Adds the class 'open' to
     31 		 * the input field when the autocompletion menu is shown.
     32 		 *
     33 		 * Does a backend call to retrieve the users.
     34 		 *
     35 		 * Optional data-attributes:
     36 		 * - data-autocomplete-type (add, search)
     37 		 *   The action that is going to be performed: search for existing users
     38 		 *   or add a new one. Default: add
     39 		 * - data-autocomplete-field (user_login, user_email)
     40 		 *   The field that is returned as the value for the suggestion.
     41 		 *   Default: user_login
     42 		 *
     43 		 * @see wp-admin/includes/admin-actions.php:wp_ajax_autocomplete_user()
     44 		 */
     45 		$( '.wp-suggest-user' ).each( function(){
     46 			var $this = $( this ),
     47 				autocompleteType = ( typeof $this.data( 'autocompleteType' ) !== 'undefined' ) ? $this.data( 'autocompleteType' ) : 'add',
     48 				autocompleteField = ( typeof $this.data( 'autocompleteField' ) !== 'undefined' ) ? $this.data( 'autocompleteField' ) : 'user_login';
     49 
     50 			$this.autocomplete({
     51 				source:    ajaxurl + '?action=autocomplete-user&autocomplete_type=' + autocompleteType + '&autocomplete_field=' + autocompleteField + id,
     52 				delay:     500,
     53 				minLength: 2,
     54 				position:  position,
     55 				open: function() {
     56 					$( this ).addClass( 'open' );
     57 				},
     58 				close: function() {
     59 					$( this ).removeClass( 'open' );
     60 				}
     61 			});
     62 		});
     63 	});
     64 })( jQuery );