app/assets/javascripts/multi-select.js in multi-select-rails-0.9.11 vs app/assets/javascripts/multi-select.js in multi-select-rails-0.9.12

- old
+ new

@@ -147,22 +147,22 @@ $selectionOptgroup.attr('id', 'optgroup-selection-'+optgroupId); $selectableOptgroup.append($(optgroupTpl)); $selectionOptgroup.append($(optgroupTpl)); if (that.options.selectableOptgroup){ $selectableOptgroup.find('.ms-optgroup-label').on('click', function(){ - var values = $optgroup.children(':not(:selected)').map(function(){ return $(this).val() }).get(); + var values = $optgroup.children(':not(:selected, :disabled)').map(function(){ return $(this).val() }).get(); that.select(values); }); $selectionOptgroup.find('.ms-optgroup-label').on('click', function(){ - var values = $optgroup.children(':selected').map(function(){ return $(this).val() }).get(); + var values = $optgroup.children(':selected:not(:disabled)').map(function(){ return $(this).val() }).get(); that.deselect(values); }); } that.$selectableUl.append($selectableOptgroup); that.$selectionUl.append($selectionOptgroup); } - index = index == undefined ? $selectableOptgroup.children().length : index + 1; + index = index == undefined ? $selectableOptgroup.find('ul').children().length : index + 1; selectableLi.insertAt(index, $selectableOptgroup.children()); selectedLi.insertAt(index, $selectionOptgroup.children()); } else { index = index == undefined ? that.$selectableUl.children().length : index; @@ -242,14 +242,10 @@ $nextElem = null, elemHeight = $elems.first().outerHeight(), containerHeight = $list.height(), containerSelector = '#'+this.$container.prop('id'); - // Deactive mouseenter event when move is active - // It fixes a bug when mouse is over the list - $elems.off('mouseenter'); - $elems.removeClass('ms-hover'); if (direction === 1){ // DOWN $nextElem = $currElem.nextAll(this.elemsSelector).first(); if ($nextElem.length === 0){ @@ -296,44 +292,39 @@ $list.scrollTop(scrollTo); } }, 'selectHighlighted' : function($list){ - var $elems = $list.find(this.elemsSelector), - $highlightedElem = $elems.filter('.ms-hover').first(); + var $elems = $list.find(this.elemsSelector), + $highlightedElem = $elems.filter('.ms-hover').first(); - if ($highlightedElem.length > 0){ - if ($list.parent().hasClass('ms-selectable')){ - this.select($highlightedElem.data('ms-value')); - } else { - this.deselect($highlightedElem.data('ms-value')); - } - $elems.removeClass('ms-hover'); + if ($highlightedElem.length > 0){ + if ($list.parent().hasClass('ms-selectable')){ + this.select($highlightedElem.data('ms-value')); + } else { + this.deselect($highlightedElem.data('ms-value')); } + $elems.removeClass('ms-hover'); + } }, 'switchList' : function($list){ $list.blur(); + this.$container.find(this.elemsSelector).removeClass('ms-hover'); if ($list.parent().hasClass('ms-selectable')){ this.$selectionUl.focus(); } else { this.$selectableUl.focus(); } }, 'activeMouse' : function($list){ var that = this; - var lastMovedDom = false; - $list.on('mousemove', function(){ - if (lastMovedDom === this) return; - lastMovedDom = this; - var elems = $list.find(that.elemsSelector); - elems.on('mouseenter', function(){ - elems.removeClass('ms-hover'); - $(this).addClass('ms-hover'); - }); + $('body').on('mouseenter', that.elemsSelector, function(){ + $(this).parents('.ms-container').find(that.elemsSelector).removeClass('ms-hover'); + $(this).addClass('ms-hover'); }); }, 'refresh' : function() { this.destroy(); @@ -362,12 +353,15 @@ } if (selectables.length > 0){ selectables.addClass('ms-selected').hide(); selections.addClass('ms-selected').show(); + options.prop('selected', true); + that.$container.find(that.elemsSelector).removeClass('ms-hover'); + 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){ @@ -381,12 +375,12 @@ if (selectionsLi.filter('.ms-selected').length > 0){ $(this).find('.ms-optgroup-label').show(); } }); } else { - if (that.options.keepOrder){ - var selectionLiLast = that.$selectionUl.find('.ms-selected'); + if (that.options.keepOrder && method !== 'init'){ + var selectionLiLast = that.$selectionUl.find('.ms-selected'); if((selectionLiLast.length > 1) && (selectionLiLast.last().get(0) != selections.get(0))) { selections.insertAfter(selectionLiLast.last()); } } } @@ -412,10 +406,12 @@ if (selections.length > 0){ selectables.removeClass('ms-selected').show(); selections.removeClass('ms-selected').hide(); options.prop('selected', false); + that.$container.find(that.elemsSelector).removeClass('ms-hover'); + 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){ @@ -472,15 +468,15 @@ this.options.afterDeselect.call(this, values); } }, sanitize: function(value){ - var hash = 0, i, char; + var hash = 0, i, character; if (value.length == 0) return hash; var ls = 0; for (i = 0, ls = value.length; i < ls; i++) { - char = value.charCodeAt(i); - hash = ((hash<<5)-hash)+char; + character = value.charCodeAt(i); + hash = ((hash<<5)-hash)+character; hash |= 0; // Convert to 32bit integer } return hash; } };