summaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authorlou <louiscuny@gmail.com>2013-05-24 19:22:50 +0200
committerlou <louiscuny@gmail.com>2013-05-24 19:22:50 +0200
commit3abad28c00247292817809b64e48c09c718df4cc (patch)
treedb3dd924e70e3dc8fe23883e00a064076e919158 /js
parent7d5cb795e6ca2779f15d2e890015956abc7eeea1 (diff)
downloadmulti-select-3abad28c00247292817809b64e48c09c718df4cc.zip
multi-select-3abad28c00247292817809b64e48c09c718df4cc.tar.gz
multi-select-3abad28c00247292817809b64e48c09c718df4cc.tar.bz2
fix refresh method + add it to demos + clean js
Diffstat (limited to 'js')
-rw-r--r--js/application.js5
-rw-r--r--js/jquery.multi-select.js108
2 files changed, 63 insertions, 50 deletions
diff --git a/js/application.js b/js/application.js
index 9d856a8..ab47ff1 100644
--- a/js/application.js
+++ b/js/application.js
@@ -41,6 +41,11 @@
}
});
+ $('#refresh').on('click', function(){
+ $('#public-methods').multiSelect('refresh');
+ return false;
+ })
+
$('#public-methods').multiSelect({});
$('#select-all').click(function(){
diff --git a/js/jquery.multi-select.js b/js/jquery.multi-select.js
index c476533..ff37d66 100644
--- a/js/jquery.multi-select.js
+++ b/js/jquery.multi-select.js
@@ -11,7 +11,7 @@
!function ($) {
- "use strict"; // jshint ;_;
+ "use strict";
/* MULTISELECT CLASS DEFINITION
@@ -30,7 +30,7 @@
this.$selectionUl = $('<ul/>', { 'class': "ms-list" });
this.scrollTo = 0;
this.sanitizeRegexp = new RegExp("\\W+", 'gi');
- }
+ };
MultiSelect.prototype = {
constructor: MultiSelect,
@@ -39,14 +39,13 @@
var that = this,
ms = this.$element;
- if (ms.next('.ms-container').length == 0){
+ 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,
optgroupContainerTemplate = '<li class="ms-optgroup-container"></li>',
optgroupUlTemplate = '<ul class="ms-optgroup"></ul>',
optgroupLiTemplate = '<li class="ms-optgroup-label"><span></span></li>';
@@ -64,12 +63,12 @@
if (that.options.selectableOptgroup){
optgroupSelectableLi.on('click', function(){
- var values = optgroup.children(':not(:selected)').map(function(){ return $(this).val() }).get();
+ var values = optgroup.children(':not(:selected)').map(function(){ return $(this).val(); }).get();
that.select(values);
});
optgroupSelectionLi.on('click', function(){
- var values = optgroup.children(':selected').map(function(){ return $(this).val() }).get();
+ var values = optgroup.children(':selected').map(function(){ return $(this).val(); }).get();
that.deselect(values);
});
}
@@ -142,17 +141,21 @@
}
});
- if (that.options.selectableHeader)
+ if (that.options.selectableHeader){
that.$selectableContainer.append(that.options.selectableHeader);
+ }
that.$selectableContainer.append(that.$selectableUl);
- if (that.options.selectableFooter)
+ if (that.options.selectableFooter){
that.$selectableContainer.append(that.options.selectableFooter);
+ }
- if (that.options.selectionHeader)
+ if (that.options.selectionHeader){
that.$selectionContainer.append(that.options.selectionHeader);
+ }
that.$selectionContainer.append(that.$selectionUl);
- if (that.options.selectionFooter)
+ if (that.options.selectionFooter){
that.$selectionContainer.append(that.options.selectionFooter);
+ }
that.$container.append(that.$selectableContainer);
that.$container.append(that.$selectionContainer);
@@ -212,12 +215,12 @@
liFocusedIndex = liFocused.length > 0 ? lis.index(liFocused) : -1,
ulHeight = ul.innerHeight(),
liHeight = lis.first().outerHeight(true),
- numberOfLisDisplayed = Math.floor(ulHeight / liHeight);
+ numberOfLisDisplayed = Math.floor(ulHeight / liHeight),
+ ulPosition = null;
- if (e.keyCode == 32){ // space
+ if (e.keyCode === 32){ // space
if (liFocused.length >0){
- var method = keyContainer == 'ms-selectable' ? 'select' : 'deselect';
- if (keyContainer == 'ms-selectable'){
+ if (keyContainer === 'ms-selectable'){
that.select(liFocused.data('ms-value'));
} else {
that.deselect(liFocused.data('ms-value'));
@@ -226,30 +229,30 @@
that.scrollTo = 0;
ul.scrollTop(that.scrollTo);
}
- } else if (e.keyCode == 40){ // Down
+ } 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,
+ nextLi = (lisNumber !== nextLiIndex) ? lis.eq(nextLiIndex) : lis.first(),
nextLiPosition = nextLi.position().top;
+ ulPosition = ul.position().top;
lis.removeClass('ms-hover');
nextLi.addClass('ms-hover');
- if (lisNumber == nextLiIndex){
+ if (lisNumber === nextLiIndex){
that.scrollTo = 0;
} else if (nextLiPosition >= (ulPosition + (numberOfLisDisplayed * liHeight))){
that.scrollTo += liHeight;
}
ul.scrollTop(that.scrollTo);
}
- } else if (e.keyCode == 38){ // Up
+ } 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;
+ ulPosition = ul.position().top;
lis.removeClass('ms-hover');
prevLi.addClass('ms-hover');
if (prevLiPosition <= ulPosition){
@@ -259,7 +262,7 @@
}
ul.scrollTop(that.scrollTo);
}
- } else if (e.keyCode == 37 || e.keyCode == 39){
+ } else if (e.keyCode === 37 || e.keyCode === 39){
if (that.$selectableUl.hasClass('ms-focus')){
that.$selectableUl.focusout();
that.$selectionUl.focusin();
@@ -268,7 +271,7 @@
that.$selectionUl.focusout();
}
}
- }
+ };
ms.on('keydown', function(e){
if (ms.is(':focus')){
@@ -278,28 +281,33 @@
});
}
- var selectedValues = ms.find('option:selected').map(function(){ return $(this).val() }).get();
- that.select(selectedValues, 'init')
+ var selectedValues = ms.find('option:selected').map(function(){ return $(this).val(); }).get();
+ that.select(selectedValues, 'init');
- if (typeof that.options.afterInit == 'function') {
+ if (typeof that.options.afterInit === 'function') {
that.options.afterInit.call(this, this.$container);
}
},
'refresh' : function() {
+ this.destroy();
+ this.$element.multiSelect(this.options);
+ },
+
+ 'destroy' : function(){
$("#ms-"+this.$element.attr("id")).remove();
- this.init(this.options);
+ this.$element.removeData('multiselect');
},
'select' : function(value, method){
- if (typeof value == 'string')
- value = [value]
+ if (typeof value === 'string'){ value = [value]; }
+
var that = this,
ms = this.$element,
- msIds = $.map(value, function(val, index){ return(that.sanitize(val, that.sanitizeRegexp)) }),
+ msIds = $.map(value, function(val){ return(that.sanitize(val, that.sanitizeRegexp)); }),
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) });
+ options = ms.find('option').filter(function(){ return($.inArray(this.value, value) > -1); });
if (selectables.length > 0){
selectables.addClass('ms-selected').hide();
@@ -310,7 +318,7 @@
if (selectableOptgroups.length > 0){
selectableOptgroups.each(function(){
var selectablesLi = $(this).find('.ms-elem-selectable');
- if (selectablesLi.length == selectablesLi.filter('.ms-selected').length){
+ if (selectablesLi.length === selectablesLi.filter('.ms-selected').length){
$(this).find('.ms-optgroup-label').hide();
}
});
@@ -323,11 +331,11 @@
}
});
}
- if (method != 'init'){
+ if (method !== 'init'){
that.$selectionUl.focusout();
that.$selectableUl.focusin();
ms.trigger('change');
- if (typeof that.options.afterSelect == 'function') {
+ if (typeof that.options.afterSelect === 'function') {
that.options.afterSelect.call(this, value);
}
}
@@ -335,14 +343,14 @@
},
'deselect' : function(value){
- if (typeof value == 'string')
- value = [value]
+ if (typeof value === 'string'){ value = [value]; }
+
var that = this,
ms = this.$element,
- msIds = $.map(value, function(val, index){ return(that.sanitize(val, that.sanitizeRegexp)) }),
+ msIds = $.map(value, function(val){ return(that.sanitize(val, that.sanitizeRegexp)); }),
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) });
+ options = ms.find('option').filter(function(){ return($.inArray(this.value, value) > -1); });
if (selections.length > 0){
selectables.removeClass('ms-selected').show();
@@ -361,7 +369,7 @@
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){
+ if (selectionsLi.filter('.ms-selected').length === 0){
$(this).find('.ms-optgroup-label').hide();
}
});
@@ -369,7 +377,7 @@
this.$selectableUl.focusout();
this.$selectionUl.focusin();
ms.trigger('change');
- if (typeof that.options.afterDeselect == 'function') {
+ if (typeof that.options.afterDeselect === 'function') {
that.options.afterDeselect.call(this, value);
}
}
@@ -387,7 +395,7 @@
this.$selectionUl.focusin();
this.$selectableUl.focusout();
ms.trigger('change');
- if (typeof this.options.afterSelect == 'function') {
+ if (typeof this.options.afterSelect === 'function') {
var selectedValues = $.grep(ms.val(), function(item){
return $.inArray(item, values) < 0;
});
@@ -407,7 +415,7 @@
this.$selectableUl.focusin();
this.$selectionUl.focusout();
ms.trigger('change');
- if (typeof this.options.afterDeselect == 'function') {
+ if (typeof this.options.afterDeselect === 'function') {
this.options.afterDeselect.call(this, values);
}
},
@@ -424,7 +432,7 @@
sanitize: function(value, reg){
return(value.replace(reg, '_'));
}
- }
+ };
/* MULTISELECT PLUGIN DEFINITION
* ======================= */
@@ -436,24 +444,24 @@
return this.each(function () {
var $this = $(this),
data = $this.data('multiselect'),
- options = $.extend({}, $.fn.multiSelect.defaults, $this.data(), typeof option == 'object' && option);
+ options = $.extend({}, $.fn.multiSelect.defaults, $this.data(), typeof option === 'object' && option);
- if (!data) $this.data('multiselect', (data = new MultiSelect(this, options)))
+ if (!data){ $this.data('multiselect', (data = new MultiSelect(this, options))); }
- if (typeof option == 'string'){
- data[option](args[1])
+ if (typeof option === 'string'){
+ data[option](args[1]);
} else {
data.init();
}
- })
- }
+ });
+ };
$.fn.multiSelect.defaults = {
selectableOptgroup: false,
disabledClass : 'disabled',
dblClick : false
- }
+ };
- $.fn.multiSelect.Constructor = MultiSelect
+ $.fn.multiSelect.Constructor = MultiSelect;
}(window.jQuery); \ No newline at end of file