forked from truckingsim/Ajax-Bootstrap-Select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajaxSelectPicker.min.js
10 lines (9 loc) · 3.25 KB
/
ajaxSelectPicker.min.js
1
2
3
4
5
6
7
8
9
10
/*!
ajax-bootstrap-select v1.0.6
https://github.com/truckingsim/Ajax-Bootstrap-Select
@author Adam Heim originally for CROSCON
@copyright 2014 ajax-bootstrap-select
@license Licensed under the MIT license
@version 1.0.6
*/
!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('<div class="menu-loading">loading...</div>');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+='<option data-hidden="true">'+g.ajaxOptions.placeHolderOption+"</option>");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"></option>';break}c+="<option",f.hasOwnProperty("disable")&&f.disable&&(c+=' disabled="disabled"'),f.hasOwnProperty("class")&&(c+=' class="'+f.class+'"'),h&&(f.data.hasOwnProperty("subtext")&&(c+=' data-subtext="'+f.data.subtext+'"'),f.data.hasOwnProperty("icon")&&(c+=' data-icon="'+f.data.icon+'"'),f.data.hasOwnProperty("content")&&(c+=' data-content="'+f.data.content+'"')),c+=' value="'+f.value+'">',c+=f.hasOwnProperty("text")?f.text+"</option>":f.value+"</option>"}}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);