mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-22 19:27:58 +01:00
use the singleton construction idiom to create vimperator.tabs
This commit is contained in:
@@ -529,7 +529,7 @@ vimperator.Buffer = function () //{{{
|
||||
urlSecurityCheck(aData.href, aPrincipal,
|
||||
Components.interfaces.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
|
||||
}
|
||||
catch(ex)
|
||||
catch (ex)
|
||||
{
|
||||
aIsFeed = false;
|
||||
}
|
||||
@@ -545,7 +545,7 @@ vimperator.Buffer = function () //{{{
|
||||
function createTable(data)
|
||||
{
|
||||
var ret = "<table><tr><th class='hl-Title' style='font-weight: bold;' align='left' colspan='2'>" +
|
||||
data[data.length -1][0] + "</th></tr>";
|
||||
data[data.length - 1][0] + "</th></tr>";
|
||||
|
||||
if (data.length - 1)
|
||||
{
|
||||
|
||||
@@ -1934,7 +1934,7 @@ vimperator.Commands = function () //{{{
|
||||
else if (/^\d+$/.test(args))
|
||||
{
|
||||
var index = parseInt(args, 10) - 1;
|
||||
if (index < vimperator.tabs.count())
|
||||
if (index < vimperator.tabs.count)
|
||||
vimperator.tabs.select(index, true);
|
||||
else
|
||||
vimperator.beep();
|
||||
|
||||
@@ -220,7 +220,7 @@ vimperator.help = function (section, easter) //{{{
|
||||
{
|
||||
doc.open();
|
||||
}
|
||||
catch(e)
|
||||
catch (e)
|
||||
{
|
||||
// FIXME: what's this all about then, eh? Works the same for if it's removed. -- djk
|
||||
// when the url is "about:" or any other xhtml page the doc is not open
|
||||
|
||||
@@ -552,7 +552,7 @@ vimperator.Mappings = function () //{{{
|
||||
addDefaultMap(new vimperator.Map([vimperator.modes.NORMAL], ["<C-^>", "<C-6>"],
|
||||
function ()
|
||||
{
|
||||
if (vimperator.tabs.getTab() == vimperator.tabs.alternate)
|
||||
if (vimperator.tabs.alternate == null || vimperator.tabs.getTab() == vimperator.tabs.alternate)
|
||||
{
|
||||
vimperator.echoerr("E23: No alternate page");
|
||||
return;
|
||||
|
||||
336
content/tabs.js
336
content/tabs.js
@@ -38,13 +38,12 @@ vimperator.Tabs = function () //{{{
|
||||
////////////////////// PRIVATE SECTION /////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////{{{
|
||||
|
||||
/** @param spec can either be:
|
||||
* - an absolute integer
|
||||
* - "" for the current tab
|
||||
* - "+1" for the next tab
|
||||
* - "-3" for the tab, which is 3 positions left of the current
|
||||
* - "$" for the last tab
|
||||
*/
|
||||
// @param spec can either be:
|
||||
// - an absolute integer
|
||||
// - "" for the current tab
|
||||
// - "+1" for the next tab
|
||||
// - "-3" for the tab, which is 3 positions left of the current
|
||||
// - "$" for the last tab
|
||||
function indexFromSpec(spec, wrap)
|
||||
{
|
||||
var position = getBrowser().tabContainer.selectedIndex;
|
||||
@@ -80,193 +79,190 @@ vimperator.Tabs = function () //{{{
|
||||
return position;
|
||||
}
|
||||
|
||||
var alternates = [null, null];
|
||||
var alternates = [getBrowser().mCurrentTab, null];
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////}}}
|
||||
////////////////////// PUBLIC SECTION //////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////{{{
|
||||
|
||||
// @returns the index of the currently selected tab starting with 0
|
||||
this.index = function (tab)
|
||||
{
|
||||
if (tab)
|
||||
return {
|
||||
|
||||
get alternate() { return alternates[1]; },
|
||||
|
||||
get count() { return getBrowser().mTabs.length; },
|
||||
|
||||
// @returns the index of the currently selected tab starting with 0
|
||||
index: function (tab)
|
||||
{
|
||||
var length = getBrowser().mTabs.length;
|
||||
for (var i = 0; i < length; i++)
|
||||
if (tab)
|
||||
{
|
||||
if (getBrowser().mTabs[i] == tab)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
return getBrowser().tabContainer.selectedIndex;
|
||||
};
|
||||
|
||||
this.count = function ()
|
||||
{
|
||||
return getBrowser().mTabs.length;
|
||||
};
|
||||
|
||||
// TODO: implement filter
|
||||
// @returns an array of tabs which match filter
|
||||
this.get = function (filter)
|
||||
{
|
||||
var buffers = [];
|
||||
var browsers = getBrowser().browsers;
|
||||
for (var i in browsers)
|
||||
{
|
||||
var title = browsers[i].contentTitle || "(Untitled)";
|
||||
var uri = browsers[i].currentURI.spec;
|
||||
var number = i + 1;
|
||||
buffers.push([number, title, uri]);
|
||||
}
|
||||
return buffers;
|
||||
};
|
||||
|
||||
this.getTab = function (index)
|
||||
{
|
||||
if (index)
|
||||
return getBrowser().mTabs[index];
|
||||
|
||||
return getBrowser().tabContainer.selectedItem;
|
||||
};
|
||||
|
||||
/* spec == "" moves the tab to the last position as per Vim
|
||||
* wrap causes the movement to wrap around the start and end of the tab list
|
||||
* NOTE: position is a 0 based index
|
||||
* FIXME: tabmove! N should probably produce an error
|
||||
*/
|
||||
this.move = function (tab, spec, wrap)
|
||||
{
|
||||
if (spec === "")
|
||||
spec = "$"; // if not specified, move to the last tab -> XXX: move to ex handling?
|
||||
|
||||
var index = indexFromSpec(spec, wrap);
|
||||
getBrowser().moveTabTo(tab, index);
|
||||
};
|
||||
|
||||
/* quit_on_last_tab = 1: quit without saving session
|
||||
* quit_on_last_tab = 2: quit and save session
|
||||
*/
|
||||
this.remove = function (tab, count, focus_left_tab, quit_on_last_tab)
|
||||
{
|
||||
function removeOrBlankTab (tab)
|
||||
{
|
||||
if (getBrowser().mTabs.length > 1)
|
||||
getBrowser().removeTab(tab);
|
||||
else
|
||||
{
|
||||
if (vimperator.buffer.URL != "about:blank" ||
|
||||
getWebNavigation().sessionHistory.count > 0)
|
||||
var length = getBrowser().mTabs.length;
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
vimperator.open("about:blank", vimperator.NEW_BACKGROUND_TAB);
|
||||
if (getBrowser().mTabs[i] == tab)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
return getBrowser().tabContainer.selectedIndex;
|
||||
},
|
||||
|
||||
// TODO: implement filter
|
||||
// @returns an array of tabs which match filter
|
||||
get: function (filter)
|
||||
{
|
||||
var buffers = [];
|
||||
var browsers = getBrowser().browsers;
|
||||
for (var i in browsers)
|
||||
{
|
||||
var title = browsers[i].contentTitle || "(Untitled)";
|
||||
var uri = browsers[i].currentURI.spec;
|
||||
var number = i + 1;
|
||||
buffers.push([number, title, uri]);
|
||||
}
|
||||
return buffers;
|
||||
},
|
||||
|
||||
getTab: function (index)
|
||||
{
|
||||
if (index)
|
||||
return getBrowser().mTabs[index];
|
||||
|
||||
return getBrowser().tabContainer.selectedItem;
|
||||
},
|
||||
|
||||
// spec == "" moves the tab to the last position as per Vim
|
||||
// wrap causes the movement to wrap around the start and end of the tab list
|
||||
// NOTE: position is a 0 based index
|
||||
// FIXME: tabmove! N should probably produce an error
|
||||
move: function (tab, spec, wrap)
|
||||
{
|
||||
if (spec === "")
|
||||
spec = "$"; // if not specified, move to the last tab -> XXX: move to ex handling?
|
||||
|
||||
var index = indexFromSpec(spec, wrap);
|
||||
getBrowser().moveTabTo(tab, index);
|
||||
},
|
||||
|
||||
// quit_on_last_tab = 1: quit without saving session
|
||||
// quit_on_last_tab = 2: quit and save session
|
||||
remove: function (tab, count, focus_left_tab, quit_on_last_tab)
|
||||
{
|
||||
function removeOrBlankTab (tab)
|
||||
{
|
||||
if (getBrowser().mTabs.length > 1)
|
||||
getBrowser().removeTab(tab);
|
||||
}
|
||||
else
|
||||
vimperator.beep();
|
||||
{
|
||||
if (vimperator.buffer.URL != "about:blank" ||
|
||||
getWebNavigation().sessionHistory.count > 0)
|
||||
{
|
||||
vimperator.open("about:blank", vimperator.NEW_BACKGROUND_TAB);
|
||||
getBrowser().removeTab(tab);
|
||||
}
|
||||
else
|
||||
vimperator.beep();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count < 1)
|
||||
count = 1;
|
||||
if (count < 1)
|
||||
count = 1;
|
||||
|
||||
if (quit_on_last_tab >= 1 && getBrowser().mTabs.length <= count)
|
||||
{
|
||||
if (vimperator.windows.length > 1)
|
||||
window.close();
|
||||
if (quit_on_last_tab >= 1 && getBrowser().mTabs.length <= count)
|
||||
{
|
||||
if (vimperator.windows.length > 1)
|
||||
window.close();
|
||||
else
|
||||
vimperator.quit(quit_on_last_tab == 2);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var index = this.index(tab);
|
||||
if (focus_left_tab)
|
||||
{
|
||||
var last_removed_tab = 0;
|
||||
for (var i = index; i > index - count && i >= 0; i--)
|
||||
{
|
||||
removeOrBlankTab(this.getTab(i));
|
||||
last_removed_tab = i > 0 ? i : 1;
|
||||
}
|
||||
getBrowser().mTabContainer.selectedIndex = last_removed_tab - 1;
|
||||
}
|
||||
else
|
||||
vimperator.quit(quit_on_last_tab == 2);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var index = this.index(tab);
|
||||
if (focus_left_tab)
|
||||
{
|
||||
var last_removed_tab = 0;
|
||||
for (var i = index; i > index - count && i >= 0; i--)
|
||||
{
|
||||
removeOrBlankTab(this.getTab(i));
|
||||
last_removed_tab = i > 0 ? i : 1;
|
||||
var i = index + count - 1;
|
||||
if (i >= this.count)
|
||||
i = this.count - 1;
|
||||
|
||||
for (; i >= index; i--)
|
||||
removeOrBlankTab(this.getTab(i));
|
||||
}
|
||||
getBrowser().mTabContainer.selectedIndex = last_removed_tab - 1;
|
||||
}
|
||||
else
|
||||
},
|
||||
|
||||
keepOnly: function (tab)
|
||||
{
|
||||
var i = index + count - 1;
|
||||
if (i >= this.count())
|
||||
i = this.count() - 1;
|
||||
getBrowser().removeAllTabsBut(tab);
|
||||
},
|
||||
|
||||
for (; i >= index; i--)
|
||||
removeOrBlankTab(this.getTab(i));
|
||||
}
|
||||
};
|
||||
|
||||
this.keepOnly = function (tab)
|
||||
{
|
||||
getBrowser().removeAllTabsBut(tab);
|
||||
};
|
||||
|
||||
this.select = function (spec, wrap)
|
||||
{
|
||||
var index = indexFromSpec(spec, wrap);
|
||||
if (index === false)
|
||||
select: function (spec, wrap)
|
||||
{
|
||||
vimperator.beep(); // XXX: move to ex-handling?
|
||||
return false;
|
||||
}
|
||||
getBrowser().mTabContainer.selectedIndex = index;
|
||||
};
|
||||
|
||||
// TODO: when restarting a session FF selects the first tab and then the
|
||||
// tab that was selected when the session was created. As a result the
|
||||
// alternate after a restart is often incorrectly tab 1 when there
|
||||
// shouldn't be one yet.
|
||||
this.updateSelectionHistory = function ()
|
||||
{
|
||||
alternates = [this.getTab(), alternates[0]];
|
||||
this.alternate = alternates[1];
|
||||
};
|
||||
|
||||
// TODO: move to v.buffers
|
||||
this.alternate = this.getTab();
|
||||
|
||||
this.reload = function (tab, bypass_cache)
|
||||
{
|
||||
if (bypass_cache)
|
||||
{
|
||||
const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
|
||||
const flags = nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE;
|
||||
getBrowser().getBrowserForTab(tab).reloadWithFlags(flags);
|
||||
}
|
||||
else
|
||||
{
|
||||
getBrowser().reloadTab(tab);
|
||||
}
|
||||
};
|
||||
|
||||
this.reloadAll = function (bypass_cache)
|
||||
{
|
||||
if (bypass_cache)
|
||||
{
|
||||
for (var i = 0; i < getBrowser().mTabs.length; i++)
|
||||
var index = indexFromSpec(spec, wrap);
|
||||
if (index === false)
|
||||
{
|
||||
try
|
||||
vimperator.beep(); // XXX: move to ex-handling?
|
||||
return false;
|
||||
}
|
||||
getBrowser().mTabContainer.selectedIndex = index;
|
||||
},
|
||||
|
||||
// TODO: when restarting a session FF selects the first tab and then the
|
||||
// tab that was selected when the session was created. As a result the
|
||||
// alternate after a restart is often incorrectly tab 1 when there
|
||||
// shouldn't be one yet.
|
||||
updateSelectionHistory: function ()
|
||||
{
|
||||
alternates = [this.getTab(), alternates[0]];
|
||||
},
|
||||
|
||||
reload: function (tab, bypass_cache)
|
||||
{
|
||||
if (bypass_cache)
|
||||
{
|
||||
const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
|
||||
const flags = nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE;
|
||||
getBrowser().getBrowserForTab(tab).reloadWithFlags(flags);
|
||||
}
|
||||
else
|
||||
{
|
||||
getBrowser().reloadTab(tab);
|
||||
}
|
||||
},
|
||||
|
||||
reloadAll: function (bypass_cache)
|
||||
{
|
||||
if (bypass_cache)
|
||||
{
|
||||
for (var i = 0; i < getBrowser().mTabs.length; i++)
|
||||
{
|
||||
this.reload(getBrowser().mTabs[i], bypass_cache);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
// FIXME: can we do anything useful here without stopping the
|
||||
// other tabs from reloading?
|
||||
try
|
||||
{
|
||||
this.reload(getBrowser().mTabs[i], bypass_cache);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
// FIXME: can we do anything useful here without stopping the
|
||||
// other tabs from reloading?
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
getBrowser().reloadAllTabs();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
getBrowser().reloadAllTabs();
|
||||
}
|
||||
|
||||
};
|
||||
//}}}
|
||||
}; //}}}
|
||||
|
||||
@@ -1184,7 +1184,7 @@ vimperator.StatusLine = function () //{{{
|
||||
if (!cur_index || typeof cur_index != "number")
|
||||
cur_index = vimperator.tabs.index() + 1;
|
||||
if (!total_tabs || typeof cur_index != "number")
|
||||
total_tabs = vimperator.tabs.count();
|
||||
total_tabs = vimperator.tabs.count;
|
||||
|
||||
tabcount_widget.value = "[" + cur_index + "/" + total_tabs + "]";
|
||||
},
|
||||
|
||||
@@ -245,9 +245,12 @@ const vimperator = (function () //{{{
|
||||
|
||||
var string = "";
|
||||
var obj = "";
|
||||
try { // for window.JSON
|
||||
try
|
||||
{ // for window.JSON
|
||||
obj = object.toString();
|
||||
} catch (e) {
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
obj = "<Object>";
|
||||
}
|
||||
|
||||
@@ -604,7 +607,7 @@ const vimperator = (function () //{{{
|
||||
vimperator.log("Loading module editor...", 3);
|
||||
vimperator.editor = vimperator.Editor();
|
||||
vimperator.log("Loading module tabs...", 3);
|
||||
vimperator.tabs = new vimperator.Tabs();
|
||||
vimperator.tabs = vimperator.Tabs();
|
||||
vimperator.log("Loading module marks...", 3);
|
||||
vimperator.marks = vimperator.Marks();
|
||||
vimperator.log("Loading module quickmarks...", 3);
|
||||
|
||||
Reference in New Issue
Block a user