forked from snapappointments/bootstrap-select
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap-select.min.js
7 lines (7 loc) · 14.9 KB
/
bootstrap-select.min.js
1
2
3
4
5
6
7
/*!
* bootstrap-select v1.3.1
* http://silviomoreto.github.io/bootstrap-select/
*
* Copyright 2013 bootstrap-select
* Licensed under the MIT license
*/!function(e){"use strict";e.expr[":"].icontains=e.expr.createPseudo(function(t){return function(n){return e(n).text().toUpperCase().indexOf(t.toUpperCase())>=0}});var t=function(n,r,i){if(i){i.stopPropagation();i.preventDefault()}this.$element=e(n);this.$newElement=null;this.$button=null;this.$menu=null;this.options=e.extend({},e.fn.selectpicker.defaults,this.$element.data(),typeof r=="object"&&r);if(this.options.title==null){this.options.title=this.$element.attr("title");this.options.initialtitle=this.$element.attr("title")}this.val=t.prototype.val;this.render=t.prototype.render;this.refresh=t.prototype.refresh;this.setStyle=t.prototype.setStyle;this.selectAll=t.prototype.selectAll;this.deselectAll=t.prototype.deselectAll;this.selectInverse=t.prototype.selectInverse;this.init()};t.prototype={constructor:t,init:function(t){this.$element.hide();this.multiple=this.$element.prop("multiple");var n=this.$element.attr("id");this.$newElement=this.createView();this.$element.after(this.$newElement);this.$menu=this.$newElement.find("> .dropdown-menu");this.$button=this.$newElement.find("> button");this.$searchbox=this.$newElement.find("input");if(n!==undefined){var r=this;this.$button.attr("data-id",n);e('label[for="'+n+'"]').click(function(e){e.preventDefault();r.$button.focus()})}this.checkDisabled();this.checkTabIndex();this.clickListener();this.liveSearchListener();this.render();this.liHeight();this.setStyle();this.setWidth();this.options.container&&this.selectPosition();this.$menu.data("this",this);this.$newElement.data("this",this)},createDropdown:function(){var t=this.multiple?" show-tick":"",n=this.options.header?'<h3 class="popover-title">'+this.options.header+'<button type="button" class="close" aria-hidden="true">×</button></h3>':"",r=this.options.liveSearch?'<div class="bootstrap-select-searchbox"><input type="text" class="input-block-level form-control" /></div>':"",i=this.multiple&&this.options.showSelectOptions?'<div class="selectAll"><a class="all">'+this.options.selectOptionsText.all+'</a> | <a class="none">'+this.options.selectOptionsText.none+'</a> | <a class="inv">'+this.options.selectOptionsText.inv+"</a></div> ":"",s="<div class='btn-group bootstrap-select"+t+"'>"+"<button type='button' class='btn dropdown-toggle' data-toggle='dropdown'>"+"<div class='filter-option pull-left'></div> "+"<div class='caret'></div>"+"</button>"+"<div class='dropdown-menu open'>"+n+r+i+"<ul class='dropdown-menu inner' role='menu'>"+"</ul>"+"</div>"+"</div>";return e(s)},createView:function(){var e=this.createDropdown(),t=this.createLi();e.find("ul").append(t);return e},reloadLi:function(){this.destroyLi();var e=this.createLi();this.$menu.find("ul").append(e)},destroyLi:function(){this.$menu.find("li").remove()},createLi:function(){var t=this,n=[],r="";this.$element.find("option").each(function(r){var i=e(this),s=i.attr("class")||"",o=i.attr("style")||"",u=i.data("content")?i.data("content"):i.html(),a=i.data("subtext")!==undefined?'<small class="muted">'+i.data("subtext")+"</small>":"",f=i.data("icon")!==undefined?'<i class="glyphicon '+i.data("icon")+'"></i> ':"";f!==""&&(i.is(":disabled")||i.parent().is(":disabled"))&&(f="<span>"+f+"</span>");i.data("content")||(u=f+'<span class="text">'+u+a+"</span>");if(t.options.hideDisabled&&(i.is(":disabled")||i.parent().is(":disabled")))n.push('<a style="min-height: 0; padding: 0"></a>');else if(i.parent().is("optgroup")&&i.data("divider")!=1)if(i.index()==0){var l=i.parent().attr("label"),c=i.parent().data("subtext")!==undefined?'<small class="muted">'+i.parent().data("subtext")+"</small>":"",h=i.parent().data("icon")?'<i class="'+i.parent().data("icon")+'"></i> ':"";l=h+'<span class="text">'+l+c+"</span>";i[0].index!=0?n.push('<div class="div-contain"><div class="divider"></div></div><dt>'+l+"</dt>"+t.createA(u,"opt "+s,o)):n.push("<dt>"+l+"</dt>"+t.createA(u,"opt "+s,o))}else n.push(t.createA(u,"opt "+s,o));else i.data("divider")==1?n.push('<div class="div-contain"><div class="divider"></div></div>'):e(this).data("hidden")==1?n.push(""):n.push(t.createA(u,s,o))});e.each(n,function(e,t){r+="<li rel="+e+">"+t+"</li>"});!this.multiple&&this.$element.find("option:selected").length==0&&!this.options.title&&this.$element.find("option").eq(0).prop("selected",!0).attr("selected","selected");return e(r)},createA:function(e,t,n){return'<a tabindex="0" class="'+t+'" style="'+n+'">'+e+'<i class="glyphicon glyphicon-ok icon-ok check-mark"></i>'+"</a>"},render:function(){var t=this;this.$element.find("option").each(function(n){t.setDisabled(n,e(this).is(":disabled")||e(this).parent().is(":disabled"));t.setSelected(n,e(this).is(":selected"))});var n=this.$element.find("option:selected").map(function(n,r){var i=e(this),s=i.data("icon")&&t.options.showIcon?'<i class="glyphicon '+i.data("icon")+'"></i> ':"",o;t.options.showSubtext&&i.attr("data-subtext")&&!t.multiple?o=' <small class="muted">'+i.data("subtext")+"</small>":o="";return i.data("content")&&t.options.showContent?i.data("content"):i.attr("title")!=undefined?i.attr("title"):s+i.html()+o}).toArray(),r=this.multiple?n.join(", "):n[0];if(this.multiple&&this.options.selectedTextFormat.indexOf("count")>-1){var i=this.options.selectedTextFormat.split(">"),s=this.options.hideDisabled?":not([disabled])":"";if(i.length>1&&n.length>i[1]||i.length==1&&n.length>=2)r=this.options.countSelectedText.replace("{0}",n.length).replace("{1}",this.$element.find('option:not([data-divider="true"]):not([data-hidden="true"])'+s).length);if(this.multiple&&this.options.selectedTextFormat.indexOf("countall")>-1){var o=this.$element.find('option:not([data-divider="true"]):not([data-hidden="true"])'+s).length,u=n.length;u>=o&&(r="All")}}else if(this.multiple&&this.options.selectedTextFormat.indexOf("comma")>-1){var s=this.options.hideDisabled?":not([disabled])":"",o=this.$element.find('option:not([data-divider="true"]):not([data-hidden="true"])'+s).length,u=n.length;u>=o&&(r="All")}this.options.keeptitle===!0&&(r=this.options.initialtitle);r||(r=this.options.title!=undefined?this.options.title:this.options.noneSelectedText);this.$newElement.find(".filter-option").html(r)},setStyle:function(e,t){this.$element.attr("class")&&this.$newElement.addClass(this.$element.attr("class").replace(/selectpicker|mobile-device/gi,""));var n=e?e:this.options.style;if(t=="add")this.$button.addClass(n);else if(t=="remove")this.$button.removeClass(n);else{this.$button.removeClass(this.options.style);this.$button.addClass(n)}},liHeight:function(){var e=this.$newElement.clone();e.appendTo("body");var t=e.addClass("open").find("> .dropdown-menu"),n=t.find("li > a").outerHeight(),r=this.options.header?t.find(".popover-title").outerHeight():0;e.remove();this.$newElement.data("liHeight",n).data("headerHeight",r)},setSize:function(){var t=this,n=this.$menu,r=n.find(".inner"),i=r.find("li > a"),s=this.$newElement.outerHeight(),o=this.$newElement.data("liHeight"),u=this.$newElement.data("headerHeight"),a=n.find("li .divider").outerHeight(!0),f=parseInt(n.css("padding-top"))+parseInt(n.css("padding-bottom"))+parseInt(n.css("border-top-width"))+parseInt(n.css("border-bottom-width")),l=this.options.hideDisabled?":not(.disabled)":"",c=e(window),h=f+parseInt(n.css("margin-top"))+parseInt(n.css("margin-bottom"))+2,p,d,v,m=function(){d=t.$newElement.offset().top-c.scrollTop();v=c.height()-d-s};m();this.options.header&&n.css("padding-top",0);if(this.options.size=="auto"){var g=function(){var e;m();p=v-h;t.$newElement.toggleClass("dropup",d>v&&p-h<n.height()&&t.options.dropupAuto);t.$newElement.hasClass("dropup")&&(p=d-h);n.find("li").length+n.find("dt").length>3?e=o*3+h-2:e=0;n.css({"max-height":p+"px",overflow:"hidden","min-height":e+"px"});r.css({"max-height":p-u-f+"px","overflow-y":"auto","min-height":e-f+"px"})};g();e(window).resize(g);e(window).scroll(g)}else if(this.options.size&&this.options.size!="auto"&&n.find("li"+l).length>this.options.size){var y=n.find("li"+l+" > *").filter(":not(.div-contain)").slice(0,this.options.size).last().parent().index(),b=n.find("li").slice(0,y+1).find(".div-contain").length;p=o*this.options.size+b*a+f;this.$newElement.toggleClass("dropup",d>v&&p<n.height()&&this.options.dropupAuto);n.css({"max-height":p+u+"px",overflow:"hidden"});r.css({"max-height":p-f+"px","overflow-y":"auto"})}},setWidth:function(){if(this.options.width=="auto"){this.$menu.css("min-width","0");var e=this.$newElement.clone().appendTo("body"),t=e.find("> .dropdown-menu").css("width");e.remove();this.$newElement.css("width",t)}else if(this.options.width=="fit"){this.$menu.css("min-width","");this.$newElement.css("width","").addClass("fit-width")}else if(this.options.width){this.$menu.css("min-width","");this.$newElement.css("width",this.options.width)}else{this.$menu.css("min-width","");this.$newElement.css("width","")}this.$newElement.hasClass("fit-width")&&this.options.width!=="fit"&&this.$newElement.removeClass("fit-width")},selectPosition:function(){var t=this,n="<div />",r=e(n),i,s,o=function(e){r.addClass(e.attr("class")).toggleClass("dropup",e.hasClass("dropup"));i=e.offset();s=e.hasClass("dropup")?0:e[0].offsetHeight;r.css({top:i.top+s,left:i.left,width:e[0].offsetWidth,position:"absolute"})};this.$newElement.on("click",function(n){o(e(this));r.appendTo(t.options.container);r.toggleClass("open",!e(this).hasClass("open"));r.append(t.$menu)});e(window).resize(function(){o(t.$newElement)});e(window).on("scroll",function(e){o(t.$newElement)});e("html").on("click",function(n){e(n.target).closest(t.$newElement).length<1&&r.removeClass("open")})},mobile:function(){this.$element.addClass("mobile-device").appendTo(this.$newElement);this.options.container&&this.$menu.hide()},refresh:function(){this.reloadLi();this.render();this.setWidth();this.setStyle();this.checkDisabled();this.liHeight()},setSelected:function(e,t){this.$menu.find("li").eq(e).toggleClass("selected",t)},setDisabled:function(e,t){t?this.$menu.find("li").eq(e).addClass("disabled").find("a").attr("href","#").attr("tabindex",-1):this.$menu.find("li").eq(e).removeClass("disabled").find("a").removeAttr("href").attr("tabindex",0)},isDisabled:function(){return this.$element.is(":disabled")},checkDisabled:function(){var e=this;if(this.isDisabled()){this.$button.addClass("disabled");this.$button.attr("tabindex","-1")}else if(this.$button.hasClass("disabled")){this.$button.removeClass("disabled");this.$button.removeAttr("tabindex")}this.$button.click(function(){return!e.isDisabled()})},checkTabIndex:function(){if(this.$element.is("[tabindex]")){var e=this.$element.attr("tabindex");this.$button.attr("tabindex",e)}},clickListener:function(){var t=this;e("body").on("touchstart.dropdown",".dropdown-menu",function(e){e.stopPropagation()});this.$newElement.on("click",function(){t.setSize()});this.$menu.on("click",".selectAll a",e.proxy(function(e){if(e.target==e.currentTarget){switch(e.target.className){case"all":this.selectAll();break;case"none":this.deselectAll();break;case"inv":this.selectInverse();break;default:}e.preventDefault();e.stopPropagation();this.$button.focus()}},this));this.$menu.on("click","li a",function(n){var r=e(this).parent().index(),i=e(this).parent(),s=t.$element.val();t.multiple&&n.stopPropagation();n.preventDefault();if(!t.isDisabled()&&!e(this).parent().hasClass("disabled")){var o=t.$element.find("option"),u=o.eq(r);if(!t.multiple){o.prop("selected",!1);u.prop("selected",!0)}else{var a=u.prop("selected");u.prop("selected",!a)}t.$button.focus();s!=t.$element.val()&&t.$element.change()}});this.$menu.on("click","li.disabled a, li dt, li .div-contain, h3.popover-title",function(e){if(e.target==this){e.preventDefault();e.stopPropagation();t.$button.focus()}});this.$searchbox.on("click",function(e){e.stopPropagation()});this.$element.change(function(){t.render()})},liveSearchListener:function(){var e=this;this.$newElement.on("click.dropdown.data-api",function(t){e.options.liveSearch&&setTimeout(function(){e.$searchbox.focus()},10)});this.$searchbox.on("input",function(){e.$newElement.find("li").show().not(":icontains("+e.$searchbox.val()+")").hide()})},val:function(e){if(e!=undefined){this.$element.val(e);this.$element.change();return this.$element}return this.$element.val()},selectAll:function(){this.$element.find("option").prop("selected",!0).attr("selected","selected");this.render()},deselectAll:function(){this.$element.find("option:not(:disabled)").prop("selected",!1).removeAttr("selected");this.render()},selectInverse:function(){this.$element.find("option:not(:disabled)").each(function(t,n){e(this).prop("selected",!e(this).prop("selected"))});this.render()},keydown:function(t){var n,r,i,s,o,u,a,f,l,c;n=e(this);i=n.parent();c=i.data("this");c.options.container&&(i=c.$menu);r=e("[role=menu] li:not(.divider):visible a",i);if(!r.length)return;if(/(38|40)/.test(t.keyCode)){s=r.index(r.filter(":focus"));u=r.parent(":not(.disabled)").first().index();a=r.parent(":not(.disabled)").last().index();o=r.eq(s).parent().nextAll(":not(.disabled)").eq(0).index();f=r.eq(s).parent().prevAll(":not(.disabled)").eq(0).index();l=r.eq(o).parent().prevAll(":not(.disabled)").eq(0).index();if(t.keyCode==38){s!=l&&s>f&&(s=f);s<u&&(s=u)}if(t.keyCode==40){s!=l&&s<o&&(s=o);s>a&&(s=a);s==-1&&(s=0)}r.eq(s).focus()}else{var h={48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",59:";",65:"a",66:"b",67:"c",68:"d",69:"e",70:"f",71:"g",72:"h",73:"i",74:"j",75:"k",76:"l",77:"m",78:"n",79:"o",80:"p",81:"q",82:"r",83:"s",84:"t",85:"u",86:"v",87:"w",88:"x",89:"y",90:"z",96:"0",97:"1",98:"2",99:"3",100:"4",101:"5",102:"6",103:"7",104:"8",105:"9"},p=[];r.each(function(){e(this).parent().is(":not(.disabled)")&&e.trim(e(this).text().toLowerCase()).substring(0,1)==h[t.keyCode]&&p.push(e(this).parent().index())});var d=e(document).data("keycount");d++;e(document).data("keycount",d);var v=e.trim(e(":focus").text().toLowerCase()).substring(0,1);if(v!=h[t.keyCode]){d=1;e(document).data("keycount",d)}else d>=p.length&&e(document).data("keycount",0);r.eq(p[d-1]).focus()}if(/(13|32)/.test(t.keyCode)){t.preventDefault();e(":focus").click();e(document).data("keycount",0)}},hide:function(){this.$newElement.hide()},show:function(){this.$newElement.show()},destroy:function(){this.$newElement.remove();this.$element.remove()}};e.fn.selectpicker=function(n,r){var i=arguments,s,o=this.each(function(){if(e(this).is("select")){var o=e(this),u=o.data("selectpicker"),a=typeof n=="object"&&n;if(!u)o.data("selectpicker",u=new t(this,a,r));else if(a)for(var f in a)u.options[f]=a[f];if(typeof n=="string"){var l=n;if(u[l]instanceof Function){[].shift.apply(i);s=u[l].apply(u,i)}else s=u.options[l]}}});return s!=undefined?s:o};e.fn.selectpicker.defaults={style:"btn-default",size:"auto",title:null,selectedTextFormat:"values",noneSelectedText:"Nothing selected",countSelectedText:"{0} of {1} selected",selectOptionsText:{all:"All",none:"None",inv:"Inverse"},width:!1,container:!1,hideDisabled:!1,showSubtext:!1,showSelectOptions:!1,showIcon:!0,showContent:!0,dropupAuto:!0,header:!1,liveSearch:!1};e(document).data("keycount",0).on("keydown","[data-toggle=dropdown], [role=menu]",t.prototype.keydown)}(window.jQuery);