Bug 103684 - Insert new filter at the current position / above the selected filter. ui-r=bwinton, r=mconley
authoraceman <acelists@atlas.sk>
Wed, 25 Apr 2012 19:23:59 -0400
changeset 11983 70892b85ee8a8b287fecbd4161f1e201d8c6f272
parent 11982 406d1046698a53068c2330cfbf3cbffbc93a0d72
child 11984 c6359e5dddab12f6d77024f5624478d060f205c8
push id599
push usermconley@mozilla.com
push dateMon, 16 Jul 2012 20:33:12 +0000
treeherdercomm-beta@c3489d5b7b65 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbwinton, mconley
bugs103684
Bug 103684 - Insert new filter at the current position / above the selected filter. ui-r=bwinton, r=mconley
mail/base/content/FilterListDialog.js
mailnews/base/search/content/FilterEditor.js
--- a/mail/base/content/FilterListDialog.js
+++ b/mail/base/content/FilterListDialog.js
@@ -247,28 +247,44 @@ function onEditFilter()
   window.openDialog("chrome://messenger/content/FilterEditor.xul", "FilterEditor", "chrome,modal,titlebar,resizable,centerscreen", args);
 
   if ("refresh" in args && args.refresh)
     rebuildFilterList(gCurrentFilterList);
 }
 
 function onNewFilter(emailAddress)
 {
-  var args = {filterList: gCurrentFilterList};
+  let list = document.getElementById("filterList");
+  let filterNodes = list.childNodes;
+  let selectedFilter = currentFilter();
+  // if no filter is selected use the first position, starting at 1
+  let position = 1;
+  if (selectedFilter) {
+    // The filterNodes[0] item is the list header, skip it.
+    for (let i = 1; i < filterNodes.length; i++) {
+      if (filterNodes[i]._filter == selectedFilter) {
+        position = i;
+        break;
+      }
+    }
+  }
+
+  // The returned position is offset by 1 (due to the list header)
+  // compared to filter indexes in gCurrentFilterList.
+  let args = {filterList: gCurrentFilterList, filterPosition: position - 1};
 
   window.openDialog("chrome://messenger/content/FilterEditor.xul", "FilterEditor", "chrome,modal,titlebar,resizable,centerscreen", args);
 
   if ("refresh" in args && args.refresh) {
     rebuildFilterList(gCurrentFilterList);
 
-    // the new filter is always added as first item on top. Select it.
-    let list = document.getElementById("filterList");
+    // select the new filter, it is at the position of previous selection
     list.clearSelection();
-    list.addItemToSelection(list.childNodes[1]);
-    updateViewPosition(1);
+    list.addItemToSelection(list.childNodes[position]);
+    updateViewPosition(position);
   }
 }
 
 function onDeleteFilter()
 {
   let list = document.getElementById("filterList");
   let items = list.selectedItems;
   if (!items.length)
--- a/mailnews/base/search/content/FilterEditor.js
+++ b/mailnews/base/search/content/FilterEditor.js
@@ -54,16 +54,17 @@ var gFilterNameElement;
 var gFilterContext;
 var gFilterBundle;
 var gPreFillName;
 var nsMsgSearchScope = Components.interfaces.nsMsgSearchScope;
 var gSessionFolderListenerAdded = false;
 var gFilterActionList;
 var gCustomActions = null;
 var gFilterType;
+var gFilterPosition = 0;
 
 var gFilterActionStrings = ["none", "movemessage", "setpriorityto", "deletemessage",
                             "markasread", "ignorethread", "watchthread", "markasflagged",
                             "label", "replytomessage", "forwardmessage", "stopexecution",
                             "deletefrompopserver",  "leaveonpopserver", "setjunkscore",
                             "fetchfrompopserver", "copymessage", "addtagtomessage",
                             "ignoresubthread", "markasunread"];
 
@@ -91,16 +92,21 @@ function filterEditorOnLoad()
       let server = gFilterList.folder.server;
       let postPluginDisabled = server.rootFolder != server.rootMsgFolder;
       document.getElementById("contextMenuListPostPlugin")
               .disabled = postPluginDisabled;
       document.getElementById("contextMenuListPostPluginBoth")
               .disabled = postPluginDisabled;
     }
 
+    if ("filterPosition" in args)
+    {
+      gFilterPosition = args.filterPosition;
+    }
+
     if ("filter" in args)
     {
       // editing a filter
       gFilter = window.arguments[0].filter;
       initializeDialog(gFilter);
     }
     else
     {
@@ -423,17 +429,17 @@ function saveFilter()
     AssignMeaningfulName();
 
   gFilter.filterType = gFilterType;
   saveSearchTerms(gFilter.searchTerms, gFilter);
 
   if (isNewFilter)
   {
     // new filter - insert into gFilterList
-    gFilterList.insertFilterAt(0, gFilter);
+    gFilterList.insertFilterAt(gFilterPosition, gFilter);
   }
 
   // success!
   return true;
 }
 
 
 function AssignMeaningfulName()