forked from montrezorro/Ajax-Bootstrap-Select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajaxSelectPicker.min.js
10 lines (10 loc) · 4.09 KB
/
ajaxSelectPicker.min.js
1
2
3
4
5
6
7
8
9
10
/*!
* ajax-bootstrap-select v1.0
* 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
*/
!function($,window){$.ajaxSelectPicker=function(element,options){var defaults={ajaxResultsPreHook:null,ajaxSearchUrl:null,ajaxOptions:{},placeHolderOption:null};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.data().hasOwnProperty("selectpicker")){console.error("ajaxSelectPicker: Cannot attach ajax without selectpicker being run first!")}else if(plugin.ajaxOptions.ajaxSearchUrl==null){console.error("ajaxSelectPicker: ajaxSearchUrl must be set!")}else{var timeout=0;$.extend(plugin,$element.data().selectpicker);plugin.$searchbox.off("input");plugin.$searchbox.on("keydown",function(){clearTimeout(timeout);timeout=setTimeout(function(){var oldOptions=$element.html();$element.find("options").remove();plugin.destroyLi();plugin.$menu.append('<div class="menu-loading">loading...</div>');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+='<option data-hidden="true">'+plugin.ajaxOptions.placeHolderOption+"</option>"}for(var i=0;i<dataLen;i++){var currentData=data[i];var hasData=currentData.hasOwnProperty(data);if(!currentData.hasOwnProperty("value")&&(currentData.hasOwnProperty("data")&¤tData.data.hasOwnProperty("divider"))){console.error("currentData must have a property of value");break}if(hasData&¤tData.data.divider){options+=' data-divider="true"></option>';break}options+="<option";if(currentData.hasOwnProperty("disable")&¤tData.disable){options+=' disabled="disabled"'}if(currentData.hasOwnProperty("class")){options+=' class="'+currentData.class+'"'}if(hasData){if(currentData.data.hasOwnProperty("subtext")){options+=' data-subtext="'+currentData.data.subtext+'"'}if(currentData.data.hasOwnProperty("icon")){options+=' data-icon="'+currentData.data.icon+'"'}if(currentData.data.hasOwnProperty("content")){options+=' data-content="'+currentData.data.content+'"'}}options+=' value="'+currentData.value+'">';if(currentData.hasOwnProperty("text")){options+=currentData.text+"</option>"}else{options+=currentData.value+"</option>"}}}plugin.$element.html(options)}else{plugin.$element.html(oldOptions)}};ajaxParams.error=function(xhr){console.error("ajaxSelectPicker:",xhr);plugin.$element.html(oldOptions)};ajaxParams.complete=function(){$(".menu-loading").remove();plugin.refresh()};var userParams=$.extend(true,{},plugin.ajaxOptions.ajaxOptions);ajaxParams.dataType=userParams.hasOwnProperty("dataType")?userParams.dataType:"json";ajaxParams.type=userParams.hasOwnProperty("type")?userParams.type:"POST";if(userParams.hasOwnProperty("data")){userParams.processedData=userParams.data;if(typeof userParams.data==="function"){userParams.processedData=userParams.data()}ajaxParams.data=userParams.processedData}else{userParams.data={q:plugin.$searchbox.val()}}var inputVal=plugin.$searchbox.val();if(Object.keys(ajaxParams.data).length){for(var dataKey in ajaxParams.data){if(ajaxParams.data.hasOwnProperty(dataKey)){if(ajaxParams.data[dataKey]==="{{{q}}}"){ajaxParams.data[dataKey]=inputVal}}}}$.ajax(ajaxParams)},300)})}$element.on("change",function(){$element.html("");if(typeof plugin.ajaxOptions.placeHolderOption==="string"){$element.html('<option data-hidden="true">'+plugin.ajaxOptions.placeHolderOption+"</option>")}plugin.$searchbox.val("");plugin.refresh()})};setTimeout(function(){plugin.init()},500)};$.fn.ajaxSelectPicker=function(options){return this.each(function(){if($(this).data("ajaxSelectPicker")==undefined){$(this).data("ajaxSelectPicker",new $.ajaxSelectPicker(this,options))}})}}(jQuery,window);