forked from scottdarby/Stylish-Select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.stylish-select.min.js
1 lines (1 loc) · 4.96 KB
/
jquery.stylish-select.min.js
1
(function(a){a("html").addClass("stylish-select");a.fn.extend({getSetSSValue:function(b){if(b){a(this).val(b).change();return this}else{return a(this).find(":selected").val()}},resetSS:function(){var b=a(this).data("ssOpts");$this=a(this);$this.next().remove();$this.unbind(".sSelect").sSelect(b)}});a.fn.sSelect=function(b){return this.each(function(){var l={defaultText:"Please select",animationSpeed:0,ddMaxHeight:"",containerClass:"",onCreate:a.noop,onChange:a.noop};var o=a.extend(l,b),f=a(this),m=a('<div class="selectedTxt"></div>'),u=a('<div class="newListSelected '+o.containerClass+(f.is(":disabled")?" newListDisabled":"")+'"></div>'),k=a('<div class="SSContainerDivWrapper" style="visibility:hidden;"></div>'),C=a('<ul class="newList"></ul>'),e=-1,j=-1,p=[],z=false,x=false,A;a(this).data("ssOpts",b);if(a(this).next(".newListSelected").length){return}u.insertAfter(f);u.attr("tabindex",f.attr("tabindex")||"0");m.prependTo(u);C.appendTo(u);C.wrap(k);k=C.parent();f.hide();m.data("ssReRender",!m.is(":visible"));function t(H,E){var G=a(H).text(),F=a(H).val(),D=a(H).is(":disabled");if(!D&&!a(H).parents().is(":disabled")){p.push(G.charAt(0).toLowerCase())}E.append(a("<li><a"+(D?' class="newListItemDisabled"':"")+' href="JavaScript:void(0);">'+G+"</a></li>").data({key:F,selected:a(H).is(":selected")}))}f.children().each(function(){if(a(this).is("option")){t(this,C)}else{var D=a(this).attr("label"),F=a('<li class="newListOptionTitle '+(a(this).is(":disabled")?"newListOptionDisabled":"")+'">'+D+"</li>"),E=a("<ul></ul>");F.appendTo(C);E.appendTo(F);a(this).children().each(function(){t(this,E)})}});A=C.find("li a:not(.newListItemDisabled)").not(function(){return a(this).parents().hasClass("newListOptionDisabled")});A.each(function(D){if(a(this).parent().data("selected")){o.defaultText=a(this).html();e=j=D}});var r=C.height(),q=u.height(),B=A.length;if(e!=-1){i(e)}else{m.text(o.defaultText)}o.onCreate(f,m,C);function s(){var E=u.offset().top,D=a(window).height(),F=a(window).scrollTop();if(r>parseInt(o.ddMaxHeight)){r=parseInt(o.ddMaxHeight)}E=E-F;if(E+r>=D){C.css({height:r});k.css({top:"-"+r+"px",height:r});f.onTop=true}else{C.css({height:r});k.css({top:q+"px",height:r});f.onTop=false}}s();a(window).bind("resize.sSelect scroll.sSelect",s);function v(){u.css("position","relative")}function d(){u.css("position","static")}m.bind("click.sSelect",function(E){E.stopPropagation();if(a(this).data("ssReRender")){k.height("").show();r=C.height("").height();q=u.height();k.hide();a(this).data("ssReRender",false);s()}if(f.is(":disabled")){return}a(".SSContainerDivWrapper").not(a(this).next()).hide().parent().css("position","static").removeClass("newListSelFocus newListSelActive");k.toggle();u.toggleClass("newListSelActive");v();if(e==-1){e=0}try{A.eq(e).focus()}catch(D){}});function c(E,D){if(E==true){j=e;f.change()}if(D==true){e=j;i(e)}k.hide();u.removeClass("newListSelActive");d()}A.bind("click.sSelect",function(E){E.preventDefault();var D=a(E.target);e=A.index(D);x=true;i(e,true);c()});A.bind("mouseenter.sSelect",function(E){var D=a(E.target);D.addClass("newListHover")}).bind("mouseleave.sSelect",function(E){var D=a(E.target);D.removeClass("newListHover")});function i(D,H){if(D==-1){m.text(o.defaultText);A.removeClass("hiLite")}else{A.removeClass("hiLite").eq(D).addClass("hiLite");var G=A.eq(D).text(),F=A.eq(D).parent().data("key");try{f.val(F)}catch(E){f[0].selectedIndex=D}m.text(G);if(H==true){j=D;f.change()}if(k.is(":visible")){try{A.eq(D).focus()}catch(E){}}}o.onChange(f,m,A.eq(D))}f.bind("change.sSelect",function(E){var D=a(E.target);if(x==true){x=false;return false}var F=D.find(":selected");e=D.find("option").index(F);i(e)});function w(){if(e<(B-1)){++e;i(e)}}function n(){if(e>0){--e;i(e)}}function h(){e=0;i(e)}function g(){e=B-1;i(e)}if(!f.is(":disabled")){u.bind("focus.sSelect",function(){a(this).addClass("newListSelFocus")});u.bind("blur.sSelect",function(){a(this).removeClass("newListSelFocus")})}u.bind("keydown.sSelect",function(F){var E=F.which;x=true;switch(E){case 40:case 39:w();return false;break;case 38:case 37:n();return false;break;case 33:case 36:h();return false;break;case 34:case 35:g();return false;break;case 13:case 27:c(true);return false;break;case 9:c(true);y();return false;break}keyPressed=String.fromCharCode(E).toLowerCase();var D=p.indexOf(keyPressed);if(typeof D!="undefined"&&D>=0){++e;e=p.indexOf(keyPressed,e);if(e==-1||e==null||z!=keyPressed){e=p.indexOf(keyPressed)}i(e);z=keyPressed;return false}});a(document).bind("click.sSelect",function(){u.removeClass("newListSelFocus");if(k.is(":visible")){c(false,true)}else{c(false)}});function y(){var D=a("body").find("button,input,textarea,select"),E=D.index(f);if(E>-1&&(E+1)<D.length){D.eq(E+1).focus()}return false}f.focus(function(){f.next().focus()});if(!f.is(":disabled")){m.bind("mouseenter.sSelect",function(E){var D=a(E.target);D.parent().addClass("newListSelHover")}).bind("mouseleave.sSelect",function(E){var D=a(E.target);D.parent().removeClass("newListSelHover")})}k.css({left:"0",display:"none",visibility:"visible"})})}})(jQuery);