[PATCH 07 of 19] autocomplete: factor out formatResult function

Thomas De Schampheleire patrickdepinguin at gmail.com
Tue Jun 30 16:43:17 EDT 2015


# HG changeset patch
# User Thomas De Schampheleire <thomas.de.schampheleire at gmail.com>
# Date 1435261603 -7200
#      Thu Jun 25 21:46:43 2015 +0200
# Node ID 623155b700e275d039d1a9065f272dc6fa4fb569
# Parent  2bc67e169d2f80e67d9e32df023e4133d8049948
autocomplete: factor out formatResult function

Note: remove an irrelevant 'mentionQuery' clause in the PullrequestAutocomplete

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
@@ -1167,6 +1167,60 @@ var autocompleteGravatar = function(res,
     return '<div class="ac-container-wrap">{0}{1}</div>'.format(elem, res);
 }
 
+// Custom formatter to highlight the matching letters
+var autocompleteFormatter = function (oResultData, sQuery, sResultMatch) {
+    var query = sQuery.toLowerCase();
+
+    // group
+    if (oResultData.grname != undefined) {
+        var grname = oResultData.grname;
+        var grmembers = oResultData.grmembers;
+        var grnameMatchIndex = grname.toLowerCase().indexOf(query);
+        var grprefix = "{0}: ".format(_TM['Group']);
+        var grsuffix = " (" + grmembers + "  )";
+        var grsuffix = " ({0}  {1})".format(grmembers, _TM['members']);
+
+        if (grnameMatchIndex > -1) {
+            return autocompleteGravatar(grprefix + autocompleteHighlightMatch(grname, query, grnameMatchIndex) + grsuffix, null, null, true);
+        }
+        return autocompleteGravatar(grprefix + oResultData.grname + grsuffix, null, null, true);
+
+    // users
+    } else if (oResultData.nname != undefined) {
+        var fname = oResultData.fname || "";
+        var lname = oResultData.lname || "";
+        var nname = oResultData.nname;
+
+        // Guard against null value
+        var fnameMatchIndex = fname.toLowerCase().indexOf(query),
+            lnameMatchIndex = lname.toLowerCase().indexOf(query),
+            nnameMatchIndex = nname.toLowerCase().indexOf(query),
+            displayfname, displaylname, displaynname;
+
+        if (fnameMatchIndex > -1) {
+            displayfname = autocompleteHighlightMatch(fname, query, fnameMatchIndex);
+        } else {
+            displayfname = fname;
+        }
+
+        if (lnameMatchIndex > -1) {
+            displaylname = autocompleteHighlightMatch(lname, query, lnameMatchIndex);
+        } else {
+            displaylname = lname;
+        }
+
+        if (nnameMatchIndex > -1) {
+            displaynname = "(" + autocompleteHighlightMatch(nname, query, nnameMatchIndex) + ")";
+        } else {
+            displaynname = nname ? "(" + nname + ")" : "";
+        }
+
+        return autocompleteGravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk, oResultData.gravatar_size);
+    } else {
+        return '';
+    }
+};
+
 var _MembersAutoComplete = function (divid, cont, users_list, groups_list) {
 
     var matchUsers = function (sQuery) {
@@ -1209,59 +1263,8 @@ var _MembersAutoComplete = function (div
     ownerAC.animHoriz = false;
     ownerAC.animSpeed = 0.1;
 
-    // Custom formatter to highlight the matching letters
-    var custom_formatter = function (oResultData, sQuery, sResultMatch) {
-            var query = sQuery.toLowerCase();
-            // group
-            if (oResultData.grname != undefined) {
-                var grname = oResultData.grname;
-                var grmembers = oResultData.grmembers;
-                var grnameMatchIndex = grname.toLowerCase().indexOf(query);
-                var grprefix = "{0}: ".format(_TM['Group']);
-                var grsuffix = " (" + grmembers + "  )";
-                var grsuffix = " ({0}  {1})".format(grmembers, _TM['members']);
-
-                if (grnameMatchIndex > -1) {
-                    return autocompleteGravatar(grprefix + autocompleteHighlightMatch(grname, query, grnameMatchIndex) + grsuffix, null, null, true);
-                }
-                return autocompleteGravatar(grprefix + oResultData.grname + grsuffix, null, null, true);
-            // Users
-            } else if (oResultData.nname != undefined) {
-                var fname = oResultData.fname || "";
-                var lname = oResultData.lname || "";
-                var nname = oResultData.nname;
-
-                // Guard against null value
-                var fnameMatchIndex = fname.toLowerCase().indexOf(query),
-                    lnameMatchIndex = lname.toLowerCase().indexOf(query),
-                    nnameMatchIndex = nname.toLowerCase().indexOf(query),
-                    displayfname, displaylname, displaynname;
-
-                if (fnameMatchIndex > -1) {
-                    displayfname = autocompleteHighlightMatch(fname, query, fnameMatchIndex);
-                } else {
-                    displayfname = fname;
-                }
-
-                if (lnameMatchIndex > -1) {
-                    displaylname = autocompleteHighlightMatch(lname, query, lnameMatchIndex);
-                } else {
-                    displaylname = lname;
-                }
-
-                if (nnameMatchIndex > -1) {
-                    displaynname = "(" + autocompleteHighlightMatch(nname, query, nnameMatchIndex) + ")";
-                } else {
-                    displaynname = nname ? "(" + nname + ")" : "";
-                }
-
-                return autocompleteGravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk, oResultData.gravatar_size);
-            } else {
-                return '';
-            }
-        };
-    membersAC.formatResult = custom_formatter;
-    ownerAC.formatResult = custom_formatter;
+    membersAC.formatResult = autocompleteFormatter;
+    ownerAC.formatResult = autocompleteFormatter;
 
     var myHandler = function (sType, aArgs) {
             var nextId = divid.split('perm_new_member_name_')[1];
@@ -1322,46 +1325,12 @@ var MentionsAutoComplete = function (div
 
     // Custom formatter to highlight the matching letters
     ownerAC.formatResult = function (oResultData, sQuery, sResultMatch) {
-            var org_sQuery = sQuery;
-            if(this.dataSource.mentionQuery != null){
-                sQuery = this.dataSource.mentionQuery;
-            }
-
-            var query = sQuery.toLowerCase();
-            if (oResultData.nname != undefined) {
-                var fname = oResultData.fname || "";
-                var lname = oResultData.lname || "";
-                var nname = oResultData.nname;
-
-                // Guard against null value
-                var fnameMatchIndex = fname.toLowerCase().indexOf(query),
-                    lnameMatchIndex = lname.toLowerCase().indexOf(query),
-                    nnameMatchIndex = nname.toLowerCase().indexOf(query),
-                    displayfname, displaylname, displaynname;
-
-                if (fnameMatchIndex > -1) {
-                    displayfname = autocompleteHighlightMatch(fname, query, fnameMatchIndex);
-                } else {
-                    displayfname = fname;
-                }
-
-                if (lnameMatchIndex > -1) {
-                    displaylname = autocompleteHighlightMatch(lname, query, lnameMatchIndex);
-                } else {
-                    displaylname = lname;
-                }
-
-                if (nnameMatchIndex > -1) {
-                    displaynname = "(" + autocompleteHighlightMatch(nname, query, nnameMatchIndex) + ")";
-                } else {
-                    displaynname = nname ? "(" + nname + ")" : "";
-                }
-
-                return autocompleteGravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk, oResultData.gravatar_size);
-            } else {
-                return '';
-            }
-        };
+        var org_sQuery = sQuery;
+        if (this.dataSource.mentionQuery != null) {
+            sQuery = this.dataSource.mentionQuery;
+        }
+        return autocompleteFormatter(oResultData, sQuery, sResultMatch);
+    }
 
     if(ownerAC.itemSelectEvent){
         ownerAC.itemSelectEvent.subscribe(function (sType, aArgs) {
@@ -1498,48 +1467,7 @@ var PullRequestAutoComplete = function (
     reviewerAC.animHoriz = false;
     reviewerAC.animSpeed = 0.1;
 
-    // Custom formatter to highlight the matching letters
-    reviewerAC.formatResult = function (oResultData, sQuery, sResultMatch) {
-            var org_sQuery = sQuery;
-            if(this.dataSource.mentionQuery != null){
-                sQuery = this.dataSource.mentionQuery;
-            }
-
-            var query = sQuery.toLowerCase();
-            if (oResultData.nname != undefined) {
-                var fname = oResultData.fname || "";
-                var lname = oResultData.lname || "";
-                var nname = oResultData.nname;
-
-                // Guard against null value
-                var fnameMatchIndex = fname.toLowerCase().indexOf(query),
-                    lnameMatchIndex = lname.toLowerCase().indexOf(query),
-                    nnameMatchIndex = nname.toLowerCase().indexOf(query),
-                    displayfname, displaylname, displaynname;
-
-                if (fnameMatchIndex > -1) {
-                    displayfname = autocompleteHighlightMatch(fname, query, fnameMatchIndex);
-                } else {
-                    displayfname = fname;
-                }
-
-                if (lnameMatchIndex > -1) {
-                    displaylname = autocompleteHighlightMatch(lname, query, lnameMatchIndex);
-                } else {
-                    displaylname = lname;
-                }
-
-                if (nnameMatchIndex > -1) {
-                    displaynname = "(" + autocompleteHighlightMatch(nname, query, nnameMatchIndex) + ")";
-                } else {
-                    displaynname = nname ? "(" + nname + ")" : "";
-                }
-
-                return autocompleteGravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk, oResultData.gravatar_size);
-            } else {
-                return '';
-            }
-        };
+    reviewerAC.formatResult = autocompleteFormatter;
 
     //members cache to catch duplicates
     reviewerAC.dataSource.cache = [];


More information about the kallithea-general mailing list