-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.selectlist.min.js
1 lines (1 loc) · 3.79 KB
/
jquery.selectlist.min.js
1
/*! selectlist 0.6.1 */ (function(t){t.selectList=function(e,i){function n(t,e){i.addAnimate&&m?"function"==typeof i.addAnimate?i.addAnimate(t.hide()[0],e):(t.hide().fadeIn(300,e),t[0].style.display=""):(t[0].style.display="",e&&e.call(t[0]))}function a(t,e){i.removeAnimate&&m?"function"==typeof i.removeAnimate?i.removeAnimate(t[0],e):t.fadeOut(300,e):(t.hide(),e&&e.call(t[0]))}function s(e,n){return"function"==typeof i.sort?i.sort(e,n):t(e).data("text")>t(n).data("text")==("desc"!=i.sort)}function o(a,o,v){if(t(a).is("option")){if(h=t(a),b>h[0].index)return;a=h.val(),o=h.text()}else h=r.find('option[value="'+a.replace("'",'\\"')+'"]'),h=h.length?h.filter(function(){return!o||t(this).text()==o}).add(h).eq(0):null;void 0===o&&(o=h?h.text():a),h&&!i.duplicates&&h.attr("disabled","disabled").data("disabled",1),u=t(i.template.replace(/%text%/g,t("<b/>").text(o).html()).replace(/%value%/g,a)).hide(),u.data("value",a).data("text",o).data("option",h).addClass(i.classPrefix+"-item"),u.click(function(){i.clickRemove&&d(t(this))});var m=function(){v!==!1&&i.onAdd(e,a,o)};if(i.sort&&(f=c.children().eq(0)).length){for(;f.length&&s(u[0],f[0]);)f=f.next();n(f.length?u.insertBefore(f):u.appendTo(c),m)}else n(u.appendTo(c),m);t(e).empty(),c.children().each(function(){t(e).append(t("<option/>").attr({value:t(this).data("value"),selected:"selected"}))}),l()}function d(n,s){a(n,function(){var n=t(this).data("value"),a=t(this).data("text");t(this).data("option")&&t(this).data("option").removeAttr("disabled").removeData("disabled"),t(this).remove(),t(e).find('option[value="'+n+'"]').remove(),l(),s!==!1&&i.onRemove(e,n,a)})}function l(){e.form&&"function"==typeof t(e.form).validate&&t(e).add(r).hasClass(t(e.form).validate().settings.errorClass)&&t(e.form).validate().element(e)}var r,c,f,u,h,v,m,p,x,A,y,b=0,g=navigator.userAgent;this.val=function(i){return void 0!==i&&(t("option",r).prop("disabled",!1).removeData("disabled"),c.empty(),null!==i&&t.each(t.makeArray(i),function(t,e){o(e)})),t(e).val()},this.add=function(t,e){o(t,e)},this.remove=function(e){c.children().each(function(){(t(this).data("value")==e||e===void 0)&&d(t(this))})},this.setOptions=function(e){var n=e.sort&&e.sort!=i.sort;if(i=t.extend(i,e),n){var a=[];c.children().each(function(){a[a.length]=t(this).data("value"),a[a.length]=t(this).data("text")}),c.empty();for(var s=0;a.length>s;s+=2)o(a[s],a[s+1],!1)}};var k=(/msie ([\w.]+)/i.exec(g)||[])[1],L=/webkit/i.test(g)&&!/chrome/i.test(g);this.setOptions(i=t.extend({addAnimate:!0,classPrefix:"selectlist",clickRemove:!0,removeAnimate:!0,template:"<li>%text%</li>",onAdd:function(){},onRemove:function(){}},i)),r=t(e).clone(),r.removeAttr("id").removeAttr("name").addClass(i.classPrefix+"-select").insertAfter(t(e)),t(e).empty().hide(),(c=t(i.list||t("<ul/>").insertAfter(r))).addClass(i.classPrefix+"-list"),r.find(":selected").each(function(){o(t(this),null,!1)}),r.removeAttr("multiple"),r[0].removeAttribute("size"),r.attr("title")&&(r.prepend(t("<option/>").text(r.attr("title"))),b=1,r[0].selectedIndex=0),v=k||L?"keydown":"keypress",r.bind(v,function(t){return A=!0,13==(t.keyCode||t.which)?(y=!0,r.change(),A=!0,!1):void 0}).change(function(){(A||x)&&(p=!0,h=r.find("option:selected"),h.data("disabled")||A&&!y||o(h),A?A=p=x=!1:b&&(r[0].selectedIndex=0),y=!1)}).mousedown(function(){x=!0}),r.find("option").click(function(){return x=!0,t(this).attr("disabled")||t(this).data("disabled")||A||p||o(t(this)),A||(p=x=!1),!1}),m=!0},t.fn.selectList=function(e){return e=e||{},this.filter("select").each(function(){t(this).data("selectList")?t(this).data("selectList").setOptions(e):t(this).data("selectList",new t.selectList(this,e))}),e.instance?this.filter("select").data("selectList"):this};var e=t.valHooks.select.set;t.valHooks.select.set=function(i,n){return t(i).data("selectList")?t(i).data("selectList").val(n):e(i,n)}})(jQuery);