refactoring around performDrop()

This commit is contained in:
SHIMODA Hiroshi 2011-06-15 17:38:14 +09:00
parent 213aa81361
commit c39d55061a
3 changed files with 123 additions and 123 deletions

View File

@ -4339,6 +4339,22 @@ TreeStyleTabBrowser.prototype = {
), this); ), this);
}, },
partTabs : function TSTBrowser_partTabs(aTabs)
{
var aTabs = Array.slice(aTabs);
for each (let tab in aTabs)
{
if (aTabs.indexOf(this.getParentTab(tab)) > -1)
continue;
this.partAllChildren(tab, {
behavior : this.getCloseParentBehaviorForTab(
tab,
this.kCLOSE_PARENT_BEHAVIOR_PROMOTE_FIRST_CHILD
)
});
}
},
getCloseParentBehaviorForTab : function TSTBrowser_getCloseParentBehaviorForTab(aTab, aDefaultBehavior) getCloseParentBehaviorForTab : function TSTBrowser_getCloseParentBehaviorForTab(aTab, aDefaultBehavior)
{ {
var closeParentBehavior = this.getTreePref('closeParentBehavior'); var closeParentBehavior = this.getTreePref('closeParentBehavior');
@ -4693,6 +4709,24 @@ TreeStyleTabBrowser.prototype = {
return false; return false;
}, },
importTab : function TSTBrowser_importTab(aTab)
{
var newTab = this.mTabBrowser.addTab();
newTab.linkedBrowser.stop();
newTab.linkedBrowser.docShell;
this.mTabBrowser.swapBrowsersAndCloseOther(newTab, aTab);
this.mTabBrowser.setTabTitle(newTab);
return newTab;
},
duplicateTabAsOrphan : function TSTBrowser_duplicateTabAsOrphan(aTab)
{
var newTab = this.mTabBrowser.duplicateTab(aTab);
this.deleteTabValue(newTab, this.kCHILDREN);
this.deleteTabValue(newTab, this.kPARENT);
return newTab;
},
/* collapse/expand */ /* collapse/expand */
collapseExpandSubtree : function TSTBrowser_collapseExpandSubtree(aTab, aCollapse, aJustNow) /* PUBLIC API */ collapseExpandSubtree : function TSTBrowser_collapseExpandSubtree(aTab, aCollapse, aJustNow) /* PUBLIC API */

View File

@ -401,21 +401,22 @@ catch(e) {
var tabsInfo = this.getDraggedTabsInfoFromOneTab(aInfo, aDraggedTab); var tabsInfo = this.getDraggedTabsInfoFromOneTab(aInfo, aDraggedTab);
if (!tabsInfo.draggedTab) return false; if (!tabsInfo.draggedTab) return false;
var sourceWindow = aDraggedTab.ownerDocument.defaultView;
var sourceBrowser = sourceWindow.TreeStyleTabService.getTabBrowserFromChild(aDraggedTab);
var sourceService = sourceBrowser.treeStyleTab;
aDraggedTab = tabsInfo.draggedTab; aDraggedTab = tabsInfo.draggedTab;
var draggedTabs = tabsInfo.draggedTabs; var draggedTabs = tabsInfo.draggedTabs;
var draggedRoots = sv.collectRootTabs(tabsInfo.draggedTabs); var draggedRoots = sourceService.collectRootTabs(tabsInfo.draggedTabs);
var targetBrowser = b; var targetBrowser = b;
var tabs = sv.getTabsArray(targetBrowser); var tabs = sourceService.getTabsArray(targetBrowser);
var sourceWindow = aDraggedTab.ownerDocument.defaultView;
var sourceBrowser = sv.getTabBrowserFromChild(aDraggedTab);
var draggedWholeTree = [].concat(draggedRoots); var draggedWholeTree = [].concat(draggedRoots);
for each (let root in draggedRoots) for each (let root in draggedRoots)
{ {
let tabs = sv.getDescendantTabs(root); let tabs = sourceService.getDescendantTabs(root);
for each (let tab in tabs) for each (let tab in tabs)
{ {
if (draggedWholeTree.indexOf(tab) < 0) if (draggedWholeTree.indexOf(tab) < 0)
@ -429,19 +430,8 @@ catch(e) {
if (draggedWholeTree.length != selectedTabs.length && if (draggedWholeTree.length != selectedTabs.length &&
selectedTabs.length) { selectedTabs.length) {
draggedTabs = draggedRoots = selectedTabs; draggedTabs = draggedRoots = selectedTabs;
if (aInfo.action & sv.kACTIONS_FOR_SOURCE) { if (aInfo.action & sv.kACTIONS_FOR_SOURCE)
for each (let tab in Array.slice(selectedTabs)) sourceService.partTabs(selectedTabs);
{
if (selectedTabs.indexOf(sv.getParentTab(tab)) > -1)
continue;
sv.partAllChildren(tab, {
behavior : sv.getCloseParentBehaviorForTab(
tab,
sv.kCLOSE_PARENT_BEHAVIOR_PROMOTE_FIRST_CHILD
)
});
}
}
} }
while (aInfo.insertBefore && draggedWholeTree.indexOf(aInfo.insertBefore) > -1) while (aInfo.insertBefore && draggedWholeTree.indexOf(aInfo.insertBefore) > -1)
@ -462,7 +452,7 @@ catch(e) {
if ( // if this move will cause no change... if ( // if this move will cause no change...
sourceBrowser == targetBrowser && sourceBrowser == targetBrowser &&
sourceBrowser.treeStyleTab.getNextVisibleTab(draggedTabs[draggedTabs.length-1]) == aInfo.insertBefore sourceService.getNextVisibleTab(draggedTabs[draggedTabs.length-1]) == aInfo.insertBefore
) { ) {
// then, do nothing // then, do nothing
return true; return true;
@ -475,105 +465,74 @@ catch(e) {
targetBrowser.movingSelectedTabs = true; targetBrowser.movingSelectedTabs = true;
var newRoots = [];
var shouldClose = ( var shouldClose = (
aInfo.action & sv.kACTION_IMPORT && aInfo.action & sv.kACTION_IMPORT &&
sv.getAllTabsArray(sourceBrowser).length == draggedTabs.length sourceService.getAllTabsArray(sourceBrowser).length == draggedTabs.length
); );
var oldTabs = [];
var newTabs = []; var newTabs = [];
var treeStructure = draggedTabs.map(function(aTab) { var treeStructure = sourceService.getTreeStructureFromTabs(draggedTabs);
var parent = sourceBrowser.treeStyleTab.getParentTab(aTab);
return parent ? draggedTabs.indexOf(parent) : -1 ;
});
var parentTabsArray = draggedTabs.map(function(aTab) {
return (aInfo.action & sv.kACTIONS_FOR_DESTINATION) ?
sourceBrowser.treeStyleTab.getParentTab(aTab) : null ;
}, this);
// Firefox fails to "move" collapsed tabs. So, expand them first // Firefox fails to "move" collapsed tabs. So, expand them first
// and collapse them after they are moved. // and collapse them after they are moved.
var collapseExpandState = []; var collapsedStates = (
if (aInfo.action & sv.kACTION_MOVE || aInfo.action & sv.kACTION_IMPORT) { aInfo.action & sv.kACTION_MOVE ||
for each (let tab in draggedWholeTree) aInfo.action & sv.kACTION_IMPORT ||
{ aInfo.action & sv.kACTION_DUPLICATE
collapseExpandState.push(sv.getTabValue(tab, sv.kSUBTREE_COLLAPSED) == 'true'); ) ?
sv.collapseExpandSubtree(tab, false, true); sourceService.forceExpandTabs(draggedWholeTree) :
sv.collapseExpandTab(tab, false, true); [] ;
}
}
var lastTabIndex = tabs[tabs.length -1]._tPos; var lastTabIndex = tabs[tabs.length -1]._tPos;
for (let i in draggedTabs) for (let i in draggedTabs)
{ {
let tab = draggedTabs[i]; let tab = draggedTabs[i];
let TST = sourceService;
if (aInfo.action & sv.kACTIONS_FOR_DESTINATION) { if (aInfo.action & sv.kACTIONS_FOR_DESTINATION) {
let parent = parentTabsArray[i];
if (tabsInfo.isMultipleMove && 'MultipleTabService' in sourceWindow) if (tabsInfo.isMultipleMove && 'MultipleTabService' in sourceWindow)
sourceWindow.MultipleTabService.setSelection(tab, false); sourceWindow.MultipleTabService.setSelection(tab, false);
if (aInfo.action & sv.kACTION_IMPORT) { tab = (aInfo.action & sv.kACTION_IMPORT) ?
let newTab = targetBrowser.addTab(); sv.importTab(tab) :
newTab.linkedBrowser.stop(); sv.duplicateTabAsOrphan(tab) ;
newTab.linkedBrowser.docShell;
targetBrowser.swapBrowsersAndCloseOther(newTab, tab);
targetBrowser.setTabTitle(newTab);
tab = newTab;
}
else {
newTab = targetBrowser.duplicateTab(tab);
sv.deleteTabValue(newTab, sv.kCHILDREN);
sv.deleteTabValue(newTab, sv.kPARENT);
if (aInfo.action & sv.kACTION_IMPORT)
oldTabs.push(tab);
tab = newTab;
}
newTabs.push(tab); newTabs.push(tab);
if (tabsInfo.isMultipleMove && 'MultipleTabService' in w) if (tabsInfo.isMultipleMove && 'MultipleTabService' in w)
w.MultipleTabService.setSelection(tab, true); w.MultipleTabService.setSelection(tab, true);
if (!parent || draggedTabs.indexOf(parent) < 0)
newRoots.push(tab);
lastTabIndex++; lastTabIndex++;
TST = sv;
} }
let newIndex = aInfo.insertBefore ? aInfo.insertBefore._tPos : lastTabIndex ; let newIndex = aInfo.insertBefore ? aInfo.insertBefore._tPos : lastTabIndex ;
if (aInfo.insertBefore && newIndex > tab._tPos) newIndex--; if (aInfo.insertBefore && newIndex > tab._tPos) newIndex--;
sv.internallyTabMovingCount++; TST.internallyTabMovingCount++;
targetBrowser.moveTabTo(tab, newIndex); targetBrowser.moveTabTo(tab, newIndex);
sv.collapseExpandTab(tab, false, true); TST.collapseExpandTab(tab, false, true);
sv.internallyTabMovingCount--; TST.internallyTabMovingCount--;
} }
// close imported tabs from the source browser
for each (let tab in oldTabs)
{
sourceBrowser.removeTab(tab, { animate : true });
}
if (shouldClose) if (shouldClose)
this.closeOwner(sourceBrowser); this.closeOwner(sourceBrowser);
// restore tree structure for newly opened tabs
for (let i in newTabs)
{
let index = treeStructure[i];
if (index < 0) continue;
sv.attachTabTo(newTabs[i], newTabs[index]);
};
if (newTabs.length || aInfo.action & sv.kACTION_MOVE || aInfo.action & sv.kACTION_IMPORT) {
for (let i = collapseExpandState.length - 1; i > -1; i--)
{
let collapsed = collapseExpandState[i];
sv.collapseExpandSubtree(draggedWholeTree[i], collapsed, true);
if (newTabs.length) if (newTabs.length)
sv.collapseExpandSubtree(newTabs[i], collapsed, true); sv.applyTreeStructureToTabs(
} newTabs,
treeStructure,
collapsedStates.map(function(aCollapsed) {
return !aCollapsed
})
);
for (let i = collapsedStates.length - 1; i > -1; i--)
{
sourceService.collapseExpandSubtree(draggedWholeTree[i], collapsedStates[i], true);
} }
if (aInfo.action & sv.kACTIONS_FOR_DESTINATION && if (newTabs.length && aInfo.action & sv.kACTION_ATTACH)
aInfo.action & sv.kACTION_ATTACH) this.attachTabsOnDrop(
this.attachTabsOnDrop(newRoots, aInfo.parent); newTabs.filter(function(aTab, aIndex) {
return treeStructure[aIndex] == -1;
}),
aInfo.parent
);
// Multiple Tab Handler // Multiple Tab Handler
targetBrowser.movingSelectedTabs = false; targetBrowser.movingSelectedTabs = false;
@ -585,10 +544,11 @@ catch(e) {
getDraggedTabsInfoFromOneTab : function TabbarDND_getDraggedTabsInfoFromOneTab(aInfo, aTab) getDraggedTabsInfoFromOneTab : function TabbarDND_getDraggedTabsInfoFromOneTab(aInfo, aTab)
{ {
var sv = this.treeStyleTab; var sv = this.treeStyleTab;
var b = this.browser; var sourceWindow = aTab.ownerDocument.defaultView;
var w = this.window; var sourceBrowser = sourceWindow.TreeStyleTabService.getTabBrowserFromChild(aTab);
var sourceService = sourceBrowser.treeStyleTab;
aTab = sv.getTabFromChild(aTab); aTab = sourceService.getTabFromChild(aTab);
if (!aTab || !aTab.parentNode) // ignore removed tabs! if (!aTab || !aTab.parentNode) // ignore removed tabs!
return { return {
draggedTab : null, draggedTab : null,
@ -596,20 +556,14 @@ catch(e) {
isMultipleMove : false isMultipleMove : false
}; };
var targetBrowser = b; var draggedTabs = sourceWindow['piro.sakura.ne.jp'].tabsDragUtils.getSelectedTabs(aTab || sourceBrowser || aInfo.event);
var tabs = sv.getTabsArray(targetBrowser);
var sourceWindow = aTab.ownerDocument.defaultView;
var sourceBrowser = sv.getTabBrowserFromChild(aTab);
var draggedTabs = w['piro.sakura.ne.jp'].tabsDragUtils.getSelectedTabs(aTab || sourceBrowser || aInfo.event);
var isMultipleMove = false; var isMultipleMove = false;
if (draggedTabs.length > 1) { if (draggedTabs.length > 1) {
isMultipleMove = true; isMultipleMove = true;
} }
else if (aInfo.action & sv.kACTIONS_FOR_DESTINATION) { else if (aInfo.action & sv.kACTIONS_FOR_DESTINATION) {
draggedTabs = [aTab].concat(sourceBrowser.treeStyleTab.getDescendantTabs(aTab)); draggedTabs = [aTab].concat(sourceService.getDescendantTabs(aTab));
} }
return { return {
@ -621,8 +575,8 @@ catch(e) {
attachTabsOnDrop : function TabbarDND_attachTabsOnDrop(aTabs, aParent) attachTabsOnDrop : function TabbarDND_attachTabsOnDrop(aTabs, aParent)
{ {
var sv = this.treeStyleTab; var b = aTabs[0].ownerDocument.defaultView.TreeStyleTabService.getTabBrowserFromChild(aTabs[0]);
var b = this.browser; var sv = b.treeStyleTab;
b.movingSelectedTabs = true; // Multiple Tab Handler b.movingSelectedTabs = true; // Multiple Tab Handler
aTabs.forEach(function(aTab) { aTabs.forEach(function(aTab) {
@ -638,8 +592,8 @@ catch(e) {
partTabsOnDrop : function TabbarDND_partTabsOnDrop(aTabs) partTabsOnDrop : function TabbarDND_partTabsOnDrop(aTabs)
{ {
var sv = this.treeStyleTab; var b = aTabs[0].ownerDocument.defaultView.TreeStyleTabService.getTabBrowserFromChild(aTabs[0]);
var b = this.browser; var sv = b.treeStyleTab;
b.movingSelectedTabs = true; // Multiple Tab Handler b.movingSelectedTabs = true; // Multiple Tab Handler
aTabs.forEach(function(aTab) { aTabs.forEach(function(aTab) {

View File

@ -2081,21 +2081,11 @@ var TreeStyleTabUtils = {
collectRootTabs : function TSTUtils_collectRootTabs(aTabs) /* PUBLIC API */ collectRootTabs : function TSTUtils_collectRootTabs(aTabs) /* PUBLIC API */
{ {
var roots = []; aTabs = Array.slice(aTabs);
aTabs.forEach(function(aTab) { return aTabs.filter(function(aTab) {
var parent = aTab, var parent = this.getParentTab(aTab);
current; return !parent || aTabs.indexOf(parent) < 0;
do {
current = parent;
parent = this.getParentTab(parent)
if (parent && aTabs.indexOf(parent) > -1) continue;
if (aTabs.indexOf(current) < 0)
aTabs.push(current);
return;
}
while (parent);
}, this); }, this);
return roots;
}, },
getChildIndex : function TSTUtils_getChildIndex(aTab, aParent) /* PUBLIC API */ getChildIndex : function TSTUtils_getChildIndex(aTab, aParent) /* PUBLIC API */
@ -2187,6 +2177,18 @@ var TreeStyleTabUtils = {
).numberValue; ).numberValue;
}, },
forceExpandTabs : function TSTUtils_forceExpandTabs(aTabs)
{
var collapsedStates = aTabs.map(function(aTab) {
return this.getTabValue(aTab, this.kSUBTREE_COLLAPSED) == 'true';
}, this);
aTabs.forEach(function(aTab) {
this.collapseExpandSubtree(aTab, false, true);
this.collapseExpandTab(aTab, false, true);
}, this);
return collapsedStates;
},
getTreeStructureFromTabs : function TSTUtils_getTreeStructureFromTabs(aTabs) getTreeStructureFromTabs : function TSTUtils_getTreeStructureFromTabs(aTabs)
{ {
/* this returns... /* this returns...
@ -2229,7 +2231,7 @@ var TreeStyleTabUtils = {
return aTreeStructure; return aTreeStructure;
}, },
applyTreeStructureToTabs : function TSTUtils_applyTreeStructureToTabs(aTabs, aTreeStructure, aExpandAllTree) applyTreeStructureToTabs : function TSTUtils_applyTreeStructureToTabs(aTabs, aTreeStructure, aExpandStates)
{ {
var b = this.getTabBrowserFromChild(aTabs[0]); var b = this.getTabBrowserFromChild(aTabs[0]);
if (!b) return; if (!b) return;
@ -2238,30 +2240,40 @@ var TreeStyleTabUtils = {
aTabs = aTabs.slice(0, aTreeStructure.length); aTabs = aTabs.slice(0, aTreeStructure.length);
aTreeStructure = aTreeStructure.slice(0, aTabs.length); aTreeStructure = aTreeStructure.slice(0, aTabs.length);
aExpandStates = (aExpandStates && typeof aExpandStates == 'object') ?
aExpandStates :
aTabs.map(function(aTab) {
return !!aExpandStates;
});
aExpandStates = aExpandStates.slice(0, aTabs.length);
while (aExpandStates.length < aTabs.length) aExpandStates.push(-1);
var parentTab = null; var parentTab = null;
aTabs.forEach(function(aTab, aIndex) { aTabs.forEach(function(aTab, aIndex) {
if (sv.isCollapsed(aTab)) sv.collapseExpandTab(aTab, false, true); if (sv.isCollapsed(aTab)) sv.collapseExpandTab(aTab, false, true);
sv.partTab(aTab); sv.partTab(aTab);
var pareintIndexInTree = aTreeStructure[aIndex]; var parentIndexInTree = aTreeStructure[aIndex];
if (pareintIndexInTree < 0) { // there is no parent, so this is a new parent! if (parentIndexInTree < 0) // there is no parent, so this is a new parent!
parentTab = aTab.getAttribute(sv.kID); parentTab = aTab.getAttribute(sv.kID);
}
var parent = sv.getTabById(parentTab); var parent = sv.getTabById(parentTab);
if (parent) { if (parent) {
let tabs = [parent].concat(sv.getDescendantTabs(parent)); let tabs = [parent].concat(sv.getDescendantTabs(parent));
parent = pareintIndexInTree < tabs.length ? tabs[pareintIndexInTree] : parent ; parent = parentIndexInTree < tabs.length ? tabs[parentIndexInTree] : parent ;
} }
if (parent) { if (parent) {
sv.attachTabTo(aTab, parent, { sv.attachTabTo(aTab, parent, {
dontExpand : true, dontExpand : true,
dontMove : true dontMove : true
}); });
if (aExpandAllTree)
sv.collapseExpandSubtree(parent, false);
} }
}, sv); });
for (let i = aTabs.length-1; i > -1; i--)
{
sv.collapseExpandSubtree(aTabs[i], !aExpandStates[i], true);
}
}, },
getTreeStructureFromTabBrowser : function TSTUtils_getTreeStructureFromTabBrowser(aTabBrowser) getTreeStructureFromTabBrowser : function TSTUtils_getTreeStructureFromTabBrowser(aTabBrowser)