autocomplete.js
2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* JavaScript for interacting with autocomplete instances.
*
* @since 1.0.0
*
* @package Media_Library_Organizer
* @author Media Library Organizer
*/
/**
* Initialize autocomplete instances for Classic Editor
* and Meta Boxes, based on the globally registered
* selectors.
*
* @since 1.1.9
*/
function mediaLibraryOrganizerAutoCompleteInit() {
( function( $ ) {
$( media_library_organizer_autocomplete.fields.join( ', ' ) ).each(
function( e ) {
if ( $( this ).data( 'autocomplete' ) ) {
$( this ).autocomplete( 'destroy' );
$( this ).removeData( 'autocomplete' );
}
// Initialize autocomplete.
mediaLibraryOrganizerAutoCompleteInitInput( this, media_library_organizer_autocomplete.terms, true );
}
);
} )( jQuery );
}
/**
* Initializes a single autocomplete input instance for the given input field
*
* @since 1.1.6
*/
function mediaLibraryOrganizerAutoCompleteInitInput( input, values, destroy_if_already_initialized ) {
( function( $ ) {
// Destroy the autocomplete instance if it's already initialized on this element.
if ( destroy_if_already_initialized && $( input ).data( 'autocomplete' ) ) {
$( input ).autocomplete( 'destroy' );
$( input ).removeData( 'autocomplete' );
}
// If the input is already initialized, don't do anything.
if ( $( input ).data( 'autocomplete' ) ) {
return;
}
// Initialize.
$( input )
.on(
'keydown',
function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB && $( this ).autocomplete( 'instance' ).menu.active ) {
event.preventDefault();
}
}
)
.autocomplete(
{
minLength: 0,
source: function( request, response ) {
response( $.ui.autocomplete.filter( values, request.term.split( /[ ,]+/ ).pop() ) );
},
focus: function() {
return false;
},
select: function( event, ui ) {
var terms = this.value.split( /[ ,]+/ );
terms.pop();
terms.push( ui.item.value );
terms.push( "" );
this.value = terms.join( " " );
return false;
}
}
);
} )( jQuery );
}
/**
* Initialize autocomplete instances when the page is ready
*
* @since 1.2.0
*/
jQuery( document ).ready(
function( $ ) {
mediaLibraryOrganizerAutoCompleteInit();
}
);