vendor/assets/javascripts/multi-select.js in multi-select-rails-0.9.2 vs vendor/assets/javascripts/multi-select.js in multi-select-rails-0.9.3

- old
+ new

@@ -1,7 +1,7 @@ /* -* MultiSelect v0.9.2 +* MultiSelect v0.9.3 * 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 @@ -102,14 +102,24 @@ } } 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(); + var value = $(this).val(), + msId = that.sanitize(value); + selectableLi + .data('ms-value', value) + .addClass('ms-elem-selectable') + .attr('id', msId+'-selectable'); + + selectedLi + .data('ms-value', value) + .addClass('ms-elem-selection') + .attr('id', msId+'-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); @@ -123,19 +133,21 @@ that.$selectionUl.append(selectedLi); } } }); - if (that.options.selectableHeader){ + if (that.options.selectableHeader) that.$selectableContainer.append(that.options.selectableHeader); - } that.$selectableContainer.append(that.$selectableUl); - - if (that.options.selectionHeader){ + if (that.options.selectableFooter) + that.$selectableContainer.append(that.options.selectableFooter); + + if (that.options.selectionHeader) that.$selectionContainer.append(that.options.selectionHeader); - } - this.$selectionContainer.append(that.$selectionUl); + that.$selectionContainer.append(that.$selectionUl); + if (that.options.selectionFooter) + that.$selectionContainer.append(that.options.selectionFooter); that.$container.append(that.$selectableContainer); that.$container.append(that.$selectionContainer); ms.after(that.$container); that.$selectableUl.on('mouseenter', '.ms-elem-selectable', function(){ @@ -145,21 +157,21 @@ $('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.select($(this).data('ms-value')); }); that.$selectionUl.on('dblclick', '.ms-elem-selection', function(){ - that.deselect($(this).attr('id').replace(/-selection/, '')); + that.deselect($(this).data('ms-value')); }); } else { that.$selectableUl.on('click', '.ms-elem-selectable', function(){ - that.select($(this).attr('id').replace(/-selectable/, '')); + that.select($(this).data('ms-value')); }); that.$selectionUl.on('click', '.ms-elem-selection', function(){ - that.deselect($(this).attr('id').replace(/-selection/, '')); + that.deselect($(this).data('ms-value')); }); } that.$selectionUl.on('mouseenter', '.ms-elem-selection', function(){ @@ -204,13 +216,13 @@ if (e.keyCode == 32){ // space if (liFocused.length >0){ var method = keyContainer == 'ms-selectable' ? 'select' : 'deselect'; if (keyContainer == 'ms-selectable'){ - that.select(liFocused.attr('id').replace('-selectable', '')); + that.select(liFocused.data('ms-value')); } else { - that.deselect(liFocused.attr('id').replace('-selection', '')); + that.deselect(liFocused.data('ms-value')); } lis.removeClass('ms-hover'); that.scrollTo = 0; ul.scrollTop(that.scrollTo); } @@ -271,21 +283,24 @@ if (typeof that.options.afterInit == 'function') { that.options.afterInit.call(this, this.$container); } }, + 'refresh' : function() { $("#ms-"+this.$element.attr("id")).remove(); this.init(this.options); }, + 'select' : function(value, method){ 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'), + msIds = $.map(value, function(val, index){ return(that.sanitize(val)) }), + selectables = this.$selectableUl.find('#' + msIds.join('-selectable, #')+'-selectable').filter(':not(.'+that.options.disabledClass+')'), + selections = this.$selectionUl.find('#' + msIds.join('-selection, #') + '-selection'), options = ms.find('option').filter(function(index){ return($.inArray(this.value, value) > -1) }); if (selectables.length > 0){ selectables.addClass('ms-selected').hide(); selections.addClass('ms-selected').show(); @@ -316,17 +331,19 @@ that.options.afterSelect.call(this, value); } } } }, + 'deselect' : function(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'), + msIds = $.map(value, function(val, index){ return(that.sanitize(val)) }), + selectables = this.$selectableUl.find('#' + msIds.join('-selectable, #')+'-selectable'), + selections = this.$selectionUl.find('#' + msIds.join('-selection, #')+'-selection').filter('.ms-selected'), options = ms.find('option').filter(function(index){ return($.inArray(this.value, value) > -1) }); if (selections.length > 0){ selectables.removeClass('ms-selected').show(); selections.removeClass('ms-selected').hide(); @@ -355,10 +372,11 @@ if (typeof that.options.afterDeselect == 'function') { that.options.afterDeselect.call(this, value); } } }, + 'select_all' : function(){ var ms = this.$element; ms.find('option').prop('selected', true); this.$selectableUl.find('.ms-elem-selectable').addClass('ms-selected').hide(); @@ -367,10 +385,11 @@ 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.$element; ms.find('option').prop('selected', false); this.$selectableUl.find('.ms-elem-selectable').removeClass('ms-selected').show(); @@ -379,16 +398,21 @@ 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" ); + }, + + sanitize: function(value){ + return(value.replace(/[^A-Za-z0-9]*/gi, '_')); } } /* MULTISELECT PLUGIN DEFINITION * ======================= */ \ No newline at end of file