1 line
4.3 KiB
JavaScript
1 line
4.3 KiB
JavaScript
"use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}!function(e){"function"==typeof define&&define.amd?define(["jquery"],e):"object"===("undefined"==typeof exports?"undefined":_typeof(exports))?module.exports=e(require("jquery")):e(jQuery)}(function(o){var e=o.fn.selectlist,d=function(e,t){this.$element=o(e),this.options=o.extend({},o.fn.selectlist.defaults,t),this.$button=this.$element.find(".btn.dropdown-toggle"),this.$hiddenField=this.$element.find(".hidden-field"),this.$label=this.$element.find(".selected-label"),this.$dropdownMenu=this.$element.find(".dropdown-menu"),this.$element.on("click.fu.selectlist",".dropdown-menu a",o.proxy(this.itemClicked,this)),this.setDefaultSelection(),"auto"!==t.resize&&"auto"!==this.$element.attr("data-resize")||this.resize(),0===this.$dropdownMenu.children("li").length&&(this.disable(),this.doSelect(o(this.options.emptyLabelHTML))),this.$element.on("shown.bs.dropdown",function(){var t=o(this);o(document).on("keypress.fu.selectlist",function(e){var i=String.fromCharCode(e.which);t.find("li").each(function(e,t){if(o(t).text().charAt(0).toLowerCase()===i)return o(t).children("a").focus(),!1})})}),this.$element.on("hide.bs.dropdown",function(){o(document).off("keypress.fu.selectlist")})};d.prototype={constructor:d,destroy:function(){return this.$element.remove(),this.$element[0].outerHTML},doSelect:function(e){var t;this.$selectedItem=t=e,this.$hiddenField.val(this.$selectedItem.attr("data-value")),this.$label.html(o(this.$selectedItem.children()[0]).html()),this.$element.find("li").each(function(){t.is(o(this))?o(this).attr("data-selected",!0):o(this).removeData("selected").removeAttr("data-selected")})},itemClicked:function(e){this.$element.trigger("clicked.fu.selectlist",this.$selectedItem),e.preventDefault(),o(e.currentTarget).parent("li").is(".disabled, :disabled")||(o(e.target).parent().is(this.$selectedItem)||this.itemChanged(e),this.$element.find(".dropdown-toggle").focus())},itemChanged:function(e){this.doSelect(o(e.target).closest("li"));var t=this.selectedItem();this.$element.trigger("changed.fu.selectlist",t)},resize:function(){var e=0,t=0,i=o("<div/>").addClass("selectlist-sizer");Boolean(o(document).find("html").hasClass("fuelux"))?o(document.body).append(i):o(".fuelux:first").append(i),i.append(this.$element.clone()),this.$element.find("a").each(function(){i.find(".selected-label").text(o(this).text()),t=i.find(".selectlist").outerWidth(),t+=i.find(".sr-only").outerWidth(),e<t&&(e=t)}),e<=1||(this.$button.css("width",e),this.$dropdownMenu.css("width",e),i.remove())},selectedItem:function(){var e=this.$selectedItem.text();return o.extend({text:e},this.$selectedItem.data())},selectByText:function(e){var t=o([]);this.$element.find("li").each(function(){if((this.textContent||this.innerText||o(this).text()||"").toLowerCase()===(e||"").toLowerCase())return t=o(this),!1}),this.doSelect(t)},selectByValue:function(e){var t='li[data-value="'+e+'"]';this.selectBySelector(t)},selectByIndex:function(e){var t="li:eq("+e+")";this.selectBySelector(t)},selectBySelector:function(e){var t=this.$element.find(e);this.doSelect(t)},setDefaultSelection:function(){var e=this.$element.find("li[data-selected=true]").eq(0);0===e.length&&(e=this.$element.find("li").has("a").eq(0)),this.doSelect(e)},enable:function(){this.$element.removeClass("disabled"),this.$button.removeClass("disabled")},disable:function(){this.$element.addClass("disabled"),this.$button.addClass("disabled")}},o.fn.selectlist=function(s){var n,l=Array.prototype.slice.call(arguments,1),e=this.each(function(){var e=o(this),t=e.data("fu.selectlist"),i="object"===_typeof(s)&&s;t||e.data("fu.selectlist",t=new d(this,i)),"string"==typeof s&&(n=t[s].apply(t,l))});return void 0===n?e:n},o.fn.selectlist.defaults={emptyLabelHTML:'<li data-value=""><a href="#">No items</a></li>'},o.fn.selectlist.Constructor=d,o.fn.selectlist.noConflict=function(){return o.fn.selectlist=e,this},o(document).on("mousedown.fu.selectlist.data-api","[data-initialize=selectlist]",function(e){var t=o(e.target).closest(".selectlist");t.data("fu.selectlist")||t.selectlist(t.data())}),o(function(){o("[data-initialize=selectlist]").each(function(){var e=o(this);e.data("fu.selectlist")||e.selectlist(e.data())})})}); |