XPathを使った簡潔な実装と、XPathを使わない高速な実装の両方を1箇所に記述するようにした

git-svn-id: http://www.cozmixng.org/repos/piro/treestyletab/trunk@6423 599a83e7-65a4-db11-8015-0010dcdd6dc2
This commit is contained in:
piro 2010-03-24 15:38:08 +00:00
parent 4f65a3a9a1
commit ea4d6ae68b
2 changed files with 201 additions and 144 deletions

View File

@ -134,29 +134,6 @@ TreeStyleTabBrowser.prototype = {
return false; return false;
}, },
getTabById : function TSTBrowser_getTabById(aId, aTabBrowserChildren) /* PUBLIC API */
{
if (aTabBrowserChildren && !(aTabBrowserChildren instanceof Ci.nsIDOMNode))
aTabBrowserChildren = null;
if (aTabBrowserChildren) {
var b = this.getTabBrowserFromChild(aTabBrowserChildren);
if (!b)
return null;
if (b != this.mTabBrowser)
return b.treeStyleTab.getTabById(aId);
}
return this._tabsCache[aId] || null;
},
getParentTab : function TSTBrowser_getParentTab(aTab) /* PUBLIC API */
{
if (!aTab) return null;
var b = this.getTabBrowserFromChild(aTab);
return b.treeStyleTab.getTabById(aTab.getAttribute(this.kPARENT));
},
/* initialize */ /* initialize */
init : function TSTBrowser_init() init : function TSTBrowser_init()
@ -166,7 +143,7 @@ TreeStyleTabBrowser.prototype = {
var b = this.mTabBrowser; var b = this.mTabBrowser;
b.tabContainer.treeStyleTab = this; b.tabContainer.treeStyleTab = this;
this._tabsCache = {}; this.tabsHash = {};
this.internallyTabMovingCount = 0; this.internallyTabMovingCount = 0;
this.subTreeMovingCount = 0; this.subTreeMovingCount = 0;
@ -695,12 +672,12 @@ TreeStyleTabBrowser.prototype = {
window.setTimeout(function(aSelf) { window.setTimeout(function(aSelf) {
if (!aSelf.getTabValue(aTab, aSelf.kID)) { if (!aSelf.getTabValue(aTab, aSelf.kID)) {
aSelf.setTabValue(aTab, aSelf.kID, id); aSelf.setTabValue(aTab, aSelf.kID, id);
if (!(id in aSelf._tabsCache)) if (!(id in aSelf.tabsHash))
aSelf._tabsCache[id] = aTab; aSelf.tabsHash[id] = aTab;
} }
}, 0, this); }, 0, this);
if (!(id in this._tabsCache)) if (!(id in this.tabsHash))
this._tabsCache[id] = aTab; this.tabsHash[id] = aTab;
} }
aTab.__treestyletab__linkedTabBrowser = this.mTabBrowser; aTab.__treestyletab__linkedTabBrowser = this.mTabBrowser;
@ -1329,8 +1306,8 @@ TreeStyleTabBrowser.prototype = {
destroyTab : function TSTBrowser_destroyTab(aTab) destroyTab : function TSTBrowser_destroyTab(aTab)
{ {
var id = aTab.getAttribute(this.kID); var id = aTab.getAttribute(this.kID);
if (id in this._tabsCache) if (id in this.tabsHash)
delete this._tabsCache[id]; delete this.tabsHash[id];
delete aTab.__treestyletab__linkedTabBrowser; delete aTab.__treestyletab__linkedTabBrowser;
}, },
@ -2197,7 +2174,7 @@ TreeStyleTabBrowser.prototype = {
this.deleteTabValue(tab, this.kCLOSED_SET_ID); this.deleteTabValue(tab, this.kCLOSED_SET_ID);
this.setTabValue(tab, this.kID, id); this.setTabValue(tab, this.kID, id);
this._tabsCache[id] = tab; this.tabsHash[id] = tab;
if (closeSetId) if (closeSetId)
this.restoreClosedSet(closeSetId, tab); this.restoreClosedSet(closeSetId, tab);

View File

@ -51,6 +51,7 @@ Components.utils.import('resource://treestyletab-modules/stringBundle.js', strin
stringBundle = stringBundle.window['piro.sakura.ne.jp'].stringBundle; stringBundle = stringBundle.window['piro.sakura.ne.jp'].stringBundle;
var TreeStyleTabUtils = { var TreeStyleTabUtils = {
tabsHash : null,
/* attributes */ /* attributes */
kID : 'treestyletab-id', kID : 'treestyletab-id',
@ -845,7 +846,15 @@ var TreeStyleTabUtils = {
getTabById : function TSTUtils_getTabById(aId, aTabBrowserChildren) getTabById : function TSTUtils_getTabById(aId, aTabBrowserChildren)
{ {
if (!aId) return null; if (!aId) return null;
if (aTabBrowserChildren && !(aTabBrowserChildren instanceof Ci.nsIDOMNode))
aTabBrowserChildren = null;
var b = this.getTabBrowserFromChild(aTabBrowserChildren) || this.browser; var b = this.getTabBrowserFromChild(aTabBrowserChildren) || this.browser;
if (this.tabsHash) // XPath-less implementation
return this.tabsHash[aId] || null;
return this.evaluateXPath( return this.evaluateXPath(
'descendant::xul:tab[@'+this.kID+' = "'+aId+'"]', 'descendant::xul:tab[@'+this.kID+' = "'+aId+'"]',
b.mTabContainer, b.mTabContainer,
@ -1259,13 +1268,14 @@ var TreeStyleTabUtils = {
getParentTab : function TSTUtils_getParentTab(aTab) /* PUBLIC API */ getParentTab : function TSTUtils_getParentTab(aTab) /* PUBLIC API */
{ {
if (!aTab) return null; if (!aTab) return null;
var id = aTab.getAttribute(this.kID);
if (!id) return null; // not initialized yet if (this.tabsHash) { // XPath-less implementation
let parent = this.getTabById(aTab.getAttribute(this.kPARENT));
return (parent && parent._tPos < aTab._tPos) ? parent : null ;
}
return this.evaluateXPath( return this.evaluateXPath(
'preceding-sibling::xul:tab[contains('+ 'preceding-sibling::xul:tab[@'+this.kID+'="'++'"][1]',
'concat("|", @'+this.kCHILDREN+', "|"),'+
'"|'+id+'|"'+
')][1]',
aTab, aTab,
Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE
).singleNodeValue; ).singleNodeValue;
@ -1273,20 +1283,31 @@ var TreeStyleTabUtils = {
getRootTab : function TSTUtils_getRootTab(aTab) /* PUBLIC API */ getRootTab : function TSTUtils_getRootTab(aTab) /* PUBLIC API */
{ {
var parent = aTab; if (!aTab) return null;
var root = aTab;
if (this.tabsHash) { // XPath-less implementation
let parent = aTab;
let root = aTab;
while (parent = this.getParentTab(parent)) while (parent = this.getParentTab(parent))
{ {
root = parent; root = parent;
} }
return root; return root;
}
return this.evaluateXPath(
'(self::*[not(@'+this.kPARENT+')] | preceding-sibling::xul:tab[not(@'+this.kPARENT+')])[last()]',
aTab,
Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE
).singleNodeValue;
}, },
getNextSiblingTab : function TSTUtils_getNextSiblingTab(aTab) /* PUBLIC API */ getNextSiblingTab : function TSTUtils_getNextSiblingTab(aTab) /* PUBLIC API */
{ {
if (!aTab) return null; if (!aTab) return null;
var parentTab = this.getParentTab(aTab); if (this.tabsHash) { // XPath-less implementation
let parentTab = this.getParentTab(aTab);
if (!parentTab) { if (!parentTab) {
let next = aTab; let next = aTab;
@ -1299,7 +1320,7 @@ var TreeStyleTabUtils = {
return next; return next;
} }
var children = parentTab.getAttribute(this.kCHILDREN); let children = parentTab.getAttribute(this.kCHILDREN);
if (children) { if (children) {
let list = ('|'+children).split('|'+aTab.getAttribute(this.kID))[1].split('|'); let list = ('|'+children).split('|'+aTab.getAttribute(this.kID))[1].split('|');
for (let i = 0, maxi = list.length; i < maxi; i++) for (let i = 0, maxi = list.length; i < maxi; i++)
@ -1309,13 +1330,24 @@ var TreeStyleTabUtils = {
} }
} }
return null; return null;
}
var parent = aTab.getAttribute(this.kPARENT);
return this.evaluateXPath(
'following-sibling::xul:tab['+
(parent ? '@'+this.kPARENT+'="'+parent+'"' : 'not(@'+this.kPARENT+')' )+
'][1]',
aTab,
Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE
).singleNodeValue;
}, },
getPreviousSiblingTab : function TSTUtils_getPreviousSiblingTab(aTab) /* PUBLIC API */ getPreviousSiblingTab : function TSTUtils_getPreviousSiblingTab(aTab) /* PUBLIC API */
{ {
if (!aTab) return null; if (!aTab) return null;
var parentTab = this.getParentTab(aTab); if (this.tabsHash) { // XPath-less implementation
let parentTab = this.getParentTab(aTab);
if (!parentTab) { if (!parentTab) {
let prev = aTab; let prev = aTab;
@ -1328,7 +1360,7 @@ var TreeStyleTabUtils = {
return prev; return prev;
} }
var children = parentTab.getAttribute(this.kCHILDREN); let children = parentTab.getAttribute(this.kCHILDREN);
if (children) { if (children) {
let list = ('|'+children).split('|'+aTab.getAttribute(this.kID))[0].split('|'); let list = ('|'+children).split('|'+aTab.getAttribute(this.kID))[0].split('|');
for (let i = list.length-1; i > -1; i--) for (let i = list.length-1; i > -1; i--)
@ -1338,6 +1370,16 @@ var TreeStyleTabUtils = {
} }
} }
return null; return null;
}
var parent = aTab.getAttribute(this.kPARENT);
return this.evaluateXPath(
'preceding-sibling::xul:tab['+
(parent ? '@'+this.kPARENT+'="'+parent+'"' : 'not(@'+this.kPARENT+')' )+
'][1]',
aTab,
Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE
).singleNodeValue;
}, },
getChildTabs : function TSTUtils_getChildTabs(aTab, aAllTabsArray) /* PUBLIC API */ getChildTabs : function TSTUtils_getChildTabs(aTab, aAllTabsArray) /* PUBLIC API */
@ -1380,8 +1422,9 @@ var TreeStyleTabUtils = {
{ {
if (!aTab) return null; if (!aTab) return null;
var children = aTab.getAttribute(this.kCHILDREN); if (this.tabsHash) { // XPath-less implementation
var firstChild = null; let children = aTab.getAttribute(this.kCHILDREN);
let firstChild = null;
if (children) { if (children) {
let list = children.split('|'); let list = children.split('|');
for (let i = 0, maxi = list.length; i < maxi; i++) for (let i = 0, maxi = list.length; i < maxi; i++)
@ -1391,14 +1434,22 @@ var TreeStyleTabUtils = {
} }
} }
return firstChild; return firstChild;
}
return this.evaluateXPath(
'following-sibling::xul:tab[@'+this.kPARENT+'="'+aTab.getAttribute(this.kID)+'"][1]',
aTab,
Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE
).singleNodeValue;
}, },
getLastChildTab : function TSTUtils_getLastChildTab(aTab) /* PUBLIC API */ getLastChildTab : function TSTUtils_getLastChildTab(aTab) /* PUBLIC API */
{ {
if (!aTab) return null; if (!aTab) return null;
var children = aTab.getAttribute(this.kCHILDREN); if (this.tabsHash) { // XPath-less implementation
var lastChild = null; let children = aTab.getAttribute(this.kCHILDREN);
let lastChild = null;
if (children) { if (children) {
let list = children.split('|'); let list = children.split('|');
for (let i = list.length-1; i > -1; i--) for (let i = list.length-1; i > -1; i--)
@ -1408,19 +1459,38 @@ var TreeStyleTabUtils = {
} }
} }
return lastChild; return lastChild;
}
return this.evaluateXPath(
'following-sibling::xul:tab[@'+this.kPARENT+'="'+aTab.getAttribute(this.kID)+'"][last()]',
aTab,
Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE
).singleNodeValue;
}, },
getLastDescendantTab : function TSTUtils_getLastDescendantTab(aTab) /* PUBLIC API */ getLastDescendantTab : function TSTUtils_getLastDescendantTab(aTab) /* PUBLIC API */
{ {
if (!aTab) return null; if (!aTab) return null;
var tabs = this.getDescendantTabs(aTab); if (this.tabsHash) { // XPath-less implementation
let tabs = this.getDescendantTabs(aTab);
return tabs.length ? tabs[tabs.length-1] : null ; return tabs.length ? tabs[tabs.length-1] : null ;
}
var parent = aTab.getAttribute(this.kPARENT);
return this.evaluateXPath(
'following-sibling::xul:tab['+
(parent ? '@'+this.kPARENT+'="'+parent+'"' : 'not(@'+this.kPARENT+')' )+
'][1]/preceding-sibling::xul:tab[1][not(@'+this.kID+'="'+aTab.getAttribute(this.kID)+'")]',
aTab,
Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE
).singleNodeValue;
}, },
getChildIndex : function TSTUtils_getChildIndex(aTab, aParent) getChildIndex : function TSTUtils_getChildIndex(aTab, aParent) /* PUBLIC API */
{ {
var parent = this.getParentTab(aTab); if (this.tabsHash) { // XPath-less implementation
let parent = this.getParentTab(aTab);
if (!aParent || !parent || aParent != parent) { if (!aParent || !parent || aParent != parent) {
parent = aTab; parent = aTab;
while (parent && parent != aParent) while (parent && parent != aParent)
@ -1450,6 +1520,16 @@ var TreeStyleTabUtils = {
if (tabs[i] == aTab) return i; if (tabs[i] == aTab) return i;
} }
} }
return -1;
}
var parent = aTab.getAttribute(this.kPARENT);
if (!parent) return -1;
return this.evaluateXPath(
'count(preceding-sibling::xul:tab[@'+this.kPARENT+' and @'+this.kPARENT+'="'+parent+'"])',
aTab,
Ci.nsIDOMXPathResult.NUMBER_TYPE
).numberValue;
}, },
getXOffsetOfTab : function TSTUtils_getXOffsetOfTab(aTab) getXOffsetOfTab : function TSTUtils_getXOffsetOfTab(aTab)