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();