forked from pioul/jQuery.SimpleSelect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.simpleselect.min.js
8 lines (8 loc) · 5.14 KB
/
jquery.simpleselect.min.js
1
2
3
4
5
6
7
8
/*
* jQuery Simple Select
* http://pioul.fr/jquery-simpleselect
*
* Copyright 2013, Philippe Masset
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function(a){a.fn.simpleselect=function(b){var c=a(this);if("object"!=typeof b&&b){var d=Array.prototype.slice.call(arguments,1);c.each(function(){var c=a(this);"function"==typeof c.data("simpleselectref")[b]&&c.data("simpleselectref")[b](d)})}else options=a.extend({},{fadeSpeed:0},b),c.each(function(){var b=a(this);b.addClass("simpleselected");var c=a('<div class="simpleselect"></div>'),d=a('<div class="placeholder"></div>').appendTo(c);b.is("[id]")&&c.attr("id","simpleselect_"+b.attr("id"));var e={ref:b,simpleselect:c,can_be_closed:!0,set_active:function(){if(!this.simpleselect.is(".active")&&!this.ref.prop("disabled")){this.last_value=this.ref.val(),this.simpleselect.addClass("active");var b=this.get_option_to_activate();this.set_option_active(b);var c=this.simpleselect.children(".options"),d=a(document).height();c.fadeTo(0,0),c.fadeTo(options.fadeSpeed,1),this.ref.is(":focus")||this.ref.focus(),this.position_around_active_option(b,d),this.simpleselect.hide().show(0),a(document).bind("click.simpleselect",function(){var b=a(".simpleselect.active");b.each(function(){a(this).data("simpleselect").set_inactive()}),a(document).unbind(".simpleselect")})}},set_inactive:function(){this.simpleselect.is(".active")&&(this.simpleselect.removeClass("active").children(".options").fadeOut(options.fadeSpeed),this.ref.is(":focus")&&this.ref.blur(),this.last_value!=this.ref.val()&&this.ref.trigger("change.simpleselect"))},set_option_active:function(a){this.simpleselect.find(".option").removeClass("active"),a.addClass("active")},get_option_to_activate:function(){var a=this.ref.data("simpleselectref").get_index_selected();return this.simpleselect.find(".option:eq("+a+")")},get_option_active:function(){return this.simpleselect.find(".option.active")},choose_option:function(a){var b=this.ref.find("option"),c=b.filter(":eq("+this.simpleselect.find(".options .option").index(a)+")"),e=(b.index(c),c.val());this.ref.val(e),this.simpleselect.children(".placeholder").text(c.text())},position_around_active_option:function(b,c){var d=this.simpleselect.children(".options");d.css({height:"auto","overflow-y":"visible"});var e=simpleoptions_future_height=d.height(),f=this.simpleselect.offset(),g=this.simpleselect.outerHeight(),h=b.position(),i=f.top,j=c-f.top-g,k=j>i?i:j,l=(a(window).height()-g)/2;k=k>l?l:k;var m=!1;if(e>k){var n=k-g/2,o=-n,p=n,q=n;p>h.top?(o+=p-h.top,p=h.top):(h.bot=e-h.top-g,p>h.bot&&(o=-p,p=h.bot));var r=h.top-p;0>r&&(r=0),simpleoptions_future_height=g+p+q,d.css({top:o+"px"}),e>simpleoptions_future_height&&(m=!0,d.css({height:simpleoptions_future_height+"px","overflow-y":"scroll"}),setTimeout(function(){d.scrollTop(r)},0),d.addClass("scrollable"))}m||(d.css({top:"-"+h.top+"px"}),d.removeClass("scrollable"))}};c.data("simpleselect",e).bind({mousedown:function(){a(this).data("simpleselect").can_be_closed=!1},click:function(b){b.stopPropagation();var c=a(this).data("simpleselect");c.set_active(),a(b.target).is(".option")&&(c.choose_option(a(b.target)),c.set_inactive())},mouseup:function(){a(this).data("simpleselect").can_be_closed=!0},mouseover:function(b){a(b.target).is(".option")&&a(this).data("simpleselect").set_option_active(a(b.target))}});var f={ref:b,simpleselect:c,get_index_selected:function(){var a=this.ref.find("option"),b=a.filter(":selected");return b.length>0?a.index(b.first()):a.index(a.first())},disable:function(){this.ref.prop("disabled",!0),this.simpleselect.addClass("disabled")},enable:function(){this.ref.prop("disabled",!1),this.simpleselect.removeClass("disabled")}};b.data("simpleselectref",f).bind({keydown:function(b){var c=a(this).data("simpleselectref"),d=c.simpleselect.data("simpleselect");if(38==b.keyCode||40==b.keyCode){b.preventDefault();var e=c.simpleselect.find(".options .option"),f=e.index(d.get_option_active()),g=e.index(e.last()),h=!1;if(38==b.keyCode&&f>0?h=f-1:40==b.keyCode&&g>f&&(h=f+1),h!==!1){var i=e.eq(h),j=i.position();d.set_option_active(i),d.choose_option(i);var k=c.simpleselect.children(".options");k.is(".scrollable")&&k.scrollTop(j.top+k.scrollTop())}}else(13==b.keyCode||9==b.keyCode)&&(d.choose_option(d.get_option_active()),13==b.keyCode&&(b.preventDefault(),d.set_inactive(),a(this).trigger("pressedEnter.simpleselect")))},focus:function(){a(this).data("simpleselectref").simpleselect.data("simpleselect").set_active()},blur:function(){var b=a(this).data("simpleselectref").simpleselect.data("simpleselect");b.can_be_closed&&b.set_inactive()}}),f.ref.prop("disabled")&&f.disable();var g='<div class="options">',h=function(a){g+='<div class="option">'+a.text()+"</div>"},i=b.children("optgroup, option");i.filter("optgroup").length&&c.addClass("has-optgroup"),i.each(function(){var b=a(this);if(b.is("optgroup")){g+='<div class="optgroup">';var c=b.attr("label");c&&(g+='<div class="optgroup-label">'+c+"</div>"),b.children("option").each(function(){h(a(this))}),g+="</div>"}else h(b)}),g+="</div>",c.append(g),d.text(c.data("simpleselect").get_option_to_activate().text()),b.after(c);var j=a('<div class="hidden_select_container"></div>');b.after(j).appendTo(j)});return c}})(jQuery);