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:
parent
4f65a3a9a1
commit
ea4d6ae68b
@ -134,29 +134,6 @@ TreeStyleTabBrowser.prototype = {
|
||||
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 */
|
||||
|
||||
init : function TSTBrowser_init()
|
||||
@ -166,7 +143,7 @@ TreeStyleTabBrowser.prototype = {
|
||||
var b = this.mTabBrowser;
|
||||
b.tabContainer.treeStyleTab = this;
|
||||
|
||||
this._tabsCache = {};
|
||||
this.tabsHash = {};
|
||||
|
||||
this.internallyTabMovingCount = 0;
|
||||
this.subTreeMovingCount = 0;
|
||||
@ -695,12 +672,12 @@ TreeStyleTabBrowser.prototype = {
|
||||
window.setTimeout(function(aSelf) {
|
||||
if (!aSelf.getTabValue(aTab, aSelf.kID)) {
|
||||
aSelf.setTabValue(aTab, aSelf.kID, id);
|
||||
if (!(id in aSelf._tabsCache))
|
||||
aSelf._tabsCache[id] = aTab;
|
||||
if (!(id in aSelf.tabsHash))
|
||||
aSelf.tabsHash[id] = aTab;
|
||||
}
|
||||
}, 0, this);
|
||||
if (!(id in this._tabsCache))
|
||||
this._tabsCache[id] = aTab;
|
||||
if (!(id in this.tabsHash))
|
||||
this.tabsHash[id] = aTab;
|
||||
}
|
||||
|
||||
aTab.__treestyletab__linkedTabBrowser = this.mTabBrowser;
|
||||
@ -1329,8 +1306,8 @@ TreeStyleTabBrowser.prototype = {
|
||||
destroyTab : function TSTBrowser_destroyTab(aTab)
|
||||
{
|
||||
var id = aTab.getAttribute(this.kID);
|
||||
if (id in this._tabsCache)
|
||||
delete this._tabsCache[id];
|
||||
if (id in this.tabsHash)
|
||||
delete this.tabsHash[id];
|
||||
|
||||
delete aTab.__treestyletab__linkedTabBrowser;
|
||||
},
|
||||
@ -2197,7 +2174,7 @@ TreeStyleTabBrowser.prototype = {
|
||||
this.deleteTabValue(tab, this.kCLOSED_SET_ID);
|
||||
|
||||
this.setTabValue(tab, this.kID, id);
|
||||
this._tabsCache[id] = tab;
|
||||
this.tabsHash[id] = tab;
|
||||
|
||||
if (closeSetId)
|
||||
this.restoreClosedSet(closeSetId, tab);
|
||||
|
118
modules/utils.js
118
modules/utils.js
@ -51,6 +51,7 @@ Components.utils.import('resource://treestyletab-modules/stringBundle.js', strin
|
||||
stringBundle = stringBundle.window['piro.sakura.ne.jp'].stringBundle;
|
||||
|
||||
var TreeStyleTabUtils = {
|
||||
tabsHash : null,
|
||||
|
||||
/* attributes */
|
||||
kID : 'treestyletab-id',
|
||||
@ -845,7 +846,15 @@ var TreeStyleTabUtils = {
|
||||
getTabById : function TSTUtils_getTabById(aId, aTabBrowserChildren)
|
||||
{
|
||||
if (!aId) return null;
|
||||
|
||||
if (aTabBrowserChildren && !(aTabBrowserChildren instanceof Ci.nsIDOMNode))
|
||||
aTabBrowserChildren = null;
|
||||
|
||||
var b = this.getTabBrowserFromChild(aTabBrowserChildren) || this.browser;
|
||||
|
||||
if (this.tabsHash) // XPath-less implementation
|
||||
return this.tabsHash[aId] || null;
|
||||
|
||||
return this.evaluateXPath(
|
||||
'descendant::xul:tab[@'+this.kID+' = "'+aId+'"]',
|
||||
b.mTabContainer,
|
||||
@ -1259,13 +1268,14 @@ var TreeStyleTabUtils = {
|
||||
getParentTab : function TSTUtils_getParentTab(aTab) /* PUBLIC API */
|
||||
{
|
||||
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(
|
||||
'preceding-sibling::xul:tab[contains('+
|
||||
'concat("|", @'+this.kCHILDREN+', "|"),'+
|
||||
'"|'+id+'|"'+
|
||||
')][1]',
|
||||
'preceding-sibling::xul:tab[@'+this.kID+'="'++'"][1]',
|
||||
aTab,
|
||||
Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE
|
||||
).singleNodeValue;
|
||||
@ -1273,20 +1283,31 @@ var TreeStyleTabUtils = {
|
||||
|
||||
getRootTab : function TSTUtils_getRootTab(aTab) /* PUBLIC API */
|
||||
{
|
||||
var parent = aTab;
|
||||
var root = aTab;
|
||||
if (!aTab) return null;
|
||||
|
||||
if (this.tabsHash) { // XPath-less implementation
|
||||
let parent = aTab;
|
||||
let root = aTab;
|
||||
while (parent = this.getParentTab(parent))
|
||||
{
|
||||
root = parent;
|
||||
}
|
||||
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 */
|
||||
{
|
||||
if (!aTab) return null;
|
||||
|
||||
var parentTab = this.getParentTab(aTab);
|
||||
if (this.tabsHash) { // XPath-less implementation
|
||||
let parentTab = this.getParentTab(aTab);
|
||||
|
||||
if (!parentTab) {
|
||||
let next = aTab;
|
||||
@ -1299,7 +1320,7 @@ var TreeStyleTabUtils = {
|
||||
return next;
|
||||
}
|
||||
|
||||
var children = parentTab.getAttribute(this.kCHILDREN);
|
||||
let children = parentTab.getAttribute(this.kCHILDREN);
|
||||
if (children) {
|
||||
let list = ('|'+children).split('|'+aTab.getAttribute(this.kID))[1].split('|');
|
||||
for (let i = 0, maxi = list.length; i < maxi; i++)
|
||||
@ -1309,13 +1330,24 @@ var TreeStyleTabUtils = {
|
||||
}
|
||||
}
|
||||
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 */
|
||||
{
|
||||
if (!aTab) return null;
|
||||
|
||||
var parentTab = this.getParentTab(aTab);
|
||||
if (this.tabsHash) { // XPath-less implementation
|
||||
let parentTab = this.getParentTab(aTab);
|
||||
|
||||
if (!parentTab) {
|
||||
let prev = aTab;
|
||||
@ -1328,7 +1360,7 @@ var TreeStyleTabUtils = {
|
||||
return prev;
|
||||
}
|
||||
|
||||
var children = parentTab.getAttribute(this.kCHILDREN);
|
||||
let children = parentTab.getAttribute(this.kCHILDREN);
|
||||
if (children) {
|
||||
let list = ('|'+children).split('|'+aTab.getAttribute(this.kID))[0].split('|');
|
||||
for (let i = list.length-1; i > -1; i--)
|
||||
@ -1338,6 +1370,16 @@ var TreeStyleTabUtils = {
|
||||
}
|
||||
}
|
||||
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 */
|
||||
@ -1380,8 +1422,9 @@ var TreeStyleTabUtils = {
|
||||
{
|
||||
if (!aTab) return null;
|
||||
|
||||
var children = aTab.getAttribute(this.kCHILDREN);
|
||||
var firstChild = null;
|
||||
if (this.tabsHash) { // XPath-less implementation
|
||||
let children = aTab.getAttribute(this.kCHILDREN);
|
||||
let firstChild = null;
|
||||
if (children) {
|
||||
let list = children.split('|');
|
||||
for (let i = 0, maxi = list.length; i < maxi; i++)
|
||||
@ -1391,14 +1434,22 @@ var TreeStyleTabUtils = {
|
||||
}
|
||||
}
|
||||
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 */
|
||||
{
|
||||
if (!aTab) return null;
|
||||
|
||||
var children = aTab.getAttribute(this.kCHILDREN);
|
||||
var lastChild = null;
|
||||
if (this.tabsHash) { // XPath-less implementation
|
||||
let children = aTab.getAttribute(this.kCHILDREN);
|
||||
let lastChild = null;
|
||||
if (children) {
|
||||
let list = children.split('|');
|
||||
for (let i = list.length-1; i > -1; i--)
|
||||
@ -1408,19 +1459,38 @@ var TreeStyleTabUtils = {
|
||||
}
|
||||
}
|
||||
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 */
|
||||
{
|
||||
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 ;
|
||||
}
|
||||
|
||||
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) {
|
||||
parent = aTab;
|
||||
while (parent && parent != aParent)
|
||||
@ -1450,6 +1520,16 @@ var TreeStyleTabUtils = {
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user