Skip to content

Commit

Permalink
fix(dropdown): properly support preventchangetrigger
Browse files Browse the repository at this point in the history
The preventChangeTrigger option for some behaviors was not optimal and not even working.
Using a behavior needed to also provide some only internally used parameters like $selectedText which is confusing also in the docs.
Such functions are now also supporting to be called as behaviors having the second argument used as the precentChangeTrigger argument (which usually is only the third or fourth option)
  • Loading branch information
lubber-de authored Apr 17, 2022
1 parent 03890ca commit b4551f6
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ $.fn.dropdown = function(parameters) {
menuConfig[fields.values] = values;
module.setup.menu(menuConfig);
$.each(values, function(index, item) {
if(item.selected == true) {
if(item.selected === true) {
module.debug('Setting initial selection to', item[fields.value]);
module.set.selected(item[fields.value]);
if(!module.is.multiple()) {
Expand Down Expand Up @@ -1399,7 +1399,7 @@ $.fn.dropdown = function(parameters) {
module.remove.userAddition();
}
module.remove.searchTerm();
if(!module.is.focusedOnSearch() && !(skipRefocus == true)) {
if(!module.is.focusedOnSearch() && skipRefocus !== true) {
module.focusSearch(true);
}
}
Expand Down Expand Up @@ -2651,7 +2651,7 @@ $.fn.dropdown = function(parameters) {
}
},
direction: function($menu) {
if(settings.direction == 'auto') {
if(settings.direction === 'auto') {
// reset position, remove upward if it's base menu
if (!$menu) {
module.remove.upward();
Expand All @@ -2670,7 +2670,7 @@ $.fn.dropdown = function(parameters) {
module.set.leftward($menu);
}
}
else if(settings.direction == 'upward') {
else if(settings.direction === 'upward') {
module.set.upward($menu);
}
},
Expand All @@ -2683,6 +2683,11 @@ $.fn.dropdown = function(parameters) {
$element.addClass(className.leftward);
},
value: function(value, text, $selected, preventChangeTrigger) {
if(typeof text === 'boolean') {
preventChangeTrigger = text;
$selected = undefined;
text = undefined;
}
if(value !== undefined && value !== '' && !(Array.isArray(value) && value.length === 0)) {
$input.removeClass(className.noselection);
} else {
Expand Down Expand Up @@ -2746,12 +2751,21 @@ $.fn.dropdown = function(parameters) {
visible: function() {
$module.addClass(className.visible);
},
exactly: function(value, $selectedItem) {
exactly: function(value, $selectedItem, preventChangeTrigger) {
if(typeof $selectedItem === 'boolean') {
preventChangeTrigger = $selectedItem;
$selectedItem = undefined;
}
module.debug('Setting selected to exact values');
module.clear();
module.set.selected(value, $selectedItem);
module.set.selected(value, $selectedItem, preventChangeTrigger);
},
selected: function(value, $selectedItem, preventChangeTrigger, keepSearchTerm) {
if(typeof $selectedItem === 'boolean') {
keepSearchTerm = preventChangeTrigger;
preventChangeTrigger = $selectedItem;
$selectedItem = undefined;
}
var
isMultiple = module.is.multiple()
;
Expand Down Expand Up @@ -2785,7 +2799,7 @@ $.fn.dropdown = function(parameters) {
isActive = $selected.hasClass(className.active),
isActionable = $selected.hasClass(className.actionable),
isUserValue = $selected.hasClass(className.addition),
shouldAnimate = (isMultiple && $selectedItem.length == 1)
shouldAnimate = (isMultiple && $selectedItem && $selectedItem.length === 1)
;
if(isActionable){
if((!isMultiple || (!isActive || isUserValue)) && settings.apiSettings && settings.saveRemoteData) {
Expand All @@ -2799,14 +2813,14 @@ $.fn.dropdown = function(parameters) {
module.save.remoteData(selectedText, selectedValue);
}
if(settings.useLabels) {
module.add.value(selectedValue, selectedText, $selected);
module.add.value(selectedValue, selectedText, $selected, preventChangeTrigger);
module.add.label(selectedValue, selectedText, shouldAnimate);
module.set.activeItem($selected);
module.filterActive();
module.select.nextAvailable($selectedItem);
}
else {
module.add.value(selectedValue, selectedText, $selected);
module.add.value(selectedValue, selectedText, $selected, preventChangeTrigger);
module.set.text(module.add.variables(message.count));
module.set.activeItem($selected);
}
Expand Down Expand Up @@ -2995,7 +3009,12 @@ $.fn.dropdown = function(parameters) {
}
return message;
},
value: function(addedValue, addedText, $selectedItem) {
value: function(addedValue, addedText, $selectedItem, preventChangeTrigger) {
if(typeof addedText === 'boolean') {
preventChangeTrigger = addedText;
$selectedItem = undefined;
addedText = undefined;
}
var
currentValue = module.get.values(true),
newValue
Expand All @@ -3010,7 +3029,7 @@ $.fn.dropdown = function(parameters) {
}
// extend current array
if(Array.isArray(currentValue)) {
newValue = $selectedItem.hasClass(className.actionable) ? currentValue : currentValue.concat([addedValue]);
newValue = $selectedItem && $selectedItem.hasClass(className.actionable) ? currentValue : currentValue.concat([addedValue]);
newValue = module.get.uniqueArray(newValue);
}
else {
Expand All @@ -3034,7 +3053,7 @@ $.fn.dropdown = function(parameters) {
else {
settings.onAdd.call(element, addedValue, addedText, $selectedItem);
}
module.set.value(newValue, addedText, $selectedItem);
module.set.value(newValue, addedText, $selectedItem, preventChangeTrigger);
module.check.maxSelections();
},
},
Expand Down

0 comments on commit b4551f6

Please sign in to comment.