Skip to content

Commit f00524c

Browse files
committed
Refactored newly added blankSortBy behavior
1 parent 1c44bdd commit f00524c

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

jquery.flexselect.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
allowMismatch: false,
2222
allowMismatchBlank: true, // If "true" a user can backspace such that the value is nothing (even if no blank value was provided in the original criteria)
2323
sortBy: 'score', // 'score' || 'name'
24-
blankSortBy: false, // 'initial' || 'score' || 'name'
24+
blankSortBy: 'initial', // 'score' || 'name' || 'initial'
2525
preSelection: true,
2626
hideDropdownOnEmptyInput: false,
2727
selectedClass: "flexselect_selected",
@@ -208,7 +208,8 @@
208208

209209
filterResults: function() {
210210
var showDisabled = this.settings.showDisabledOptions;
211-
var abbreviation = this.input.val();
211+
var abbreviation = $.trim(this.input.val());
212+
var sortByMechanism = (abbreviation == "") ? this.settings.blankSortBy : this.settings.sortBy;
212213
if (abbreviation == this.lastAbbreviation) return;
213214

214215
var results = [];
@@ -218,33 +219,30 @@
218219
if (this.score > 0.0) results.push(this);
219220
});
220221
this.results = results;
221-
222-
if(this.settings.blankSortBy && $.trim(abbreviation) == ''){
223-
if (this.settings.blankSortBy == 'score')
224-
this.sortResultsByScore();
225-
else if (this.settings.blankSortBy == 'name')
226-
this.sortResultsByName();
227-
} else {
228-
if (this.settings.sortBy == 'score')
229-
this.sortResultsByScore();
230-
else if (this.settings.sortBy == 'name')
231-
this.sortResultsByName();
232-
}
233222

223+
this.sortResultsBy(sortByMechanism);
234224
this.renderDropdown();
235225
this.markFirst();
236226
this.lastAbbreviation = abbreviation;
237227
this.picked = false;
238228
this.allowMouseMove = false;
239-
229+
240230
if (this.settings.hideDropdownOnEmptyInput){
241-
if(this.input.val() == "")
231+
if (abbreviation == "")
242232
this.dropdown.hide();
243233
else
244234
this.dropdown.show();
245235
}
246236
},
247237

238+
sortResultsBy: function(mechanism) {
239+
if (mechanism == "score") {
240+
this.sortResultsByScore();
241+
} else if (mechanism == "name") {
242+
this.sortResultsByName();
243+
}
244+
},
245+
248246
sortResultsByScore: function() {
249247
this.results.sort(function(a, b) { return b.score - a.score; });
250248
},

0 commit comments

Comments
 (0)