Blame | Last modification | View Log | RSS feed
!function(a){EditableSelect=function(t,i){this.options=i,this.$select=a(t),this.$input=a('<input type="text" autocomplete="off">'),this.$list=a('<ul class="es-list">'),this.utility=new EditableSelectUtility(this),["focus","manual"].indexOf(this.options.trigger)<0&&(this.options.trigger="focus"),["default","fade","slide"].indexOf(this.options.effects)<0&&(this.options.effects="default"),isNaN(this.options.duration)&&["fast","slow"].indexOf(this.options.duration)<0&&(this.options.duration="fast"),this.$select.replaceWith(this.$input),this.$list.appendTo(this.options.appendTo||this.$input.parent()),this.utility.initialize(),this.utility.initializeList(),this.utility.initializeInput(),this.utility.trigger("created")},EditableSelect.DEFAULTS={filter:!0,effects:"default",duration:"fast",trigger:"focus"},EditableSelect.prototype.filter=function(){var t=0,e=this.$input.val().toLowerCase().trim();this.$list.find("li").addClass("es-visible").show(),this.options.filter&&(t=this.$list.find("li").filter(function(t,i){ret
urn a(i).text().toLowerCase().indexOf(e)<0}).hide().removeClass("es-visible").length,this.$list.find("li").length==t&&this.hide())},EditableSelect.prototype.show=function(){if(this.$list.css({top:this.$input.position().top+this.$input.outerHeight()-1,left:this.$input.position().left,width:this.$input.outerWidth()}),!this.$list.is(":visible")&&0<this.$list.find("li.es-visible").length){var t={default:"show",fade:"fadeIn",slide:"slideDown"}[this.options.effects];this.utility.trigger("show"),this.$input.addClass("open"),this.$list[t](this.options.duration,a.proxy(this.utility.trigger,this.utility,"shown"))}},EditableSelect.prototype.hide=function(){var t={default:"hide",fade:"fadeOut",slide:"slideUp"}[this.options.effects];this.utility.trigger("hide"),this.$input.removeClass("open"),this.$list[t](this.options.duration,a.proxy(this.utility.trigger,this.utility,"hidden"))},EditableSelect.prototype.select=function(t){this.$list.has(t)&&t.is("li.es-visible:not([disabled])")&&(this.$input.val(t.text()),this.options.
filter&&this.hide(),this.filter(),this.utility.trigger("select",t))},EditableSelect.prototype.add=function(t,i,e,s){var l=a("<li>").html(t),o=a("<option>").text(t),n=this.$list.find("li").length;0==(i=isNaN(i)?n:Math.min(Math.max(0,i),n))?(this.$list.prepend(l),this.$select.prepend(o)):(this.$list.find("li").eq(i-1).after(l),this.$select.find("option").eq(i-1).after(o)),this.utility.setAttributes(l,e,s),this.utility.setAttributes(o,e,s),this.filter()},EditableSelect.prototype.remove=function(t){var i=this.$list.find("li").length;t=isNaN(t)?i:Math.min(Math.max(0,t),i-1),this.$list.find("li").eq(t).remove(),this.$select.find("option").eq(t).remove(),this.filter()},EditableSelect.prototype.clear=function(){this.$list.find("li").remove(),this.$select.find("option").remove(),this.filter()},EditableSelect.prototype.destroy=function(){this.$list.off("mousemove mousedown mouseup"),this.$input.off("focus blur input keydown"),this.$input.replaceWith(this.$select),this.$list.remove(),this.$select.removeData("editable-s
elect")},EditableSelectUtility=function(t){this.es=t},EditableSelectUtility.prototype.initialize=function(){var s=this;s.setAttributes(s.es.$input,s.es.$select[0].attributes,s.es.$select.data()),s.es.$input.addClass("es-input").data("editable-select",s.es),s.es.$select.find("option").each(function(t,i){var e=a(i).remove();s.es.add(e.text(),t,i.attributes,e.data()),e.attr("selected")&&s.es.$input.val(e.text())}),s.es.filter()},EditableSelectUtility.prototype.initializeList=function(){var i=this;i.es.$list.on("mousemove","li:not([disabled])",function(){i.es.$list.find(".selected").removeClass("selected"),a(this).addClass("selected")}).on("mousedown","li",function(t){a(this).is("[disabled]")?t.preventDefault():i.es.select(a(this))}).on("mouseup",function(){i.es.$list.find("li.selected").removeClass("selected")})},EditableSelectUtility.prototype.initializeInput=function(){var s=this;switch(this.es.options.trigger){default:case"focus":s.es.$input.on("focus",a.proxy(s.es.show,s.es)).on("blur",a.proxy(s.es.hide,s.e
s));break;case"manual":}s.es.$input.on("input keydown",function(t){switch(t.keyCode){case 38:var i=(e=s.es.$list.find("li.es-visible:not([disabled])")).index(e.filter("li.selected"));s.highlight(i-1),t.preventDefault();break;case 40:var e;i=(e=s.es.$list.find("li.es-visible:not([disabled])")).index(e.filter("li.selected"));s.highlight(i+1),t.preventDefault();break;case 13:s.es.$list.is(":visible")&&(s.es.select(s.es.$list.find("li.selected")),t.preventDefault());break;case 9:case 27:s.es.hide();break;default:s.es.filter(),s.highlight(0)}})},EditableSelectUtility.prototype.highlight=function(n){var a=this;a.es.show(),setTimeout(function(){var t=a.es.$list.find("li.es-visible"),i=a.es.$list.find("li.selected").removeClass("selected"),e=t.index(i);if(0<t.length){var s=(t.length+n)%t.length,l=t.eq(s),o=l.position().top;l.addClass("selected"),s<e&&o<0&&a.es.$list.scrollTop(a.es.$list.scrollTop()+o),e<s&&o+l.outerHeight()>a.es.$list.outerHeight()&&a.es.$list.scrollTop(a.es.$list.scrollTop()+l.outerHeight()+2*(o-a.
es.$list.outerHeight()))}})},EditableSelectUtility.prototype.setAttributes=function(e,t,i){a.each(t||{},function(t,i){e.attr(i.name,i.value)}),e.data(i)},EditableSelectUtility.prototype.trigger=function(t){var i=Array.prototype.slice.call(arguments,1),e=[t+".editable-select"];e.push(i),this.es.$select.trigger.apply(this.es.$select,e),this.es.$input.trigger.apply(this.es.$input,e)},Plugin=function(s){var l=Array.prototype.slice.call(arguments,1);return this.each(function(){var t=a(this),i=t.data("editable-select"),e=a.extend({},EditableSelect.DEFAULTS,t.data(),"object"==typeof s&&s);i||(i=new EditableSelect(this,e)),"string"==typeof s&&i[s].apply(i,l)})},a.fn.editableSelect=Plugin,a.fn.editableSelect.Constructor=EditableSelect}(jQuery);