Bug 262329 - Sort By Name should sort between separators. r=sspitzer.
authormozilla.mano@sent.com
Sun, 01 Jul 2007 05:23:38 -0700
changeset 2998 c4561b783f25078caeaf46f6c160b63f28003489
parent 2997 6437244db1939a5ec48780651595e315d7baf37f
child 2999 c09e02d9725f45be11c551e49a7bde6fe7acefc8
push idunknown
push userunknown
push dateunknown
reviewerssspitzer
bugs262329
milestone1.9a6pre
Bug 262329 - Sort By Name should sort between separators. r=sspitzer.
browser/components/places/content/controller.js
--- a/browser/components/places/content/controller.js
+++ b/browser/components/places/content/controller.js
@@ -213,17 +213,19 @@ PlacesController.prototype = {
           return true;
 #endif
       }
       return false;
     case "placesCmd_sortBy:name":
       var selectedNode = this._view.selectedNode;
       return selectedNode &&
              PlacesUtils.nodeIsFolder(selectedNode) &&
-             !PlacesUtils.nodeIsReadOnly(selectedNode);
+             !PlacesUtils.nodeIsReadOnly(selectedNode) &&
+             this._view.getResult().sortingMode ==
+                 Ci.nsINavHistoryQueryOptions.SORT_BY_NONE;
     case "placesCmd_setAsBookmarksToolbarFolder":
       if (this._view.hasSingleSelection) {
         var selectedNode = this._view.selectedNode;
         if (PlacesUtils.nodeIsFolder(selectedNode) &&
             selectedNode.itemId != PlacesUtils.toolbarFolderId) {
           return true;
         }
       }
@@ -2228,29 +2230,48 @@ function PlacesSortFolderByNameTransacti
   this.redoTransaction = this.doTransaction;
 }
 PlacesSortFolderByNameTransaction.prototype = {
   __proto__: PlacesBaseTransaction.prototype,
 
   doTransaction: function PSSFBN_doTransaction() {
     this._oldOrder = [];
 
-    var items = [];
     var contents = this.utils.getFolderContents(this._folderId, false, false);
     var count = contents.childCount;
+
+    // sort between separators
+    var newOrder = []; 
+    var preSep = []; // temporary array for sorting each group of items
+    var sortingMethod =
+      function (a, b) { return a.title.localeCompare(b.title); };
+
     for (var i = 0; i < count; ++i) {
       var item = contents.getChild(i);
       this._oldOrder[item.itemId] = i;
-      items.push(item);
+      if (this.utils.nodeIsSeparator(item)) {
+        if (preSep.length > 0) {
+          preSep.sort(sortingMethod);
+          newOrder = newOrder.concat(preSep);
+          preSep.splice(0);
+        }
+        newOrder.push(item);
+      }
+      else
+        preSep.push(item);
+    }
+    if (preSep.length > 0) {
+      preSep.sort(sortingMethod);
+      newOrder = newOrder.concat(preSep);
     }
 
-    items.sort(function (a, b) { return a.title.localeCompare(b.title); });
-
-    for (var i = 0; i < count; ++i)
-      this.bookmarks.setItemIndex(items[i].itemId, i);
+    // set the nex indexs
+    for (var i = 0; i < count; ++i) {
+      this.bookmarks.setItemIndex(newOrder[i].itemId, i);
+    }
   },
 
   undoTransaction: function PSSFBN_undoTransaction() {
     for (item in this._oldOrder)
       this.bookmarks.setItemIndex(item, this._oldOrder[item]);
   }
 };