diff options
Diffstat (limited to 'js/jquery.multi-select.js')
-rw-r--r-- | js/jquery.multi-select.js | 85 |
1 files changed, 39 insertions, 46 deletions
diff --git a/js/jquery.multi-select.js b/js/jquery.multi-select.js index 54da005..9a4e470 100644 --- a/js/jquery.multi-select.js +++ b/js/jquery.multi-select.js @@ -1,6 +1,6 @@ (function($){ - var methods = { + var msMethods = { 'init' : function(options){ this.settings = { @@ -13,19 +13,16 @@ multiSelects.hide(); multiSelects.each(function(){ - var ms = $(this); - var msId = 'ms-'+ms.attr('id'); - var container = $('<div id="'+msId+'" class="ms-container"></div>'); - var selectableContainer = $('<div class="ms-selectable"></div>'); - var selectedContainer = $('<div class="ms-selection"></div>'); - var selectableUl = $('<ul></ul>'); - var selectedUl = $('<ul></ul>'); + var ms = $(this), + container = $('<div id="ms-'+ms.attr('id')+'" class="ms-container"></div>').detach(), + selectableContainer = $('<div class="ms-selectable"></div>').detach(), + selectedContainer = $('<div class="ms-selection"></div>').detach(), + selectableUl = $('<ul></ul>').detach(), + selectedUl = $('<ul></ul>').detach(); ms.data('settings', multiSelects.settings); ms.children('option').each(function(){ - var selectableLi = $('<li ms-value="'+$(this).val()+'"></li>'); - - selectableLi.html($(this).text()); + var selectableLi = $('<li ms-value="'+$(this).val()+'">'+$(this).text()+'</li>').detach(); selectableLi.click(function(){ ms.multiSelect('select', $(this).attr('ms-value')); }); @@ -42,22 +39,19 @@ }); }, 'select' : function(value, method){ - var ms = this; - var msValues = (ms.val() ? ms.val() : []); - var alreadyPresent = $.inArray(value, ms.val()); + var ms = this, + msValues = (ms.val() ? ms.val() : []), + alreadyPresent = $.inArray(value, msValues); if(alreadyPresent == -1 || method == 'init'){ - var option = ms.find('option[value="'+value+'"]'); - var selectedLi = $('<li ms-value="'+value+'"></li>'); - var newValues = $.merge(msValues, [value]); - var selectableUl = $('#ms-'+ms.attr('id')+' .ms-selectable ul'); - var selectedUl = $('#ms-'+ms.attr('id')+' .ms-selection ul'); - var selectableLi = selectableUl.children('li[ms-value="'+value+'"]'); - var text = option.text(); + var selectedLi = $('<li ms-value="'+value+'">'+ms.find('option[value="'+value+'"]').text()+'</li>').detach(), + newValues = $.merge(msValues, [value]), + selectableUl = $('#ms-'+ms.attr('id')+' .ms-selectable ul'), + selectedUl = $('#ms-'+ms.attr('id')+' .ms-selection ul'), + selectableLi = selectableUl.children('li[ms-value="'+value+'"]'); selectableLi.hide(); ms.val(newValues); - selectedLi.html(text); selectedLi.click(function(){ ms.multiSelect('deselect', $(this).attr('ms-value')); }); @@ -67,6 +61,26 @@ } } }, + 'deselect' : function(value){ + var ms = this, + msValues = (ms.val() ? ms.val() : []), + present = false, + newValues = $.map(msValues, function(e){ if(e != value){ return e; }else{ present = true}}); + + if(present){ + 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+'"]').detach(); + + ms.val(newValues); + selectableLi.show(); + selectedLi.remove(); + if (typeof ms.data('settings').afterDeselect == 'function') { + ms.data('settings').afterDeselect.call(this, value, selectedLi.text()); + } + } + }, 'select_all' : function(){ var ms = this; ms.children('option').each(function(){ @@ -78,35 +92,14 @@ ms.children('option').each(function(){ ms.multiSelect('deselect', $(this).val(), 'deselect_all'); }); - }, - 'deselect' : function(value){ - var ms = this; - var msValues = (ms.val() ? ms.val() : []); - var present = false; - var newValues = $.map(msValues, function(e){ if(e != value){ return e; }else{ present = true}}); - - if(present){ - var selectableUl = $('#ms-'+ms.attr('id')+' .ms-selectable ul'); - var selectedUl = $('#ms-'+ms.attr('id')+' .ms-selection ul'); - var selectableLi = selectableUl.children('li[ms-value="'+value+'"]'); - var selectedLi = selectedUl.children('li[ms-value="'+value+'"]'); - var text = selectedLi.text(); - - ms.val(newValues); - selectableLi.show(); - selectedLi.remove(); - if (typeof ms.data('settings').afterDeselect == 'function') { - ms.data('settings').afterDeselect.call(this, value, text); - } - } } } $.fn.multiSelect = function(method){ - if ( methods[method] ) { - return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); + if ( msMethods[method] ) { + return msMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); } else if ( typeof method === 'object' || ! method ) { - return methods.init.apply( this, arguments ); + return msMethods.init.apply( this, arguments ); } else { if(console.log) console.log( 'Method ' + method + ' does not exist on jquery.multiSelect' ); } |