[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/js/ -> tags-suggest.js (source)

   1  /**
   2   * Default settings for jQuery UI Autocomplete for use with non-hierarchical taxonomies.
   3   *
   4   * @output wp-admin/js/tags-suggest.js
   5   */
   6  ( function( $ ) {
   7      if ( typeof window.tagsSuggestL10n === 'undefined' || typeof window.uiAutocompleteL10n === 'undefined' ) {
   8          return;
   9      }
  10  
  11      var tempID = 0;
  12      var separator = window.tagsSuggestL10n.tagDelimiter || ',';
  13  
  14  	function split( val ) {
  15          return val.split( new RegExp( separator + '\\s*' ) );
  16      }
  17  
  18  	function getLast( term ) {
  19          return split( term ).pop();
  20      }
  21  
  22      /**
  23       * Add UI Autocomplete to an input or textarea element with presets for use
  24       * with non-hierarchical taxonomies.
  25       *
  26       * Example: `$( element ).wpTagsSuggest( options )`.
  27       *
  28       * The taxonomy can be passed in a `data-wp-taxonomy` attribute on the element or
  29       * can be in `options.taxonomy`.
  30       *
  31       * @since 4.7.0
  32       *
  33       * @param {object} options Options that are passed to UI Autocomplete. Can be used to override the default settings.
  34       * @returns {object} jQuery instance.
  35       */
  36      $.fn.wpTagsSuggest = function( options ) {
  37          var cache;
  38          var last;
  39          var $element = $( this );
  40  
  41          options = options || {};
  42  
  43          var taxonomy = options.taxonomy || $element.attr( 'data-wp-taxonomy' ) || 'post_tag';
  44  
  45          delete( options.taxonomy );
  46  
  47          options = $.extend( {
  48              source: function( request, response ) {
  49                  var term;
  50  
  51                  if ( last === request.term ) {
  52                      response( cache );
  53                      return;
  54                  }
  55  
  56                  term = getLast( request.term );
  57  
  58                  $.get( window.ajaxurl, {
  59                      action: 'ajax-tag-search',
  60                      tax: taxonomy,
  61                      q: term
  62                  } ).always( function() {
  63                      $element.removeClass( 'ui-autocomplete-loading' ); // UI fails to remove this sometimes?
  64                  } ).done( function( data ) {
  65                      var tagName;
  66                      var tags = [];
  67  
  68                      if ( data ) {
  69                          data = data.split( '\n' );
  70  
  71                          for ( tagName in data ) {
  72                              var id = ++tempID;
  73  
  74                              tags.push({
  75                                  id: id,
  76                                  name: data[tagName]
  77                              });
  78                          }
  79  
  80                          cache = tags;
  81                          response( tags );
  82                      } else {
  83                          response( tags );
  84                      }
  85                  } );
  86  
  87                  last = request.term;
  88              },
  89              focus: function( event, ui ) {
  90                  $element.attr( 'aria-activedescendant', 'wp-tags-autocomplete-' + ui.item.id );
  91  
  92                  // Don't empty the input field when using the arrow keys to
  93                  // highlight items. See api.jqueryui.com/autocomplete/#event-focus
  94                  event.preventDefault();
  95              },
  96              select: function( event, ui ) {
  97                  var tags = split( $element.val() );
  98                  // Remove the last user input.
  99                  tags.pop();
 100                  // Append the new tag and an empty element to get one more separator at the end.
 101                  tags.push( ui.item.name, '' );
 102  
 103                  $element.val( tags.join( separator + ' ' ) );
 104  
 105                  if ( $.ui.keyCode.TAB === event.keyCode ) {
 106                      // Audible confirmation message when a tag has been selected.
 107                      window.wp.a11y.speak( window.tagsSuggestL10n.termSelected, 'assertive' );
 108                      event.preventDefault();
 109                  } else if ( $.ui.keyCode.ENTER === event.keyCode ) {
 110                      // If we're in the edit post Tags meta box, add the tag.
 111                      if ( window.tagBox ) {
 112                          window.tagBox.userAction = 'add';
 113                          window.tagBox.flushTags( $( this ).closest( '.tagsdiv' ) );
 114                      }
 115  
 116                      // Do not close Quick Edit / Bulk Edit
 117                      event.preventDefault();
 118                      event.stopPropagation();
 119                  }
 120  
 121                  return false;
 122              },
 123              open: function() {
 124                  $element.attr( 'aria-expanded', 'true' );
 125              },
 126              close: function() {
 127                  $element.attr( 'aria-expanded', 'false' );
 128              },
 129              minLength: 2,
 130              position: {
 131                  my: 'left top+2',
 132                  at: 'left bottom',
 133                  collision: 'none'
 134              },
 135              messages: {
 136                  noResults: window.uiAutocompleteL10n.noResults,
 137                  results: function( number ) {
 138                      if ( number > 1 ) {
 139                          return window.uiAutocompleteL10n.manyResults.replace( '%d', number );
 140                      }
 141  
 142                      return window.uiAutocompleteL10n.oneResult;
 143                  }
 144              }
 145          }, options );
 146  
 147          $element.on( 'keydown', function() {
 148              $element.removeAttr( 'aria-activedescendant' );
 149          } )
 150          .autocomplete( options )
 151          .autocomplete( 'instance' )._renderItem = function( ul, item ) {
 152              return $( '<li role="option" id="wp-tags-autocomplete-' + item.id + '">' )
 153                  .text( item.name )
 154                  .appendTo( ul );
 155          };
 156  
 157          $element.attr( {
 158              'role': 'combobox',
 159              'aria-autocomplete': 'list',
 160              'aria-expanded': 'false',
 161              'aria-owns': $element.autocomplete( 'widget' ).attr( 'id' )
 162          } )
 163          .on( 'focus', function() {
 164              var inputValue = split( $element.val() ).pop();
 165  
 166              // Don't trigger a search if the field is empty.
 167              // Also, avoids screen readers announce `No search results`.
 168              if ( inputValue ) {
 169                  $element.autocomplete( 'search' );
 170              }
 171          } )
 172          // Returns a jQuery object containing the menu element.
 173          .autocomplete( 'widget' )
 174              .addClass( 'wp-tags-autocomplete' )
 175              .attr( 'role', 'listbox' )
 176              .removeAttr( 'tabindex' ) // Remove the `tabindex=0` attribute added by jQuery UI.
 177  
 178              // Looks like Safari and VoiceOver need an `aria-selected` attribute. See ticket #33301.
 179              // The `menufocus` and `menublur` events are the same events used to add and remove
 180              // the `ui-state-focus` CSS class on the menu items. See jQuery UI Menu Widget.
 181              .on( 'menufocus', function( event, ui ) {
 182                  ui.item.attr( 'aria-selected', 'true' );
 183              })
 184              .on( 'menublur', function() {
 185                  // The `menublur` event returns an object where the item is `null`
 186                  // so we need to find the active item with other means.
 187                  $( this ).find( '[aria-selected="true"]' ).removeAttr( 'aria-selected' );
 188              });
 189  
 190          return this;
 191      };
 192  
 193  }( jQuery ) );


Generated: Tue Sep 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1