kist-selectdown
Version:
Select with customizable menu.
2 lines • 10.1 kB
JavaScript
/*! kist-selectdown 0.2.2 - Select with customizable menu. | Author: Ivan Nikolić <niksy5@gmail.com> (http://ivannikolic.com/), 2016 | License: MIT */
!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){(function(c){function d(a){return f.isNumeric(a)?Number(a):a}function e(a){return function(b,c){return d(this.getOptionToggler(f(c)).data("val"))===d(a)}}a(6);var f="undefined"!=typeof window?window.$:"undefined"!=typeof c?c.$:null,g=a(11),h=a(2),i=a(4),j=a(8),k=a(5),l=a(3)(g.name),m=b.exports=function(a,b){this.element=a,this.options=f.extend(!0,{},this.defaults,b),this.setupInstance(),this.setupDom(),this.setupEvents(),l(this,"create",[this.$el,this.$select])};f.extend(m.prototype,{destroy:function(){this.destroyDom(),this.destroyEvents(),this.destroyInstance()},setValue:function(a){this.$el.val(a)},getValue:function(a){return a=a||this.$el,a.val()},getContent:function(a){return a=a||this.$el.children(":selected"),a.html()},getOriginalOption:function(a){return this.$el.children("option").filter(function(b,c){return d(c.value)===d(a)})},disableSelect:function(a){this.$select.prop("disabled",a)},disableOption:function(a,b){a.prop("disabled",b)},renderSelect:function(a){this.$select.html(this.options.templates.select.call(this.element,{content:a}))},renderOptions:function(){this.$optionItem=this.$el.children().map(f.proxy(function(a,b){var c=f(b),d=g.ns.htmlClass+"-option-"+this.uid+"-"+a,e=f("<li />",{"class":this.options.classes.optionItem}),h=f("<button />",{id:d,tabindex:-1,type:"button","class":this.options.classes.option,role:"option",html:this.options.templates.option.call(this.element,{content:c.html(),value:c.val(),selected:c.prop("selected"),disabled:c.prop("disabled")})});return h.data("val",c.val()),this.disableOption(h,c.prop("disabled")),h.appendTo(e),e.get()},this)),this.$optionList.html(this.$optionItem)},getOption:function(a){var b=e(a);return this.$optionItem.filter(f.proxy(b,this))},setActiveOption:function(a,b){var c=[this.options.classes.isActive,this.options.classes.isFocused].join(" ");this.$activeOptionItem=this.getOption(a),this.$optionItem.removeClass(c),this.$activeOptionItem.addClass(c),this.$wrapper.attr("aria-activedescendant",this.getOptionToggler(this.$activeOptionItem).attr("id")),b||l(this,"select",[this.$activeOptionItem,a,this.getOriginalOption(a)])},setFocusedOption:function(a){var b=[this.options.classes.isFocused].join(" ");this.$focusedOptionItem=this.getOption(a),this.$optionItem.removeClass(b),this.$focusedOptionItem.addClass(b)},getOptionToggler:function(a){return a.children("button")},displayOptions:function(a){var b=this.$optionList.hasClass(this.options.classes.isHidden);a?b&&(l(this,"open",[this.$el,this.$select]),this.$optionList.attr("aria-expanded",!0)):b||(l(this,"close",[this.$el,this.$select]),this.$optionList.attr("aria-expanded",!1)),this.$select[a?"addClass":"removeClass"](this.options.classes.isActive),this.$optionList[a?"removeClass":"addClass"](this.options.classes.isHidden)},navigate:function(a){var b,c,d=this.$optionList.children(k(this.options.classes.isFocused)),e=this.$optionList.children(),f=e.index(d);f="down"===a?++f:--f,f>=e.length?f=0:0>f&&(f=e.length-1),b=e.eq(f),c=this.getOptionToggler(b),this.setFocusedOption(c.data("val")),c.prop("disabled")&&this.navigate(a)},refresh:function(){this.assignInstanceToChildren(),this.renderOptions(),this.renderSelect(this.getContent()),this.setActiveOption(this.getValue())},defaults:{classes:{wrapper:g.ns.htmlClass,originalSelect:g.ns.htmlClass+"-originalSelect",select:g.ns.htmlClass+"-select",optionList:g.ns.htmlClass+"-optionList",optionItem:g.ns.htmlClass+"-optionItem",option:g.ns.htmlClass+"-option",isActive:"is-active",isHidden:"is-hidden",isFocused:"is-focused"},create:f.noop,open:f.noop,close:f.noop,select:f.noop,templates:{select:function(a){return a.content},option:function(a){return a.content}}}},h,i,j)}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{11:11,2:2,3:3,4:4,5:5,6:6,8:8}],2:[function(a,b){(function(c){var d="undefined"!=typeof window?window.$:"undefined"!=typeof c?c.$:null,e=a(11);b.exports={$doc:d(document),setupDom:function(){var a=e.ns.htmlClass+"-list-"+this.uid;this.$el=d(this.element),this.$el.addClass(this.options.classes.originalSelect).attr({tabindex:-1,"aria-autocomplete":"list","aria-owns":a,"aria-readonly":!0}),this.$wrapper=d("<div />",{"class":this.options.classes.wrapper,role:"combobox","aria-activedescendant":""}),this.$select=d("<button />",{type:"button","class":this.options.classes.select,"aria-controls":a}),this.$optionList=d("<ul />",{id:a,"class":[this.options.classes.optionList,this.options.classes.isHidden].join(" "),role:"listbox","aria-expanded":!1}),this.renderOptions(),this.renderSelect(this.getContent()),this.setActiveOption(this.getValue(),!0),this.setFocusedOption(this.getValue()),this.disableSelect(this.$el.prop("disabled")),this.$wrapper.insertBefore(this.$el).append(this.$el,this.$select,this.$optionList)},destroyDom:function(){this.$el.removeClass(this.options.classes.originalSelect).insertBefore(this.$wrapper).removeAttr("aria-autocomplete aria-owns aria-readonly tabindex"),this.$wrapper.remove()}}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{11:11}],3:[function(a,b){(function(a){var c="undefined"!=typeof window?window.$:"undefined"!=typeof a?a.$:null;b.exports=function(a){return function(b,d,e,f){var g=b.dom&&b.dom.el||b.$el||c({});b.options[d]&&b.options[d].apply(1===g.length?g[0]:g.toArray(),e),(f||g).trigger(((a||"")+d).toLowerCase(),e)}}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}],4:[function(a,b){(function(c){function d(a){var b=a.which,c=f(a.target);(b===h.escape||c.closest(this.$select)&&0===c.closest(this.$wrapper).length||!c.closest(this.$select)&&0===c.closest(i(this.options.classes.option)).length)&&this.displayOptions(!1)}function e(a){var b=a.which;if(!this.$optionList.hasClass(this.options.classes.isHidden))switch(b){case h.up:case h.down:this.navigate(b===h.down?"down":"up");break;case h.enter:this.setValue(this.getOptionToggler(this.$focusedOptionItem).data("val"))}}var f="undefined"!=typeof window?window.$:"undefined"!=typeof c?c.$:null,g=a(11),h=a(10),i=a(5);b.exports={setupEvents:function(){this.$el.on("change"+this.ens,f.proxy(function(a,b){b!==g.name+"syntheticChange"&&(this.renderSelect(this.getContent()),this.setActiveOption(this.getValue()))},this)),this.$select.on("click"+this.ens,f.proxy(function(){this.displayOptions(this.$optionList.hasClass(this.options.classes.isHidden))},this)),this.$wrapper.on("click"+this.ens,i(this.options.classes.option),f.proxy(function(a){var b=f(a.currentTarget),c=b.data("val");this.setValue(c),this.displayOptions(!1),this.$el.trigger("change",[g.name+"syntheticChange"])},this)),this.$doc.on("click"+this.ens,f.proxy(d,this)).on("keydown"+this.ens,f.proxy(d,this)).on("keydown"+this.ens,f.proxy(e,this))},destroyEvents:function(){this.$el.off(this.ens),this.$select.off(this.ens),this.$wrapper.off(this.ens),this.$doc.off(this.ens)}}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{10:10,11:11,5:5}],5:[function(a,b){b.exports=function(a){return"."+a.split(" ").join(".")}},{}],6:[function(a){(function(b){var c="undefined"!=typeof window?window.$:"undefined"!=typeof b?b.$:null,d=a(11),e=c.valHooks,f=c.propHooks,g={set:function(a,b,e){var f,g=c.data(a,d.name);if(g)if("disabled"===e)f=c(a),f.is("select")?g.disableSelect(b):g.disableOption(g.getOptionToggler(g.getOption(a.value)),b);else{var h=g.getOriginalOption(b);g.renderSelect(g.getContent(h)),g.setActiveOption(g.getValue(h))}return void 0}};e.select=g,f.value=g,f.disabled=g}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{11:11}],7:[function(a,b){(function(c){var d="undefined"!=typeof window?window.$:"undefined"!=typeof c?c.$:null,e=a(1),f=a(11),g=a(9)(f.publicMethods),h=d.fn[f.name]=b.exports=function(a){return a=a||{},this.each(function(){var b=d.data(this,f.name);g(a)&&b?b[a]():"object"!=typeof a||b||d.data(this,f.name,new e(this,a))})};h.defaults=e.prototype.defaults}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{1:1,11:11,9:9}],8:[function(a,b){(function(c){var d="undefined"!=typeof window?window.$:"undefined"!=typeof c?c.$:null,e=a(11),f=0;b.exports={setupInstance:function(){this.uid=f++,this.ens=e.ns.event+"."+this.uid,this.assignInstanceToChildren()},destroyInstance:function(){d.removeData(this.element,e.name),this.removeInstanceFromChildren()},assignInstanceToChildren:function(){d(this.element).children("option").data(e.name,this)},removeInstanceFromChildren:function(){d(this.element).children("option").removeData(e.name)}}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{11:11}],9:[function(a,b){(function(a){var c="undefined"!=typeof window?window.$:"undefined"!=typeof a?a.$:null;b.exports=function(a){return function(b){return"string"==typeof b&&-1!==c.inArray(b,a||[])}}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}],10:[function(a,b){b.exports={enter:13,escape:27,up:38,down:40}},{}],11:[function(a,b){b.exports={name:"selectdown",ns:{htmlClass:"kist-Selectdown",event:".kist.selectdown",dataAttr:"kist-selectdown"},publicMethods:["destroy","refresh"]}},{}]},{},[7]);