[PATCH 08 of 19] autocomplete: clean up handlers for itemSelectEvent

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


# HG changeset patch
# User Thomas De Schampheleire <thomas.de.schampheleire at gmail.com>
# Date 1435349220 -7200
#      Fri Jun 26 22:07:00 2015 +0200
# Node ID 98519504e88827b408447efcb9ea01ca270a7b0b
# Parent  623155b700e275d039d1a9065f272dc6fa4fb569
autocomplete: clean up handlers for itemSelectEvent

The handlers that execute when an autocompleted item is selected are really
specific and cannot be factored out. Nevertheless, some cleanup:

- rationalize indentation
- remove checks on the existence of xAC.itemSelectEvent: there seems to be
  no reason at all to check this, the YUI documentation also does not
  mention it.
- remove redundant copy/pasted comments
- remove redundant group handling in MentionsAutoComplete
- remove redundant checks on 'oData.nname != undefined', used to
  differentiate user vs. group completion, in places that only allow user
  autocompletion

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
@@ -1266,27 +1266,25 @@ var _MembersAutoComplete = function (div
     membersAC.formatResult = autocompleteFormatter;
     ownerAC.formatResult = autocompleteFormatter;
 
-    var myHandler = function (sType, aArgs) {
-            var nextId = divid.split('perm_new_member_name_')[1];
-            var myAC = aArgs[0]; // reference back to the AC instance
-            var elLI = aArgs[1]; // reference to the selected LI element
-            var oData = aArgs[2]; // object literal of selected item's result data
-            //fill the autocomplete with value
-            if (oData.nname != undefined) {
-                //users
-                myAC.getInputEl().value = oData.nname;
-                $('#perm_new_member_type_'+nextId).val('user');
-            } else {
-                //groups
-                myAC.getInputEl().value = oData.grname;
-                $('#perm_new_member_type_'+nextId).val('users_group');
-            }
-        };
-
-    membersAC.itemSelectEvent.subscribe(myHandler);
-    if(ownerAC.itemSelectEvent){
-        ownerAC.itemSelectEvent.subscribe(myHandler);
-    }
+    // Handler for selection of an entry
+    var itemSelectHandler = function (sType, aArgs) {
+        var nextId = divid.split('perm_new_member_name_')[1];
+        var myAC = aArgs[0]; // reference back to the AC instance
+        var elLI = aArgs[1]; // reference to the selected LI element
+        var oData = aArgs[2]; // object literal of selected item's result data
+        //fill the autocomplete with value
+        if (oData.nname != undefined) {
+            //users
+            myAC.getInputEl().value = oData.nname;
+            $('#perm_new_member_type_'+nextId).val('user');
+        } else {
+            //groups
+            myAC.getInputEl().value = oData.grname;
+            $('#perm_new_member_type_'+nextId).val('users_group');
+        }
+    };
+    membersAC.itemSelectEvent.subscribe(itemSelectHandler);
+    ownerAC.itemSelectEvent.subscribe(itemSelectHandler);
 
     return {
         memberDS: memberDS,
@@ -1332,30 +1330,21 @@ var MentionsAutoComplete = function (div
         return autocompleteFormatter(oResultData, sQuery, sResultMatch);
     }
 
-    if(ownerAC.itemSelectEvent){
-        ownerAC.itemSelectEvent.subscribe(function (sType, aArgs) {
-            var myAC = aArgs[0]; // reference back to the AC instance
-            var elLI = aArgs[1]; // reference to the selected LI element
-            var oData = aArgs[2]; // object literal of selected item's result data
-            //fill the autocomplete with value
-            if (oData.nname != undefined) {
-                //users
-                //Replace the mention name with replaced
-                var re = new RegExp();
-                var org = myAC.getInputEl().value;
-                var chunks = myAC.dataSource.chunks
-                // replace middle chunk(the search term) with actuall  match
-                chunks[1] = chunks[1].replace('@'+myAC.dataSource.mentionQuery,
-                                              '@'+oData.nname+' ');
-                myAC.getInputEl().value = chunks.join('')
-                myAC.getInputEl().focus(); // Y U NO WORK !?
-            } else {
-                //groups
-                myAC.getInputEl().value = oData.grname;
-                $('#perm_new_member_type').val('users_group');
-            }
-        });
-    }
+    // Handler for selection of an entry
+    ownerAC.itemSelectEvent.subscribe(function (sType, aArgs) {
+        var myAC = aArgs[0]; // reference back to the AC instance
+        var elLI = aArgs[1]; // reference to the selected LI element
+        var oData = aArgs[2]; // object literal of selected item's result data
+        //Replace the mention name with replaced
+        var re = new RegExp();
+        var org = myAC.getInputEl().value;
+        var chunks = myAC.dataSource.chunks
+        // replace middle chunk(the search term) with actuall  match
+        chunks[1] = chunks[1].replace('@'+myAC.dataSource.mentionQuery,
+                                      '@'+oData.nname+' ');
+        myAC.getInputEl().value = chunks.join('')
+        myAC.getInputEl().focus(); // Y U NO WORK !?
+    });
 
     // in this keybuffer we will gather current value of search !
     // since we need to get this just when someone does `@` then we do the
@@ -1471,23 +1460,18 @@ var PullRequestAutoComplete = function (
 
     //members cache to catch duplicates
     reviewerAC.dataSource.cache = [];
-    // hack into select event
-    if(reviewerAC.itemSelectEvent){
-        reviewerAC.itemSelectEvent.subscribe(function (sType, aArgs) {
 
-            var myAC = aArgs[0]; // reference back to the AC instance
-            var elLI = aArgs[1]; // reference to the selected LI element
-            var oData = aArgs[2]; // object literal of selected item's result data
+    // Handler for selection of an entry
+    reviewerAC.itemSelectEvent.subscribe(function (sType, aArgs) {
+        var myAC = aArgs[0]; // reference back to the AC instance
+        var elLI = aArgs[1]; // reference to the selected LI element
+        var oData = aArgs[2]; // object literal of selected item's result data
 
-            //fill the autocomplete with value
-            if (oData.nname != undefined) {
-                addReviewMember(oData.id, oData.fname, oData.lname, oData.nname,
-                                oData.gravatar_lnk, oData.gravatar_size);
-                myAC.dataSource.cache.push(oData.id);
-                $('#user').val('');
-            }
-        });
-    }
+        addReviewMember(oData.id, oData.fname, oData.lname, oData.nname,
+                        oData.gravatar_lnk, oData.gravatar_size);
+        myAC.dataSource.cache.push(oData.id);
+        $('#user').val('');
+    });
 }
 
 /**


More information about the kallithea-general mailing list