Browse Source

#2573, refactoring add option value. Adds remove option value when deselected. Consolidated checks into has conditions

pull/2710/head
jlukic 9 years ago
parent
commit
54918e9deb
1 changed files with 52 additions and 11 deletions
  1. 63
      src/definitions/modules/dropdown.js

63
src/definitions/modules/dropdown.js

@ -309,15 +309,18 @@ $.fn.dropdown = function(parameters) {
.html( templates.dropdown(selectValues) )
.insertBefore($input)
;
if($input.hasClass(className.multiple)) {
$input.prop('multiple', true);
}
if($input.is('[multiple]')) {
module.set.multiple();
}
$input
.removeAttr('class')
.detach()
.prependTo($module)
;
}
if($input.is('[multiple]')) {
module.set.multiple();
}
module.refresh();
},
menu: function(values) {
@ -1449,8 +1452,8 @@ $.fn.dropdown = function(parameters) {
if(value === '') {
return '';
}
return (!$input.is('select') && module.is.multiple())
? typeof value == 'string'
return ( !module.has.selectInput() && module.is.multiple() )
? (typeof value == 'string') // delimited string
? value.split(settings.delimiter)
: ''
: value
@ -2091,8 +2094,8 @@ $.fn.dropdown = function(parameters) {
}
}
if( $input.is('select') && (settings.allowAdditions || settings.apiSettings) ) {
module.debug('Adding an option to the select before setting the value', value);
if( module.is.single() && module.has.selectInput() && module.can.extendSelect() ) {
module.debug('Adding user option', value);
module.add.optionValue(value);
}
@ -2266,8 +2269,13 @@ $.fn.dropdown = function(parameters) {
selectObserver.disconnect();
module.verbose('Temporarily disconnecting mutation observer', value);
}
if( module.is.single() ) {
module.verbose('Removing previous user addition');
$input.find('option.' + className.addition).remove();
}
$('<option/>')
.prop('value', value)
.addClass(className.addition)
.html(value)
.appendTo($input)
;
@ -2344,11 +2352,12 @@ $.fn.dropdown = function(parameters) {
currentValue = module.get.values(),
newValue
;
console.log(currentValue);
if(addedValue === '') {
module.debug('Cannot select blank values from multiselect');
return;
}
// extend currently array
// extend current array
if($.isArray(currentValue)) {
newValue = currentValue.concat([addedValue]);
newValue = module.get.uniqueArray(newValue);
@ -2357,8 +2366,8 @@ $.fn.dropdown = function(parameters) {
newValue = [addedValue];
}
// add values
if($input.is('select')) {
if(settings.allowAdditions || settings.apiSettings) {
if( module.has.selectInput() ) {
if(module.can.extendSelect()) {
module.debug('Adding value to select', addedValue, newValue, $input);
module.add.optionValue(addedValue);
}
@ -2413,6 +2422,28 @@ $.fn.dropdown = function(parameters) {
$item.removeClass(className.filtered);
}
},
optionValue: function(value) {
var
$option = $input.find('option[value="' + value + '"]'),
hasOption = ($option.length > 0)
;
if(!hasOption || !$option.hasClass(className.addition)) {
return;
}
// temporarily disconnect observer
if(selectObserver) {
selectObserver.disconnect();
module.verbose('Temporarily disconnecting mutation observer', value);
}
$option.remove();
module.verbose('Removing user addition as an <option>', value);
if(selectObserver) {
selectObserver.observe($input[0], {
childList : true,
subtree : true
});
}
},
message: function() {
$menu.children(selector.message).remove();
},
@ -2469,9 +2500,10 @@ $.fn.dropdown = function(parameters) {
values = $input.val(),
newValue
;
if( $input.is('select') ) {
if( module.has.selectInput() ) {
module.verbose('Input is <select> removing selected option', removedValue);
newValue = module.remove.arrayValue(removedValue, values);
module.remove.optionValue(removedValue);
}
else {
module.verbose('Removing from delimited values', removedValue);
@ -2489,6 +2521,9 @@ $.fn.dropdown = function(parameters) {
module.check.maxSelections();
},
arrayValue: function(removedValue, values) {
if( !$.isArray(values) ) {
values = [values];
}
values = $.grep(values, function(value){
return (removedValue != value);
});
@ -2567,6 +2602,9 @@ $.fn.dropdown = function(parameters) {
search: function() {
return ($search.length > 0);
},
selectInput: function() {
return ( $input.is('select') );
},
firstLetter: function($item, letter) {
var
text,
@ -2744,6 +2782,9 @@ $.fn.dropdown = function(parameters) {
click: function() {
return (hasTouch || settings.on == 'click');
},
extendSelect: function() {
return settings.allowAdditions || settings.apiSettings;
},
show: function() {
return !module.is.disabled() && (module.has.items() || module.has.message());
},

Loading…
Cancel
Save