Bug 1343385 - consolidate selection checks in am-smtp.js. r=jorgk a=jorgk
authoraceman <acelists@atlas.sk>
Mon, 12 Mar 2018 11:35:00 +0100
changeset 30461 f6fdb18ef5e8f2856752f15f575368afa2d909c5
parent 30460 3c4321f4a9925db0636704223fccf3fc561a5185
child 30462 caa39736adf8e8e54c02962e45b112741640d3a8
push id2141
push usermozilla@jorgk.com
push dateTue, 13 Mar 2018 18:01:08 +0000
treeherdercomm-beta@653f2039952a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorgk, jorgk
bugs1343385
Bug 1343385 - consolidate selection checks in am-smtp.js. r=jorgk a=jorgk
mailnews/base/prefs/content/am-smtp.js
mailnews/base/prefs/content/am-smtp.xul
--- a/mailnews/base/prefs/content/am-smtp.js
+++ b/mailnews/base/prefs/content/am-smtp.js
@@ -23,85 +23,91 @@ var gSmtpServerListWindow =
     this.mServerList = document.getElementById("smtpList");
     this.mAddButton = document.getElementById("addButton");
     this.mEditButton = document.getElementById("editButton");
     this.mDeleteButton = document.getElementById("deleteButton");
     this.mSetDefaultServerButton = document.getElementById("setDefaultButton");
 
     this.refreshServerList("", false);
 
-    this.updateButtons(this.getSelectedServer());
+    this.updateButtons();
   },
 
   onSelectionChanged: function(aEvent)
   {
-    if (this.mServerList.selectedItems.length <= 0)
+    var server = this.getSelectedServer();
+    if (!server)
       return;
 
-    var server = this.getSelectedServer();
-    this.updateButtons(server);
+    this.updateButtons();
     this.updateServerInfoBox(server);
   },
 
   onDeleteServer: function (aEvent)
   {
     var server = this.getSelectedServer();
-    if (server)
+    if (!server)
+      return;
+
+    // confirm deletion
+    let cancel = Services.prompt.confirmEx(window,
+      this.mBundle.getString("smtpServers-confirmServerDeletionTitle"),
+      this.mBundle.getFormattedString("smtpServers-confirmServerDeletion",
+                                      [server.hostname], 1),
+      Services.prompt.STD_YES_NO_BUTTONS, null, null, null, null, { });
+
+    if (!cancel)
     {
-      // confirm deletion
-      let cancel = Services.prompt.confirmEx(window,
-        this.mBundle.getString('smtpServers-confirmServerDeletionTitle'),
-        this.mBundle.getFormattedString('smtpServers-confirmServerDeletion', [server.hostname], 1),
-        Services.prompt.STD_YES_NO_BUTTONS, null, null, null, null, { });
-
-      if (!cancel)
-      {
-        MailServices.smtp.deleteServer(server);
-        parent.replaceWithDefaultSmtpServer(server.key);
-        this.refreshServerList("", true);
-      }
+      MailServices.smtp.deleteServer(server);
+      parent.replaceWithDefaultSmtpServer(server.key);
+      this.refreshServerList("", true);
     }
   },
 
   onAddServer: function (aEvent)
   {
     this.openServerEditor(null);
   },
 
   onEditServer: function (aEvent)
   {
-    if (this.mServerList.selectedItems.length <= 0)
+    let server = this.getSelectedServer();
+    if (!server)
       return;
-    this.openServerEditor(this.getSelectedServer());
+
+    this.openServerEditor(server);
   },
 
   onSetDefaultServer: function(aEvent)
   {
-    if (this.mServerList.selectedItems.length <= 0)
+    let server = this.getSelectedServer();
+    if (!server)
       return;
 
-    MailServices.smtp.defaultServer = this.getSelectedServer();
+    MailServices.smtp.defaultServer = server;
     this.refreshServerList(MailServices.smtp.defaultServer.key, true);
   },
 
-  updateButtons: function(aServer)
+  updateButtons: function()
   {
+    let server = this.getSelectedServer();
+
     // can't delete default server
-    if (MailServices.smtp.defaultServer == aServer)
+    if (server && MailServices.smtp.defaultServer == server)
     {
       this.mSetDefaultServerButton.setAttribute("disabled", "true");
       this.mDeleteButton.setAttribute("disabled", "true");
     }
     else
     {
       this.mSetDefaultServerButton.removeAttribute("disabled");
       this.mDeleteButton.removeAttribute("disabled");
     }
 
-    if (this.mServerList.selectedItems.length == 0)
+    if (!server)
       this.mEditButton.setAttribute("disabled", "true");
     else
       this.mEditButton.removeAttribute("disabled");
   },
 
   updateServerInfoBox: function(aServer)
   {
     var noneSelected = this.mBundle.getString("smtpServerList-NotSpecified");
@@ -237,15 +243,19 @@ var gSmtpServerListWindow =
     setTimeout(function(aServerList) {
       aServerList.ensureElementIsVisible(aServer);
       aServerList.selectItem(aServer);
     }, 0, this.mServerList);
   },
 
   getSelectedServer: function()
   {
-    if (this.mServerList.selectedItems.length == 0)
+    // The list of servers is a single selection listbox
+    // therefore 1 item is always selected.
+    // But if there are no SMTP servers defined yet, nothing will be selected.
+    let selection = this.mServerList.selectedItem;
+    if (!selection)
       return null;
 
-    let serverKey = this.mServerList.selectedItems[0].getAttribute("key");
+    let serverKey = selection.getAttribute("key");
     return MailServices.smtp.getServerByKey(serverKey);
   }
 };
--- a/mailnews/base/prefs/content/am-smtp.xul
+++ b/mailnews/base/prefs/content/am-smtp.xul
@@ -24,16 +24,17 @@
 
     <label control="smtpList">&smtpDescription.label;</label>
     <separator class="thin"/>
 
     <hbox flex="1">
       <listbox id="smtpList"
                onselect="gSmtpServerListWindow.onSelectionChanged(event);"
                ondblclick="gSmtpServerListWindow.onEditServer(event);"
+               seltype="single"
                flex="1"/>
 
       <vbox>
         <button id="addButton"
                 oncommand="gSmtpServerListWindow.onAddServer(event);"
                 label="&smtpListAdd.label;"
                 accesskey="&smtpListAdd.accesskey;"/>
         <button id="editButton"