2010-07-06 19:32:42 -07:00
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
# 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
# http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
# for the specific language governing rights and limitations under the
|
|
|
|
# License.
|
|
|
|
#
|
2010-07-29 12:37:25 -07:00
|
|
|
# The Original Code is the Tab View
|
2010-07-06 19:32:42 -07:00
|
|
|
#
|
2010-07-29 12:37:25 -07:00
|
|
|
# The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
# Portions created by the Initial Developer are Copyright (C) 2010
|
2010-07-06 19:32:42 -07:00
|
|
|
# the Initial Developer. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
# Raymond Lee <raymond@appcoast.com>
|
2010-08-11 14:48:02 -07:00
|
|
|
# Ian Gilman <ian@iangilman.com>
|
2010-07-06 19:32:42 -07:00
|
|
|
#
|
|
|
|
# Alternatively, the contents of this file may be used under the terms of
|
|
|
|
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
# in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
# of those above. If you wish to allow use of your version of this file only
|
|
|
|
# under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
# use your version of this file under the terms of the MPL, indicate your
|
|
|
|
# decision by deleting the provisions above and replace them with the notice
|
|
|
|
# and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
# the provisions above, a recipient may use your version of this file under
|
|
|
|
# the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
#
|
|
|
|
# ***** END LICENSE BLOCK *****
|
|
|
|
|
2010-07-29 12:37:25 -07:00
|
|
|
let TabView = {
|
2010-08-10 14:23:53 -07:00
|
|
|
_deck: null,
|
2010-08-09 17:24:08 -07:00
|
|
|
_window: null,
|
|
|
|
_sessionstore: null,
|
|
|
|
_visibilityID: "tabview-visibility",
|
2010-11-12 10:37:34 -08:00
|
|
|
|
2010-08-11 12:17:57 -07:00
|
|
|
// ----------
|
|
|
|
get windowTitle() {
|
|
|
|
delete this.windowTitle;
|
|
|
|
let brandBundle = document.getElementById("bundle_brand");
|
|
|
|
let brandShortName = brandBundle.getString("brandShortName");
|
2010-08-13 18:51:32 -07:00
|
|
|
let title = gNavigatorBundle.getFormattedString("tabView2.title", [brandShortName]);
|
2010-08-11 12:17:57 -07:00
|
|
|
return this.windowTitle = title;
|
|
|
|
},
|
|
|
|
|
2010-08-09 17:24:08 -07:00
|
|
|
// ----------
|
2010-11-12 10:37:34 -08:00
|
|
|
init: function TabView_init() {
|
2010-08-09 17:24:08 -07:00
|
|
|
// ___ keys
|
|
|
|
this._setBrowserKeyHandlers();
|
|
|
|
|
|
|
|
// ___ visibility
|
|
|
|
this._sessionstore =
|
2010-08-11 12:17:57 -07:00
|
|
|
Cc["@mozilla.org/browser/sessionstore;1"].
|
|
|
|
getService(Ci.nsISessionStore);
|
2010-08-09 17:24:08 -07:00
|
|
|
|
|
|
|
let data = this._sessionstore.getWindowValue(window, this._visibilityID);
|
2010-11-12 10:37:34 -08:00
|
|
|
if (data && data == "true") {
|
2010-08-11 14:48:02 -07:00
|
|
|
this.show();
|
2010-11-12 10:37:34 -08:00
|
|
|
} else {
|
|
|
|
let self = this;
|
|
|
|
// if a tab is changed from hidden to unhidden and the iframe is not
|
|
|
|
// initialized, load the iframe and setup the tab.
|
|
|
|
this._tabShowEventListener = function (event) {
|
|
|
|
if (!self._window)
|
|
|
|
self._initFrame(function() {
|
|
|
|
self._window.UI.onTabSelect(gBrowser.selectedTab);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
gBrowser.tabContainer.addEventListener(
|
|
|
|
"TabShow", this._tabShowEventListener, true);
|
|
|
|
}
|
2010-08-09 17:24:08 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-08-10 14:23:53 -07:00
|
|
|
// Creates the frame and calls the callback once it's loaded.
|
|
|
|
// If the frame already exists, calls the callback immediately.
|
|
|
|
_initFrame: function TabView__initFrame(callback) {
|
|
|
|
if (this._window) {
|
|
|
|
if (typeof callback == "function")
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
// ___ find the deck
|
|
|
|
this._deck = document.getElementById("tab-view-deck");
|
2010-11-12 10:37:34 -08:00
|
|
|
|
2010-08-10 14:23:53 -07:00
|
|
|
// ___ create the frame
|
2010-08-11 12:17:57 -07:00
|
|
|
let iframe = document.createElement("iframe");
|
2010-08-10 14:23:53 -07:00
|
|
|
iframe.id = "tab-view";
|
|
|
|
iframe.setAttribute("transparent", "true");
|
|
|
|
iframe.flex = 1;
|
2010-11-12 10:37:34 -08:00
|
|
|
|
2010-08-10 14:23:53 -07:00
|
|
|
if (typeof callback == "function")
|
|
|
|
iframe.addEventListener("DOMContentLoaded", callback, false);
|
2010-11-12 10:37:34 -08:00
|
|
|
|
2010-08-10 14:23:53 -07:00
|
|
|
iframe.setAttribute("src", "chrome://browser/content/tabview.html");
|
|
|
|
this._deck.appendChild(iframe);
|
|
|
|
this._window = iframe.contentWindow;
|
|
|
|
|
|
|
|
// ___ visibility storage handler
|
|
|
|
let self = this;
|
|
|
|
function observer(subject, topic, data) {
|
2010-08-09 17:24:08 -07:00
|
|
|
if (topic == "quit-application-requested") {
|
2010-08-11 14:48:02 -07:00
|
|
|
let data = (self.isVisible() ? "true" : "false");
|
|
|
|
self._sessionstore.setWindowValue(window, self._visibilityID, data);
|
2010-08-09 17:24:08 -07:00
|
|
|
}
|
|
|
|
}
|
2010-08-10 14:23:53 -07:00
|
|
|
Services.obs.addObserver(observer, "quit-application-requested", false);
|
2010-11-12 10:37:34 -08:00
|
|
|
|
|
|
|
if (this._tabShowEventListener) {
|
|
|
|
gBrowser.tabContainer.removeEventListener(
|
|
|
|
"TabShow", this._tabShowEventListener, true);
|
|
|
|
}
|
2010-08-10 14:23:53 -07:00
|
|
|
}
|
2010-08-07 15:32:17 -07:00
|
|
|
},
|
2010-11-12 10:37:34 -08:00
|
|
|
|
2010-09-21 14:56:52 -07:00
|
|
|
// ----------
|
|
|
|
getContentWindow: function TabView_getContentWindow() {
|
|
|
|
return this._window;
|
|
|
|
},
|
2010-08-07 15:32:17 -07:00
|
|
|
|
2010-08-09 17:24:08 -07:00
|
|
|
// ----------
|
2010-08-06 07:17:01 -07:00
|
|
|
isVisible: function() {
|
2010-08-10 14:23:53 -07:00
|
|
|
return (this._deck ? this._deck.selectedIndex == 1 : false);
|
2010-08-06 07:17:01 -07:00
|
|
|
},
|
|
|
|
|
2010-08-09 17:24:08 -07:00
|
|
|
// ----------
|
2010-08-10 14:23:53 -07:00
|
|
|
show: function() {
|
|
|
|
if (this.isVisible())
|
|
|
|
return;
|
2010-08-09 17:24:08 -07:00
|
|
|
|
2010-08-10 14:23:53 -07:00
|
|
|
this._initFrame(function() {
|
|
|
|
let event = document.createEvent("Events");
|
|
|
|
event.initEvent("tabviewshow", false, false);
|
2010-08-09 17:24:08 -07:00
|
|
|
dispatchEvent(event);
|
2010-08-10 14:23:53 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
hide: function() {
|
|
|
|
if (!this.isVisible())
|
|
|
|
return;
|
|
|
|
|
|
|
|
let event = document.createEvent("Events");
|
|
|
|
event.initEvent("tabviewhide", false, false);
|
|
|
|
dispatchEvent(event);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
toggle: function() {
|
|
|
|
if (this.isVisible())
|
|
|
|
this.hide();
|
|
|
|
else
|
|
|
|
this.show();
|
2010-07-06 19:32:42 -07:00
|
|
|
},
|
2010-09-13 14:05:03 -07:00
|
|
|
|
|
|
|
getActiveGroupName: function Tabview_getActiveGroupName() {
|
|
|
|
// We get the active group this way, instead of querying
|
|
|
|
// GroupItems.getActiveGroupItem() because the tabSelect event
|
|
|
|
// will not have happened by the time the browser tries to
|
|
|
|
// update the title.
|
|
|
|
let activeTab = window.gBrowser.selectedTab;
|
|
|
|
if (activeTab.tabItem && activeTab.tabItem.parent){
|
|
|
|
let groupName = activeTab.tabItem.parent.getTitle();
|
|
|
|
if (groupName)
|
|
|
|
return groupName;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
2010-07-06 19:32:42 -07:00
|
|
|
|
2010-08-09 17:24:08 -07:00
|
|
|
// ----------
|
2010-08-06 07:17:01 -07:00
|
|
|
updateContextMenu: function(tab, popup) {
|
2010-08-19 13:23:18 -07:00
|
|
|
let separator = document.getElementById("context_tabViewNamedGroups");
|
2010-08-08 10:28:24 -07:00
|
|
|
let isEmpty = true;
|
|
|
|
|
2010-08-19 13:23:18 -07:00
|
|
|
while (popup.firstChild && popup.firstChild != separator)
|
|
|
|
popup.removeChild(popup.firstChild);
|
2010-08-06 07:17:01 -07:00
|
|
|
|
2010-08-08 10:28:24 -07:00
|
|
|
let self = this;
|
2010-08-10 14:23:53 -07:00
|
|
|
this._initFrame(function() {
|
|
|
|
let activeGroup = tab.tabItem.parent;
|
|
|
|
let groupItems = self._window.GroupItems.groupItems;
|
2010-08-19 13:23:18 -07:00
|
|
|
|
2010-09-10 07:40:27 -07:00
|
|
|
groupItems.forEach(function(groupItem) {
|
|
|
|
// if group has title, it's not hidden and there is no active group or
|
|
|
|
// the active group id doesn't match the group id, a group menu item
|
|
|
|
// would be added.
|
|
|
|
if (groupItem.getTitle().length > 0 && !groupItem.hidden &&
|
2010-08-10 14:23:53 -07:00
|
|
|
(!activeGroup || activeGroup.id != groupItem.id)) {
|
|
|
|
let menuItem = self._createGroupMenuItem(groupItem);
|
2010-08-19 13:23:18 -07:00
|
|
|
popup.insertBefore(menuItem, separator);
|
2010-08-10 14:23:53 -07:00
|
|
|
isEmpty = false;
|
|
|
|
}
|
|
|
|
});
|
2010-08-19 13:23:18 -07:00
|
|
|
separator.hidden = isEmpty;
|
2010-08-08 10:28:24 -07:00
|
|
|
});
|
2010-08-06 07:17:01 -07:00
|
|
|
},
|
|
|
|
|
2010-08-09 17:24:08 -07:00
|
|
|
// ----------
|
2010-08-08 10:03:11 -07:00
|
|
|
_createGroupMenuItem : function(groupItem) {
|
2010-08-06 07:17:01 -07:00
|
|
|
let menuItem = document.createElement("menuitem")
|
2010-08-08 10:03:11 -07:00
|
|
|
menuItem.setAttribute("label", groupItem.getTitle());
|
2010-08-06 12:52:14 -07:00
|
|
|
menuItem.setAttribute(
|
|
|
|
"oncommand",
|
2010-08-08 10:03:11 -07:00
|
|
|
"TabView.moveTabTo(TabContextMenu.contextTab,'" + groupItem.id + "')");
|
2010-08-06 07:17:01 -07:00
|
|
|
|
|
|
|
return menuItem;
|
|
|
|
},
|
|
|
|
|
2010-08-09 17:24:08 -07:00
|
|
|
// ----------
|
2010-08-08 10:03:11 -07:00
|
|
|
moveTabTo: function(tab, groupItemId) {
|
2010-08-09 17:24:08 -07:00
|
|
|
if (this._window)
|
|
|
|
this._window.GroupItems.moveTabToGroupItem(tab, groupItemId);
|
|
|
|
},
|
|
|
|
|
2010-11-23 23:55:18 -08:00
|
|
|
// ----------
|
|
|
|
enableSearch: function Tabview_enableSearch(event) {
|
|
|
|
if (this._window)
|
|
|
|
this._window.UI.enableSearch(event);
|
|
|
|
},
|
|
|
|
|
2010-08-09 17:24:08 -07:00
|
|
|
// ----------
|
2010-08-11 12:17:57 -07:00
|
|
|
// Adds new key commands to the browser, for invoking the Tab Candy UI
|
|
|
|
// and for switching between groups of tabs when outside of the Tab Candy UI.
|
2010-08-09 17:24:08 -07:00
|
|
|
_setBrowserKeyHandlers : function() {
|
2010-08-11 12:17:57 -07:00
|
|
|
let self = this;
|
2010-08-09 17:24:08 -07:00
|
|
|
|
|
|
|
window.addEventListener("keypress", function(event) {
|
|
|
|
if (self.isVisible())
|
|
|
|
return;
|
|
|
|
|
2010-08-11 12:17:57 -07:00
|
|
|
let charCode = event.charCode;
|
2010-08-09 17:24:08 -07:00
|
|
|
// Control (+ Shift) + `
|
|
|
|
if (event.ctrlKey && !event.metaKey && !event.altKey &&
|
|
|
|
(charCode == 96 || charCode == 126)) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
|
2010-08-10 14:23:53 -07:00
|
|
|
self._initFrame(function() {
|
2010-08-11 12:17:57 -07:00
|
|
|
let tabItem = self._window.GroupItems.getNextGroupItemTab(event.shiftKey);
|
2010-08-10 14:23:53 -07:00
|
|
|
if (tabItem)
|
|
|
|
window.gBrowser.selectedTab = tabItem.tab;
|
|
|
|
});
|
2010-08-09 17:24:08 -07:00
|
|
|
}
|
|
|
|
}, true);
|
2010-07-06 19:32:42 -07:00
|
|
|
}
|
|
|
|
};
|