(function($){var defaults={columns:1,search:false,searchOptions:{delay:250,showOptGroups:false,searchText:true,searchValue:false,onSearch:function(element){}},texts:{placeholder:'Select options',search:'Search',selectedOptions:'Selected ',selectAll:'Select all',unselectAll:'Unselect all',noneSelected:'None Selected'},selectAll:false,selectGroup:false,minHeight:200,maxHeight:null,maxWidth:null,maxPlaceholderWidth:null,maxPlaceholderOpts:10,showCheckbox:true,checkboxAutoFit:false,optionAttributes:[],onLoad:function(element){},onOptionClick:function(element,option){},onControlClose:function(element){},onSelectAll:function(element,selected){},onPlaceholder:function(element,placeholder,selectedOpts){},};var msCounter=1;var msOptCounter=1;if(typeof Array.prototype.map!=='function'){Array.prototype.map=function(callback,thisArg){if(typeof thisArg==='undefined'){thisArg=this;} return $.isArray(thisArg)?$.map(thisArg,callback):[];};} if(typeof String.prototype.trim!=='function'){String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,'');};} function MultiSelect(element,options) {this.element=element;this.options=$.extend(true,{},defaults,options);this.updateSelectAll=true;this.updatePlaceholder=true;this.listNumber=msCounter;msCounter=msCounter+1;if(!$(this.element).attr('multiple')){throw new Error('[jQuery-MultiSelect] Select list must be a multiselect list in order to use this plugin');} if(this.options.search){if(!this.options.searchOptions.searchText&&!this.options.searchOptions.searchValue){throw new Error('[jQuery-MultiSelect] Either searchText or searchValue should be true.');}} if('placeholder'in this.options){this.options.texts.placeholder=this.options.placeholder;delete this.options.placeholder;} if('default'in this.options.searchOptions){this.options.texts.search=this.options.searchOptions['default'];delete this.options.searchOptions['default'];} this.load();} MultiSelect.prototype={load:function(){var instance=this;if((instance.element.nodeName!='SELECT')||$(instance.element).hasClass('jqmsLoaded')){return true;} $(instance.element).addClass('jqmsLoaded ms-list-'+instance.listNumber).data('plugin_multiselect-instance',instance);$(instance.element).after('
');var placeholder=$(instance.element).siblings('#ms-list-'+instance.listNumber+'.ms-options-wrap').find('> button:first-child');var optionsWrap=$(instance.element).siblings('#ms-list-'+instance.listNumber+'.ms-options-wrap').find('> .ms-options');var optionsList=optionsWrap.find('> ul');if(!instance.options.showCheckbox){optionsWrap.addClass('hide-checkbox');} else if(instance.options.checkboxAutoFit){optionsWrap.addClass('checkbox-autofit');} if($(instance.element).prop('disabled')){placeholder.prop('disabled',true);} if(instance.options.maxPlaceholderWidth){placeholder.css('maxWidth',instance.options.maxPlaceholderWidth);} if(instance.options.maxHeight){var maxHeight=instance.options.maxHeight;} else{var maxHeight=($(window).height()-optionsWrap.offset().top+$(window).scrollTop()-20);} maxHeight=maxHeight$(this)[0].scrollHeight){e.preventDefault();this.scrollTop+=(delta<0?1:-1);}}});$(document).off('click.ms-hideopts').on('click.ms-hideopts',function(event){if(!$(event.target).closest('.ms-options-wrap').length){$('.ms-options-wrap.ms-active > .ms-options').each(function(){$(this).closest('.ms-options-wrap').removeClass('ms-active');var listID=$(this).closest('.ms-options-wrap').attr('id');var thisInst=$(this).parent().siblings('.'+listID+'.jqmsLoaded').data('plugin_multiselect-instance');if(typeof thisInst.options.onControlClose=='function'){thisInst.options.onControlClose(thisInst.element);}});}}).on('keydown',function(event){if((event.keyCode||event.which)==27){$(this).trigger('click.ms-hideopts');}});placeholder.on('keydown',function(event){var code=(event.keyCode||event.which);if((code==13)||(code==32)){placeholder.trigger('mousedown');}});placeholder.on('mousedown',function(event){if(event.which&&(event.which!=1)){return true;} $('.ms-options-wrap.ms-active').each(function(){if($(this).siblings('.'+$(this).attr('id')+'.jqmsLoaded')[0]!=optionsWrap.parent().siblings('.ms-list-'+instance.listNumber+'.jqmsLoaded')[0]){$(this).removeClass('ms-active');var thisInst=$(this).siblings('.'+$(this).attr('id')+'.jqmsLoaded').data('plugin_multiselect-instance');if(typeof thisInst.options.onControlClose=='function'){thisInst.options.onControlClose(thisInst.element);}}});optionsWrap.closest('.ms-options-wrap').toggleClass('ms-active');if(optionsWrap.closest('.ms-options-wrap').hasClass('ms-active')){optionsWrap.css('maxHeight','');if(instance.options.maxHeight){var maxHeight=instance.options.maxHeight;} else{var maxHeight=($(window).height()-optionsWrap.offset().top+$(window).scrollTop()-20);} if(maxHeight){maxHeight=maxHeight');var search=optionsWrap.find('.ms-search input');search.on('keyup',function(){if($(this).data('lastsearch')==$(this).val()){return true;} if($(this).data('searchTimeout')){clearTimeout($(this).data('searchTimeout'));} var thisSearchElem=$(this);$(this).data('searchTimeout',setTimeout(function(){thisSearchElem.data('lastsearch',thisSearchElem.val());if(typeof instance.options.searchOptions.onSearch=='function'){instance.options.searchOptions.onSearch(instance.element);} var searchString=$.trim(search.val().toLowerCase());if(searchString){optionsList.find('li[data-search-term*="'+searchString+'"]:not(.optgroup)').removeClass('ms-hidden');optionsList.find('li:not([data-search-term*="'+searchString+'"], .optgroup)').addClass('ms-hidden');} else{optionsList.find('.ms-hidden').removeClass('ms-hidden');} if(!instance.options.searchOptions.showOptGroups){optionsList.find('.optgroup').each(function(){if($(this).find('li:not(.ms-hidden)').length){$(this).show();} else{$(this).hide();}});} instance._updateSelectAllText();},instance.options.searchOptions.delay));});} if(instance.options.selectAll){optionsList.before(''+instance.options.texts.selectAll+'');} optionsWrap.on('click','.ms-selectall',function(event){event.preventDefault();instance.updateSelectAll=false;instance.updatePlaceholder=false;var select=optionsWrap.parent().siblings('.ms-list-'+instance.listNumber+'.jqmsLoaded');if($(this).hasClass('global')){if(optionsList.find('li:not(.optgroup, .selected, .ms-hidden) input[type="checkbox"]:not(:disabled)').length){optionsList.find('li:not(.optgroup, .selected, .ms-hidden) input[type="checkbox"]:not(:disabled)').closest('li').addClass('selected');optionsList.find('li.selected input[type="checkbox"]:not(:disabled)').prop('checked',true);} else{optionsList.find('li:not(.optgroup, .ms-hidden).selected input[type="checkbox"]:not(:disabled)').closest('li').removeClass('selected');optionsList.find('li:not(.optgroup, .ms-hidden, .selected) input[type="checkbox"]:not(:disabled)').prop('checked',false);}} else if($(this).closest('li').hasClass('optgroup')){var optgroup=$(this).closest('li.optgroup');if(optgroup.find('li:not(.selected, .ms-hidden) input[type="checkbox"]:not(:disabled)').length){optgroup.find('li:not(.selected, .ms-hidden) input[type="checkbox"]:not(:disabled)').closest('li').addClass('selected');optgroup.find('li.selected input[type="checkbox"]:not(:disabled)').prop('checked',true);} else{optgroup.find('li:not(.ms-hidden).selected input[type="checkbox"]:not(:disabled)').closest('li').removeClass('selected');optgroup.find('li:not(.ms-hidden, .selected) input[type="checkbox"]:not(:disabled)').prop('checked',false);}} var vals=[];optionsList.find('li.selected input[type="checkbox"]').each(function(){vals.push($(this).val());});select.val(vals).trigger('change');instance.updateSelectAll=true;instance.updatePlaceholder=true;if(typeof instance.options.onSelectAll=='function'){instance.options.onSelectAll(instance.element,vals.length);} instance._updateSelectAllText();instance._updatePlaceholderText();});var options=[];$(instance.element).children().each(function(){if(this.nodeName=='OPTGROUP'){var groupOptions=[];$(this).children('option').each(function(){var thisOptionAtts={};for(var i=0;i .ms-options > ul');var optionsWrap=select.siblings('#ms-list-'+instance.listNumber+'.ms-options-wrap').find('> .ms-options');if(overwrite){optionsList.find('> li').remove();if(updateSelect){select.find('> *').remove();}} var containers=[];for(var key in options){if(!options.hasOwnProperty(key)){continue;} var thisOption=options[key];var container=$('
  • ');var appendContainer=true;if(thisOption.hasOwnProperty('value')){if(instance.options.showCheckbox&&instance.options.checkboxAutoFit){container.addClass('ms-reflow');} instance._addOption(container,thisOption);if(updateSelect){var selOption=$('',{label:thisOption.label});optionsList.find('> li.optgroup > span.label').each(function(){if($(this).text()==thisOption.label){container=$(this).closest('.optgroup');appendContainer=false;}});if(updateSelect){if(select.find('optgroup[label="'+thisOption.label+'"]').length){optGroup=select.find('optgroup[label="'+thisOption.label+'"]');} else{select.append(optGroup);}} if(appendContainer){container.addClass('optgroup');container.append(''+thisOption.label+'');container.find('> .label').css({clear:'both'});if(instance.options.selectGroup){container.append(''+instance.options.texts.selectAll+'');} container.append('
      ');} for(var gKey in thisOption.options){if(!thisOption.options.hasOwnProperty(gKey)){continue;} var thisGOption=thisOption.options[gKey];var gContainer=$('
    • ');if(instance.options.showCheckbox&&instance.options.checkboxAutoFit){gContainer.addClass('ms-reflow');} if(!thisGOption.hasOwnProperty('value')){continue;} instance._addOption(gContainer,thisGOption);container.find('> ul').append(gContainer);if(updateSelect){var selOption=$('