Skip to content

Commit

Permalink
Fixes #3879, #3622 - mutation observers occur erroneously with intern…
Browse files Browse the repository at this point in the history
…al updates, causing issues
  • Loading branch information
jlukic committed May 15, 2016
1 parent 50f5d8d commit 6562429
Showing 1 changed file with 60 additions and 28 deletions.
88 changes: 60 additions & 28 deletions src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,37 +134,48 @@ $.fn.dropdown = function(parameters) {
$document
.off(elementNamespace)
;
if(selectObserver) {
selectObserver.disconnect();
}
if(menuObserver) {
menuObserver.disconnect();
}
module.disconnect.menuObserver();
module.disconnect.selectObserver();
},

observeChanges: function() {
if('MutationObserver' in window) {
selectObserver = new MutationObserver(function(mutations) {
module.debug('<select> modified, recreating menu');
module.setup.select();
});
menuObserver = new MutationObserver(function(mutations) {
module.debug('Menu modified, updating selector cache');
module.refresh();
});
selectObserver = new MutationObserver(module.event.select.mutation);
menuObserver = new MutationObserver(module.event.menu.mutation);
module.debug('Setting up mutation observer', selectObserver, menuObserver);
module.observe.select();
module.observe.menu();
}
},

disconnect: {
menuObserver: function() {
if(menuObserver) {
menuObserver.disconnect();
}
},
selectObserver: function() {
if(menuObserver) {
menuObserver.disconnect();
}
}
},
observe: {
select: function() {
if(module.has.input()) {
selectObserver.observe($input[0], {
childList : true,
subtree : true
});
}
},
menu: function() {
if(module.has.menu()) {
menuObserver.observe($menu[0], {
childList : true,
subtree : true
});
}
module.debug('Setting up mutation observer', selectObserver, menuObserver);
}
},

Expand Down Expand Up @@ -1033,7 +1044,32 @@ $.fn.dropdown = function(parameters) {
module.determine.eventInModule(event, module.hide);
}
},
select: {
mutation: function(mutations) {
module.debug('<select> modified, recreating menu');
module.setup.select();
}
},
menu: {
mutation: function(mutations) {
module.debug('Menu modified, updating selector cache');
var
mutation = mutations[0],
$addedNode = mutation.addedNodes
? $(mutation.addedNodes[0])
: $(false),
$removedNode = mutation.removedNodes
? $(mutation.removedNodes[0])
: $(false),
$changedNodes = $addedNode.add($removedNode),
isUserAddition = $changedNodes.is(selector.addition) || $changedNodes.closest(selector.addition).length > 0,
isMessage = $changedNodes.is(selector.message) || $changedNodes.closest(selector.message).length > 0
;
if(isUserAddition || isMessage) {
return
}
module.refresh();
},
mousedown: function() {
itemActivated = true;
},
Expand Down Expand Up @@ -2491,10 +2527,7 @@ $.fn.dropdown = function(parameters) {
return;
}
// temporarily disconnect observer
if(selectObserver) {
selectObserver.disconnect();
module.verbose('Temporarily disconnecting mutation observer', escapedValue);
}
module.disconnect.selectObserver();
if( module.is.single() ) {
module.verbose('Removing previous user addition');
$input.find('option.' + className.addition).remove();
Expand All @@ -2506,12 +2539,7 @@ $.fn.dropdown = function(parameters) {
.appendTo($input)
;
module.verbose('Adding user addition as an <option>', value);
if(selectObserver) {
selectObserver.observe($input[0], {
childList : true,
subtree : true
});
}
module.observe.select();
},
userSuggestion: function(value) {
var
Expand All @@ -2529,13 +2557,17 @@ $.fn.dropdown = function(parameters) {
return;
}
if(hasUserSuggestion) {
html = settings.templates.addition( module.add.variables(message.addResult, value) );
$addition
.html(html)
.attr('data-' + metadata.value, value)
.attr('data-' + metadata.text, value)
.removeClass(className.filtered)
;
if(!settings.hideAdditions) {
html = settings.templates.addition( module.add.variables(message.addResult, value) );
$addition
.html(html)
;
}
module.verbose('Replacing user suggestion with new value', $addition);
}
else {
Expand Down Expand Up @@ -3442,7 +3474,7 @@ $.fn.dropdown.settings = {
forceSelection : true, // force a choice on blur with search selection

allowAdditions : false, // whether multiple select should allow user added values
hideAdditions : true, // whether or not to hide special message prompting a user they can enter a value
hideAdditions : false, // whether or not to hide special message prompting a user they can enter a value

This comment has been minimized.

Copy link
@JakeAlmer

JakeAlmer Jul 19, 2016

was this change of default behavior intended?


maxSelections : false, // When set to a number limits the number of selections to this count
useLabels : true, // whether multiple select should filter currently active selections from choices
Expand Down

0 comments on commit 6562429

Please sign in to comment.