You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
5.2 KiB
1 lines
5.2 KiB
!function(n){n.fn.niceSelect=function(e){if("string"==typeof e)return"update"==e?this.each(function(e,t){var s=n(this),l=n(this).next(".nice-select"),c=l.hasClass("open");l.length&&(l.remove(),a(s,e),c&&s.next().trigger("click"))}):"destroy"==e?(this.each(function(){var e=n(this),t=n(this).next(".nice-select");t.length&&(t.remove(),e.css("display",""))}),0==n(".nice-select").length&&n(document).off(".nice_select")):console.log('Method "'+e+'" does not exist.'),this;function a(e,t){var s=e.attr("search")?'<div class="nice-select-search-box"><input type="text" class="nice-select-search" placeholder="搜索"/></div>':"";e.after(n("<div></div>").addClass("nice-select").addClass(e.attr("class")||"").addClass(e.attr("disabled")?"disabled":"").addClass(e.attr("multiple")?"has-multiple":"").attr("tabindex",e.attr("disabled")?null:"0").css("zIndex",1001-t).html(s+(e.attr("multiple")?'<span class="multiple-options"></span><ul class="list"></ul><div class="empty-box">暂无匹配结果</div>':'<span class="current"></span><ul class="list"></ul><div class="empty-box">暂无匹配结果</div>')));var c=e.next(),l=e.find("option");if(e.attr("multiple")){var a=e.find("option:selected"),i="";a.each(function(){$selected_option=n(this),$selected_text=$selected_option.data("display")||$selected_option.text(),$selected_option.val()&&(i+='<span class="current">'+$selected_text+"</span>")}),$select_placeholder=e.data("js-placeholder")||e.attr("js-placeholder"),$select_placeholder=$select_placeholder||"Select",console.log($select_placeholder),i=""===i?$select_placeholder:i,c.find(".multiple-options").html(i)}else{a=e.find("option:selected");c.find(".current").html(a.data("display")||a.text())}l.each(function(e){var t=n(this),s=t.data("display"),l=t.data("url")?'<a href="'+t.data("url")+'"> '+t.text()+"</a>":t.text();c.find("ul").append(n("<li></li>").attr("data-value",t.val()).attr("data-display",s||null).addClass("option"+(t.is(":selected")?" selected":"")+(t.is(":disabled")?" disabled":"")).html(l))}),c.find("ul").outerWidth()&&c.css("width",c.find("ul").outerWidth())}this.hide(),this.each(function(e,t){var s=n(this);s.next().hasClass("nice-select")||a(s,e)}),n(document).off(".nice_select"),n(document).on("click.nice_select",".nice-select",function(e){var t=n(this);n(".nice-select").not(t).removeClass("open"),t.toggleClass("open"),t.hasClass("open")?(t.find(".option"),t.find(".nice-select-search").val(""),t.find(".nice-select-search").focus(),t.find(".focus").removeClass("focus"),t.find(".selected").addClass("focus"),t.find("ul li").show(),t.removeClass("empty")):t.focus()}),n(document).on("click",".nice-select-search-box",function(e){return e.stopPropagation(),!1}),n(document).on("keyup.nice-select-search",".nice-select",function(){var e=n(this),t=e.find(".nice-select-search").val(),s=e.find("ul li");if(e.removeClass("empty"),""==t)s.show();else if(e.hasClass("open")){t=t.toLowerCase();var l=new RegExp(t);if(0<s.length){var c=!0;s.each(function(){var e=n(this),t=e.text().toLowerCase(),s=l.test(t);s&&(c=!1),s?e.show():e.hide()}),c&&e.addClass("empty")}else s.show()}e.find(".option"),e.find(".focus").removeClass("focus"),e.find(".selected").addClass("focus")}),n(document).on("click.nice_select",function(e){0===n(e.target).closest(".nice-select").length&&n(".nice-select").removeClass("open").find(".option")}),n(document).on("click.nice_select",".nice-select .option:not(.disabled)",function(e){var t=n(this),s=t.closest(".nice-select");if(s.hasClass("has-multiple"))console.log("clicked",t),t.hasClass("selected")?t.removeClass("selected"):t.addClass("selected"),$selected_html="",$selected_values=[],s.find(".selected").each(function(){$selected_option=n(this);var e=$selected_option.data("display")||$selected_option.text();$selected_html+='<span class="current">'+e+"</span>",$selected_values.push($selected_option.data("value"))}),$select_placeholder=s.prev("select").data("js-placeholder")||s.prev("select").attr("js-placeholder"),console.log(s.prev("select")),$select_placeholder=$select_placeholder||"Select",$selected_html=""===$selected_html?$select_placeholder:$selected_html,s.find(".multiple-options").html($selected_html),s.prev("select").val($selected_values).trigger("change");else{s.find(".selected").removeClass("selected"),t.addClass("selected");var l=t.data("display")||t.text();s.find(".current").text(l),s.prev("select").val(t.data("value")).trigger("change")}}),n(document).on("keydown.nice_select",".nice-select",function(e){var t=n(this),s=n(t.find(".focus")||t.find(".list .option.selected"));if(32==e.keyCode||13==e.keyCode)return t.hasClass("open")?s.trigger("click"):t.trigger("click"),!1;if(40==e.keyCode){if(t.hasClass("open")){var l=s.nextAll(".option:not(.disabled)").first();0<l.length&&(t.find(".focus").removeClass("focus"),l.addClass("focus"))}else t.trigger("click");return!1}if(38==e.keyCode){if(t.hasClass("open")){var c=s.prevAll(".option:not(.disabled)").first();0<c.length&&(t.find(".focus").removeClass("focus"),c.addClass("focus"))}else t.trigger("click");return!1}if(27==e.keyCode)t.hasClass("open")&&t.trigger("click");else if(9==e.keyCode&&t.hasClass("open"))return!1});var t=document.createElement("a").style;return t.cssText="pointer-events:auto","auto"!==t.pointerEvents&&n("html").addClass("no-csspointerevents"),this}}(jQuery);
|