From 481c43883e23874e9c35879d173eb8cc5b994b12 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sat, 14 May 2016 22:04:29 -0400 Subject: [PATCH] selectOnClose now properly works with closeOnSelect Previously we hacked around the infinite loop between closeOnSelect and selectOnClose by attempting to detect what event was being triggered without knowing what event triggered it. Now we properly relay the Select2 event and the jQuery event that triggered the select or unselect This closes https://github.com/select2/select2/issues/4012 --- src/js/select2/dropdown/closeOnSelect.js | 5 ++++- src/js/select2/dropdown/selectOnClose.js | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/js/select2/dropdown/closeOnSelect.js b/src/js/select2/dropdown/closeOnSelect.js index 1a47607e..fa6a82c4 100644 --- a/src/js/select2/dropdown/closeOnSelect.js +++ b/src/js/select2/dropdown/closeOnSelect.js @@ -25,7 +25,10 @@ define([ return; } - this.trigger('close', {}); + this.trigger('close', { + originalEvent: originalEvent, + originalSelect2Event: evt + }); }; return CloseOnSelect; diff --git a/src/js/select2/dropdown/selectOnClose.js b/src/js/select2/dropdown/selectOnClose.js index ee45de10..430c0329 100644 --- a/src/js/select2/dropdown/selectOnClose.js +++ b/src/js/select2/dropdown/selectOnClose.js @@ -8,12 +8,22 @@ define([ decorated.call(this, container, $container); - container.on('close', function () { - self._handleSelectOnClose(); + container.on('close', function (params) { + self._handleSelectOnClose(params); }); }; - SelectOnClose.prototype._handleSelectOnClose = function () { + SelectOnClose.prototype._handleSelectOnClose = function (_, params) { + if (params && params.originalSelect2Event != null) { + var event = params.originalSelect2Event; + + // Don't select an item if the close event was triggered from a select or + // unselect event + if (event._type === 'select' || event._type === 'unselect') { + return; + } + } + var $highlightedResults = this.getHighlightedResults(); // Only select highlighted results