-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcustom-select-menu.jquery.min.js
1 lines (1 loc) · 2.66 KB
/
custom-select-menu.jquery.min.js
1
(function(a){a.fn.customSelectMenu=function(b){var d=a.extend({menuClass:"custom-select-menu",openedClass:"opened",selectedClass:"selected",selectionMadeClass:"selection-made"},b);function c(h){var f=h.parent().attr("data-select-name"),e=h.attr("data-option-value"),i=h.text(),g=a('input[name="'+f+'"]');h.parent().find("."+d.selectedClass).removeClass(d.selectedClass);h.addClass(d.selectedClass);g.val(e);h.parent().parent().find("label").text(i);if(g.val()!==""){h.parent().parent().find("label").addClass(d.selectionMadeClass)}else{h.parent().parent().find("label").removeClass(d.selectionMadeClass)}h.parent().hide();if(h.parent().css("display")!=="none"){h.parent().parent().find("label").toggleClass(d.openedClass)}else{h.parent().parent().find("label").removeClass(d.openedClass)}}return this.each(function(){var n=a(this),i=n.attr("name"),h=n.attr("id"),o,e,f,k,j,m,l,g;n.hide();e=a('<div class="'+d.menuClass+'">').attr("tabindex",0);if(h){e.attr("id",h);n.removeAttr("id")}o=a('<input type="hidden" name="'+i+'" value="" />');if(n.find(":selected")){l=n.find(":selected");g=l.attr("value");k=l.text();if(!g){f=a("<label>"+k+"</label>")}else{f=a('<label class="'+d.selectionMadeClass+'">'+k+"</label>");o.val(g)}}else{k=n.find(":first").text();f=a("<label>"+k+"</label>")}j=a('<ul data-select-name="'+i+'">').hide();n.after(e.append(f,j,o));n.find("option").each(function(){var p=a(this).text(),r=a(this).attr("value"),q=(p===k)?' class="'+d.selectedClass+'"':"";if(!r){m=a("<li"+q+">"+p+"</li>")}else{m=a('<li data-option-value="'+r+'"'+q+">"+p+"</li>")}j.append(m)});e.on("click",function(p){var q=a(p.target);if(q.is("label")){f.toggleClass(d.openedClass);f.parent().find("ul").toggle()}if(q.is("li")){c(q)}});e.on("keyup",function(s){if(s.keyCode===38||s.keyCode===40){a(this).find(f).addClass(d.openedClass);a(this).find(j).show()}var p=a(this).find("li"),q=a(this).find("."+d.selectedClass),r="",t="";if(s.keyCode===40){q.removeClass(d.selectedClass);r=q.next();if(r.length>0){r.addClass(d.selectedClass)}else{p.first().addClass(d.selectedClass)}}if(s.keyCode===38){q.removeClass(d.selectedClass);t=q.prev();if(t.length>0){t.addClass(d.selectedClass)}else{p.last().addClass(d.selectedClass)}}if(s.keyCode===13){c(a(this).find("."+d.selectedClass))}});e.on("blur",function(){if(a(this).find(j).is(":visible")){a(this).find(f).removeClass(d.openedClass);a(this).find(j).hide()}});a("html").on("keyup",function(p){if(p.keyCode===27){f.removeClass(d.openedClass);j.hide()}});a("html").on("mousedown",function(p){var q=a(p.target);if(!q.parents().addBack().is("."+d.menuClass+" ul, ."+d.menuClass+" label")){a("."+d.menuClass+" label").removeClass(d.openedClass);a("."+d.menuClass+" ul").hide()}})})}})(jQuery);