-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.shCheckset-min.js
9 lines (9 loc) · 3.34 KB
/
jquery.shCheckset-min.js
1
2
3
4
5
6
7
8
9
/*!
* jQuery shCheckset v1.2
* http://jquery.sunhater.com/shCheckset
* 2014-02-02
*
* Copyright (c) 2010-2014 Pavel Tzonkov <sunhater@sunhater.com>
* Dual licensed under the MIT and GPL licenses.
*/
(function(b){var a=0;b.fn.shCheckset=function(e){var g={namespace:"shcs",search:false,uniform:true,labels:{search:"Search..."},widthOffset:0},d=function(c,h,j){try{c.add(new Option(j,h),null)}catch(i){c.add(new Option(j,h))}};b.extend(g,e);if(!a){var f=b("<div></div>").css({width:100,height:100,overflow:"auto",position:"absolute",top:-1000,left:-1000}).prependTo("body").append("<div></div>").find("div").css({width:"100%",height:200});a=100-f.width();f.parent().remove()}this.each(function(){var l=this;if(!b(l).is("select")||!l.length){return}b(l).after('<div class="'+g.namespace+'"></div>');var o=b(l).next().get(0),h=b(l).attr("name"),c={width:l.style.width?l.style.width:b(l).outerWidth()+"px",height:l.style.height?l.style.height:b(l).outerHeight()+"px"};if(!h){h=g.namespace+"[]"}else{if(h.substr(h.length-2,2)!="[]"){h+="[]"}}b(o).html((g.search?'<input type="text" placeholder="'+g.labels.search+'" />':"")+"<div></div>");var k=b(o).find('input[type="text"]'),n=0,j=0,m=b(o).find("div");m.css({overflow:"auto",height:parseInt(c.height)-k.outerHeight(true)-parseInt(m.css("marginTop"))-parseInt(m.css("marginBottom"))-parseInt(m.css("paddingTop"))-parseInt(m.css("paddingBottom"))-parseInt(m.css("borderTopWidth"))-parseInt(m.css("borderBottomWidth"))+"px"});b(l).find("option").each(function(){var p,r,i,s,q=this;do{s=g.namespace+"_"+h.substr(0,h.length-2)+"_"+j++}while(b("#"+s).get(0));b(o).find("div").append("<label></label>");p=b(o).find("label").last();p.attr("for",s).html('<input type="checkbox" /><span>'+q.text+"</span>").css({whitespace:"nowrap","float":"left"});r=p.find("input").first();r.attr({name:h,id:s,value:q.value,checked:q.selected});if(q.selected){p.addClass("checked")}if(g.uniform&&b.uniform){r.uniform();k.uniform()}i=p.outerWidth(true);if(n<i){n=i}});b(o).find("label[for]").css({display:"block",whiteSpace:"nowrap","float":"none"}).find("label").detach();b(o).find('input[type="checkbox"]').click(function(){var p=this,i=b(p).parents("label[for]").first(),q=b(p).val();b(l).find('option[value="'+q+'"]').get(0).selected=p.checked;if(p.checked){i.addClass("checked")}else{i.removeClass("checked")}if(document.selection){document.selection.empty()}else{if(window.getSelection){window.getSelection().removeAllRanges()}}});if(!l.style.width){c.width=n+a+g.widthOffset+parseInt(m.css("marginLeft"))+parseInt(m.css("marginRight"))+parseInt(m.css("paddingLeft"))+parseInt(m.css("paddingRight"))+parseInt(m.css("borderLeftWidth"))+parseInt(m.css("borderRightWidth"))}b(o).css(c);k.attr({spellcheck:false}).css({width:parseInt(c.width)-(document.doctype?(parseInt(k.css("borderRightWidth"))+parseInt(k.css("borderLeftWidth"))+parseInt(k.css("paddingRight"))+parseInt(k.css("paddingLeft"))+parseInt(k.css("marginRight"))+parseInt(k.css("marginLeft"))):0)+"px"}).focus(function(){m.addClass("focus")}).blur(function(){m.removeClass("focus")}).keyup(function(){var p=b(this).val(),i=(p.substr(p.length-1,1)==" ");p=p.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s+/g," ");b(this).val(p+(i?" ":""));b(o).find("label").each(function(q){b(this).css("display",(p.length&&(b(this).find("span").text().toLowerCase().indexOf(p.toLowerCase())==-1))?"none":"block")})});b(l).detach()})}})(jQuery);