(function($){
var oldget = $.fn.get;
$.fn.extend({
autocomplete : function (options = new Array()) {
let caja_id = 'autocomplete-'+this[0].id;
var that = this;
this.__create(that,caja_id)
this.__hover(that,caja_id)
this.__click(that,caja_id)
this.__keys(that,options,caja_id)
return this
},
__create : function(that,caja_id){
$(that).attr('autocomplete', 'off');
var myTextBox = $(that);
var element_width = $(that).width() + 18;
var element_height = $(that).height() + 10;
var element_coordenadas = $(that).offset();
$('body').append('<div class="autocomplete-panel" id="'+caja_id+'"><ul class="list-group"></ul></div>');
$('#'+caja_id).width(element_width);
$('#'+caja_id).offset({top:element_coordenadas.top + element_height,left:element_coordenadas.left});
},
__keys : function(that,options,caja_id){
$(that).keyup(function(event) {
$('#'+caja_id+' .list-group').empty();
var myText = $(that).val();
var mySearch = '';
var n = 1;
$.each(options.source, function(index, val) {
mySearch = val;
let myReg = new RegExp(myText.toUpperCase() + '(\n.*)*');
let myMatch = (mySearch.toUpperCase()).match(myReg);
if (myMatch){
var res = (mySearch).toUpperCase().replace(myText.toUpperCase(), "<strong>"+myText.toUpperCase()+"</strong>");
$('#'+caja_id+' .list-group').append('<li class="list-group-item list-group-item-'+n+(n==1 ? ' active' : '')+'" data-position="'+n+'" data-catacter="'+mySearch+'">'+res+'</li>');
n++;
}
});
});
},
__hover : function(that,caja_id){
$(document).on('mouseover', '#'+caja_id+' .list-group .list-group-item', function(event) {
var position = $(this).data('position');
$('#'+caja_id+' .list-group .list-group-item').removeClass( "active" );
$('#'+caja_id+' .list-group .list-group-item-'+position).addClass( "active" );
});
},
__click : function(that,caja_id){
$(document).on('click', '#'+caja_id+' .list-group .list-group-item', function(event) {
var datos = [];
$.each($(this).data(), function(index, val) {
datos.push(val);
$(that).val(datos[1]);
});
$('#'+caja_id+' .list-group').empty();
that.success(that,datos);
});
},
success : function(that,datos){
var ret = oldget.apply(that, arguments);
return ret;
}
});
})(jQuery);
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$('#txtautocomplete').autocomplete({
source:availableTags,
}).success(function(response){
console.log(response);
});