# HG changeset patch # User Daniel Hobley # Date 2015-05-18 17:22:04 # Node ID 4f4d2e899a02037a96f5847e099e2e21cb7d0ed7 # Parent 06c4ff173b5be0e2b90316575708ec83ceebace5 select2: move "exact prefix matches" to the top of the search Further improvements to this could be to sort by the position of your filter in the results so searching for foo means that release/foo comes before a/branch/of/doom//foo . diff --git a/kallithea/public/js/base.js b/kallithea/public/js/base.js --- a/kallithea/public/js/base.js +++ b/kallithea/public/js/base.js @@ -2138,6 +2138,46 @@ var YUI_datatable = function(data, field }); } +/** + Branch Sorting callback for select2, modifying the filtered result so prefix + matches come before matches in the line. + **/ +var branchSort = function(results, container, query) { + if (query.term) { + return results.sort(function (a, b) { + // Put closed branches after open ones (a bit of a hack ...) + var aClosed = a.text.indexOf("(closed)") > -1, + bClosed = b.text.indexOf("(closed)") > -1; + if (aClosed && !bClosed) { + return 1; + } + if (bClosed && !aClosed) { + return -1; + } + + // Put prefix matches before matches in the line + var aPos = a.text.indexOf(query.term), + bPos = b.text.indexOf(query.term); + if (aPos === 0 && bPos !== 0) { + return -1; + } + if (bPos === 0 && aPos !== 0) { + return 1; + } + + // Default sorting + if (a.text > b.text) { + return 1; + } + if (a.text < b.text) { + return -1; + } + return 0; + }); + } + return results; +}; + // global hooks after DOM is loaded $(document).ready(function(){ diff --git a/kallithea/templates/changelog/changelog.html b/kallithea/templates/changelog/changelog.html --- a/kallithea/templates/changelog/changelog.html +++ b/kallithea/templates/changelog/changelog.html @@ -266,7 +266,8 @@ ${self.repo_context_bar('changelog', c.f // change branch filter $("#branch_filter").select2({ dropdownAutoWidth: true, - minimumInputLength: 1 + minimumInputLength: 1, + sortResults: branchSort }); $("#branch_filter").change(function(e){ diff --git a/kallithea/templates/files/files.html b/kallithea/templates/files/files.html --- a/kallithea/templates/files/files.html +++ b/kallithea/templates/files/files.html @@ -233,7 +233,8 @@ $(document).ready(function(){ // change branch filter $("#branch_selector").select2({ dropdownAutoWidth: true, - minimumInputLength: 1 + minimumInputLength: 1, + sortResults: branchSort }); $("#branch_selector").change(function(e){ diff --git a/kallithea/templates/pullrequests/pullrequest.html b/kallithea/templates/pullrequests/pullrequest.html --- a/kallithea/templates/pullrequests/pullrequest.html +++ b/kallithea/templates/pullrequests/pullrequest.html @@ -224,7 +224,8 @@ ${self.repo_context_bar('showpullrequest ## (org_repo can't change) $("#org_ref").select2({ - dropdownAutoWidth: true + dropdownAutoWidth: true, + sortResults: branchSort }); $("#org_ref").on("change", function(e){ loadPreview(); @@ -238,7 +239,8 @@ ${self.repo_context_bar('showpullrequest }); $("#other_ref").select2({ - dropdownAutoWidth: true + dropdownAutoWidth: true, + sortResults: branchSort }); $("#other_ref").on("change", function(e){ loadPreview();