-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.fancyselect.min.js
23 lines (22 loc) · 4.66 KB
/
jquery.fancyselect.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*!
* jQuery.FancySelect()
*
* @author Christian Jung <[email protected]>
* @license CC-BY-NC-SA
*
* Date: Wed Mar 30 20:18:25 2011 +0000
*
* Tutorial for developing jQuery plugins
* http://www.learningjquery.com/2007/10/a-plugin-development-pattern
*
*/
var searchString="";
(function(a){a.fn.FancySelect=function(c){opts=a.extend({},a.fn.FancySelect.defaults,c);return this.each(function(){var b=a(this);b.wrap('<div class="select_replace" id="'+b.attr("name")+'_dropdown"></div>').hide();var d=b.parents(".select_replace");d.append('<div class="display">'+b.find("option:selected").text()+"</div>");d.click(function(){a("div.options:visible").length>0?a.fn.FancySelect.closeList(d):a.fn.FancySelect.openList(d)})})};a.fn.extend({setVal:function(c){a(this).val(c).trigger("change");a(this).parents(".select_replace").find(".display").text(a(this).find(":selected").text())},
update:function(){a(this).find("option:selected").length>0?a(this).parents(".select_replace").find(".display").text(a(this).find("option:selected").text()):a(this).parents(".select_replace").find(".display").text(a(this).find("option:first").text())}});a.fn.FancySelect.openList=function(c){a.fn.FancySelect.closeList();var b=a(c);b.css({zIndex:9999});var d=b.find("select"),e=b.find("select optgroup"),i=e.length,f=b.find("select option"),h=f.length,g=b.find(".options").length;if(h>0&&g===0){b.append('<div class="options"></div>');
b=b.find(".options").empty();for(g=0;g<i;g++){b.append('<div class="group"></div>');var k=b.find("div.group:last");a(e[g]).attr("label")!==""&&k.append("<strong>"+a(e[g]).attr("label")+"</strong>");var l=a(e[g]).find("option");a.fn.FancySelect.renderOptions(l,l.length,k)}i===0&&a.fn.FancySelect.renderOptions(f,h,b)}else if(b.find("div.options:hidden").length)b.find("div.options").show();else{a.fn.FancySelect.closeList();return}e=a("div.options");e.css({overflow:"auto",height:a(c).find("li").outerHeight()*
opts.maxEntries+"px"});e.find("li").removeClass("entry_selected");$options_actual=e.find('li[clickvalue="'+d.val()+'"]');$options_actual.addClass("entry_selected");c=$options_actual;for(d=0;c.hasClass("options")===false;){d+=c.position().top;c=c.parent()}d>=e.outerHeight()&&e.scrollTop(d);a(document).keydown(function(j){a.fn.FancySelect.findEntryByKey(j)});a("body").click(function(j){a(j.target).parents("div.select_replace").length===0&&!a(j.target).hasClass("select_replace")&&a("div.options:visible").length>
0&&a.fn.FancySelect.closeList()})};a.fn.FancySelect.renderOptions=function(c,b,d){var e;$ul=a("<ul/>");for(e=0;e<b;e++){var i=a("<li/>",{clickvalue:a(c[e]).attr("value"),text:a(c[e]).text(),mouseenter:function(){var f=a(this);f.addClass("entry_hover");var h=f.parents("div.options").offset().top,g=h+f.parents("div.options").innerHeight();for(f.offset().top+f.outerHeight()>g&&a("div.options:visible").scrollTop(a("div.options:visible").scrollTop()+f.outerHeight());f.offset().top<h;)a("div.options:visible").scrollTop(a("div.options:visible").scrollTop()-
f.outerHeight())},mouseleave:function(){a(this).removeClass("entry_hover")},click:function(f){a.fn.FancySelect.clickEntry(a(this));f.stopPropagation()}});$ul.append(i)}a(d).append($ul)};a.fn.FancySelect.clickEntry=function(c){var b=a(c).parents(".select_replace");b.find(".display").text(a(c).text());b.find("select").val(a(c).attr("clickvalue"));b.find("select").trigger("change");a.fn.FancySelect.closeList()};a.fn.FancySelect.closeList=function(c){a(c).add("div.select_replace").css({zIndex:0});opts.otfRendering?
a("div.options:visible").remove():a("div.options:visible").hide();a(document).unbind("keypress");a("body").unbind("click")};a.fn.FancySelect.findEntryByKey=function(c){var b="",d;b=c.which;try{window.clearTimeout(clearString)}catch(e){}if(b==32||b>=48&&b<=122){d=String.fromCharCode(b);searchString+=d;clearString=window.setTimeout('searchString="";',opts.searchResetTime);d=a("div.options:visible li").filter(function(){return(new RegExp("^"+searchString,"i")).test(a(this).text())});d.parents("ul").find("li").trigger("mouseleave");
d.first().trigger("mouseenter")}if(c.keyCode==38||c.keyCode==40){c.preventDefault();$this=a("div.options:visible li.entry_hover");if($this.length===0)$this=a("div.options:visible li.entry_selected");if(c.keyCode==38){$new=$this.prev();if($new.length===0)$new=$this.parents("div.group").prev().find("li").last()}else{$new=$this.next();if($new.length===0)$new=$this.parents("div.group").next().find("li").first()}if($new.length>0){$this.trigger("mouseleave");$new.trigger("mouseenter")}}if(b==13){a("div.options:visible li.entry_hover").trigger("click");
a("div.options:visible li.entry_selected").trigger("click")}};a.fn.FancySelect.defaults={maxEntries:"8",searchResetTime:600,otfRendering:true}})(jQuery);