diff --git a/select.coffee b/select.coffee index 7e096fb..3e77b20 100644 --- a/select.coffee +++ b/select.coffee @@ -87,7 +87,7 @@ class Select @setupDrop() @renderDrop() - @setupSelectEvents() + @setupSelect() setupTarget: -> $options = @$select.find('option') @@ -160,7 +160,9 @@ class Select @dropSelect.$drop.find('.drop-content').html $dropSelectOptions[0] - setupSelectEvents: -> + setupSelect: -> + @$select.data 'select', @ + @$select.on 'change', => @renderDrop() @renderTarget() diff --git a/select.js b/select.js index c1a925c..00cee36 100644 --- a/select.js +++ b/select.js @@ -115,7 +115,7 @@ this.renderTarget(); this.setupDrop(); this.renderDrop(); - this.setupSelectEvents(); + this.setupSelect(); } Select.prototype.setupTarget = function() { @@ -202,8 +202,9 @@ return this.dropSelect.$drop.find('.drop-content').html($dropSelectOptions[0]); }; - Select.prototype.setupSelectEvents = function() { + Select.prototype.setupSelect = function() { var _this = this; + this.$select.data('select', this); return this.$select.on('change', function() { _this.renderDrop(); return _this.renderTarget(); diff --git a/select.min.js b/select.min.js index 6c56c46..fc39e5c 100644 --- a/select.min.js +++ b/select.min.js @@ -1,2 +1,2 @@ /*! select.js 0.1.0 */ -(function(){var a,b,c,d,e,f,g,h,i,j,k,l;b=Drop.createContext(),c=13,d=27,e=32,g=38,a=40,l=function(a){var b,c,d,e,f;if(!(a.length>1))return!1;for(c=a.charAt(0),f=a.split(""),d=0,e=f.length;e>d;d++)if(b=f[d],b!==c)return!1;return!0},h=function(){var a;return a=$(".select-target-focused:first"),(null!=a?a.length:void 0)&&a.data("select")},j="",k=void 0,i=void 0,$(window).on("keypress",function(a){var b,c;return c=h(),c&&0!==a.charCode?(b=String.fromCharCode(a.charCode),l(j)&&!l(j+b)?j=b:(j+=b,i===b&&(j+=b)),i=b,a.keyCode===e&&a.preventDefault(),c.dropSelect.isOpened()?c.highlightOptionByText(j):c.selectOptionByText(j),clearTimeout(k),k=setTimeout(function(){return j=""},500)):void 0}),$(window).on("keydown",function(b){var f,i,j;if(f=h())if(((i=b.keyCode)===g||i===a||i===d)&&b.preventDefault(),f.dropSelect.isOpened())switch(b.keyCode){case g:case a:return f.moveHighlight(b.keyCode);case c:return f.selectHighlightedOption();case d:return f.dropSelect.close(),f.$target.focus()}else if((j=b.keyCode)===g||j===a||j===e)return f.dropSelect.open()}),f=function(){function a(b){this.options=b,this.options=$.extend({},a.defaults,this.options),this.$select=$(this.options.el),this.setupTarget(),this.renderTarget(),this.setupDrop(),this.renderDrop(),this.setupSelectEvents()}return a.defaults={selectLikeAlignment:"auto",className:"select-theme-default"},a.prototype.setupTarget=function(){var a,b=this;return a=this.$select.find("option"),this.$target=$(''),this.$target.data("select",this),this.$target.on("click",function(){return b.dropSelect.isOpened()?b.$target.blur():b.$target.focus()}),this.$target.on("focus",function(){return b.$target.addClass("select-target-focused")}),this.$target.on("blur",function(a){return b.dropSelect.isOpened()?a.relatedTarget&&!$(a.relatedTarget).parents(".drop:first").is(b.dropSelect.$drop)?b.dropSelect.close():void 0:b.$target.removeClass("select-target-focused")}),this.$select.after(this.$target).hide()},a.prototype.renderTarget=function(){return this.$target.text(this.$select.find("option:selected").text()),this.$target.append("")},a.prototype.setupDrop=function(){var a=this;return this.dropSelect=new b({target:this.$target[0],className:this.options.className,attach:"bottom left",constrainToWindow:!0,constrainToScrollParent:!1,openOn:"click"}),this.dropSelect.$drop.on("click",".select-option",function(b){return a.selectOption(b.target)}),this.dropSelect.$drop.on("mousemove",".select-option",function(b){return a.highlightOption(b.target)}),this.dropSelect.$drop.on("dropopen",function(){var b,c,d;return c=a.dropSelect.$drop.find('[data-selected="true"]'),b=a.dropSelect.$drop.find(".drop-content"),d=function(){var b;return a.dropSelect.$drop.hasClass("tether-abutted-left tether-abutted-bottom")?(b=a.dropSelect.$drop.offset().top-(c.offset().top+c.outerHeight()),a.dropSelect.$drop.css({top:"+="+b})):void 0},a.highlightOption(c[0]),a.scrollDropContentToOption(c[0]),"always"===a.options.selectLikeAlignment||"auto"===a.options.selectLikeAlignment&&b[0].scrollHeight<=b[0].clientHeight?setTimeout(d):void 0}),this.dropSelect.$drop.on("dropclose",function(){return a.$target.removeClass("select-target-focused")})},a.prototype.renderDrop=function(){var a;return a=$(''),this.$select.find("option").each(function(){var b;return b=$(this),a.append('
  • '+b.text()+"
  • ")}),this.dropSelect.$drop.find(".drop-content").html(a[0])},a.prototype.setupSelectEvents=function(){var a=this;return this.$select.on("change",function(){return a.renderDrop(),a.renderTarget()})},a.prototype.selectOptionByText=function(a){var b,c,d,e,f,g,h;if(g=this.$select.find("option").toArray(),c=this.$select.find("option:selected").index(),null!=c)for(e=l(a),d=c,e&&(d+=1),h=0;hg.length-1&&(d=0),f=g[d],b=$(f),e&&b.text().toLowerCase().charAt(0)===a.toLowerCase().charAt(0)||b.text().toLowerCase().substr(0,a.length)===a.toLowerCase())return this.$select.val(b.val()).change(),void 0;h+=1,d+=1}},a.prototype.highlightOptionByText=function(a){var b,c,d,e,f,g,h;if(this.dropSelect.isOpened()&&(g=this.dropSelect.$drop.find(".select-option").toArray(),c=this.dropSelect.$drop.find(".select-option-highlight").index(),null!=c))for(e=l(a),d=c,e&&(d+=1),h=0;hg.length-1&&(d=0),f=g[d],b=$(f),e&&b.text().toLowerCase().charAt(0)===a.toLowerCase().charAt(0)||b.text().toLowerCase().substr(0,a.length)===a.toLowerCase())return this.highlightOption(f),this.scrollDropContentToOption(f),void 0;h+=1,d+=1}},a.prototype.highlightOption=function(a){return this.dropSelect.$drop.find(".select-option-highlight").removeClass("select-option-highlight"),$(a).addClass("select-option-highlight")},a.prototype.moveHighlight=function(a){var b,c;return b=this.dropSelect.$drop.find(".select-option-highlight"),b.length?(c=a===g?b.prev():b.next(),c.length?(this.highlightOption(c[0]),this.scrollDropContentToOption(c[0])):void 0):this.highlightOption(this.dropSelect.$drop.find(".select-option:first"))},a.prototype.scrollDropContentToOption=function(a){var b,c;return c=$(a),b=this.dropSelect.$drop.find(".drop-content"),b[0].scrollHeight>b[0].clientHeight?b.scrollTop(c.offset().top-(b.offset().top-b.scrollTop())):void 0},a.prototype.selectHighlightedOption=function(){return this.selectOption(this.dropSelect.$drop.find(".select-option-highlight")[0])},a.prototype.selectOption=function(a){var b=this;return this.$select.val($(a).data("value")).change(),setTimeout(function(){return b.dropSelect.close(),b.$target.focus()},0)},a}(),window.Select=f}).call(this); \ No newline at end of file +(function(){var a,b,c,d,e,f,g,h,i,j,k,l;b=Drop.createContext(),c=13,d=27,e=32,g=38,a=40,l=function(a){var b,c,d,e,f;if(!(a.length>1))return!1;for(c=a.charAt(0),f=a.split(""),d=0,e=f.length;e>d;d++)if(b=f[d],b!==c)return!1;return!0},h=function(){var a;return a=$(".select-target-focused:first"),(null!=a?a.length:void 0)&&a.data("select")},j="",k=void 0,i=void 0,$(window).on("keypress",function(a){var b,c;return c=h(),c&&0!==a.charCode?(b=String.fromCharCode(a.charCode),l(j)&&!l(j+b)?j=b:(j+=b,i===b&&(j+=b)),i=b,a.keyCode===e&&a.preventDefault(),c.dropSelect.isOpened()?c.highlightOptionByText(j):c.selectOptionByText(j),clearTimeout(k),k=setTimeout(function(){return j=""},500)):void 0}),$(window).on("keydown",function(b){var f,i,j;if(f=h())if(((i=b.keyCode)===g||i===a||i===d)&&b.preventDefault(),f.dropSelect.isOpened())switch(b.keyCode){case g:case a:return f.moveHighlight(b.keyCode);case c:return f.selectHighlightedOption();case d:return f.dropSelect.close(),f.$target.focus()}else if((j=b.keyCode)===g||j===a||j===e)return f.dropSelect.open()}),f=function(){function a(b){this.options=b,this.options=$.extend({},a.defaults,this.options),this.$select=$(this.options.el),this.setupTarget(),this.renderTarget(),this.setupDrop(),this.renderDrop(),this.setupSelect()}return a.defaults={selectLikeAlignment:"auto",className:"select-theme-default"},a.prototype.setupTarget=function(){var a,b=this;return a=this.$select.find("option"),this.$target=$(''),this.$target.data("select",this),this.$target.on("click",function(){return b.dropSelect.isOpened()?b.$target.blur():b.$target.focus()}),this.$target.on("focus",function(){return b.$target.addClass("select-target-focused")}),this.$target.on("blur",function(a){return b.dropSelect.isOpened()?a.relatedTarget&&!$(a.relatedTarget).parents(".drop:first").is(b.dropSelect.$drop)?b.dropSelect.close():void 0:b.$target.removeClass("select-target-focused")}),this.$select.after(this.$target).hide()},a.prototype.renderTarget=function(){return this.$target.text(this.$select.find("option:selected").text()),this.$target.append("")},a.prototype.setupDrop=function(){var a=this;return this.dropSelect=new b({target:this.$target[0],className:this.options.className,attach:"bottom left",constrainToWindow:!0,constrainToScrollParent:!1,openOn:"click"}),this.dropSelect.$drop.on("click",".select-option",function(b){return a.selectOption(b.target)}),this.dropSelect.$drop.on("mousemove",".select-option",function(b){return a.highlightOption(b.target)}),this.dropSelect.$drop.on("dropopen",function(){var b,c,d;return c=a.dropSelect.$drop.find('[data-selected="true"]'),b=a.dropSelect.$drop.find(".drop-content"),d=function(){var b;return a.dropSelect.$drop.hasClass("tether-abutted-left tether-abutted-bottom")?(b=a.dropSelect.$drop.offset().top-(c.offset().top+c.outerHeight()),a.dropSelect.$drop.css({top:"+="+b})):void 0},a.highlightOption(c[0]),a.scrollDropContentToOption(c[0]),"always"===a.options.selectLikeAlignment||"auto"===a.options.selectLikeAlignment&&b[0].scrollHeight<=b[0].clientHeight?setTimeout(d):void 0}),this.dropSelect.$drop.on("dropclose",function(){return a.$target.removeClass("select-target-focused")})},a.prototype.renderDrop=function(){var a;return a=$(''),this.$select.find("option").each(function(){var b;return b=$(this),a.append('
  • '+b.text()+"
  • ")}),this.dropSelect.$drop.find(".drop-content").html(a[0])},a.prototype.setupSelect=function(){var a=this;return this.$select.data("select",this),this.$select.on("change",function(){return a.renderDrop(),a.renderTarget()})},a.prototype.selectOptionByText=function(a){var b,c,d,e,f,g,h;if(g=this.$select.find("option").toArray(),c=this.$select.find("option:selected").index(),null!=c)for(e=l(a),d=c,e&&(d+=1),h=0;hg.length-1&&(d=0),f=g[d],b=$(f),e&&b.text().toLowerCase().charAt(0)===a.toLowerCase().charAt(0)||b.text().toLowerCase().substr(0,a.length)===a.toLowerCase())return this.$select.val(b.val()).change(),void 0;h+=1,d+=1}},a.prototype.highlightOptionByText=function(a){var b,c,d,e,f,g,h;if(this.dropSelect.isOpened()&&(g=this.dropSelect.$drop.find(".select-option").toArray(),c=this.dropSelect.$drop.find(".select-option-highlight").index(),null!=c))for(e=l(a),d=c,e&&(d+=1),h=0;hg.length-1&&(d=0),f=g[d],b=$(f),e&&b.text().toLowerCase().charAt(0)===a.toLowerCase().charAt(0)||b.text().toLowerCase().substr(0,a.length)===a.toLowerCase())return this.highlightOption(f),this.scrollDropContentToOption(f),void 0;h+=1,d+=1}},a.prototype.highlightOption=function(a){return this.dropSelect.$drop.find(".select-option-highlight").removeClass("select-option-highlight"),$(a).addClass("select-option-highlight")},a.prototype.moveHighlight=function(a){var b,c;return b=this.dropSelect.$drop.find(".select-option-highlight"),b.length?(c=a===g?b.prev():b.next(),c.length?(this.highlightOption(c[0]),this.scrollDropContentToOption(c[0])):void 0):this.highlightOption(this.dropSelect.$drop.find(".select-option:first"))},a.prototype.scrollDropContentToOption=function(a){var b,c;return c=$(a),b=this.dropSelect.$drop.find(".drop-content"),b[0].scrollHeight>b[0].clientHeight?b.scrollTop(c.offset().top-(b.offset().top-b.scrollTop())):void 0},a.prototype.selectHighlightedOption=function(){return this.selectOption(this.dropSelect.$drop.find(".select-option-highlight")[0])},a.prototype.selectOption=function(a){var b=this;return this.$select.val($(a).data("value")).change(),setTimeout(function(){return b.dropSelect.close(),b.$target.focus()},0)},a}(),window.Select=f}).call(this); \ No newline at end of file