Bug 461152 - Move Up/Down in Message Filters doesn't scroll selected filter into view; r=mkmelin
authorKent James <kent@caspia.com>
Mon, 04 Jan 2010 10:08:36 -0800
changeset 4626 57a3752779a0247a8017a42e780ecad055401789
parent 4625 d87bb12163c6ab4f1005a1ae98187ab51efc00a1
child 4627 916675862b3a120245cdcf47a9c669b98a4645fb
push id3614
push userkent@caspia.com
push dateMon, 04 Jan 2010 18:10:36 +0000
treeherdercomm-central@57a3752779a0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin
bugs461152
Bug 461152 - Move Up/Down in Message Filters doesn't scroll selected filter into view; r=mkmelin
mail/base/content/FilterListDialog.js
--- a/mail/base/content/FilterListDialog.js
+++ b/mail/base/content/FilterListDialog.js
@@ -374,21 +374,31 @@ function rebuildFilterList(aFilterList)
   gCurrentFilterList = aFilterList;
   var list = document.getElementById("filterList");
 
   // Make a note of which filters were previously selected
   var selectedNames = [];
   for (var i = 0; i < list.selectedItems.length; i++)
     selectedNames.push(list.selectedItems[i]._filter.filterName);
 
+  // Save scroll position so we can try to restore it later.
+  let firstVisibleRowIndex = list.getIndexOfFirstVisibleRow();
+
   // Remove any existing child nodes, but not our headers
   for (var i = list.childNodes.length - 1; i > 0; i--) {
     list.removeChild(list.childNodes[i]);
   }
 
+  // listbox.xml seems to cache the value of the first selected item in a
+  // range at _selectionStart. The old value though is now obsolete,
+  // since we will recreate all of the elements. We need to clear this,
+  // and one way to do this is with a call to clearSelection. This might be
+  // ugly from an accessibility perspective, since it fires an onSelect event.
+  list.clearSelection();
+
   for (i = 0; i < aFilterList.filterCount; i++) {
     var filter = aFilterList.getFilterAt(i);
     var listitem = document.createElement("listitem");
     var nameCell = document.createElement("listcell");
     nameCell.setAttribute("label", filter.filterName);
     var enabledCell = document.createElement("listcell");
     enabledCell.setAttribute("enabled", filter.enabled);
     enabledCell.setAttribute("class", "listcell-iconic");
@@ -402,16 +412,30 @@ function rebuildFilterList(aFilterList)
 
     listitem.addEventListener("dblclick", onFilterDoubleClick, true);
     listitem._filter = filter;
     list.appendChild(listitem);
 
     if (selectedNames.indexOf(filter.filterName) != -1)
       list.addItemToSelection(listitem);
   }
+
+  // Restore to the extent possible the scroll position.
+  if (firstVisibleRowIndex && list.itemCount)
+    list.scrollToIndex(Math.min(firstVisibleRowIndex, list.itemCount - 1));
+
+  if (list.selectedCount) {
+    // Make sure that at least the first selected item is visible.
+    list.ensureElementIsVisible(list.selectedItems[0]);
+
+    // The current item should be the first selected item, so that keyboard
+    // selection extension can work.
+    list.currentItem = list.selectedItems[0];
+  }
+
   updateButtons();
   list.focus();
 }
 
 function updateButtons()
 {
     var list = document.getElementById("filterList");
     var numFiltersSelected = list.selectedItems.length;