changeset 5164:4f4d2e899a02

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 .
author Daniel Hobley <danielh@unity3d.com>
date Mon, 18 May 2015 17:22:04 +0200
parents 06c4ff173b5b
children 6ec5fd198084
files kallithea/public/js/base.js kallithea/templates/changelog/changelog.html kallithea/templates/files/files.html kallithea/templates/pullrequests/pullrequest.html
diffstat 4 files changed, 48 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/public/js/base.js	Sat Jun 06 17:41:27 2015 +0200
+++ b/kallithea/public/js/base.js	Mon May 18 17:22:04 2015 +0200
@@ -2138,6 +2138,46 @@
         });
 }
 
+/**
+ 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(){
--- a/kallithea/templates/changelog/changelog.html	Sat Jun 06 17:41:27 2015 +0200
+++ b/kallithea/templates/changelog/changelog.html	Mon May 18 17:22:04 2015 +0200
@@ -266,7 +266,8 @@
                 // change branch filter
                 $("#branch_filter").select2({
                     dropdownAutoWidth: true,
-                    minimumInputLength: 1
+                    minimumInputLength: 1,
+                    sortResults: branchSort
                     });
 
                 $("#branch_filter").change(function(e){
--- a/kallithea/templates/files/files.html	Sat Jun 06 17:41:27 2015 +0200
+++ b/kallithea/templates/files/files.html	Mon May 18 17:22:04 2015 +0200
@@ -233,7 +233,8 @@
     // change branch filter
     $("#branch_selector").select2({
         dropdownAutoWidth: true,
-        minimumInputLength: 1
+        minimumInputLength: 1,
+        sortResults: branchSort
         });
 
     $("#branch_selector").change(function(e){
--- a/kallithea/templates/pullrequests/pullrequest.html	Sat Jun 06 17:41:27 2015 +0200
+++ b/kallithea/templates/pullrequests/pullrequest.html	Mon May 18 17:22:04 2015 +0200
@@ -224,7 +224,8 @@
       ## (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 @@
       });
 
       $("#other_ref").select2({
-          dropdownAutoWidth: true
+          dropdownAutoWidth: true,
+          sortResults: branchSort
       });
       $("#other_ref").on("change", function(e){
           loadPreview();