vendor/assets/javascripts/multi-select.js in multi-select-rails-0.8.0.1 vs vendor/assets/javascripts/multi-select.js in multi-select-rails-0.9.1

- old
+ new

@@ -1,334 +1,398 @@ /* -* MultiSelect v0.8 +* MultiSelect v0.9.1 * Copyright (c) 2012 Louis Cuny * * This program is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The Fuck You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://sam.zoy.org/wtfpl/COPYING for more details. */ -(function($){ - var msMethods = { - 'init' : function(options){ - this.settings = { - disabledClass : 'disabled', - selectCallbackOnInit: false, - keepOrder : false, - dblClick : false - }; - if(options) { - this.settings = $.extend(this.settings, options); - } - var multiSelects = this; - multiSelects.css('position', 'absolute').css('left', '-9999px'); - return multiSelects.each(function(){ - var ms = $(this); +!function ($) { - if (ms.next('.ms-container').length == 0){ - ms.attr('id', ms.attr('id') ? ms.attr('id') : 'ms-'+Math.ceil(Math.random()*1000)); - var container = $('<div id="ms-'+ms.attr('id')+'" class="ms-container"></div>'), - selectableContainer = $('<div class="ms-selectable"></div>'), - selectedContainer = $('<div class="ms-selection"></div>'), - selectableUl = $('<ul class="ms-list"></ul>'), - selectedUl = $('<ul class="ms-list"></ul>'); + "use strict"; // jshint ;_; - ms.data('settings', multiSelects.settings); - var optgroupLabel = null, - optgroupId = null, - optgroupCpt = 0, - scroll = 0; - ms.find('optgroup,option').each(function(){ - if ($(this).is('optgroup')){ - optgroupLabel = $(this).attr('label'); - optgroupId = 'ms-'+ms.attr('id')+'-optgroup-'+optgroupCpt; - selectableUl.append($('<li class="ms-optgroup-container" id="'+ - optgroupId+'"><ul class="ms-optgroup"><li class="ms-optgroup-label">'+ - optgroupLabel+'</li></ul></li>')); - optgroupCpt++; - } else { - var klass = $(this).attr('class') ? ' '+$(this).attr('class') : ''; - var selectableLi = $('<li class="ms-elem-selectable'+klass+'" ms-value="'+$(this).val()+'">'+$(this).text()+'</li>'); + /* MULTISELECT CLASS DEFINITION + * ====================== */ - if ($(this).attr('title')) - selectableLi.attr('title', $(this).attr('title')); - if ($(this).attr('disabled') || ms.attr('disabled')){ - selectableLi.attr('disabled', 'disabled'); - selectableLi.addClass(multiSelects.settings.disabledClass); + var MultiSelect = function (element, options) { + this.options = options; + this.$element = $(element); + this.$container = $('<div id="ms-'+this.$element.attr('id')+'" class="ms-container"></div>'); + this.$selectableContainer = $('<div class="ms-selectable"></div>'); + this.$selectionContainer = $('<div class="ms-selection"></div>'); + this.$selectableUl = $('<ul class="ms-list"></ul>'); + this.$selectionUl = $('<ul class="ms-list"></ul>'); + this.scrollTo = 0; + } + + MultiSelect.prototype = { + constructor: MultiSelect, + + init: function(){ + var that = this, + ms = this.$element; + + if (ms.next('.ms-container').length == 0){ + ms.css({ position: 'absolute', left: '-9999px' }); + ms.attr('id', ms.attr('id') ? ms.attr('id') : 'ms-'+Math.ceil(Math.random()*1000)); + + + + var optgroupLabel = null, + optgroupId = null, + optgroupCpt = 0, + scroll = 0; + + + ms.find('optgroup, option').each(function(){ + if ($(this).is('optgroup')){ + optgroupLabel = $(this).attr('label'); + optgroupId = 'ms-'+ms.attr('id')+'-optgroup-'+optgroupCpt; + + that.$selectableUl.append( + '<li class="ms-optgroup-container" id="'+optgroupId+'-selectable">\ + <ul class="ms-optgroup">\ + <li class="ms-optgroup-label"><span>'+optgroupLabel+'</span></li>\ + </ul>\ + </li>'); + that.$selectionUl.append( + '<li class="ms-optgroup-container" id="'+optgroupId+'-selection">\ + <ul class="ms-optgroup">\ + <li class="ms-optgroup-label"><span>'+optgroupLabel+'</span></li>\ + </ul>\ + </li>'); + optgroupCpt++; + } else { + + var attributes = ""; + + for (var cpt = 0; cpt < this.attributes.length; cpt++){ + var attr = this.attributes[cpt]; + + if(that.isDomNode(attr.name)){ + attributes += attr.name+'="'+attr.value+'" '; } - if(multiSelects.settings.dblClick) { - selectableLi.dblclick(function(){ - ms.multiSelect('select', $(this).attr('ms-value')); - }); - } else { - selectableLi.click(function(){ - ms.multiSelect('select', $(this).attr('ms-value')); - }); - } - var container = optgroupId ? selectableUl.children('#'+optgroupId).find('ul').first() : selectableUl; - container.append(selectableLi); } - }); - if (multiSelects.settings.selectableHeader){ - selectableContainer.append(multiSelects.settings.selectableHeader); + var selectableLi = $('<li '+attributes+'><span>'+$(this).text()+'</span></li>'), + selectedLi = selectableLi.clone(); + + var value = $(this).val(); + selectableLi.addClass('ms-elem-selectable').attr('id', value+'-selectable'); + selectedLi.addClass('ms-elem-selection').attr('id', value+'-selection').hide(); + + that.$selectionUl.find('.ms-optgroup-label').hide(); + + if ($(this).prop('disabled') || ms.prop('disabled')){ + selectableLi.prop('disabled', true); + selectableLi.addClass(that.options.disabledClass); + } + + if (optgroupId){ + that.$selectableUl.children('#'+optgroupId+'-selectable').find('ul').first().append(selectableLi); + that.$selectionUl.children('#'+optgroupId+'-selection').find('ul').first().append(selectedLi); + } else { + that.$selectableUl.append(selectableLi); + that.$selectionUl.append(selectedLi); + } } - selectableContainer.append(selectableUl); - if (multiSelects.settings.selectedHeader){ - selectedContainer.append(multiSelects.settings.selectedHeader); - } - selectedContainer.append(selectedUl); - container.append(selectableContainer); - container.append(selectedContainer); - ms.after(container); - ms.find('option:selected').each(function(){ - ms.multiSelect('select', $(this).val(), 'init'); - }); + }); - $('.ms-elem-selectable', selectableUl).on('mouseenter', function(){ - $('li', container).removeClass('ms-hover'); - $(this).addClass('ms-hover'); - }).on('mouseout', function(){ - $('li', container).removeClass('ms-hover'); + if (that.options.selectableHeader){ + that.$selectableContainer.append(that.options.selectableHeader); + } + that.$selectableContainer.append(that.$selectableUl); + + if (that.options.selectionHeader){ + that.$selectionContainer.append(that.options.selectionHeader); + } + this.$selectionContainer.append(that.$selectionUl); + + that.$container.append(that.$selectableContainer); + that.$container.append(that.$selectionContainer); + ms.after(that.$container); + that.$selectableUl.on('mouseenter', '.ms-elem-selectable', function(){ + $('li', that.$container).removeClass('ms-hover'); + $(this).addClass('ms-hover'); + }).on('mouseleave', function(){ + $('li', that.$container).removeClass('ms-hover'); + }); + + if(that.options.dblClick) { + that.$selectableUl.on('dblclick', '.ms-elem-selectable', function(){ + that.select($(this).attr('id').replace(/-selectable/, '')); }); + that.$selectionUl.on('dblclick', '.ms-elem-selection', function(){ + that.deselect($(this).attr('id').replace(/-selection/, '')); + }); + } else { + that.$selectableUl.on('click', '.ms-elem-selectable', function(){ + that.select($(this).attr('id').replace(/-selectable/, '')); + }); + that.$selectionUl.on('click', '.ms-elem-selection', function(){ + that.deselect($(this).attr('id').replace(/-selection/, '')); + }); + } + that.$selectionUl.on('mouseenter', '.ms-elem-selection', function(){ + $('li', that.$selectionUl).removeClass('ms-hover'); + $(this).addClass('ms-hover'); + }).on('mouseleave', function(){ + $('li', that.$selectionUl).removeClass('ms-hover'); + }); - selectableUl.on('focusin', function(){ - $(this).addClass('ms-focus'); - selectedUl.focusout(); - }).on('focusout', function(){ - $(this).removeClass('ms-focus'); - $('li', container).removeClass('ms-hover'); - }); + that.$selectableUl.on('focusin', function(){ + $(this).addClass('ms-focus'); + that.$selectionUl.focusout(); + }).on('focusout', function(){ + $(this).removeClass('ms-focus'); + $('li', that.$container).removeClass('ms-hover'); + }); - selectedUl.on('focusin', function(){ - $(this).addClass('ms-focus'); - }).on('focusout', function(){ - $(this).removeClass('ms-focus'); - $('li', container).removeClass('ms-hover'); - }); + that.$selectionUl.on('focusin', function(){ + $(this).addClass('ms-focus'); + }).on('focusout', function(){ + $(this).removeClass('ms-focus'); + $('li', that.$container).removeClass('ms-hover'); + }); - ms.on('focusin', function(){ - selectableUl.focus(); - }).on('focusout', function(){ - selectableUl.removeClass('ms-focus'); - selectedUl.removeClass('ms-focus'); - }); + ms.on('focusin', function(){ + ms.focusout(); + that.$selectableUl.focusin(); + }).on('focusout', function(){ + that.$selectableUl.removeClass('ms-focus'); + that.$selectionUl.removeClass('ms-focus'); + }); - ms.onKeyDown = function(e, keyContainer){ - var selectables = $('.'+keyContainer+' li:visible:not(.ms-optgroup-label, .ms-optgroup-container)', container), - selectablesLength = selectables.length, - selectableFocused = $('.'+keyContainer+' li.ms-hover', container), - selectableFocusedIndex = $('.'+keyContainer+' li:visible:not(.ms-optgroup-label, .ms-optgroup-container)', container).index(selectableFocused), - liHeight = selectables.first().outerHeight(), - numberOfItemsDisplayed = Math.ceil(container.outerHeight()/liHeight), - scrollStart = Math.ceil(numberOfItemsDisplayed/4); + ms.onKeyDown = function(e, keyContainer){ + var ul = that.$container.find('.'+keyContainer).find('.ms-list'), + lis = ul.find('li:visible:not(.ms-optgroup-label, .ms-optgroup-container)'), + lisNumber = lis.length, + liFocused = ul.find('li.ms-hover'), + liFocusedIndex = liFocused.length > 0 ? lis.index(liFocused) : -1, + ulHeight = ul.innerHeight(), + liHeight = lis.first().outerHeight(true), + numberOfLisDisplayed = Math.floor(ulHeight / liHeight); - selectables.removeClass('ms-hover'); - if (e.keyCode == 32){ // space + if (e.keyCode == 32){ // space + if (liFocused.length >0){ var method = keyContainer == 'ms-selectable' ? 'select' : 'deselect'; - ms.multiSelect(method, selectableFocused.first().attr('ms-value')); + if (keyContainer == 'ms-selectable'){ + that.select(liFocused.attr('id').replace('-selectable', '')); + } else { + that.deselect(liFocused.attr('id').replace('-selection', '')); + } + lis.removeClass('ms-hover'); + that.scrollTo = 0; + ul.scrollTop(that.scrollTo); + } + } else if (e.keyCode == 40){ // Down + if (lis.length > 0){ + var nextLiIndex = liFocusedIndex+1, + nextLi = (lisNumber != nextLiIndex) ? lis.eq(nextLiIndex) : lis.first(), + ulPosition = ul.position().top, + nextLiPosition = nextLi.position().top; - } else if (e.keyCode == 40){ // Down - var nextIndex = (selectableFocusedIndex+1 != selectablesLength) ? selectableFocusedIndex+1 : 0, - nextSelectableLi = selectables.eq(nextIndex); + lis.removeClass('ms-hover'); + nextLi.addClass('ms-hover'); - nextSelectableLi.addClass('ms-hover'); - if (nextIndex > scrollStart){ - scroll += liHeight; - } else if (nextIndex == 0){ - scroll = 0; + if (lisNumber == nextLiIndex){ + that.scrollTo = 0; + } else if (nextLiPosition >= (ulPosition + (numberOfLisDisplayed * liHeight))){ + that.scrollTo += liHeight; } - $('.'+keyContainer+' ul', container).scrollTop(scroll); - } else if (e.keyCode == 38){ // Up - var prevIndex = (selectableFocusedIndex-1 >= 0) ? selectableFocusedIndex-1 : selectablesLength-1, - prevSelectableLi = selectables.eq(prevIndex); - selectables.removeClass('ms-hover'); - prevSelectableLi.addClass('ms-hover'); - if (selectablesLength-prevIndex+1 < scrollStart){ - scroll = liHeight*(selectablesLength-scrollStart); - } else { - scroll -= liHeight; - } - $('.'+keyContainer+' ul', container).scrollTop(scroll); - } else if (e.keyCode == 37 || e.keyCode == 39){ // Right and Left - if (selectableUl.hasClass('ms-focus')){ - selectableUl.focusout(); - selectedUl.focusin(); - } else { - selectableUl.focusin(); - selectedUl.focusout(); - } + ul.scrollTop(that.scrollTo); } - } + } else if (e.keyCode == 38){ // Up + if (lis.length > 0){ + var prevLiIndex = Math.max(liFocusedIndex-1, -1), + prevLi = lis.eq(prevLiIndex), + ulPosition = ul.position().top, + prevLiPosition = prevLi.position().top; - ms.on('keydown', function(e){ - if (ms.is(':focus')){ - var keyContainer = selectableUl.hasClass('ms-focus') ? 'ms-selectable' : 'ms-selection'; - ms.onKeyDown(e, keyContainer); + lis.removeClass('ms-hover'); + prevLi.addClass('ms-hover'); + if (prevLiPosition <= ulPosition){ + that.scrollTo -= liHeight; + } else if (prevLiIndex < 0){ + that.scrollTo = (lisNumber - numberOfLisDisplayed) * liHeight; + } + ul.scrollTop(that.scrollTo); } - }); + } else if (e.keyCode == 37 || e.keyCode == 39){ + if (that.$selectableUl.hasClass('ms-focus')){ + that.$selectableUl.focusout(); + that.$selectionUl.focusin(); + } else { + that.$selectableUl.focusin(); + that.$selectionUl.focusout(); + } + } } - }); + + ms.on('keydown', function(e){ + if (ms.is(':focus')){ + var keyContainer = that.$selectableUl.hasClass('ms-focus') ? 'ms-selectable' : 'ms-selection'; + ms.onKeyDown(e, keyContainer); + } + }); + } + + var selectedValues = ms.find('option:selected').map(function(){ return $(this).val() }).get(); + that.select(selectedValues, 'init') + + if (typeof that.options.afterInit == 'function') { + that.options.afterInit.call(this, this.$container); + } }, 'refresh' : function() { - $("#ms-"+$(this).attr("id")).remove(); - $(this).multiSelect("init", $(this).data("settings")); + $("#ms-"+this.$element.attr("id")).remove(); + this.init(this.options); }, 'select' : function(value, method){ - var ms = this, - selectedOption = ms.find('option[value="'+value +'"]'), - text = selectedOption.text(), - klass = selectedOption.attr('class'), - titleAttr = selectedOption.attr('title'); + if (typeof value == 'string') + value = [value] + var that = this, + ms = this.$element, + selectables = this.$selectableUl.find('#' + value.join('-selectable, #')+'-selectable').filter(':not(.'+that.options.disabledClass+')'), + selections = this.$selectionUl.find('#' + value.join('-selection, #') + '-selection'), + options = ms.find('option').filter(function(index){ return($.inArray(this.value, value) > -1) }); - var selectedLi = $('<li class="ms-elem-selected'+(klass ? ' '+klass : '')+'" ms-value="'+value+'">'+text+'</li>'), - selectableUl = $('#ms-'+ms.attr('id')+' .ms-selectable ul'), - selectedUl = $('#ms-'+ms.attr('id')+' .ms-selection ul'), - selectableLi = selectableUl.children('li[ms-value="'+value+'"]'), - haveToSelect = null; + if (selectables.length > 0){ + selectables.addClass('ms-selected').hide(); + selections.addClass('ms-selected').show(); + options.prop('selected', true); - if (method == 'init'){ - haveToSelect = !selectableLi.hasClass(ms.data('settings').disabledClass) && selectedOption.prop('selected'); - } else { - haveToSelect = !selectableLi.hasClass(ms.data('settings').disabledClass); - ms.focus(); - } - if (haveToSelect && selectedUl.children('li[ms-value="'+value+'"]').length == 0){ - var parentOptgroup = selectableLi.parent('.ms-optgroup'); - if (parentOptgroup.length > 0) - if (parentOptgroup.children('.ms-elem-selectable:not(:hidden)').length == 1) - parentOptgroup.children('.ms-optgroup-label').hide(); - selectableLi.addClass('ms-selected'); - selectableLi.hide(); - selectedOption.prop('selected', true); - if(titleAttr){ - selectedLi.attr('title', titleAttr) - } - if (selectableLi.hasClass(ms.data('settings').disabledClass)){ - selectedLi.addClass(ms.data('settings').disabledClass); - } else { - if(ms.data('settings').dblClick) { - selectedLi.dblclick(function(){ - ms.multiSelect('deselect', $(this).attr('ms-value')); - }); - } else { - selectedLi.click(function(){ - ms.multiSelect('deselect', $(this).attr('ms-value')); - }); - } - } + var selectableOptgroups = that.$selectableUl.children('.ms-optgroup-container'); + if (selectableOptgroups.length > 0){ + selectableOptgroups.each(function(){ + var selectablesLi = $(this).find('.ms-elem-selectable'); + if (selectablesLi.length == selectablesLi.filter('.ms-selected').length){ + $(this).find('.ms-optgroup-label').hide(); + } + }); - var selectedUlLis = selectedUl.children('.ms-elem-selected'); - if (method != 'init' && ms.data('settings').keepOrder && selectedUlLis.length > 0) { - - var getIndexOf = function(value) { - elems = selectableUl.children('.ms-elem-selectable'); - return(elems.index(elems.closest('[ms-value="'+value+'"]'))); - } - - var index = getIndexOf(selectedLi.attr('ms-value')); - if (index == 0) - selectedUl.prepend(selectedLi); - else { - for (i = index - 1; i >= 0; i--){ - if (selectedUlLis[i] && getIndexOf($(selectedUlLis[i]).attr('ms-value')) < index) { - $(selectedUlLis[i]).after(selectedLi); - break; - } else if (i == 0) { - $(selectedUlLis[i]).before(selectedLi); - } + var selectionOptgroups = that.$selectionUl.children('.ms-optgroup-container'); + selectionOptgroups.each(function(){ + var selectionsLi = $(this).find('.ms-elem-selection'); + if (selectionsLi.filter('.ms-selected').length > 0){ + $(this).find('.ms-optgroup-label').show(); } - } - } else { - selectedUl.append(selectedLi); + }); } - selectedLi.on('mouseenter', function(){ - $('li', selectedUl).removeClass('ms-hover'); - $(this).addClass('ms-hover'); - }).on('mouseout', function(){ - $('li', selectedUl).removeClass('ms-hover'); - }); - if (method == "select_all" && parentOptgroup.children('.ms-elem-selectable').length > 0){ - parentOptgroup.children('.ms-optgroup-label').hide(); - } - if(method != 'init' || ms.data('settings').selectCallbackOnInit){ + if (method != 'init'){ + that.$selectionUl.focusout(); + that.$selectableUl.focusin(); ms.trigger('change'); - selectedUl.trigger('change'); - selectableUl.trigger('change'); - if (typeof ms.data('settings').afterSelect == 'function' && - (method != 'init' || ms.data('settings').selectCallbackOnInit)) { - ms.data('settings').afterSelect.call(this, value, text); + if (typeof that.options.afterSelect == 'function') { + that.options.afterSelect.call(this, value); } } } }, 'deselect' : function(value){ - var ms = this, - selectedUl = $('#ms-'+ms.attr('id')+' .ms-selection ul'), - selectedOption = ms.find('option[value="'+value +'"]'), - selectedLi = selectedUl.children('li[ms-value="'+value+'"]'); + if (typeof value == 'string') + value = [value] + var that = this, + ms = this.$element, + selectables = this.$selectableUl.find('#' + value.join('-selectable, #')+'-selectable'), + selections = this.$selectionUl.find('#' + value.join('-selection, #')+'-selection').filter('.ms-selected'), + options = ms.find('option').filter(function(index){ return($.inArray(this.value, value) > -1) }); - if(selectedLi){ - selectedUl.focusin(); - var selectableUl = $('#ms-'+ms.attr('id')+' .ms-selectable ul'), - selectedUl = $('#ms-'+ms.attr('id')+' .ms-selection ul'), - selectableLi = selectableUl.children('li[ms-value="'+value+'"]'), - selectedLi = selectedUl.children('li[ms-value="'+value+'"]'); + if (selections.length > 0){ + selectables.removeClass('ms-selected').show(); + selections.removeClass('ms-selected').hide(); + options.prop('selected', false); - var parentOptgroup = selectableLi.parent('.ms-optgroup'); - if (parentOptgroup.length > 0){ - parentOptgroup.children('.ms-optgroup-label').addClass('ms-collapse').show(); - parentOptgroup.children('.ms-elem-selectable:not(.ms-selected)').show(); + var selectableOptgroups = that.$selectableUl.children('.ms-optgroup-container'); + if (selectableOptgroups.length > 0){ + selectableOptgroups.each(function(){ + var selectablesLi = $(this).find('.ms-elem-selectable'); + if (selectablesLi.filter(':not(.ms-selected)').length > 0){ + $(this).find('.ms-optgroup-label').show(); + } + }); + + var selectionOptgroups = that.$selectionUl.children('.ms-optgroup-container'); + selectionOptgroups.each(function(){ + var selectionsLi = $(this).find('.ms-elem-selection'); + if (selectionsLi.filter('.ms-selected').length == 0){ + $(this).find('.ms-optgroup-label').hide(); + } + }); } - selectedOption.prop('selected', false); - selectableLi.show(); - selectableLi.removeClass('ms-selected'); - selectedLi.remove(); - selectedUl.trigger('change'); - selectableUl.trigger('change'); + this.$selectableUl.focusout(); + this.$selectionUl.focusin(); ms.trigger('change'); - if (typeof ms.data('settings').afterDeselect == 'function') { - ms.data('settings').afterDeselect.call(this, value, selectedLi.text()); + if (typeof that.options.afterDeselect == 'function') { + that.options.afterDeselect.call(this, value); } } }, - 'select_all' : function(visible){ - var ms = this, - selectableUl = $('#ms-'+ms.attr('id')+' .ms-selectable ul'); + 'select_all' : function(){ + var ms = this.$element; - ms.find("option:not(:selected)").each(function(){ - var value = $(this).val(); - if (visible){ - var selectableLi = selectableUl.children('li[ms-value="'+value+'"]'); - if (selectableLi.filter(':visible').length > 0){ - ms.multiSelect('select', value, 'select_all'); - } - } else { - ms.multiSelect('select', value, 'select_all'); - } - }); + ms.find('option').prop('selected', true); + 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(); + ms.trigger('change'); }, 'deselect_all' : function(){ - var ms = this, - selectedUl = $('#ms-'+ms.attr('id')+' .ms-selection ul'); + var ms = this.$element; - ms.find("option:selected").each(function(){ - ms.multiSelect('deselect', $(this).val(), 'deselect_all'); - }); + ms.find('option').prop('selected', false); + 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(); + ms.trigger('change'); + }, + isDomNode: function (attr){ + return ( + attr && + typeof attr === "object" && + typeof attr.nodeType === "number" && + typeof attr.nodeName === "string" + ); } - }; + } - $.fn.multiSelect = function(method){ - if ( msMethods[method] ) { - return msMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); - } else if ( typeof method === 'object' || ! method ) { - return msMethods.init.apply( this, arguments ); - } else { - if(console.log) console.log( 'Method ' + method + ' does not exist on jquery.multiSelect' ); - } - return false; - }; -})(jQuery); + /* MULTISELECT PLUGIN DEFINITION + * ======================= */ + + $.fn.multiSelect = function () { + var option = arguments[0], + args = arguments; + + return this.each(function () { + var $this = $(this), + data = $this.data('multiselect'), + options = $.extend({}, $.fn.multiSelect.defaults, $this.data(), typeof option == 'object' && option); + + if (!data) $this.data('multiselect', (data = new MultiSelect(this, options))) + + if (typeof option == 'string'){ + data[option](args[1]) + } else { + data.init(); + } + }) + } + + $.fn.multiSelect.defaults = { + disabledClass : 'disabled', + dblClick : false + } + + $.fn.multiSelect.Constructor = MultiSelect + +}(window.jQuery); \ No newline at end of file