# HG changeset patch # User Takumi IINO # Date 2015-11-12 16:22:35 # Node ID bfde3237d6ad7af686f75a627c64b260b1c5ee12 # Parent a1eb43a1e83e9546bd74f185b642557248b0047a select2: show prefix matches first in #repo-switcher Do as 4f4d2e899a02 introduced for the PR/changelog branch select2 boxes. 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 @@ -1882,6 +1882,37 @@ var branchSort = function(results, conta return results; }; +var prefixFirstSort = function(results, container, query) { + if (query.term) { + return results.sort(function (a, b) { + // if parent node, no sorting + if (a.children != undefined || b.children != undefined) { + return 0; + } + + // Put prefix matches before matches in the line + var aPos = a.text.toLowerCase().indexOf(query.term.toLowerCase()), + bPos = b.text.toLowerCase().indexOf(query.term.toLowerCase()); + 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/base/base.html b/kallithea/templates/base/base.html --- a/kallithea/templates/base/base.html +++ b/kallithea/templates/base/base.html @@ -390,6 +390,7 @@ $("#repo_switcher").select2({ placeholder: ' ${_('Repositories')}', dropdownAutoWidth: true, + sortResults: prefixFirstSort, formatResult: format, formatSelection: format, formatNoMatches: function(term){