jquery.multi-select.min.js 9.2 KB
!function(e){"use strict";var t=function(t,s){this.options=s,this.$element=e(t);var l=this.$element.attr("id");this.$container=e("<div/>",{id:"ms-"+l,"class":"ms-container"}),this.$selectableContainer=e("<div/>",{"class":"ms-selectable"}),this.$selectionContainer=e("<div/>",{"class":"ms-selection"}),this.$selectableUl=e("<ul/>",{"class":"ms-list"}),this.$selectionUl=e("<ul/>",{"class":"ms-list"}),this.scrollTo=0,this.sanitizeRegexp=new RegExp("\\W+","gi")};t.prototype={constructor:t,init:function(){var t=this,s=this.$element;if(0===s.next(".ms-container").length){s.css({position:"absolute",left:"-9999px"}),s.attr("id",s.attr("id")?s.attr("id"):"ms-"+Math.ceil(1e3*Math.random()));var l=null,i=null,o=0,n='<li class="ms-optgroup-container"></li>',a='<ul class="ms-optgroup"></ul>',c='<li class="ms-optgroup-label"><span></span></li>';s.find("optgroup, option").each(function(){if(e(this).is("optgroup")){l="<span>"+e(this).attr("label")+"</span>",i="ms-"+s.attr("id")+"-optgroup-"+o;var r=e(this),d=e(n),p=e(n),f=e(c),u=e(c);t.options.selectableOptgroup&&(u.on("click",function(){var s=r.children(":not(:selected)").map(function(){return e(this).val()}).get();t.select(s)}),f.on("click",function(){var s=r.children(":selected").map(function(){return e(this).val()}).get();t.deselect(s)})),u.html(l),d.attr("id",i+"-selectable").append(e(a).append(u)),t.$selectableUl.append(d),f.html(l),p.attr("id",i+"-selection").append(e(a).append(f)),t.$selectionUl.append(p),o++}else{for(var h="",m=0;m<this.attributes.length;m++){var b=this.attributes[m];t.isDomNode(b.name)&&(h+=b.name+'="'+b.value+'" ')}var v=e("<li "+h+"><span>"+e(this).text()+"</span></li>"),$=v.clone(),g=e(this).val(),C=t.sanitize(g,t.sanitizeRegexp);v.data("ms-value",g).addClass("ms-elem-selectable").attr("id",C+"-selectable"),$.data("ms-value",g).addClass("ms-elem-selection").attr("id",C+"-selection").hide(),t.$selectionUl.find(".ms-optgroup-label").hide(),(e(this).prop("disabled")||s.prop("disabled"))&&(this.selected?($.prop("disabled",!0),$.addClass(t.options.disabledClass)):(v.prop("disabled",!0),v.addClass(t.options.disabledClass))),i?(t.$selectableUl.children("#"+i+"-selectable").find("ul").first().append(v),t.$selectionUl.children("#"+i+"-selection").find("ul").first().append($)):(t.$selectableUl.append(v),t.$selectionUl.append($))}}),t.options.selectableHeader&&t.$selectableContainer.append(t.options.selectableHeader),t.$selectableContainer.append(t.$selectableUl),t.options.selectableFooter&&t.$selectableContainer.append(t.options.selectableFooter),t.options.selectionHeader&&t.$selectionContainer.append(t.options.selectionHeader),t.$selectionContainer.append(t.$selectionUl),t.options.selectionFooter&&t.$selectionContainer.append(t.options.selectionFooter),t.$container.append(t.$selectableContainer),t.$container.append(t.$selectionContainer),s.after(t.$container),t.$selectableUl.on("mouseenter",".ms-elem-selectable",function(){e("li",t.$container).removeClass("ms-hover"),e(this).addClass("ms-hover")}).on("mouseleave",function(){e("li",t.$container).removeClass("ms-hover")});var r=t.options.dblClick?"dblclick":"click";t.$selectableUl.on(r,".ms-elem-selectable",function(){t.select(e(this).data("ms-value"))}),t.$selectionUl.on(r,".ms-elem-selection",function(){t.deselect(e(this).data("ms-value"))}),t.$selectionUl.on("mouseenter",".ms-elem-selection",function(){e("li",t.$selectionUl).removeClass("ms-hover"),e(this).addClass("ms-hover")}).on("mouseleave",function(){e("li",t.$selectionUl).removeClass("ms-hover")}),t.$selectableUl.on("focusin",function(){e(this).addClass("ms-focus"),t.$selectionUl.focusout()}).on("focusout",function(){e(this).removeClass("ms-focus"),e("li",t.$container).removeClass("ms-hover")}),t.$selectionUl.on("focusin",function(){e(this).addClass("ms-focus")}).on("focusout",function(){e(this).removeClass("ms-focus"),e("li",t.$container).removeClass("ms-hover")}),s.on("focusin",function(){s.focusout(),t.$selectableUl.focusin()}).on("focusout",function(){t.$selectableUl.removeClass("ms-focus"),t.$selectionUl.removeClass("ms-focus")}),s.onKeyDown=function(e,s){var l=t.$container.find("."+s).find(".ms-list"),i=l.find("li:visible:not(.ms-optgroup-label, .ms-optgroup-container)"),o=i.length,n=l.find("li.ms-hover"),a=n.length>0?i.index(n):-1,c=l.innerHeight(),r=i.first().outerHeight(!0),d=Math.floor(c/r),p=null;if(32===e.keyCode)n.length>0&&("ms-selectable"===s?t.select(n.data("ms-value")):t.deselect(n.data("ms-value")),i.removeClass("ms-hover"),t.scrollTo=0,l.scrollTop(t.scrollTo));else if(40===e.keyCode){if(i.length>0){var f=a+1,u=o!==f?i.eq(f):i.first(),h=u.position().top;p=l.position().top,i.removeClass("ms-hover"),u.addClass("ms-hover"),o===f?t.scrollTo=0:h>=p+d*r&&(t.scrollTo+=r),l.scrollTop(t.scrollTo)}}else if(38===e.keyCode){if(i.length>0){var m=Math.max(a-1,-1),b=i.eq(m),v=b.position().top;p=l.position().top,i.removeClass("ms-hover"),b.addClass("ms-hover"),p>=v?t.scrollTo-=r:0>m&&(t.scrollTo=(o-d)*r),l.scrollTop(t.scrollTo)}}else(37===e.keyCode||39===e.keyCode)&&(t.$selectableUl.hasClass("ms-focus")?(t.$selectableUl.focusout(),t.$selectionUl.focusin()):(t.$selectableUl.focusin(),t.$selectionUl.focusout()))},s.on("keydown",function(e){if(s.is(":focus")){var l=t.$selectableUl.hasClass("ms-focus")?"ms-selectable":"ms-selection";s.onKeyDown(e,l)}})}var d=s.find("option:selected").map(function(){return e(this).val()}).get();t.select(d,"init"),"function"==typeof t.options.afterInit&&t.options.afterInit.call(this,this.$container)},refresh:function(){e("#ms-"+this.$element.attr("id")).remove(),this.init(this.options)},select:function(t,s){"string"==typeof t&&(t=[t]);var l=this,i=this.$element,o=e.map(t,function(e){return l.sanitize(e,l.sanitizeRegexp)}),n=this.$selectableUl.find("#"+o.join("-selectable, #")+"-selectable").filter(":not(."+l.options.disabledClass+")"),a=this.$selectionUl.find("#"+o.join("-selection, #")+"-selection"),c=i.find("option").filter(function(){return e.inArray(this.value,t)>-1});if(n.length>0){n.addClass("ms-selected").hide(),a.addClass("ms-selected").show(),c.prop("selected",!0);var r=l.$selectableUl.children(".ms-optgroup-container");if(r.length>0){r.each(function(){var t=e(this).find(".ms-elem-selectable");t.length===t.filter(".ms-selected").length&&e(this).find(".ms-optgroup-label").hide()});var d=l.$selectionUl.children(".ms-optgroup-container");d.each(function(){var t=e(this).find(".ms-elem-selection");t.filter(".ms-selected").length>0&&e(this).find(".ms-optgroup-label").show()})}"init"!==s&&(l.$selectionUl.focusout(),l.$selectableUl.focusin(),i.trigger("change"),"function"==typeof l.options.afterSelect&&l.options.afterSelect.call(this,t))}},deselect:function(t){"string"==typeof t&&(t=[t]);var s=this,l=this.$element,i=e.map(t,function(e){return s.sanitize(e,s.sanitizeRegexp)}),o=this.$selectableUl.find("#"+i.join("-selectable, #")+"-selectable"),n=this.$selectionUl.find("#"+i.join("-selection, #")+"-selection").filter(".ms-selected"),a=l.find("option").filter(function(){return e.inArray(this.value,t)>-1});if(n.length>0){o.removeClass("ms-selected").show(),n.removeClass("ms-selected").hide(),a.prop("selected",!1);var c=s.$selectableUl.children(".ms-optgroup-container");if(c.length>0){c.each(function(){var t=e(this).find(".ms-elem-selectable");t.filter(":not(.ms-selected)").length>0&&e(this).find(".ms-optgroup-label").show()});var r=s.$selectionUl.children(".ms-optgroup-container");r.each(function(){var t=e(this).find(".ms-elem-selection");0===t.filter(".ms-selected").length&&e(this).find(".ms-optgroup-label").hide()})}this.$selectableUl.focusout(),this.$selectionUl.focusin(),l.trigger("change"),"function"==typeof s.options.afterDeselect&&s.options.afterDeselect.call(this,t)}},select_all:function(){var t=this.$element,s=t.val();if(t.find("option").prop("selected",!0),this.$selectableUl.find(".ms-elem-selectable").addClass("ms-selected").hide(),this.$selectionUl.find(".ms-optgroup-label").show(),this.$selectableUl.find(".ms-optgroup-label").hide(),this.$selectionUl.find(".ms-elem-selection").addClass("ms-selected").show(),this.$selectionUl.focusin(),this.$selectableUl.focusout(),t.trigger("change"),"function"==typeof this.options.afterSelect){var l=e.grep(t.val(),function(t){return e.inArray(t,s)<0});this.options.afterSelect.call(this,l)}},deselect_all:function(){var e=this.$element,t=e.val();e.find("option").prop("selected",!1),this.$selectableUl.find(".ms-elem-selectable").removeClass("ms-selected").show(),this.$selectionUl.find(".ms-optgroup-label").hide(),this.$selectableUl.find(".ms-optgroup-label").show(),this.$selectionUl.find(".ms-elem-selection").removeClass("ms-selected").hide(),this.$selectableUl.focusin(),this.$selectionUl.focusout(),e.trigger("change"),"function"==typeof this.options.afterDeselect&&this.options.afterDeselect.call(this,t)},isDomNode:function(e){return e&&"object"==typeof e&&"number"==typeof e.nodeType&&"string"==typeof e.nodeName},sanitize:function(e,t){return e.replace(t,"_")}},e.fn.multiSelect=function(){var s=arguments[0],l=arguments;return this.each(function(){var i=e(this),o=i.data("multiselect"),n=e.extend({},e.fn.multiSelect.defaults,i.data(),"object"==typeof s&&s);o||i.data("multiselect",o=new t(this,n)),"string"==typeof s?o[s](l[1]):o.init()})},e.fn.multiSelect.defaults={selectableOptgroup:!1,disabledClass:"disabled",dblClick:!1},e.fn.multiSelect.Constructor=t}(window.jQuery);