Bug 780173 - The "connect" button of the "Show Accounts" dialog stays disabled after disconnecting an IRC account, r=bwinton, a=Standard8.
authorFlorian Quèze <florian@queze.net>
Tue, 07 Aug 2012 19:13:43 +0200
changeset 12531 8cdfef4a530899bfdb6b76f2af5a2369a6eb9e3e
parent 12530 0f13613427fc91ee6521bc06d121cb3595a9b316
child 12532 ca5e57b83447d0eab2a64caa33a2e047a5adbefc
push idunknown
push userunknown
push dateunknown
reviewersbwinton, Standard8
bugs780173
Bug 780173 - The "connect" button of the "Show Accounts" dialog stays disabled after disconnecting an IRC account, r=bwinton, a=Standard8.
mail/components/im/content/imAccounts.js
mail/components/im/content/imAccounts.xul
--- a/mail/components/im/content/imAccounts.js
+++ b/mail/components/im/content/imAccounts.js
@@ -194,41 +194,41 @@ var gAccountManager = {
     }
   },
   cancelReconnection: function am_cancelReconnection() {
     this.accountList.selectedItem.cancelReconnection();
   },
   connect: function am_connect() {
     let account = this.accountList.selectedItem.account;
     if (account.disconnected) {
-      let disconnect = document.getElementById("cmd_disconnect");
-      disconnect.setAttribute("disabled", "true");
-      this.restoreButtonTimer();
+      this.temporarilyDisableButtons();
       account.connect();
     }
   },
   disconnect: function am_disconnect() {
     let account = this.accountList.selectedItem.account;
     if (account.connected || account.connecting) {
-      let connect = document.getElementById("cmd_connect");
-      connect.setAttribute("disabled", "true");
-      this.restoreButtonTimer();
+      this.temporarilyDisableButtons();
       account.disconnect();
     }
   },
   updateConnectedLabels: function am_updateConnectedLabels() {
     for (let i = 0; i < gAccountManager.accountList.itemCount; ++i) {
       let item = gAccountManager.accountList.getItemAtIndex(i);
       if (item.account.connected)
         item.refreshConnectedLabel();
     }
   },
-  /* This function restores the disabled attribute of the currently visible
-     button (and context menu item) after `this._disabledDelay` ms */
-  restoreButtonTimer: function am_restoreButtonTimer() {
+  /* This function disables the connect/disconnect buttons for
+   * `this._disabledDelay` ms before calling disableCommandItems to restore
+   * the state of the buttons.
+   */
+  temporarilyDisableButtons: function am_temporarilyDisableButtons() {
+    document.getElementById("cmd_disconnect").setAttribute("disabled", "true");
+    document.getElementById("cmd_connect").setAttribute("disabled", "true");
     clearTimeout(this.disableTimerID);
     this.accountList.focus();
     this.disableTimerID = setTimeout(function(aItem) {
       gAccountManager.disableTimerID = 0;
       gAccountManager.disableCommandItems();
       aItem.buttons.setFocus();
     }, this._disabledDelay, this.accountList.selectedItem);
   },
@@ -288,54 +288,49 @@ var gAccountManager = {
       return;
 
     // If the timer that disables the button (for a short time) already exists,
     // we don't want to interfere and set the button as enabled.
     if (this.disableTimerID)
       return;
 
     let account = selectedItem.account;
-    let activeCommandName =
-      (this.isOffline || account.disconnected) ? "connect" : "disconnect";
-    let activeCommandElt = document.getElementById("cmd_" + activeCommandName);
     let isCommandDisabled =
       (this.isOffline ||
        (account.disconnected &&
         account.connectionErrorReason == Ci.imIAccount.ERROR_UNKNOWN_PRPL));
-    
-    [[activeCommandElt, isCommandDisabled],
-     [document.getElementById("cmd_moveup"), accountList.selectedIndex == 0],
-     [document.getElementById("cmd_movedown"),
-      accountList.selectedIndex == accountList.itemCount - 1]
-    ].forEach(function (aEltArray) {
-      let [elt, state] = aEltArray;
+
+    let disabledItems = {
+      connect: isCommandDisabled,
+      disconnect: isCommandDisabled,
+      moveup: accountList.selectedIndex == 0,
+      movedown: accountList.selectedIndex == accountList.itemCount - 1
+    };
+    for each (let [name, state] in Iterator(disabledItems)) {
+      let elt = document.getElementById("cmd_" + name);
       if (state)
         elt.setAttribute("disabled", "true");
       else
         elt.removeAttribute("disabled");
-    });
+    }
   },
   onContextMenuShowing: function am_onContextMenuShowing() {
     let targetElt = document.popupNode;
     let isAccount = targetElt instanceof Ci.nsIDOMXULSelectControlItemElement;
     document.getElementById("contextAccountsItems").hidden = !isAccount;
     if (isAccount) {
-       /* we want to hide either "connect" or "disconnect" depending on the
-          context and we can't use the broadcast of the command element here
-          because the item already observes "contextAccountsItems" */
-      let itemNameToHide, itemNameToShow;
-      if (targetElt.account.disconnected)
-        [itemNameToHide, itemNameToShow] = ["disconnect",  "connect"];
-      else
-        [itemNameToHide, itemNameToShow] = ["connect", "disconnect"];
-      document.getElementById("context_" + itemNameToHide).hidden = true;
-      document.getElementById("context_" + itemNameToShow).hidden = false;
-
-      document.getElementById("context_cancelReconnection").hidden =
-        !targetElt.hasAttribute("reconnectPending");
+      let account = targetElt.account;
+      let hiddenItems = {
+        connect: !account.disconnected,
+        disconnect: account.disconnected || account.disconnecting,
+        cancelReconnection: !targetElt.hasAttribute("reconnectPending"),
+        accountsItemsSeparator: account.disconnecting
+      };
+      for (let name in hiddenItems)
+        document.getElementById("context_" + name).hidden = hiddenItems[name];
     }
   },
 
   selectAccount: function am_selectAccount(aAccountId) {
     this.accountList.selectedItem = document.getElementById(aAccountId);
     this.accountList.ensureSelectedElementIsVisible();
   },
   onAccountSelect: function am_onAccountSelect() {
--- a/mail/components/im/content/imAccounts.xul
+++ b/mail/components/im/content/imAccounts.xul
@@ -82,17 +82,18 @@
              command="cmd_connect"
              observes="contextAccountsItems"/>
    <menuitem id="context_disconnect"
              command="cmd_disconnect"
              observes="contextAccountsItems"/>
    <menuitem id="context_cancelReconnection"
              command="cmd_cancelReconnection"
              observes="contextAccountsItems"/>
-   <menuseparator observes="contextAccountsItems"/>
+   <menuseparator id="context_accountsItemsSeparator"
+                  observes="contextAccountsItems"/>
    <menuitem command="cmd_new"/>
    <menuseparator observes="contextAccountsItems"/>
    <menuitem command="cmd_moveup" observes="contextAccountsItems"/>
    <menuitem command="cmd_movedown" observes="contextAccountsItems"/>
    <menuseparator observes="contextAccountsItems"/>
    <menuitem command="cmd_edit" observes="contextAccountsItems"/>
  </menupopup>