Bug 1147534 - Don't use an anonymous click handler for each item in about:passwords r=liuche
authorMark Finkle <mfinkle@mozilla.com>
Thu, 09 Apr 2015 15:48:45 -0400
changeset 238476 7150312e33c9c2209a95ab280fbef0218b3c6a35
parent 238475 a1e6f8f259dc0dcf848c5c15ea1f05ce8eae0e44
child 238477 624e6c8bb11d4d11be517968cfd6370d54fc1b81
push idunknown
push userunknown
push dateunknown
reviewersliuche
bugs1147534
milestone40.0a1
Bug 1147534 - Don't use an anonymous click handler for each item in about:passwords r=liuche
mobile/android/chrome/content/aboutPasswords.js
--- a/mobile/android/chrome/content/aboutPasswords.js
+++ b/mobile/android/chrome/content/aboutPasswords.js
@@ -118,65 +118,74 @@ let Passwords = {
     } else {
       // Clear any previous detail addon
       let detailItem = document.querySelector("#login-details > .login-item");
       detailItem.login = null;
       this._showList();
     }
   },
 
+  _onLoginClick: function (event) {
+    let loginItem = event.currentTarget;
+    let login = loginItem.login;
+    if (!login) {
+      debug("No login!");
+      return;
+    }
+
+    let prompt = new Prompt({
+      window: window,
+    });
+    let menuItems = [
+      { label: gStringBundle.GetStringFromName("passwordsMenu.copyPassword") },
+      { label: gStringBundle.GetStringFromName("passwordsMenu.copyUsername") },
+      { label: gStringBundle.GetStringFromName("passwordsMenu.details") },
+      { label: gStringBundle.GetStringFromName("passwordsMenu.delete") }
+    ];
+
+    prompt.setSingleChoiceItems(menuItems);
+    prompt.show((data) => {
+      // Switch on indices of buttons, as they were added when creating login item.
+      switch (data.button) {
+        case 0:
+          copyStringAndToast(login.password, gStringBundle.GetStringFromName("passwordsDetails.passwordCopied"));
+          break;
+        case 1:
+          copyStringAndToast(login.username, gStringBundle.GetStringFromName("passwordsDetails.usernameCopied"));
+          break;
+        case 2:
+          this._showDetails(loginItem);
+          history.pushState({ id: login.guid }, document.title);
+          break;
+        case 3:
+          let confirmPrompt = new Prompt({
+            window: window,
+            message: gStringBundle.GetStringFromName("passwordsDialog.confirmDelete"),
+            buttons: [
+              gStringBundle.GetStringFromName("passwordsDialog.confirm"),
+              gStringBundle.GetStringFromName("passwordsDialog.cancel") ]
+          });
+          confirmPrompt.show((data) => {
+            switch (data.button) {
+              case 0:
+                // Corresponds to "confirm" button.
+                Services.logins.removeLogin(login);
+            }
+          });
+      }
+    });
+  },
+
   _createItemForLogin: function (login) {
     let loginItem = document.createElement("div");
 
     loginItem.setAttribute("loginID", login.guid);
     loginItem.className = "login-item list-item";
 
-    loginItem.addEventListener("click", () => {
-      let prompt = new Prompt({
-        window: window,
-      });
-      let menuItems = [
-        { label: gStringBundle.GetStringFromName("passwordsMenu.copyPassword") },
-        { label: gStringBundle.GetStringFromName("passwordsMenu.copyUsername") },
-        { label: gStringBundle.GetStringFromName("passwordsMenu.details") },
-        { label: gStringBundle.GetStringFromName("passwordsMenu.delete") } ];
-
-      prompt.setSingleChoiceItems(menuItems);
-      prompt.show((data) => {
-        // Switch on indices of buttons, as they were added when creating login item.
-        switch (data.button) {
-          case 0:
-            copyStringAndToast(login.password, gStringBundle.GetStringFromName("passwordsDetails.passwordCopied"));
-            break;
-          case 1:
-            copyStringAndToast(login.username, gStringBundle.GetStringFromName("passwordsDetails.usernameCopied"));
-            break;
-          case 2:
-            this._showDetails(loginItem);
-            history.pushState({ id: login.guid }, document.title);
-            break;
-          case 3:
-            let confirmPrompt = new Prompt({
-              window: window,
-              message: gStringBundle.GetStringFromName("passwordsDialog.confirmDelete"),
-              buttons: [
-                gStringBundle.GetStringFromName("passwordsDialog.confirm"),
-                gStringBundle.GetStringFromName("passwordsDialog.cancel") ]
-            });
-            confirmPrompt.show((data) => {
-              switch (data.button) {
-                case 0:
-                  // Corresponds to "confirm" button.
-                  Services.logins.removeLogin(login);
-              }
-            });
-        }
-      });
-
-    }, true);
+    loginItem.addEventListener("click", this, true);
 
     // Create item icon.
     let img = document.createElement("div");
     img.className = "icon";
 
     // Load favicon from cache.
     Messaging.sendRequestForResult({
       type: "Favicon:CacheLoad",
@@ -225,16 +234,20 @@ let Passwords = {
   },
 
   handleEvent: function (event) {
     switch (event.type) {
       case "popstate": {
         this._onPopState(event);
         break;
       }
+      case "click": {
+        this._onLoginClick(event);
+        break;
+      }
     }
   },
 
   observe: function (subject, topic, data) {
     switch(topic) {
       case "passwordmgr-storage-changed": {
         // Reload passwords content.
         this._loadList(this._getLogins());