diff --git a/content/treestyletab/bookmarksOverlay.js b/content/treestyletab/bookmarksOverlay.js index 4178687a..03b803df 100644 --- a/content/treestyletab/bookmarksOverlay.js +++ b/content/treestyletab/bookmarksOverlay.js @@ -90,9 +90,17 @@ var TreeStyleTabBookmarksService = { getTreeStructureFromItems : function(aIDs) { + /* this returns... + [A] => -1 (parent is not in this tree) + [B] => 0 (parent is 1st item in this tree) + [C] => 0 (parent is 1st item in this tree) + [D] => 2 (parent is 2nd in this tree) + [E] => -1 (parent is not in this tree, and this creates another tree) + [F] => 0 (parent is 1st item in this another tree) + */ var treeStructure = aIDs.map(function(aId, aIndex) { let id = this.getParentItem(aId); - let index = id < 0 ? -1 : aIDs.indexOf(id); + let index = aIDs.indexOf(id); return index < aIndex ? index : -1 ; }, this); @@ -116,8 +124,17 @@ var TreeStyleTabBookmarksService = { }); treeStructure = treeStructure.reverse(); - treeStructure = treeStructure.map(function(aPosition, aIndex) { + var offset = 0; + treeStructure = treeStructure + .map(function(aPosition, aIndex) { return (aPosition == aIndex) ? -1 : aPosition ; + }) + .map(function(aPosition, aIndex) { + if (aPosition == -1) { + offset = aIndex; + return aPosition; + } + return aPosition - offset; }); return treeStructure; }, diff --git a/content/treestyletab/treestyletabbrowser.js b/content/treestyletab/treestyletabbrowser.js index fe360f10..75ea9a39 100644 --- a/content/treestyletab/treestyletabbrowser.js +++ b/content/treestyletab/treestyletabbrowser.js @@ -1400,13 +1400,17 @@ TreeStyleTabBrowser.prototype = { this.initTab(tab); var hasStructure = this.treeStructure && this.treeStructure.length; - var positionInTree = hasStructure ? this.treeStructure.shift() : -1 ; + var pareintIndexInTree = hasStructure ? this.treeStructure.shift() : 0 ; if (this.readiedToAttachNewTab) { + if (pareintIndexInTree < 0) { // there is no parent, so this is a new parent! + this.parentTab = tab.getAttribute(this.kID); + } + let parent = this.getTabById(this.parentTab); if (parent) { let tabs = [parent].concat(this.getDescendantTabs(parent)); - parent = (positionInTree > -1 && positionInTree < tabs.length) ? tabs[positionInTree] : parent ; + parent = pareintIndexInTree < tabs.length ? tabs[pareintIndexInTree] : parent ; } if (parent) { this.attachTabTo(tab, parent);