Bug 78808 - Enable Cert Manager buttons only when they would have an effect. Original patch by Scott Johnson. r=keeler
authorCykesiopka <cykesiopka.bmo@gmail.com>
Sat, 31 Jan 2015 14:20:00 +0100
changeset 226981 645f4595673d9d9064ebb980d52bf1c5fa6dc2d1
parent 226980 f77580c492c3386fab00b2c143614d3e40df1a37
child 226982 760865ff21a877d79745256ab0f0ec87a1b24b74
push id54975
push usercbook@mozilla.com
push dateMon, 02 Feb 2015 08:02:01 +0000
treeherdermozilla-inbound@645f4595673d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskeeler
bugs78808
milestone38.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 78808 - Enable Cert Manager buttons only when they would have an effect. Original patch by Scott Johnson. r=keeler
security/manager/pki/resources/content/certManager.js
--- a/security/manager/pki/resources/content/certManager.js
+++ b/security/manager/pki/resources/content/certManager.js
@@ -192,140 +192,110 @@ function getSelectedTreeItems()
           selected_tree_items[sc] = tree_item;
           selected_index[sc] = j;
         }
       }
     }
   }
 }
 
+/**
+ * Returns true if nothing in the given cert tree is selected or if the
+ * selection includes a container. Returns false otherwise.
+ *
+ * @param {nsICertTree} certTree
+ * @returns {Boolean}
+ */
+function nothingOrContainerSelected(certTree)
+{
+  var certTreeSelection = certTree.selection;
+  var numSelectionRanges = certTreeSelection.getRangeCount();
+
+  if (numSelectionRanges == 0) {
+    return true;
+  }
+
+  for (var i = 0; i < numSelectionRanges; i++) {
+    var o1 = {};
+    var o2 = {};
+    certTreeSelection.getRangeAt(i, o1, o2);
+    var minIndex = o1.value;
+    var maxIndex = o2.value;
+    for (var j = minIndex; j <= maxIndex; j++) {
+      if (certTree.isContainer(j)) {
+        return true;
+      }
+    }
+  }
+
+  return false;
+}
+
 function ca_enableButtons()
 {
-  var items = caTreeView.selection;
-  var nr = items.getRangeCount();
-  var toggle="false";
-  if (nr == 0) {
-    toggle="true";
-  }
-  var edit_toggle=toggle;
-/*
-  var edit_toggle="true";
-  if (nr > 0) {
-    for (var i=0; i<nr; i++) {
-      var o1 = {};
-      var o2 = {};
-      items.getRangeAt(i, o1, o2);
-      var min = o1.value;
-      var max = o2.value;
-      var stop = false;
-      for (var j=min; j<=max; j++) {
-        var tokenName = items.tree.view.getCellText(j, "tokencol");
-	if (tokenName == "Builtin Object Token") { stop = true; } break;
-      }
-      if (stop) break;
-    }
-    if (i == nr) {
-      edit_toggle="false";
-    }
-  }
-*/
+  var disableButtons = nothingOrContainerSelected(caTreeView);
+
   var enableViewButton=document.getElementById('ca_viewButton');
-  enableViewButton.setAttribute("disabled",toggle);
+  enableViewButton.setAttribute("disabled", disableButtons);
   var enableEditButton=document.getElementById('ca_editButton');
-  enableEditButton.setAttribute("disabled",edit_toggle);
+  enableEditButton.setAttribute("disabled", disableButtons);
   var enableExportButton=document.getElementById('ca_exportButton');
-  enableExportButton.setAttribute("disabled",toggle);
+  enableExportButton.setAttribute("disabled", disableButtons);
   var enableDeleteButton=document.getElementById('ca_deleteButton');
-  enableDeleteButton.setAttribute("disabled",toggle);
+  enableDeleteButton.setAttribute("disabled", disableButtons);
 }
 
 function mine_enableButtons()
 {
-  var items = userTreeView.selection;
-  var toggle="false";
-  if (items.getRangeCount() == 0) {
-    toggle="true";
-  }
+  var disableButtons = nothingOrContainerSelected(userTreeView);
+
   var enableViewButton=document.getElementById('mine_viewButton');
-  enableViewButton.setAttribute("disabled",toggle);
+  enableViewButton.setAttribute("disabled", disableButtons);
   var enableBackupButton=document.getElementById('mine_backupButton');
-  enableBackupButton.setAttribute("disabled",toggle);
+  enableBackupButton.setAttribute("disabled", disableButtons);
   var enableDeleteButton=document.getElementById('mine_deleteButton');
-  enableDeleteButton.setAttribute("disabled",toggle);
+  enableDeleteButton.setAttribute("disabled", disableButtons);
 }
 
 function websites_enableButtons()
 {
-  var items = serverTreeView.selection;
-  var count_ranges = items.getRangeCount();
-
-  var enable_delete = false;
-  var enable_view = false;
-
-  if (count_ranges > 0) {
-    enable_delete = true;
-  }
-
-  if (count_ranges == 1) {
-    var o1 = {};
-    var o2 = {};
-    items.getRangeAt(0, o1, o2); // the first range
-    if (o1.value == o2.value) {
-      // only a single item is selected
-      try {
-        var ti = serverTreeView.getTreeItem(o1.value);
-        if (ti) {
-          if (ti.cert) {
-            enable_view = true;
-          }
-        }
-      }
-      catch (e) {
-      }
-    }
-  }
+  var disableButtons = nothingOrContainerSelected(serverTreeView);
 
   var enableViewButton=document.getElementById('websites_viewButton');
-  enableViewButton.setAttribute("disabled", !enable_view);
+  enableViewButton.setAttribute("disabled", disableButtons);
   var enableExportButton=document.getElementById('websites_exportButton');
-  enableExportButton.setAttribute("disabled", !enable_view);
+  enableExportButton.setAttribute("disabled", disableButtons);
   var enableDeleteButton=document.getElementById('websites_deleteButton');
-  enableDeleteButton.setAttribute("disabled", !enable_delete);
+  enableDeleteButton.setAttribute("disabled", disableButtons);
 }
 
 function email_enableButtons()
 {
-  var items = emailTreeView.selection;
-  var toggle="false";
-  if (items.getRangeCount() == 0) {
-    toggle="true";
-  }
+  var disableButtons = nothingOrContainerSelected(emailTreeView);
+
   var enableViewButton=document.getElementById('email_viewButton');
-  enableViewButton.setAttribute("disabled",toggle);
+  enableViewButton.setAttribute("disabled", disableButtons);
   var enableEditButton=document.getElementById('email_editButton');
-  enableEditButton.setAttribute("disabled",toggle);
+  enableEditButton.setAttribute("disabled", disableButtons);
   var enableExportButton=document.getElementById('email_exportButton');
-  enableExportButton.setAttribute("disabled",toggle);
+  enableExportButton.setAttribute("disabled", disableButtons);
   var enableDeleteButton=document.getElementById('email_deleteButton');
-  enableDeleteButton.setAttribute("disabled",toggle);
+  enableDeleteButton.setAttribute("disabled", disableButtons);
 }
 
 function orphan_enableButtons()
 {
-  var items = orphanTreeView.selection;
-  var toggle="false";
-  if (items.getRangeCount() == 0) {
-    toggle="true";
-  }
+  var disableButtons = nothingOrContainerSelected(orphanTreeView);
+
   var enableViewButton=document.getElementById('orphan_viewButton');
-  enableViewButton.setAttribute("disabled",toggle);
+  enableViewButton.setAttribute("disabled", disableButtons);
   var enableExportButton=document.getElementById('orphan_exportButton');
-  enableExportButton.setAttribute("disabled",toggle);
+  enableExportButton.setAttribute("disabled", disableButtons);
   var enableDeleteButton=document.getElementById('orphan_deleteButton');
-  enableDeleteButton.setAttribute("disabled",toggle);
+  enableDeleteButton.setAttribute("disabled", disableButtons);
 }
 
 function backupCerts()
 {
   getSelectedCerts();
   var numcerts = selected_certs.length;
   if (!numcerts)
     return;