diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53a5344 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/nbproject diff --git a/dist/jquery.bootcomplete.js b/dist/jquery.bootcomplete.js index 25e6c10..d4d6840 100644 --- a/dist/jquery.bootcomplete.js +++ b/dist/jquery.bootcomplete.js @@ -1,115 +1,95 @@ /* -* @name jQuery.bootcomplete -* @projectDescription Lightweight AJAX autocomplete for Bootstrap 3 -* @author Rick Somers | http://getwebhelp.com/bootcomplete -* @version 1.0 -* @license MIT License -* -*/ -(function ( $ ) { - - $.fn.bootcomplete = function(options) { - + * @name jQuery.bootcomplete + * @projectDescription Lightweight AJAX autocomplete for Bootstrap 3 + * @author Rick Somers | http://getwebhelp.com/bootcomplete + * @version 1.0 + * @license MIT License + * + */ +(function ($) { + + $.fn.bootcomplete = function (options) { var defaults = { - url : "/search.php", - method : 'get', - wrapperClass : "bc-wrapper", - menuClass : "bc-menu", - idField : true, - idFieldName : $(this).attr('name')+"_id", - minLength : 3, - dataParams : {}, - formParams : {} - } - - var settings = $.extend( {}, defaults, options ); - - $(this).attr('autocomplete','off') - $(this).wrap('
') + url: "/search.php", + method: 'get', + wrapperClass: "bc-wrapper", + menuClass: "bc-menu", + idField: true, + idFieldName: $(this).attr('name') + "_id", + minLength: 3, + dataParams: {}, + formParams: {} + }; + var settings = $.extend({}, defaults, options); + $(this).attr('autocomplete', 'off'); + $(this).wrap('
'); if (settings.idField) { - if ($(this).parent().parent().find('input[name="' + settings.idFieldName + '"]').length !== 0) { - //use existing id field - } else { - //there is no existing id field so create one - $('').insertBefore($(this)) - } + $('').insertBefore($(this)); } - $('
').insertAfter($(this)) - + $('
').insertAfter($(this)); + $(this).on("keyup", searchQuery); - $(this).on("focusout", hideThat) + + + var xhr; - var that = $(this) + var that = $(this); - function hideThat() { - if ($('.list-group-item' + ':hover').length) { - return; - } - $(that).next('.' + settings.menuClass).hide(); - } - - function searchQuery(){ - + function searchQuery() { var arr = []; - $.each(settings.formParams,function(k,v){ - arr[k]=$(v).val() - }) - var dyFormParams = $.extend({}, arr ); + $.each(settings.formParams, function (k, v) { + arr[k] = $(v).val(); + }); + var dyFormParams = $.extend({}, arr); var Data = $.extend({query: $(this).val()}, settings.dataParams, dyFormParams); - - if(!Data.query){ - $(this).next('.'+settings.menuClass).html('') - $(this).next('.'+settings.menuClass).hide() + + if (!Data.query) { + $(this).next('.' + settings.menuClass).html(''); + $(this).next('.' + settings.menuClass).hide(); } - - if(Data.query.length >= settings.minLength){ - - if(xhr && xhr.readyState != 4){ + + if (Data.query.length >= settings.minLength) { + + if (xhr && xhr.readyState !== 4) { xhr.abort(); } - + xhr = $.ajax({ type: settings.method, url: settings.url, data: Data, dataType: "json", - success: function( json ) { - var results = '' - $.each( json, function(i, j) { - results += ''+j.label+'' + success: function (json) { + var results = ''; + $.each(json, function (i, j) { + results += '' + j.label + ''; }); - - $(that).next('.'+settings.menuClass).html(results) - $(that).next('.'+settings.menuClass).children().on("click", selectResult) - $(that).next('.'+settings.menuClass).show() - + $(that).next('.' + settings.menuClass).html(results); + $(that).next('.' + settings.menuClass).children().on("click", selectResult); + $(that).next('.' + settings.menuClass).show(); } - }) + }); } } - - function selectResult(){ - $(that).val($(this).data('label')) + + function selectResult() { + $(that).val($(this).data('label')); if (settings.idField) { - if ($(that).parent().parent().find('input[name="' + settings.idFieldName + '"]').length !== 0) { - //use existed id field - $(that).parent().parent().find('input[name="' + settings.idFieldName + '"]').val($(this).data('id')); - //ensure we trigger the onchange so we can do stuff - $(that).parent().parent().find('input[name="' + settings.idFieldName + '"]').trigger('change'); - } - else { - //use created id field - $(that).prev('input[name="' + settings.idFieldName + '"]').val($(this).data('id')); - //ensure we trigger the onchange so we can do stuff - $(that).prev('input[name="' + settings.idFieldName + '"]').trigger('change'); - } + $(that).prev('input[name="' + settings.idFieldName + '"]').val($(this).data('id')); } $(that).next('.' + settings.menuClass).hide(); return false; } + $(this).keyup(function (e) { + if (e.keyCode === 27) { + $(that).next('.' + settings.menuClass).hide(); + xhr.abort(); + } + }); + return this; }; - -}( jQuery )); + +}(jQuery));