diff --git a/select2.js b/select2.js index 625c786c..077e6cf7 100644 --- a/select2.js +++ b/select2.js @@ -814,8 +814,6 @@ the specific language governing permissions and limitations under the Apache Lic // focusin can cause focus wars between modals and select2 since the dropdown is outside the modal. this.dropdown.on("click mouseup mousedown touchstart touchend focusin", function (e) { e.stopPropagation(); }); - this.nextSearchTerm = undefined; - if ($.isFunction(this.opts.initSelection)) { // initialize selection based on the current value of the source element this.initSelection(); @@ -1499,6 +1497,23 @@ the specific language governing permissions and limitations under the Apache Lic }, + /** + * @private + */ + prefillNextSearchTerm: function () { + // initializes search's value with nextSearchTerm (if defined by user) + // ignore nextSearchTerm if the dropdown is opened by the user pressing a letter + if(this.search.val() !== "") { + return; + } + + var nextSearchTerm = this.opts.nextSearchTerm(this.data(), this.search.val()); + if(nextSearchTerm != undefined){ + this.search.val(nextSearchTerm); + this.search.select(); + } + }, + //abstract getMaximumSelectionSize: function() { return evaluate(this.opts.maximumSelectionSize, this.opts.element); @@ -2015,14 +2030,7 @@ the specific language governing permissions and limitations under the Apache Lic } } - // initializes search's value with nextSearchTerm (if defined by user) - // ignore nextSearchTerm if the dropdown is opened by the user pressing a letter - if(this.search.val() === "") { - if(this.nextSearchTerm != undefined){ - this.search.val(this.nextSearchTerm); - this.search.select(); - } - } + this.prefillNextSearchTerm(); this.focusser.prop("disabled", true).val(""); this.updateResults(true); @@ -2310,7 +2318,6 @@ the specific language governing permissions and limitations under the Apache Lic self.updateSelection(selected); self.close(); self.setPlaceholder(); - self.nextSearchTerm = self.opts.nextSearchTerm(selected, self.search.val()); } }); } @@ -2447,7 +2454,6 @@ the specific language governing permissions and limitations under the Apache Lic this.opts.element.trigger({ type: "select2-selected", val: this.id(data), choice: data }); - this.nextSearchTerm = this.opts.nextSearchTerm(data, this.search.val()); this.close(); if ((!options || !options.noFocus) && this.opts.shouldFocusInput(this)) { @@ -2914,16 +2920,9 @@ the specific language governing permissions and limitations under the Apache Lic this.focusSearch(); - // initializes search's value with nextSearchTerm (if defined by user) - // ignore nextSearchTerm if the dropdown is opened by the user pressing a letter - if(this.search.val() === "") { - if(this.nextSearchTerm != undefined){ - this.search.val(this.nextSearchTerm); - this.search.select(); - } - } - + this.prefillNextSearchTerm(); this.updateResults(true); + if (this.opts.shouldFocusInput(this)) { this.search.focus(); } @@ -2989,9 +2988,6 @@ the specific language governing permissions and limitations under the Apache Lic this.opts.element.trigger({ type: "selected", val: this.id(data), choice: data }); - // keep track of the search's value before it gets cleared - this.nextSearchTerm = this.opts.nextSearchTerm(data, this.search.val()); - this.clearSearch(); this.updateResults(); @@ -3010,11 +3006,8 @@ the specific language governing permissions and limitations under the Apache Lic this.updateResults(true); } else { // initializes search's value with nextSearchTerm and update search result - if(this.nextSearchTerm != undefined){ - this.search.val(this.nextSearchTerm); - this.updateResults(); - this.search.select(); - } + this.prefillNextSearchTerm(); + this.updateResults(); } this.positionDropdown(); } else {