changeset 5578:bfde3237d6ad

select2: show prefix matches first in #repo-switcher Do as 4f4d2e899a02 introduced for the PR/changelog branch select2 boxes.
author Takumi IINO <trot.thunder@gmail.com>
date Fri, 13 Nov 2015 00:22:35 +0900
parents a1eb43a1e83e
children a33448d81f70
files kallithea/public/js/base.js kallithea/templates/base/base.html
diffstat 2 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/public/js/base.js	Mon Nov 16 19:33:37 2015 +0100
+++ b/kallithea/public/js/base.js	Fri Nov 13 00:22:35 2015 +0900
@@ -1882,6 +1882,37 @@
     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(){
--- a/kallithea/templates/base/base.html	Mon Nov 16 19:33:37 2015 +0100
+++ b/kallithea/templates/base/base.html	Fri Nov 13 00:22:35 2015 +0900
@@ -390,6 +390,7 @@
             $("#repo_switcher").select2({
                 placeholder: '<i class="icon-database"></i> ${_('Repositories')}',
                 dropdownAutoWidth: true,
+                sortResults: prefixFirstSort,
                 formatResult: format,
                 formatSelection: format,
                 formatNoMatches: function(term){