diff options
Diffstat (limited to 'js/jquery.multi-select.js')
-rw-r--r-- | js/jquery.multi-select.js | 87 |
1 files changed, 66 insertions, 21 deletions
diff --git a/js/jquery.multi-select.js b/js/jquery.multi-select.js index e561d60..fc4164b 100644 --- a/js/jquery.multi-select.js +++ b/js/jquery.multi-select.js @@ -1,5 +1,5 @@ /* -* MultiSelect v0.9.8 +* MultiSelect v0.9.10 * Copyright (c) 2012 Louis Cuny * * This program is free software. It comes without any warranty, to @@ -20,14 +20,12 @@ var MultiSelect = function (element, options) { this.options = options; this.$element = $(element); - this.$container = $('<div/>', { 'class': "ms-container" }); this.$selectableContainer = $('<div/>', { 'class': 'ms-selectable' }); this.$selectionContainer = $('<div/>', { 'class': 'ms-selection' }); this.$selectableUl = $('<ul/>', { 'class': "ms-list", 'tabindex' : '-1' }); this.$selectionUl = $('<ul/>', { 'class': "ms-list", 'tabindex' : '-1' }); this.scrollTo = 0; - this.sanitizeRegexp = new RegExp("\\W+", 'gi'); this.elemsSelector = 'li:visible:not(.ms-optgroup-label,.ms-optgroup-container,.'+options.disabledClass+')'; }; @@ -42,7 +40,7 @@ ms.css({ position: 'absolute', left: '-9999px' }); ms.attr('id', ms.attr('id') ? ms.attr('id') : Math.ceil(Math.random()*1000)+'multiselect'); this.$container.attr('id', 'ms-'+ms.attr('id')); - + this.$container.addClass(that.options.cssClass); ms.find('option').each(function(){ that.generateLisFromOption(this); }); @@ -97,7 +95,7 @@ } }, - 'generateLisFromOption' : function(option){ + 'generateLisFromOption' : function(option, index, $container){ var that = this, ms = that.$element, attributes = "", @@ -110,10 +108,10 @@ attributes += attr.name+'="'+attr.value+'" '; } } - var selectableLi = $('<li '+attributes+'><span>'+$option.text()+'</span></li>'), + var selectableLi = $('<li '+attributes+'><span>'+that.escapeHTML($option.text())+'</span></li>'), selectedLi = selectableLi.clone(), value = $option.val(), - elementId = that.sanitize(value, that.sanitizeRegexp); + elementId = that.sanitize(value); selectableLi .data('ms-value', value) @@ -135,14 +133,14 @@ if ($optgroup.length > 0){ var optgroupLabel = $optgroup.attr('label'), - optgroupId = that.sanitize(optgroupLabel, that.sanitizeRegexp), + optgroupId = that.sanitize(optgroupLabel), $selectableOptgroup = that.$selectableUl.find('#optgroup-selectable-'+optgroupId), $selectionOptgroup = that.$selectionUl.find('#optgroup-selection-'+optgroupId); - + if ($selectableOptgroup.length === 0){ var optgroupContainerTpl = '<li class="ms-optgroup-container"></li>', optgroupTpl = '<ul class="ms-optgroup"><li class="ms-optgroup-label"><span>'+optgroupLabel+'</span></li></ul>'; - + $selectableOptgroup = $(optgroupContainerTpl); $selectionOptgroup = $(optgroupContainerTpl); $selectableOptgroup.attr('id', 'optgroup-selectable-'+optgroupId); @@ -162,14 +160,37 @@ that.$selectableUl.append($selectableOptgroup); that.$selectionUl.append($selectionOptgroup); } - $selectableOptgroup.children().append(selectableLi); - $selectionOptgroup.children().append(selectedLi); + index = index == undefined ? $selectableOptgroup.children().length : index + 1; + selectableLi.insertAt(index, $selectableOptgroup.children()); + selectedLi.insertAt(index, $selectionOptgroup.children()); } else { - that.$selectableUl.append(selectableLi); - that.$selectionUl.append(selectedLi); + index = index == undefined ? that.$selectableUl.children().length : index; + + selectableLi.insertAt(index, that.$selectableUl); + selectedLi.insertAt(index, that.$selectionUl); } }, + 'addOption' : function(options){ + var that = this; + + if (options.value) options = [options]; + $.each(options, function(index, option){ + if (option.value && that.$element.find("option[value='"+option.value+"']").length === 0){ + var $option = $('<option value="'+option.value+'">'+option.text+'</option>'), + index = parseInt((typeof option.index === 'undefined' ? that.$element.children().length : option.index)), + $container = option.nested == undefined ? that.$element : $("optgroup[label='"+option.nested+"']") + + $option.insertAt(index, $container); + that.generateLisFromOption($option.get(0), index, option.nested); + } + }) + }, + + 'escapeHTML' : function(text){ + return $("<div>").text(text).html(); + }, + 'activeKeyboard' : function($list){ var that = this; @@ -329,11 +350,16 @@ var that = this, ms = this.$element, - msIds = $.map(value, function(val){ return(that.sanitize(val, that.sanitizeRegexp)); }), + msIds = $.map(value, function(val){ 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').filter(':not(.'+that.options.disabledClass+')'), options = ms.find('option:not(:disabled)').filter(function(){ return($.inArray(this.value, value) > -1); }); + if (method === 'init'){ + selectables = this.$selectableUl.find('#' + msIds.join('-selectable, #')+'-selectable'), + selections = this.$selectionUl.find('#' + msIds.join('-selection, #') + '-selection'); + } + if (selectables.length > 0){ selectables.addClass('ms-selected').hide(); selections.addClass('ms-selected').show(); @@ -377,9 +403,9 @@ var that = this, ms = this.$element, - msIds = $.map(value, function(val){ return(that.sanitize(val, that.sanitizeRegexp)); }), + msIds = $.map(value, function(val){ return(that.sanitize(val)); }), selectables = this.$selectableUl.find('#' + msIds.join('-selectable, #')+'-selectable'), - selections = this.$selectionUl.find('#' + msIds.join('-selection, #')+'-selection').filter('.ms-selected'), + selections = this.$selectionUl.find('#' + msIds.join('-selection, #')+'-selection').filter('.ms-selected').filter(':not(.'+that.options.disabledClass+')'), options = ms.find('option').filter(function(){ return($.inArray(this.value, value) > -1); }); if (selections.length > 0){ @@ -446,8 +472,16 @@ } }, - sanitize: function(value, reg){ - return(value.replace(reg, '_')); + sanitize: function(value){ + var hash = 0, i, char; + 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; + hash |= 0; // Convert to 32bit integer + } + return hash; } }; @@ -478,9 +512,20 @@ selectableOptgroup: false, disabledClass : 'disabled', dblClick : false, - keepOrder: false + keepOrder: false, + cssClass: '' }; $.fn.multiSelect.Constructor = MultiSelect; -}(window.jQuery);
\ No newline at end of file + $.fn.insertAt = function(index, $parent) { + return this.each(function() { + if (index === 0) { + $parent.prepend(this); + } else { + $parent.children().eq(index - 1).after(this); + } + }); +} + +}(window.jQuery); |