-
Notifications
You must be signed in to change notification settings - Fork 9
/
search-autocomplete.js
66 lines (64 loc) · 1.81 KB
/
search-autocomplete.js
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
(function($) {
$(function() {
var options = $.extend({
'fieldName': '#s',
'maxRows': 10,
'minLength': 4
}, SearchAutocomplete);
options.fieldName = $('<div />').html(options.fieldName).text();
$(options.fieldName).autocomplete({
source: function( request, response ) {
$.ajax({
url: options.ajaxurl,
dataType: "json",
data: {
action: 'autocompleteCallback',
term: this.term
},
success: function( data ) {
response( $.map( data.results, function( item ) {
return {
label: item.title,
value: item.title,
url: item.url
}
}));
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
}
});
},
delay: options.delay,
minLength: options.minLength,
autoFocus: ( options.autoFocus === 'true' ),
search: function(event, ui) {
$(event.currentTarget).addClass('sa_searching');
},
create: function() {
},
select: function( event, ui ) {
if ( ui.item.url !== '#' ) {
location = ui.item.url;
} else {
return true;
}
},
open: function(event, ui) {
var acData = $(this).data('uiAutocomplete');
acData
.menu
.element
.find('a')
.each(function () {
var $self = $( this ),
keywords = $.trim( acData.term ).split( ' ' ).join('|');
$self.html($self.text().replace(new RegExp("(" + keywords + ")", "gi"), '<span class="sa-found-text">$1</span>'));
});
$(event.target).removeClass('sa_searching');
},
close: function() {
}
});
});
})(jQuery);