2016-10-23 20:16:42 +02:00
|
|
|
/**
|
|
|
|
* Default settings for jQuery UI Autocomplete for use with non-hierarchical taxonomies.
|
2019-01-10 03:50:51 +01:00
|
|
|
*
|
|
|
|
* @output wp-admin/js/tags-suggest.js
|
2016-10-23 20:16:42 +02:00
|
|
|
*/
|
2016-10-15 00:40:28 +02:00
|
|
|
( function( $ ) {
|
2020-07-06 23:00:03 +02:00
|
|
|
if ( typeof window.uiAutocompleteL10n === 'undefined' ) {
|
2016-10-23 20:16:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-10 03:37:51 +01:00
|
|
|
var tempID = 0;
|
2020-07-06 23:00:03 +02:00
|
|
|
var separator = wp.i18n._x( ',', 'tag delimiter' ) || ',';
|
2016-10-15 00:40:28 +02:00
|
|
|
|
|
|
|
function split( val ) {
|
|
|
|
return val.split( new RegExp( separator + '\\s*' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLast( term ) {
|
|
|
|
return split( term ).pop();
|
|
|
|
}
|
|
|
|
|
2016-10-23 20:16:42 +02:00
|
|
|
/**
|
|
|
|
* Add UI Autocomplete to an input or textarea element with presets for use
|
|
|
|
* with non-hierarchical taxonomies.
|
|
|
|
*
|
|
|
|
* Example: `$( element ).wpTagsSuggest( options )`.
|
|
|
|
*
|
|
|
|
* The taxonomy can be passed in a `data-wp-taxonomy` attribute on the element or
|
|
|
|
* can be in `options.taxonomy`.
|
|
|
|
*
|
2016-11-17 19:28:30 +01:00
|
|
|
* @since 4.7.0
|
2016-10-23 20:16:42 +02:00
|
|
|
*
|
2020-07-28 01:35:02 +02:00
|
|
|
* @param {Object} options Options that are passed to UI Autocomplete. Can be used to override the default settings.
|
|
|
|
* @return {Object} jQuery instance.
|
2016-10-23 20:16:42 +02:00
|
|
|
*/
|
2016-10-15 00:40:28 +02:00
|
|
|
$.fn.wpTagsSuggest = function( options ) {
|
|
|
|
var cache;
|
|
|
|
var last;
|
|
|
|
var $element = $( this );
|
|
|
|
|
2020-11-28 13:46:05 +01:00
|
|
|
// Do not initialize if the element doesn't exist.
|
|
|
|
if ( ! $element.length ) {
|
2020-11-30 15:31:04 +01:00
|
|
|
return this;
|
2020-11-28 13:46:05 +01:00
|
|
|
}
|
|
|
|
|
2016-10-15 00:40:28 +02:00
|
|
|
options = options || {};
|
2016-10-23 20:16:42 +02:00
|
|
|
|
2016-10-15 00:40:28 +02:00
|
|
|
var taxonomy = options.taxonomy || $element.attr( 'data-wp-taxonomy' ) || 'post_tag';
|
2016-10-23 20:16:42 +02:00
|
|
|
|
2016-10-15 00:40:28 +02:00
|
|
|
delete( options.taxonomy );
|
|
|
|
|
|
|
|
options = $.extend( {
|
2016-10-23 20:16:42 +02:00
|
|
|
source: function( request, response ) {
|
2016-10-15 00:40:28 +02:00
|
|
|
var term;
|
|
|
|
|
|
|
|
if ( last === request.term ) {
|
|
|
|
response( cache );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
term = getLast( request.term );
|
|
|
|
|
2019-01-10 03:37:51 +01:00
|
|
|
$.get( window.ajaxurl, {
|
|
|
|
action: 'ajax-tag-search',
|
|
|
|
tax: taxonomy,
|
2022-04-07 06:06:05 +02:00
|
|
|
q: term,
|
|
|
|
number: 20
|
2016-10-15 00:40:28 +02:00
|
|
|
} ).always( function() {
|
|
|
|
$element.removeClass( 'ui-autocomplete-loading' ); // UI fails to remove this sometimes?
|
|
|
|
} ).done( function( data ) {
|
2019-01-10 03:37:51 +01:00
|
|
|
var tagName;
|
|
|
|
var tags = [];
|
|
|
|
|
|
|
|
if ( data ) {
|
|
|
|
data = data.split( '\n' );
|
|
|
|
|
|
|
|
for ( tagName in data ) {
|
|
|
|
var id = ++tempID;
|
|
|
|
|
|
|
|
tags.push({
|
|
|
|
id: id,
|
|
|
|
name: data[tagName]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cache = tags;
|
|
|
|
response( tags );
|
|
|
|
} else {
|
|
|
|
response( tags );
|
|
|
|
}
|
2016-10-15 00:40:28 +02:00
|
|
|
} );
|
|
|
|
|
|
|
|
last = request.term;
|
|
|
|
},
|
|
|
|
focus: function( event, ui ) {
|
|
|
|
$element.attr( 'aria-activedescendant', 'wp-tags-autocomplete-' + ui.item.id );
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Don't empty the input field when using the arrow keys
|
|
|
|
// to highlight items. See api.jqueryui.com/autocomplete/#event-focus
|
2016-10-15 00:40:28 +02:00
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
select: function( event, ui ) {
|
|
|
|
var tags = split( $element.val() );
|
|
|
|
// Remove the last user input.
|
|
|
|
tags.pop();
|
|
|
|
// Append the new tag and an empty element to get one more separator at the end.
|
|
|
|
tags.push( ui.item.name, '' );
|
|
|
|
|
|
|
|
$element.val( tags.join( separator + ' ' ) );
|
|
|
|
|
|
|
|
if ( $.ui.keyCode.TAB === event.keyCode ) {
|
2016-10-23 20:16:42 +02:00
|
|
|
// Audible confirmation message when a tag has been selected.
|
2020-07-06 23:00:03 +02:00
|
|
|
window.wp.a11y.speak( wp.i18n.__( 'Term selected.' ), 'assertive' );
|
2016-10-15 00:40:28 +02:00
|
|
|
event.preventDefault();
|
|
|
|
} else if ( $.ui.keyCode.ENTER === event.keyCode ) {
|
2019-04-17 09:18:51 +02:00
|
|
|
// If we're in the edit post Tags meta box, add the tag.
|
|
|
|
if ( window.tagBox ) {
|
|
|
|
window.tagBox.userAction = 'add';
|
|
|
|
window.tagBox.flushTags( $( this ).closest( '.tagsdiv' ) );
|
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Do not close Quick Edit / Bulk Edit.
|
2016-10-15 00:40:28 +02:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
open: function() {
|
|
|
|
$element.attr( 'aria-expanded', 'true' );
|
|
|
|
},
|
|
|
|
close: function() {
|
|
|
|
$element.attr( 'aria-expanded', 'false' );
|
|
|
|
},
|
2019-01-10 03:37:51 +01:00
|
|
|
minLength: 2,
|
2016-10-15 00:40:28 +02:00
|
|
|
position: {
|
2017-03-31 18:35:45 +02:00
|
|
|
my: 'left top+2',
|
|
|
|
at: 'left bottom',
|
|
|
|
collision: 'none'
|
2016-10-15 00:40:28 +02:00
|
|
|
},
|
|
|
|
messages: {
|
2016-10-23 20:16:42 +02:00
|
|
|
noResults: window.uiAutocompleteL10n.noResults,
|
2016-10-15 00:40:28 +02:00
|
|
|
results: function( number ) {
|
2016-10-23 20:16:42 +02:00
|
|
|
if ( number > 1 ) {
|
|
|
|
return window.uiAutocompleteL10n.manyResults.replace( '%d', number );
|
2016-10-15 00:40:28 +02:00
|
|
|
}
|
2016-10-23 20:16:42 +02:00
|
|
|
|
|
|
|
return window.uiAutocompleteL10n.oneResult;
|
2016-10-15 00:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, options );
|
|
|
|
|
|
|
|
$element.on( 'keydown', function() {
|
|
|
|
$element.removeAttr( 'aria-activedescendant' );
|
2020-11-28 13:46:05 +01:00
|
|
|
} );
|
|
|
|
|
|
|
|
$element.autocomplete( options );
|
|
|
|
|
|
|
|
// Ensure the autocomplete instance exists.
|
2020-11-30 15:31:04 +01:00
|
|
|
if ( ! $element.autocomplete( 'instance' ) ) {
|
|
|
|
return this;
|
2020-11-28 13:46:05 +01:00
|
|
|
}
|
2016-10-15 00:40:28 +02:00
|
|
|
|
2020-11-30 15:31:04 +01:00
|
|
|
$element.autocomplete( 'instance' )._renderItem = function( ul, item ) {
|
|
|
|
return $( '<li role="option" id="wp-tags-autocomplete-' + item.id + '">' )
|
|
|
|
.text( item.name )
|
|
|
|
.appendTo( ul );
|
|
|
|
};
|
|
|
|
|
2016-10-15 00:40:28 +02:00
|
|
|
$element.attr( {
|
|
|
|
'role': 'combobox',
|
|
|
|
'aria-autocomplete': 'list',
|
|
|
|
'aria-expanded': 'false',
|
|
|
|
'aria-owns': $element.autocomplete( 'widget' ).attr( 'id' )
|
|
|
|
} )
|
|
|
|
.on( 'focus', function() {
|
|
|
|
var inputValue = split( $element.val() ).pop();
|
|
|
|
|
|
|
|
// Don't trigger a search if the field is empty.
|
|
|
|
// Also, avoids screen readers announce `No search results`.
|
|
|
|
if ( inputValue ) {
|
|
|
|
$element.autocomplete( 'search' );
|
|
|
|
}
|
2020-11-30 15:31:04 +01:00
|
|
|
} );
|
|
|
|
|
2016-10-15 00:40:28 +02:00
|
|
|
// Returns a jQuery object containing the menu element.
|
2020-11-30 15:31:04 +01:00
|
|
|
$element.autocomplete( 'widget' )
|
2016-10-15 00:40:28 +02:00
|
|
|
.addClass( 'wp-tags-autocomplete' )
|
|
|
|
.attr( 'role', 'listbox' )
|
|
|
|
.removeAttr( 'tabindex' ) // Remove the `tabindex=0` attribute added by jQuery UI.
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
/*
|
|
|
|
* Looks like Safari and VoiceOver need an `aria-selected` attribute. See ticket #33301.
|
|
|
|
* The `menufocus` and `menublur` events are the same events used to add and remove
|
|
|
|
* the `ui-state-focus` CSS class on the menu items. See jQuery UI Menu Widget.
|
|
|
|
*/
|
2016-10-15 00:40:28 +02:00
|
|
|
.on( 'menufocus', function( event, ui ) {
|
|
|
|
ui.item.attr( 'aria-selected', 'true' );
|
|
|
|
})
|
|
|
|
.on( 'menublur', function() {
|
2020-01-29 01:45:18 +01:00
|
|
|
// The `menublur` event returns an object where the item is `null`,
|
2016-10-15 00:40:28 +02:00
|
|
|
// so we need to find the active item with other means.
|
|
|
|
$( this ).find( '[aria-selected="true"]' ).removeAttr( 'aria-selected' );
|
|
|
|
});
|
2016-10-23 20:16:42 +02:00
|
|
|
|
2016-10-15 00:40:28 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
}( jQuery ) );
|