Skip to content

Commit

Permalink
fix(dropdown): fix menu item refresh logic when using remote api
Browse files Browse the repository at this point in the history
when apiSettings.cache is set false, the whole list is not refresh by clicking on the down arrow icon.
  • Loading branch information
dutrieux authored Oct 21, 2020
1 parent 68bd08f commit c0e49df
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ $.fn.dropdown = function(parameters) {
internalChange = false,
iconClicked = false,
element = this,
focused = false,
instance = $module.data(moduleNamespace),

selectActionActive,
Expand Down Expand Up @@ -497,6 +498,11 @@ $.fn.dropdown = function(parameters) {
;
},

clearItems: function() {
$menu.empty();
module.refreshItems();
},

toggle: function() {
module.verbose('Toggling menu visibility');
if( !module.is.active() ) {
Expand All @@ -512,6 +518,9 @@ $.fn.dropdown = function(parameters) {
? callback
: function(){}
;
if ((focused || iconClicked) && module.is.remote() && module.is.noApiCache()) {
module.clearItems();
}
if(!module.can.show() && module.is.remote()) {
module.debug('No API results retrieved, searching before show');
module.queryRemote(module.get.query(), module.show, [callback, preventFocus]);
Expand Down Expand Up @@ -638,6 +647,7 @@ $.fn.dropdown = function(parameters) {
if(module.is.multiple()) {
$module
.on(clickEvent + eventNamespace, module.event.click)
.on(clickEvent + eventNamespace, module.event.search.focus)
;
}
}
Expand Down Expand Up @@ -767,11 +777,13 @@ $.fn.dropdown = function(parameters) {
if(!Array.isArray(preSelected)) {
preSelected = preSelected && preSelected!=="" ? preSelected.split(settings.delimiter) : [];
}
$.each(preSelected,function(index,value){
$item.filter('[data-value="'+value+'"]')
.addClass(className.filtered)
;
});
if (module.is.multiple()) {
$.each(preSelected,function(index,value){
$item.filter('[data-value="'+value+'"]')
.addClass(className.filtered)
;
});
}
afterFiltered();
});
}
Expand Down Expand Up @@ -799,10 +811,14 @@ $.fn.dropdown = function(parameters) {
},
onError: function() {
module.add.message(message.serverError);
iconClicked = false;
focused = false;
callback.apply(null, callbackParameters);
},
onFailure: function() {
module.add.message(message.serverError);
iconClicked = false;
focused = false;
callback.apply(null, callbackParameters);
},
onSuccess : function(response) {
Expand All @@ -820,6 +836,15 @@ $.fn.dropdown = function(parameters) {
if(values.length===0 && !settings.allowAdditions) {
module.add.message(message.noResults);
}
else {
var value = module.is.multiple() ? module.get.values() : module.get.value();
if (value !== '') {
module.verbose('Value(s) present after click icon, select value(s) in items');
module.set.selected(value, null, null, true);
}
}
iconClicked = false;
focused = false;
callback.apply(null, callbackParameters);
}
}
Expand Down Expand Up @@ -1052,6 +1077,7 @@ $.fn.dropdown = function(parameters) {
},
focus: function() {
if(settings.showOnFocus && !activated && module.is.hidden() && !pageLostFocus) {
focused = true;
module.show();
}
},
Expand Down Expand Up @@ -1101,7 +1127,8 @@ $.fn.dropdown = function(parameters) {
if(module.is.multiple()) {
module.remove.activeLabel();
}
if(settings.showOnFocus || (event.type !== 'focus' && event.type !== 'focusin')) {
if(!module.is.active() && (settings.showOnFocus || (event.type !== 'focus' && event.type !== 'focusin'))) {
focused = true;
module.search();
}
},
Expand Down Expand Up @@ -1146,6 +1173,7 @@ $.fn.dropdown = function(parameters) {
} else {
module.toggle();
}
event.stopPropagation();
}
},
text: {
Expand Down Expand Up @@ -1185,10 +1213,11 @@ $.fn.dropdown = function(parameters) {
$label.addClass(className.active);
}
settings.onLabelSelect.apply(this, $labels.filter('.' + className.active));
event.stopPropagation();
}
},
remove: {
click: function() {
click: function(event) {
var
$label = $(this).parent()
;
Expand All @@ -1200,6 +1229,7 @@ $.fn.dropdown = function(parameters) {
// remove this label only
module.remove.activeLabels( $label );
}
event.stopPropagation();
}
},
test: {
Expand All @@ -1212,6 +1242,9 @@ $.fn.dropdown = function(parameters) {
if(module.is.bubbledLabelClick(event) || module.is.bubbledIconClick(event)) {
return;
}
if (!module.is.multiple() || (module.is.multiple() && !module.is.active())) {
focused = true;
}
if( module.determine.eventOnElement(event, toggleBehavior) ) {
event.preventDefault();
}
Expand Down Expand Up @@ -2696,7 +2729,7 @@ $.fn.dropdown = function(parameters) {
module.clear();
module.set.selected(value, $selectedItem);
},
selected: function(value, $selectedItem, preventChangeTrigger) {
selected: function(value, $selectedItem, preventChangeTrigger, keepSearchTerm) {
var
isMultiple = module.is.multiple()
;
Expand Down Expand Up @@ -2758,7 +2791,9 @@ $.fn.dropdown = function(parameters) {
if(settings.apiSettings && settings.saveRemoteData) {
module.save.remoteData(selectedText, selectedValue);
}
module.set.text(selectedText);
if (!keepSearchTerm) {
module.set.text(selectedText);
}
module.set.value(selectedValue, selectedText, $selected, preventChangeTrigger);
$selected
.addClass(className.active)
Expand All @@ -2767,7 +2802,9 @@ $.fn.dropdown = function(parameters) {
}
})
;
module.remove.searchTerm();
if (!keepSearchTerm) {
module.remove.searchTerm();
}
}
},

Expand Down Expand Up @@ -3386,6 +3423,9 @@ $.fn.dropdown = function(parameters) {
remote: function() {
return settings.apiSettings && module.can.useAPI();
},
noApiCache: function() {
return settings.apiSettings && !settings.apiSettings.cache
},
single: function() {
return !module.is.multiple();
},
Expand Down

0 comments on commit c0e49df

Please sign in to comment.