!function(t,e){"function"==typeof define&&define.amd?define(["tether"],e):"object"==typeof exports?module.exports=e(require("tether")):t.Select=e(t.Tether)}(this,function(t){"use strict";function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function i(){var t=window.innerWidth,e=window.innerHeight;return b&&(640>=t||640>=e)}function o(t){return Array.prototype.reduce.call(t,function(t,e){return t===e?e:!1})}function r(){var t=document.querySelector(".select-target-focused");return t?t.selectInstance:null}var s=function(){function t(t,e){for(var n=0;n1&&o(E)){var i=e.findOptionsByPrefix(E[0]);if(i.length){var s=i.indexOf(e.getChosen());return s+=1,s%=i.length,void e.selectOption(i[s])}}return n.length?void e.selectOption(n[0]):void 0}}),document.addEventListener("keydown",function(t){var e=r();if(e)if([m,O,v].indexOf(t.keyCode)>=0&&t.preventDefault(),e.isOpen())switch(t.keyCode){case m:case O:e.moveHighlight(t.keyCode);break;case f:e.selectHighlightedOption();break;case v:e.close(),e.target.focus()}else[m,O,y].indexOf(t.keyCode)>=0&&e.open()});var C=function(o){function r(t){if(e(this,r),a(Object.getPrototypeOf(r.prototype),"constructor",this).call(this,t),this.options=c({},r.defaults,t),this.select=this.options.el,"undefined"!=typeof this.select.selectInstance)throw new Error("This element has already been turned into a Select");this.update=this.update.bind(this),this.setupTarget(),this.renderTarget(),this.setupDrop(),this.renderDrop(),this.setupSelect(),this.setupTether(),this.bindClick(),this.bindMutationEvents(),this.value=this.select.value}return n(r,o),s(r,[{key:"useNative",value:function(){var t=this.options.useNative;return t===!0||i()&&t!==!1}},{key:"setupTarget",value:function(){var t=this;this.target=document.createElement("a"),this.target.href="javascript:;",h(this.target,"select-target");var e=this.select.getAttribute("tabindex")||0;this.target.setAttribute("tabindex",e),this.options.className&&h(this.target,this.options.className),this.target.selectInstance=this,this.target.addEventListener("click",function(){t.isOpen()?t.target.blur():t.target.focus()}),this.target.addEventListener("focus",function(){h(t.target,"select-target-focused")}),this.target.addEventListener("blur",function(e){var n=e.relatedTarget;t.isOpen()&&n&&!t.drop.contains(n)&&t.close(),u(t.target,"select-target-focused")}),this.select.parentNode.insertBefore(this.target,this.select.nextSibling)}},{key:"setupDrop",value:function(){var t=this;this.drop=document.createElement("div"),h(this.drop,"select"),this.options.className&&h(this.drop,this.options.className),document.body.appendChild(this.drop),this.drop.addEventListener("click",function(e){p(e.target,"select-option")&&t.pickOption(e.target),e.stopPropagation()}),this.drop.addEventListener("mousemove",function(e){p(e.target,"select-option")&&t.highlightOption(e.target)}),this.content=document.createElement("div"),h(this.content,"select-content"),this.drop.appendChild(this.content)}},{key:"open",value:function(){var t=this;if(h(this.target,"select-open"),this.useNative()){var e=document.createEvent("MouseEvents");return e.initEvent("mousedown",!0,!0),void this.select.dispatchEvent(e)}h(this.drop,"select-open"),setTimeout(function(){t.tether.enable()});var n=this.drop.querySelector(".select-option-selected");if(n){this.highlightOption(n),this.scrollDropContentToOption(n);var i=function(){if(p(t.drop,"tether-abutted-left")||p(t.drop,"tether-abutted-bottom")){var e=d(t.drop),i=d(n),o=e.top-(i.top+i.height);t.drop.style.top=(parseFloat(t.drop.style.top)||0)+o+"px"}},o=this.options.alignToHighlighted,r=this.content,s=r.scrollHeight,a=r.clientHeight;("always"===o||"auto"===o&&a>=s)&&setTimeout(function(){i()}),this.trigger("open")}}},{key:"close",value:function(){u(this.target,"select-open"),this.useNative()&&this.select.blur(),this.tether.disable(),u(this.drop,"select-open"),this.trigger("close")}},{key:"toggle",value:function(){this.isOpen()?this.close():this.open()}},{key:"isOpen",value:function(){return p(this.drop,"select-open")}},{key:"bindClick",value:function(){var t=this;this.target.addEventListener(k,function(e){e.preventDefault(),t.toggle()}),document.addEventListener(k,function(e){t.isOpen()&&(e.target===t.drop||t.drop.contains(e.target)||e.target===t.target||t.target.contains(e.target)||t.close())})}},{key:"setupTether",value:function(){this.tether=new t(c({element:this.drop,target:this.target,attachment:"top left",targetAttachment:"bottom left",classPrefix:"select",constraints:[{to:"window",attachment:"together"}]},this.options.tetherOptions))}},{key:"renderTarget",value:function(){this.target.innerHTML="";for(var t=this.select.querySelectorAll("option"),e=0;e=0&&(t===m?i-=1:i+=1,!(0>i||i>=n.length))){var o=n[i];this.highlightOption(o),this.scrollDropContentToOption(o)}}},{key:"scrollDropContentToOption",value:function(t){var e=this.content,n=e.scrollHeight,i=e.clientHeight,o=e.scrollTop;if(n>i){var r=d(this.content),s=d(t);this.content.scrollTop=s.top-(r.top-o)}}},{key:"selectHighlightedOption",value:function(){this.pickOption(this.drop.querySelector(".select-option-highlight"))}},{key:"pickOption",value:function(t){var e=this,n=arguments.length<=1||void 0===arguments[1]?!0:arguments[1];this.value=this.select.value=t.getAttribute("data-value"),this.triggerChange(),n&&setTimeout(function(){e.close(),e.target.focus()})}},{key:"triggerChange",value:function(){var t=document.createEvent("HTMLEvents");t.initEvent("change",!0,!1),this.select.dispatchEvent(t),this.trigger("change",{value:this.select.value})}},{key:"change",value:function(t){var e=this.findOptionsByValue(t);if(!e.length)throw new Error('Select Error: An option with the value "'+t+"\" doesn't exist");this.pickOption(e[0],!1)}}]),r}(g);return C.defaults={alignToHighlighed:"auto",className:"select-theme-default"},C.init=function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];if("loading"===document.readyState)return void document.addEventListener("DOMContentLoaded",function(){return C.init(t)});"undefined"==typeof t.selector&&(t.selector="select");for(var e=document.querySelectorAll(t.selector),n=0;n