-
Notifications
You must be signed in to change notification settings - Fork 0
/
replaceDropdown.min.js
1 lines (1 loc) · 3.16 KB
/
replaceDropdown.min.js
1
(function(a){a.fn.replaceDropdown=function(c){var d={init:function(d){var c=this;c.replaceDropdown.settings=a.extend({},c.replaceDropdown.defaults,d);return c.each(function(){var d=a(this),g=this,f=this.id,e=f+c.replaceDropdown.settings.divIDtext,i,h=a("#"+e);c.replaceDropdown.settings.linkText=c.replaceDropdown.settings.linkText||d.find("option").eq(0).text();e=b.buildDropdown(d,g,c);d.css({position:"absolute",left:"-99999px"});b.initEvents(e,c)})}},b={initEvents:function(a,c){b.setEventsForDropdown(a,c);b.assignDropdownLinkEvents(c);b.closeDropdown(a)},buildDropdown:function(d,f,c){for(var g=f.id+c.replaceDropdown.settings.divIDtext,h=f.id+c.replaceDropdown.settings.linkIDtext,b=d.find("option"),e='<div id="'+g+'" class="'+c.replaceDropdown.settings.ddClass+'">\n<a href="#" id="'+h+'">'+c.replaceDropdown.settings.linkText+"</a>\n<ul>\n",a=0;a<b.length;a++){if(c.replaceDropdown.settings.hideFirst&&a===0){b.eq(a).remove();continue}e+='<li><a href="'+b.eq(a).text()+'" rel="'+b.eq(a).val()+'">'+b.eq(a).text()+"</a></li>\n"}e+="</ul>\n</div>\n";d.next("button").remove();d.after(e);return g},setEventsForDropdown:function(c,d){var e=a("#"+c);e.unbind().click(function(g){g.preventDefault();g.stopPropagation();var j=e.attr("id");a("."+d.replaceDropdown.settings.ddClass+".open:not('#"+j+"')").each(function(){b.closeDropdown(this.id)});if(a(this).hasClass("open")===false&&a(this).hasClass("disabled")===false){var i=a(this).find("a.selected").parent().index(),h=25*i;d.replaceDropdown.settings.flipTop&&b.flipDropdown(this,d);var f=a(this).find("ul");f.scrollTop(h);f.css({visibility:"visible"});f.slideDown("fast");a(this).addClass("open");a("body").click(function(){a("body").unbind();b.closeDropdown(c)})}else{b.closeDropdown(c);a("body").unbind()}})},assignDropdownLinkEvents:function(b){a("."+b.replaceDropdown.settings.ddClass+" ul li a").bind("click",function(e){e.preventDefault();var d=a(this).parents("."+b.replaceDropdown.settings.ddClass).attr("id").replace(b.replaceDropdown.settings.divIDtext,""),c=a("#"+d);c.children("option").attr("selected","");c.children("option").eq(a(this).parent().index()).attr("selected","selected");a("."+b.replaceDropdown.settings.ddClass+" ul").slideUp("fast");a(this).parents("."+b.replaceDropdown.settings.ddClass).find("#"+d+b.replaceDropdown.settings.linkIDtext).text(a(this).text());c.change()})},closeDropdown:function(c){var b=a("#"+c);if(b.hasClass("open")){b.children("ul").css({visibility:"hidden"});b.removeClass("open");b.find("a").blur()}},flipDropdown:function(b,e){a(b).find("ul").removeClass("showTop");var c=a(b).find("ul").offset().top,f=a(b).parents(e.replaceDropdown.settings.parent).length?a(b).parents(e.replaceDropdown.settings.parent).offset().top:0,d=a(b).find("ul").height()*1+6,g=a(window).height()*1;g-c<d&&c-f>d&&a(b).find("ul").addClass("showTop")}};if(d[c])return d[c].apply(this,Array.prototype.slice.call(arguments,1));else if(typeof c==="object"||!c)return d.init.apply(this,arguments);else a.error('Method "'+c+'" does not exist in replaceDropdown plugin!')};a.fn.replaceDropdown.defaults={divIDtext:"LinkList",linkIDtext:"Link",linkText:"",parent:"",flipTop:false,ddClass:"ddlist",hideFirst:true};a.fn.replaceDropdown.settings={}})(jQuery)