diff --git a/ajaxSelectPicker.js b/ajaxSelectPicker.js index 00c455e..a21b9c6 100644 --- a/ajaxSelectPicker.js +++ b/ajaxSelectPicker.js @@ -5,7 +5,7 @@ * @author Adam Heim originally for CROSCON * @copyright 2014 ajax-bootstrap-select * @license Licensed under the MIT license - * @version 1.0.6 + * @version 1.1.0 */ !(function ($, window) { diff --git a/ajaxSelectPicker.min.js b/ajaxSelectPicker.min.js index 5245c2e..070c5bb 100644 --- a/ajaxSelectPicker.min.js +++ b/ajaxSelectPicker.min.js @@ -5,6 +5,6 @@ @author Adam Heim originally for CROSCON @copyright 2014 ajax-bootstrap-select @license Licensed under the MIT license - @version 1.0.6 + @version 1.1.0 */ -!function(a,b){a.ajaxSelectPicker=function(c,d){var e={9:"tab",13:"enter",16:"shift",17:"ctrl",18:"alt",27:"esc",37:"left",39:"right",13:"enter",38:"up",40:"down",229:"unknown"},f={ajaxResultsPreHook:null,ajaxSearchUrl:null,ajaxOptions:{},placeHolderOption:null,debug:!1},g=this,h=a(c),i={destroyLi:function(){this.$menu.find("li").remove()}};a.extend(g,i),g.ajaxOptions=a.extend(f,d,{}),g.init=function(){if(h.attr("data-search-url")&&(g.ajaxOptions.ajaxSearchUrl=h.attr("data-search-url")),h.data().hasOwnProperty("selectpicker"))if(null==g.ajaxOptions.ajaxSearchUrl)this.log("ajaxSelectPicker: ajaxSearchUrl must be set!",!0);else{var b=0;a.extend(g,h.data().selectpicker),g.$searchbox.off("input"),g.$searchbox.on("keydown",function(c){return e[c.keyCode]?!0:(clearTimeout(b),b=setTimeout(function(){var b=h.html();h.find("options").remove(),g.destroyLi(),g.$menu.append('
');var c={};c.url=g.ajaxOptions.ajaxSearchUrl,c.success=function(a){if("function"==typeof g.ajaxOptions.ajaxResultsPreHook&&(a=g.ajaxOptions.ajaxResultsPreHook(a)),Array.isArray(a)){var c="",d=a.length;if(a.length){"string"==typeof g.ajaxOptions.placeHolderOption&&(c+='");for(var e=0;d>e;e++){var f=a[e],h=f.hasOwnProperty("data");if(!f.hasOwnProperty("value")&&f.hasOwnProperty("data")&&f.data.hasOwnProperty("divider")){this.log("currentData must have a property of value",!0);break}if(h&&f.data.divider){c+=' data-divider="true">';break}c+="":f.value+""}}g.$element.html(c)}else g.$element.html(b)},c.error=function(a){this.log(["ajaxSelectPicker:",a],!0),g.$element.html(b)},c.complete=function(){a(".menu-loading").remove(),g.refresh()};var d=a.extend(!0,{},g.ajaxOptions.ajaxOptions);c.dataType=d.hasOwnProperty("dataType")?d.dataType:"json",c.type=d.hasOwnProperty("type")?d.type:"POST",d.hasOwnProperty("data")?(d.processedData=d.data,"function"==typeof d.data&&(d.processedData=d.data()),c.data=d.processedData):d.data={q:g.$searchbox.val()};var e=g.$searchbox.val();if(Object.keys(c.data).length)for(var f in c.data)c.data.hasOwnProperty(f)&&"{{{q}}}"===c.data[f]&&(c.data[f]=e);a.ajax(c)},300),void 0)})}else this.log("ajaxSelectPicker: Cannot attach ajax without selectpicker being run first!",!0)},g.log=function(a,c){a=a instanceof Array?a:[a],b.console&&this.ajaxOptions.debug&&(c?console.error:console.log).apply(console,a)},setTimeout(function(){g.init()},500)},a.fn.ajaxSelectPicker=function(b){return this.each(function(){void 0==a(this).data("ajaxSelectPicker")&&a(this).data("ajaxSelectPicker",new a.ajaxSelectPicker(this,b))})}}(jQuery,window); \ No newline at end of file +!(function($,window){$.ajaxSelectPicker=function(element,options){var specialKeyCodeMap={9:"tab",13:"enter",16:"shift",17:"ctrl",18:"alt",27:"esc",37:"left",39:"right",13:"enter",38:"up",40:"down",229:"unknown"};var defaults={ajaxResultsPreHook:null,ajaxSearchUrl:null,ajaxOptions:{},placeHolderOption:null,debug:false,mixWithCurrents:false};var plugin=this,$element=$(element);var selectPickerFunctions={destroyLi:function(){this.$menu.find('li').remove();}};$.extend(plugin,selectPickerFunctions);plugin.ajaxOptions=$.extend(defaults,options,{});plugin.init=function(){if($element.attr("data-search-url")){plugin.ajaxOptions.ajaxSearchUrl=$element.attr("data-search-url");}if(!$element.data().hasOwnProperty('selectpicker')){this.log('ajaxSelectPicker: Cannot attach ajax without selectpicker being run first!',true);}else if(plugin.ajaxOptions.ajaxSearchUrl==null){this.log('ajaxSelectPicker: ajaxSearchUrl must be set!',true)}else{var timeout=0;$.extend(plugin,$element.data().selectpicker);plugin.$searchbox.off('input');plugin.$searchbox.on('keydown',function(e){if(specialKeyCodeMap[e.keyCode]){return true;}clearTimeout(timeout);timeout=setTimeout(function(){var oldOptions=$element.html(),currentOptions=[];if(plugin.ajaxOptions.mixWithCurrents==true){$(element).find("option:selected").each(function(){currentOptions.push({value:$(this).val(),text:$(this).text(),html:this})});}$element.find('options').remove();plugin.destroyLi();plugin.$menu.append(' ');var ajaxParams={};ajaxParams.url=plugin.ajaxOptions.ajaxSearchUrl;ajaxParams.success=function(data){if(typeof plugin.ajaxOptions.ajaxResultsPreHook==='function'){data=plugin.ajaxOptions.ajaxResultsPreHook(data);}if(Array.isArray(data)){var options='',dataLen=data.length;if(data.length){if(typeof plugin.ajaxOptions.placeHolderOption==='string'){options+='';}var optionsObject={};for(var i=0;i