treestyletab/modules/window.js

1718 lines
54 KiB
JavaScript
Raw Normal View History

/* ***** 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.
*
* The Original Code is the Tree Style Tab.
*
2012-10-13 14:31:06 -04:00
* The Initial Developer of the Original Code is YUKI "Piro" Hiroshi.
2012-01-13 11:15:12 -05:00
* Portions created by the Initial Developer are Copyright (C) 2012
* the Initial Developer. All Rights Reserved.
*
2012-10-13 14:31:06 -04:00
* Contributor(s): YUKI "Piro" Hiroshi <piro.outsider.reflex@gmail.com>
*
* 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 ******/
const EXPORTED_SYMBOLS = ['TreeStyleTabWindow'];
const Cc = Components.classes;
const Ci = Components.interfaces;
2011-05-25 23:11:08 -04:00
Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
XPCOMUtils.defineLazyGetter(this, 'prefs', function() {
Components.utils.import('resource://treestyletab-modules/lib/prefs.js');
return window['piro.sakura.ne.jp'].prefs;
});
XPCOMUtils.defineLazyModuleGetter(this, "Services", "resource://gre/modules/Services.jsm");
Components.utils.import('resource://treestyletab-modules/base.js');
2012-11-11 08:48:42 -05:00
XPCOMUtils.defineLazyModuleGetter(this, 'TreeStyleTabBrowser', 'resource://treestyletab-modules/browser.js');
XPCOMUtils.defineLazyModuleGetter(this, 'utils', 'resource://treestyletab-modules/utils.js', 'TreeStyleTabUtils');
XPCOMUtils.defineLazyModuleGetter(this, 'AutoHideWindow', 'resource://treestyletab-modules/autoHide.js');
XPCOMUtils.defineLazyModuleGetter(this, 'TreeStyleTabThemeManager', 'resource://treestyletab-modules/themeManager.js');
function TreeStyleTabWindow(aWindow)
{
this.window = aWindow;
this.document = aWindow.document;
this._restoringTabs = [];
this._shownPopups = [];
this.restoringCount = 0;
aWindow.addEventListener('DOMContentLoaded', this, true);
aWindow.addEventListener('load', this, false);
aWindow.TreeStyleTabService = this;
2011-05-25 23:11:08 -04:00
2012-11-11 08:48:42 -05:00
XPCOMUtils.defineLazyModuleGetter(aWindow, 'TreeStyleTabBrowser', 'resource://treestyletab-modules/browser.js');
}
TreeStyleTabWindow.prototype = {
2012-10-29 14:11:39 -04:00
base : TreeStyleTabBase,
__proto__ : TreeStyleTabBase,
window : null,
document : null,
get MutationObserver()
{
return this.window.MutationObserver || this.window.MozMutationObserver;
},
/* API */
changeTabbarPosition : function TSTWindow_changeTabbarPosition(aNewPosition) /* PUBLIC API (obsolete, for backward compatibility) */
{
this.position = aNewPosition;
},
get position() /* PUBLIC API */
{
2011-02-04 05:50:21 -05:00
return this.preInitialized && this.browser.treeStyleTab ?
this.browser.treeStyleTab.position :
2012-10-29 14:11:39 -04:00
this.base.position ;
},
set position(aValue)
{
if ('UndoTabService' in this.window && this.window.UndoTabService.isUndoable()) {
2012-10-29 14:11:39 -04:00
var current = this.base.position;
var self = this;
this.window.UndoTabService.doOperation(
function() {
2012-10-29 14:11:39 -04:00
self.base.position = aValue;
},
{
label : self.treeBundle.getString('undo_changeTabbarPosition_label'),
name : 'treestyletab-changeTabbarPosition',
data : {
oldPosition : current,
newPosition : aValue
}
}
);
}
else {
2012-10-29 14:11:39 -04:00
this.base.position = aValue;
}
return aValue;
},
undoChangeTabbarPosition : function TSTWindow_undoChangeTabbarPosition() /* PUBLIC API */
{
2012-10-29 14:11:39 -04:00
return this.base.undoChangeTabbarPosition();
},
redoChangeTabbarPosition : function TSTWindow_redoChangeTabbarPosition() /* PUBLIC API */
{
2012-10-29 14:11:39 -04:00
return this.base.redoChangeTabbarPosition();
},
get treeViewEnabled() /* PUBLIC API */
{
2012-10-29 14:11:39 -04:00
return this.base.treeViewEnabled;
},
set treeViewEnabled(aValue)
{
2012-10-29 14:11:39 -04:00
return this.base.treeViewEnabled = aValue;
},
get useTMPSessionAPI() /* PUBLIC API */
{
2012-10-29 14:11:39 -04:00
return this.base.useTMPSessionAPI;
},
set useTMPSessionAPI(aValue)
{
2012-10-29 14:11:39 -04:00
return this.base.useTMPSessionAPI = aValue;
},
get browser()
{
2011-05-25 17:23:02 -04:00
var w = this.window;
2012-01-23 20:15:25 -05:00
this.assertBeforeDestruction(w);
2011-05-25 17:23:02 -04:00
return 'SplitBrowser' in w ? w.SplitBrowser.activeBrowser :
w.gBrowser ;
},
get browserBottomBox()
{
return this.document.getElementById('browser-bottombox');
},
get isPopupWindow()
{
return this.document && this.document.documentElement.getAttribute('chromehidden') != '';
},
/* backward compatibility */
getTempTreeStyleTab : function TSTWindow_getTempTreeStyleTab(aTabBrowser)
{
2012-11-11 08:48:42 -05:00
return aTabBrowser.treeStyleTab || new TreeStyleTabBrowser(this, aTabBrowser);
},
initTabAttributes : function TSTWindow_initTabAttributes(aTab, aTabBrowser)
{
var b = aTabBrowser || this.getTabBrowserFromChild(aTab);
this.getTempTreeStyleTab(b).initTabAttributes(aTab);
},
initTabContents : function TSTWindow_initTabContents(aTab, aTabBrowser)
{
var b = aTabBrowser || this.getTabBrowserFromChild(aTab);
this.getTempTreeStyleTab(b).initTabContents(aTab);
},
initTabContentsOrder : function TSTWindow_initTabContentsOrder(aTab, aTabBrowser)
{
var b = aTabBrowser || this.getTabBrowserFromChild(aTab);
this.getTempTreeStyleTab(b).initTabContentsOrder(aTab);
},
/* Utilities */
stopRendering : function TSTWindow_stopRendering()
{
this.window['piro.sakura.ne.jp'].stopRendering.stop();
},
startRendering : function TSTWindow_startRendering()
{
this.window['piro.sakura.ne.jp'].stopRendering.start();
},
getPropertyPixelValue : function TSTWindow_getPropertyPixelValue(aElementOrStyle, aProp)
{
var style = aElementOrStyle instanceof Ci.nsIDOMCSSStyleDeclaration ?
aElementOrStyle :
this.window.getComputedStyle(aElementOrStyle, null) ;
return Number(style.getPropertyValue(aProp).replace(/px$/, ''));
},
get isToolbarCustomizing()
{
2011-05-25 17:23:02 -04:00
var w = this.window;
var toolbox = w.gToolbox || w.gNavToolbox;
return toolbox && toolbox.customizing;
},
get maximized()
{
var sizemode = this.document.documentElement.getAttribute('sizemode');
return (
this.window.fullScreen ||
this.window.windowState == this.window.STATE_MAXIMIZED ||
sizemode == 'maximized' ||
sizemode == 'fullscreen'
);
},
maxTabbarWidth : function TSTWindow_maxTabbarWidth(aWidth, aTabBrowser)
{
aTabBrowser = aTabBrowser || this.browser;
var safePadding = 20; // for window border, etc.
2011-12-14 01:28:47 -05:00
var windowWidth = this.maximized ? this.window.screen.availWidth - safePadding : this.window.outerWidth ;
var rootWidth = parseInt(this.document.documentElement.getAttribute('width') || 0);
var max = Math.max(windowWidth, rootWidth);
return Math.max(0, Math.min(aWidth, max * this.MAX_TABBAR_SIZE_RATIO));
},
maxTabbarHeight : function TSTWindow_maxTabbarHeight(aHeight, aTabBrowser)
{
aTabBrowser = aTabBrowser || this.browser;
var safePadding = 20; // for window border, etc.
2011-12-14 01:28:47 -05:00
var windowHeight = this.maximized ? this.window.screen.availHeight - safePadding : this.window.outerHeight ;
var rootHeight = parseInt(this.document.documentElement.getAttribute('height') || 0);
var max = Math.max(windowHeight, rootHeight);
return Math.max(0, Math.min(aHeight, max * this.MAX_TABBAR_SIZE_RATIO));
},
shouldOpenSearchResultAsChild : function TSTWindow_shouldOpenSearchResultAsChild(aTerm)
{
aTerm = aTerm.replace(/^\s+|\s+$/g, '');
var mode = utils.getTreePref('autoAttach.searchResult');
if (mode == this.kSEARCH_RESULT_ATTACH_ALWAYS) {
return true;
}
else if (!aTerm || mode == this.kSEARCH_RESULT_DO_NOT_ATTACH) {
return false;
}
var w = this.document.commandDispatcher.focusedWindow;
if (!w || w.top != this.browser.contentWindow)
w = this.browser.contentWindow;
return (function(aWindow) {
if (!aWindow || !(aWindow instanceof Ci.nsIDOMWindow))
return false;
var selection = aWindow.getSelection();
if (selection && selection.toString().replace(/^\s+|\s+$/g, '') == aTerm)
return true;
return aWindow.frames ? Array.slice(aWindow.frames).some(arguments.callee) : false ;
})(w);
},
kSEARCH_RESULT_DO_NOT_ATTACH : 0,
kSEARCH_RESULT_ATTACH_IF_SELECTED : 1,
kSEARCH_RESULT_ATTACH_ALWAYS : 2,
2011-05-25 11:52:28 -04:00
get isAutoHide()
{
return this.window.fullScreen ?
2011-05-25 11:52:28 -04:00
(
this.getPref('browser.fullscreen.autohide') &&
utils.getTreePref('tabbar.autoHide.mode.fullscreen')
2011-05-25 11:52:28 -04:00
) :
utils.getTreePref('tabbar.autoHide.mode');
2011-05-25 11:52:28 -04:00
},
2011-05-26 10:22:42 -04:00
get autoHideWindow()
{
if (!this._autoHideWindow) {
2012-11-11 08:48:42 -05:00
this._autoHideWindow = new AutoHideWindow(this.window);
2011-05-26 10:22:42 -04:00
}
return this._autoHideWindow;
},
get themeManager()
{
if (!this._themeManager) {
2012-11-11 08:48:42 -05:00
this._themeManager = new TreeStyleTabThemeManager(this.window);
2011-05-26 10:22:42 -04:00
}
return this._themeManager;
},
/* Initializing */
preInit : function TSTWindow_preInit()
{
if (this.preInitialized) return;
this.preInitialized = true;
2011-05-25 17:23:02 -04:00
var w = this.window;
w.removeEventListener('DOMContentLoaded', this, true);
if (w.location.href.indexOf('chrome://browser/content/browser.xul') != 0)
return;
2011-05-25 17:23:02 -04:00
w.addEventListener('SSTabRestoring', this, true);
2011-05-25 17:23:02 -04:00
w.TreeStyleTabWindowHelper.preInit();
2011-05-26 10:22:42 -04:00
// initialize theme
this.onPrefChange('extensions.treestyletab.tabbar.style');
},
preInitialized : false,
init : function TSTWindow_init()
{
2011-05-25 17:23:02 -04:00
var w = this.window;
w.removeEventListener('load', this, false);
2011-06-13 13:04:37 -04:00
w.addEventListener('unload', this, false);
2011-05-26 10:22:42 -04:00
if (
w.location.href.indexOf('chrome://browser/content/browser.xul') != 0 ||
!this.browser
)
return;
if (this.initialized) return;
if (!this.preInitialized) {
this.preInit();
}
2011-05-25 17:23:02 -04:00
w.removeEventListener('SSTabRestoring', this, true);
2011-05-25 17:23:02 -04:00
var d = this.document;
d.addEventListener('popupshowing', this, false);
d.addEventListener('popuphiding', this, true);
d.addEventListener(this.kEVENT_TYPE_TAB_COLLAPSED_STATE_CHANGED, this, false);
d.addEventListener(this.kEVENT_TYPE_TABBAR_POSITION_CHANGED, this, false);
d.addEventListener(this.kEVENT_TYPE_TABBAR_STATE_CHANGED, this, false);
d.addEventListener(this.kEVENT_TYPE_FOCUS_NEXT_TAB, this, false);
if (this.MutationObserver)
this.initBrowserBottomBoxObserver();
2011-05-25 17:23:02 -04:00
var appcontent = d.getElementById('appcontent');
appcontent.addEventListener('SubBrowserAdded', this, false);
appcontent.addEventListener('SubBrowserRemoveRequest', this, false);
2011-05-25 17:23:02 -04:00
w.addEventListener('UIOperationHistoryUndo:TabbarOperations', this, false);
w.addEventListener('UIOperationHistoryRedo:TabbarOperations', this, false);
this.addPrefListener(this);
this.initUninstallationListener();
2011-05-25 21:47:28 -04:00
w.TreeStyleTabWindowHelper.onBeforeBrowserInit();
this.initTabBrowser(this.browser);
2011-05-25 21:47:28 -04:00
w.TreeStyleTabWindowHelper.onAfterBrowserInit();
this.processRestoredTabs();
this.updateTabsOnTop();
2011-05-25 11:52:28 -04:00
// Init autohide service only if it have to be activated.
if (this.isAutoHide)
this.onPrefChange('extensions.treestyletab.tabbar.autoHide.mode');
this.onPrefChange('extensions.treestyletab.autoCollapseExpandSubtreeOnSelect.whileFocusMovingByShortcut');
this.initialized = true;
},
initialized : false,
initUninstallationListener : function TSTWindow_initUninstallationListener()
{
var restorePrefs = function() {
if (prefs.getPref('extensions.treestyletab.tabsOnTop.originalState')) {
prefs.clearPref('extensions.treestyletab.tabsOnTop.originalState');
try {
2013-01-02 19:07:55 -05:00
this.browser.treeStyleTab.position = 'top';
}
catch(e) {
}
2013-01-02 19:07:55 -05:00
this.window.TabsOnTop.enabled = true;
}
2012-02-04 16:31:03 -05:00
let restorePrefs = [
'browser.tabs.loadFolderAndReplace',
'browser.tabs.insertRelatedAfterCurrent',
'extensions.stm.tabBarMultiRows' // Super Tab Mode
2012-02-04 16:31:03 -05:00
];
2013-01-01 14:35:42 -05:00
for (let i = 0, maxi = restorePrefs.length; i < maxi; i++)
{
2012-02-04 16:31:03 -05:00
let pref = restorePrefs[i];
let backup = prefs.getPref(pref+'.backup');
if (backup === null) continue;
// we have to set to ".override" pref, to avoid unexpectedly reset by the preference listener.
prefs.setPref(pref+'.override', backup);
// restore user preference.
prefs.setPref(pref, backup);
// clear backup pref.
prefs.clearPref(pref+'.backup');
}
2013-01-02 19:07:55 -05:00
}.bind(this);
new this.window['piro.sakura.ne.jp'].UninstallationListener({
id : 'treestyletab@piro.sakura.ne.jp',
onuninstalled : restorePrefs,
ondisabled : restorePrefs
});
},
initTabBrowser : function TSTWindow_initTabBrowser(aTabBrowser)
{
if (aTabBrowser.localName != 'tabbrowser') return;
2012-11-11 08:48:42 -05:00
(new TreeStyleTabBrowser(this, aTabBrowser)).init();
},
updateAllTabsButton : function TSTWindow_updateAllTabsButton(aTabBrowser)
{
2011-05-25 17:23:02 -04:00
var d = this.document;
aTabBrowser = aTabBrowser || this.browser;
2012-01-13 03:38:12 -05:00
var allTabsButton = d.getElementById('alltabs-button') ||
( // Tab Mix Plus
utils.getTreePref('compatibility.TMP') &&
2011-05-25 17:23:02 -04:00
d.getAnonymousElementByAttribute(aTabBrowser.mTabContainer, 'anonid', 'alltabs-button')
);
if (allTabsButton && allTabsButton.hasChildNodes() && aTabBrowser.treeStyleTab)
allTabsButton.firstChild.setAttribute('position', aTabBrowser.treeStyleTab.isVertical ? 'before_start' : 'after_end' );
},
updateAllTabsPopup : function TSTWindow_updateAllTabsPopup(aEvent)
{
if (!utils.getTreePref('enableSubtreeIndent.allTabsPopup')) return;
var items = Array.slice(aEvent.originalTarget.childNodes);
var firstItemIndex = 0;
// ignore menu items inserted by Weave (Firefox Sync), Tab Utilities, and others.
2012-02-04 16:31:03 -05:00
for (let i = 0, maxi = items.length; i < maxi; i++)
{
2012-02-04 16:31:03 -05:00
let item = items[i];
if (
item.getAttribute('anonid') ||
item.id ||
item.hidden ||
item.localName != 'menuitem'
)
firstItemIndex = i + 1;
}
items = items.slice(firstItemIndex);
var b = this.getTabBrowserFromChild(aEvent.originalTarget) || this.browser;
2012-09-23 02:43:49 -04:00
var tabs = this.getTabs(b);
2012-02-04 16:31:03 -05:00
for (let i = 0, maxi = tabs.length; i < maxi; i++)
{
2012-02-04 16:31:03 -05:00
items[i].style.marginLeft = tabs[i].getAttribute(this.kNEST)+'em';
}
},
initBrowserBottomBoxObserver : function TSTWindow_initBrowserBottomBoxObserver()
{
var self = this;
this.browserBottomBoxObserver = new this.MutationObserver(function(aMutations, aObserver) {
self.handleMutations(aMutations, aObserver);
});
this.browserBottomBoxObserver.observe(this.browserBottomBox, { childList : true });
this.initBrowserBottomBoxChildListeners();
},
initBrowserBottomBoxChildListeners : function TSTWindow_initBrowserBottomBoxChildListeners()
{
Array.forEach(this.browserBottomBox.childNodes, function(aChild) {
var observer = aChild.__treestyletab__attributeObserver;
if (!observer) {
var self = this;
observer = new this.MutationObserver(function(aMutations, aObserver) {
self.handleMutations(aMutations, aObserver);
});
observer.observe(aChild, { attributes : true });
aChild.__treestyletab__attributeObserver = observer;
}
}, this)
},
destroy : function TSTWindow_destroy()
{
2011-05-27 17:08:45 -04:00
var w = this.window;
if (this.browser) {
2012-10-29 14:11:39 -04:00
this.base.inWindowDestoructionProcess = true;
try {
2011-05-25 17:23:02 -04:00
w.removeEventListener('unload', this, false);
2011-05-26 10:22:42 -04:00
this.autoHideWindow.destroy();
delete this._autoHideWindow;
this.themeManager.destroy();
delete this._themeManager;
this.browser.treeStyleTab.saveCurrentState();
this.destroyTabBrowser(this.browser);
this.endListenKeyEventsFor(this.LISTEN_FOR_AUTOHIDE);
this.endListenKeyEventsFor(this.LISTEN_FOR_AUTOEXPAND_BY_FOCUSCHANGE);
2011-05-25 17:23:02 -04:00
let d = this.document;
d.removeEventListener('popupshowing', this, false);
d.removeEventListener('popuphiding', this, true);
d.removeEventListener(this.kEVENT_TYPE_TAB_COLLAPSED_STATE_CHANGED, this, false);
d.removeEventListener(this.kEVENT_TYPE_TABBAR_POSITION_CHANGED, this, false);
d.removeEventListener(this.kEVENT_TYPE_TABBAR_STATE_CHANGED, this, false);
d.removeEventListener(this.kEVENT_TYPE_FOCUS_NEXT_TAB, this, false);
2010-11-30 05:05:00 -05:00
if (this.browserBottomBoxObserver) {
this.destroyBrowserBottomBoxChildListeners();
this.browserBottomBoxObserver.disconnect();
delete this.browserBottomBoxObserver;
}
2012-02-04 16:31:03 -05:00
for (let i = 0, maxi = this._tabFocusAllowance.length; i < maxi; i++)
{
2012-02-04 16:31:03 -05:00
w.removeEventListener(this.kEVENT_TYPE_FOCUS_NEXT_TAB, this._tabFocusAllowance[i], false);
}
2011-05-25 17:23:02 -04:00
var appcontent = d.getElementById('appcontent');
appcontent.removeEventListener('SubBrowserAdded', this, false);
appcontent.removeEventListener('SubBrowserRemoveRequest', this, false);
2011-05-25 17:23:02 -04:00
w.removeEventListener('UIOperationHistoryUndo:TabbarOperations', this, false);
w.removeEventListener('UIOperationHistoryRedo:TabbarOperations', this, false);
this.removePrefListener(this);
}
catch(e) {
throw e;
}
finally {
2012-10-29 14:11:39 -04:00
this.base.inWindowDestoructionProcess = false;
}
}
2011-05-25 17:23:02 -04:00
delete w.TreeStyleTabService;
delete this.window;
delete this.document;
},
destroyTabBrowser : function TSTWindow_destroyTabBrowser(aTabBrowser)
{
if (aTabBrowser.localName != 'tabbrowser') return;
aTabBrowser.treeStyleTab.destroy();
delete aTabBrowser.treeStyleTab;
},
destroyBrowserBottomBoxChildListeners : function TSTWindow_destroyBrowserBottomBoxChildListeners()
{
Array.forEach(this.browserBottomBox.childNodes, function(aChild) {
var observer = aChild.__treestyletab__attributeObserver;
if (observer) {
observer.disconnect();
delete aChild.__treestyletab__attributeObserver;
}
}, this)
},
/* Event Handling */
handleEvent : function TSTWindow_handleEvent(aEvent)
{
switch (aEvent.type)
{
case 'DOMContentLoaded':
return this.preInit();
case 'load':
return this.init();
case 'unload':
return this.destroy();
case 'SSTabRestoring':
return this.onTabRestored(aEvent);
case 'popupshowing':
this.onPopupShown(aEvent.originalTarget);
if ((aEvent.originalTarget.getAttribute('anonid') || aEvent.originalTarget.id) == 'alltabs-popup')
this.updateAllTabsPopup(aEvent);
return;
case 'popuphiding':
return this.onPopupHidden(aEvent.originalTarget);
2010-11-30 05:05:00 -05:00
case this.kEVENT_TYPE_TAB_COLLAPSED_STATE_CHANGED:
return this.updateAeroPeekPreviews();
2010-11-30 05:05:00 -05:00
case this.kEVENT_TYPE_TABBAR_POSITION_CHANGED:
case this.kEVENT_TYPE_TABBAR_STATE_CHANGED:
return this.updateTabsOnTop();
2010-11-30 05:05:00 -05:00
case this.kEVENT_TYPE_FOCUS_NEXT_TAB:
return this.onFocusNextTab(aEvent);
case 'keydown':
return this.onKeyDown(aEvent);
case 'keyup':
case 'keypress':
return this.onKeyRelease(aEvent);
case 'mousedown':
return this.onTabbarResizeStart(aEvent);
case 'mouseup':
return this.onTabbarResizeEnd(aEvent);
case 'mousemove':
return this.onTabbarResizing(aEvent);
case 'dblclick':
return this.onTabbarReset(aEvent);
case 'click':
return this.handleNewTabActionOnButton(aEvent);
case 'SubBrowserAdded':
return this.initTabBrowser(aEvent.originalTarget.browser);
case 'SubBrowserRemoveRequest':
return this.destroyTabBrowser(aEvent.originalTarget.browser);
case 'UIOperationHistoryUndo:TabbarOperations':
switch (aEvent.entry.name)
{
case 'treestyletab-changeTabbarPosition':
this.position = aEvent.entry.data.oldPosition;
return;
case 'treestyletab-changeTabbarPosition-private':
aEvent.entry.data.target.treeStyleTab.position = aEvent.entry.data.oldPosition;
return;
}
return;
case 'UIOperationHistoryRedo:TabbarOperations':
switch (aEvent.entry.name)
{
case 'treestyletab-changeTabbarPosition':
this.position = aEvent.entry.data.newPosition;
return;
case 'treestyletab-changeTabbarPosition-private':
aEvent.entry.data.target.treeStyleTab.position = aEvent.entry.data.newPosition;
return;
}
return;
}
},
keyEventListening : false,
keyEventListeningFlags : 0,
LISTEN_FOR_AUTOHIDE : 1,
LISTEN_FOR_AUTOEXPAND_BY_FOCUSCHANGE : 2,
startListenKeyEventsFor : function TSTWindow_startListenKeyEventsFor(aReason)
{
if (this.keyEventListeningFlags & aReason) return;
if (!this.keyEventListening) {
2011-05-25 17:23:02 -04:00
let w = this.window;
w.addEventListener('keydown', this, true);
w.addEventListener('keyup', this, true);
w.addEventListener('keypress', this, true);
this.keyEventListening = true;
}
this.keyEventListeningFlags |= aReason;
},
endListenKeyEventsFor : function TSTWindow_endListenKeyEventsFor(aReason)
{
if (!(this.keyEventListeningFlags & aReason)) return;
this.keyEventListeningFlags ^= aReason;
if (!this.keyEventListeningFlags && this.keyEventListening) {
2011-05-25 17:23:02 -04:00
let w = this.window;
w.removeEventListener('keydown', this, true);
w.removeEventListener('keyup', this, true);
w.removeEventListener('keypress', this, true);
this.keyEventListening = false;
}
},
onKeyDown : function TSTWindow_onKeyDown(aEvent)
{
/**
* On Mac OS X, default accel key is the Command key (metaKey), but
* Cmd-Tab is used to switch applications by the OS itself. So Firefox
* uses Ctrl-Tab to switch tabs on all platforms.
*/
// this.accelKeyPressed = this.isAccelKeyPressed(aEvent);
this.accelKeyPressed = aEvent.ctrlKey || aEvent.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_CONTROL;
var left = aEvent.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_LEFT;
var right = aEvent.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_RIGHT;
var up = aEvent.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_UP;
var down = aEvent.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_DOWN;
if (
this.FocusManager &&
this.FocusManager.focusedElement == this.browser.selectedTab &&
(up || down || left || right)
)
this.arrowKeyEventOnTab = {
keyCode : aEvent.keyCode,
left : left,
right : right,
up : up,
down : down,
altKey : aEvent.altKey,
ctrlKey : aEvent.ctrlKey,
metaKey : aEvent.metaKey,
shiftKey : aEvent.shiftKey
};
var b = this.browser;
var data = {
sourceEvent : aEvent
};
/* PUBLIC API */
this.fireDataContainerEvent(this.kEVENT_TYPE_TAB_FOCUS_SWITCHING_KEY_DOWN, b, true, false, data);
// for backward compatibility
this.fireDataContainerEvent(this.kEVENT_TYPE_TAB_FOCUS_SWITCHING_KEY_DOWN.replace(/^nsDOM/, ''), b, true, false, data);
},
accelKeyPressed : false,
arrowKeyEventOnTab : null,
onKeyRelease : function TSTWindow_onKeyRelease(aEvent)
{
var b = this.browser;
if (!b || !b.treeStyleTab) return;
var sv = b.treeStyleTab;
var scrollDown,
scrollUp;
// this.accelKeyPressed = this.isAccelKeyPressed(aEvent);
this.accelKeyPressed = aEvent.ctrlKey || aEvent.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_CONTROL;
this.window.setTimeout(function(aSelf) {
aSelf.arrowKeyEventOnTab = null;
}, 10, this);
var standBy = scrollDown = scrollUp = (!aEvent.altKey && this.accelKeyPressed);
scrollDown = scrollDown && (
!aEvent.shiftKey &&
(
aEvent.keyCode == aEvent.DOM_VK_TAB ||
aEvent.keyCode == aEvent.DOM_VK_PAGE_DOWN
)
);
scrollUp = scrollUp && (
aEvent.shiftKey ? (aEvent.keyCode == aEvent.DOM_VK_TAB) : (aEvent.keyCode == aEvent.DOM_VK_PAGE_UP)
);
var onlyShiftKey = (!aEvent.shiftKey && aEvent.keyCode == 16 && (aEvent.type == 'keyup' || aEvent.charCode == 0));
var data = {
scrollDown : scrollDown,
scrollUp : scrollUp,
standBy : standBy,
onlyShiftKey : onlyShiftKey,
sourceEvent : aEvent
};
if (
scrollDown ||
scrollUp ||
( // when you release "shift" key
standBy && onlyShiftKey
)
) {
/* PUBLIC API */
this.fireDataContainerEvent(this.kEVENT_TYPE_TAB_FOCUS_SWITCHING_START, b, true, false, data);
2011-01-10 23:20:01 -05:00
// for backward compatibility
this.fireDataContainerEvent(this.kEVENT_TYPE_TAB_FOCUS_SWITCHING_START.replace(/^nsDOM/, ''), b, true, false, data);
return;
}
// when you just release accel key...
/* PUBLIC API */
let (event) {
this.fireDataContainerEvent(this.kEVENT_TYPE_TAB_FOCUS_SWITCHING_END, b, true, false, data);
2011-01-10 23:20:01 -05:00
// for backward compatibility
this.fireDataContainerEvent(this.kEVENT_TYPE_TAB_FOCUS_SWITCHING_END.replace(/^nsDOM/, ''), b, true, false, data);
}
if (this._tabShouldBeExpandedAfterKeyReleased) {
let tab = this._tabShouldBeExpandedAfterKeyReleased;
if (this.hasChildTabs(tab) &&
this.isSubtreeCollapsed(tab)) {
this.getTabBrowserFromChild(tab)
.treeStyleTab
.collapseExpandTreesIntelligentlyFor(tab);
}
}
this._tabShouldBeExpandedAfterKeyReleased = null;
},
get shouldListenKeyEventsForAutoExpandByFocusChange()
{
return !this.ctrlTabPreviewsEnabled &&
(
utils.getTreePref('autoExpandSubtreeOnSelect.whileFocusMovingByShortcut') ||
utils.getTreePref('autoCollapseExpandSubtreeOnSelect')
);
},
get ctrlTabPreviewsEnabled()
{
return 'allTabs' in this.window &&
this.getPref('browser.ctrlTab.previews');
},
onTabbarResizeStart : function TSTWindow_onTabbarResizeStart(aEvent)
{
if (aEvent.button != 0)
return;
if (!this.isEventFiredOnGrippy(aEvent))
aEvent.stopPropagation();
if ('setCapture' in aEvent.currentTarget)
aEvent.currentTarget.setCapture(true);
aEvent.currentTarget.addEventListener('mousemove', this, false);
var b = this.getTabBrowserFromChild(aEvent.currentTarget);
var box = aEvent.currentTarget.id == 'treestyletab-tabbar-resizer-splitter' ?
this.getTabStrip(b) :
b.treeStyleTab.tabStripPlaceHolder || b.tabContainer ;
this.tabbarResizeStartWidth = box.boxObject.width;
this.tabbarResizeStartHeight = box.boxObject.height;
this.tabbarResizeStartX = aEvent.screenX;
this.tabbarResizeStartY = aEvent.screenY;
},
onTabbarResizeEnd : function TSTWindow_onTabbarResizeEnd(aEvent)
{
if (this.tabbarResizeStartWidth < 0)
return;
var target = aEvent.currentTarget;
var b = this.getTabBrowserFromChild(target);
aEvent.stopPropagation();
if ('releaseCapture' in target)
target.releaseCapture();
target.removeEventListener('mousemove', this, false);
this.tabbarResizeStartWidth = -1;
this.tabbarResizeStartHeight = -1;
this.tabbarResizeStartX = -1;
this.tabbarResizeStartY = -1;
this.Deferred.next(function() {
b.treeStyleTab.fixTooNarrowTabbar();
2012-02-06 12:13:46 -05:00
}).error(this.defaultDeferredErrorHandler);
},
onTabbarResizing : function TSTWindow_onTabbarResizing(aEvent)
{
var target = aEvent.currentTarget;
var b = this.getTabBrowserFromChild(target);
var expanded = target.id == 'treestyletab-tabbar-resizer-splitter';
if (expanded)
aEvent.stopPropagation();
var width = this.tabbarResizeStartWidth;
var height = this.tabbarResizeStartHeight;
var pos = b.treeStyleTab.position;
if (b.treeStyleTab.isVertical) {
let delta = aEvent.screenX - this.tabbarResizeStartX;
width += (pos == 'left' ? delta : -delta );
width = this.maxTabbarWidth(width, b);
if (expanded || b.treeStyleTab.autoHide.expanded) {
this.setPrefForActiveWindow(function() {
utils.setTreePref('tabbar.width', width);
});
if (b.treeStyleTab.autoHide.mode == b.treeStyleTab.autoHide.kMODE_SHRINK &&
b.treeStyleTab.tabStripPlaceHolder)
b.treeStyleTab.tabStripPlaceHolder.setAttribute('width', utils.getTreePref('tabbar.shrunkenWidth'));
}
else {
this.setPrefForActiveWindow(function() {
utils.setTreePref('tabbar.shrunkenWidth', width);
});
}
}
else {
let delta = aEvent.screenY - this.tabbarResizeStartY;
height += (pos == 'top' ? delta : -delta );
this.setPrefForActiveWindow(function() {
utils.setTreePref('tabbar.height', this.maxTabbarHeight(height, b));
});
}
b.treeStyleTab.updateFloatingTabbar(this.kTABBAR_UPDATE_BY_TABBAR_RESIZE);
},
tabbarResizeStartWidth : -1,
tabbarResizeStartHeight : -1,
tabbarResizeStartX : -1,
tabbarResizeStartY : -1,
onTabbarReset : function TSTWindow_onTabbarReset(aEvent)
{
if (aEvent.button != 0)
return;
var b = this.getTabBrowserFromChild(aEvent.currentTarget);
if (b) {
b.treeStyleTab.resetTabbarSize();
aEvent.stopPropagation();
}
},
onFocusNextTab : function TSTWindow_onFocusNextTab(aEvent)
2010-11-30 05:05:00 -05:00
{
var tab = aEvent.originalTarget;
var b = this.getTabBrowserFromChild(tab);
2010-11-30 05:05:00 -05:00
if (
this.getPref('browser.tabs.selectOwnerOnClose') &&
tab.owner &&
(
!b._removingTabs ||
b._removingTabs.indexOf(tab.owner) < 0
2010-11-30 05:05:00 -05:00
)
)
aEvent.preventDefault();
},
showHideSubtreeMenuItem : function TSTWindow_showHideSubtreeMenuItem(aMenuItem, aTabs)
{
if (!aMenuItem ||
aMenuItem.getAttribute('hidden') == 'true' ||
!aTabs ||
!aTabs.length)
return;
var hasSubtree = false;
for (var i = 0, maxi = aTabs.length; i < maxi; i++)
{
if (!this.hasChildTabs(aTabs[i])) continue;
hasSubtree = true;
break;
}
if (hasSubtree)
aMenuItem.removeAttribute('hidden');
else
aMenuItem.setAttribute('hidden', true);
},
showHideSubTreeMenuItem : function() { return this.showHideSubtreeMenuItem.apply(this, arguments); }, // obsolete, for backward compatibility
updateAeroPeekPreviews : function TSTWindow_updateAeroPeekPreviews()
{
2011-05-25 17:23:02 -04:00
var w = this.window;
if (
this.updateAeroPeekPreviewsTimer ||
!this.getPref('browser.taskbar.previews.enable') ||
!utils.getTreePref('taskbarPreviews.hideCollapsedTabs') ||
2011-05-25 17:23:02 -04:00
!('Win7Features' in w) ||
!w.Win7Features ||
!this.AeroPeek ||
!this.AeroPeek.windows
)
return;
2011-05-25 17:23:02 -04:00
this.updateAeroPeekPreviewsTimer = w.setTimeout(function(aSelf) {
aSelf.updateAeroPeekPreviewsTimer = null;
try {
aSelf.updateAeroPeekPreviewsInternal();
}
catch(e) {
dump(e+'\n');
aSelf.updateAeroPeekPreviews();
}
}, 250, this);
},
updateAeroPeekPreviewsTimer : null,
updateAeroPeekPreviewsInternal : function TSTWindow_updateAeroPeekPreviewsInternal()
{
if (
!this.getPref('browser.taskbar.previews.enable') ||
!utils.getTreePref('taskbarPreviews.hideCollapsedTabs')
)
return;
this.AeroPeek.windows.some(function(aTabWindow) {
if (aTabWindow.win == this.window) {
2012-02-04 16:31:03 -05:00
let previews = aTabWindow.previews;
for (let i = 0, maxi = previews.length; i < maxi; i++)
{
2012-02-04 16:31:03 -05:00
let preview = previews[i];
if (!preview) continue;
let tab = preview.controller.wrappedJSObject.tab;
preview.visible = !this.isCollapsed(tab);
}
this.AeroPeek.checkPreviewCount();
return true;
}
return false;
}, this);
},
updateTabsOnTop : function TSTWindow_updateTabsOnTop()
{
2011-05-25 17:23:02 -04:00
var w = this.window;
if (
this.isPopupWindow ||
this.tabsOnTopChangingByUI ||
this.tabsOnTopChangingByTST ||
!('TabsOnTop' in w) ||
!('enabled' in w.TabsOnTop)
)
return;
this.tabsOnTopChangingByTST = true;
try {
var TabsOnTop = w.TabsOnTop;
var originalState = utils.getTreePref('tabsOnTop.originalState');
if (originalState === null) {
let current = this.getDefaultPref('browser.tabs.onTop') === null ?
TabsOnTop.enabled :
this.getPref('browser.tabs.onTop') ;
utils.setTreePref('tabsOnTop.originalState', originalState = current);
}
if (this.browser.treeStyleTab.position != 'top' ||
!this.browser.treeStyleTab.fixed) {
if (TabsOnTop.enabled)
TabsOnTop.enabled = false;
}
else {
if (TabsOnTop.enabled != originalState)
TabsOnTop.enabled = originalState;
utils.clearTreePref('tabsOnTop.originalState');
}
}
finally {
this.tabsOnTopChangingByTST = false;
}
},
onPopupShown : function TSTWindow_onPopupShown(aPopup)
{
if (!aPopup.boxObject ||
this.evaluateXPath(
2012-09-23 03:08:08 -04:00
'parent::*/ancestor-or-self::*[local-name()="tooltip" or local-name()="panel" or local-name()="popup" or local-name()="menupopup"]',
aPopup,
Ci.nsIDOMXPathResult.BOOLEAN_TYPE
).booleanValue)
return;
this.window.setTimeout(function(aSelf) {
if ((!aPopup.boxObject.width && !aPopup.boxObject.height) ||
aPopup.boxObject.popupState == 'closed')
return;
var id = aPopup.id;
var item = id && aSelf.document.getElementById(id) ? id : aPopup ;
var index = aSelf._shownPopups.indexOf(item);
if (index < 0)
aSelf._shownPopups.push(item);
}, 10, this);
},
onPopupHidden : function TSTWindow_onPopupHidden(aPopup)
{
var id = aPopup.id;
aPopup = id && this.document.getElementById(id) ? id : aPopup ;
var index = this._shownPopups.indexOf(aPopup);
if (index > -1)
this._shownPopups.splice(index, 1);
},
isPopupShown : function TSTWindow_isPopupShown()
{
this._shownPopups = this._shownPopups.filter(function(aItem) {
if (typeof aItem == 'string')
aItem = this.document.getElementById(aItem);
return (
aItem &&
aItem.getAttribute(this.kIGNORE_POPUP_STATE) != 'true' &&
aItem.boxObject &&
(aItem.boxObject.width || aItem.boxObject.height) &&
aItem.state != 'closed'
);
2011-06-05 12:05:22 -04:00
}, this);
return this._shownPopups.length > 0;
},
kNEWTAB_DO_NOTHING : -1,
kNEWTAB_OPEN_AS_ORPHAN : 0,
kNEWTAB_OPEN_AS_CHILD : 1,
kNEWTAB_OPEN_AS_SIBLING : 2,
kNEWTAB_OPEN_AS_NEXT_SIBLING : 3,
_handleNewTabCommand : function TSTWindow_handleNewTabCommand(aBaseTab, aBehavior)
{
switch (aBehavior)
{
case this.kNEWTAB_OPEN_AS_ORPHAN:
case this.kNEWTAB_DO_NOTHING:
default:
break;
case this.kNEWTAB_OPEN_AS_CHILD:
this.readyToOpenChildTabNow(aBaseTab);
break;
case this.kNEWTAB_OPEN_AS_SIBLING:
let (parentTab = this.getParentTab(aBaseTab)) {
if (parentTab)
this.readyToOpenChildTabNow(parentTab);
}
break;
case this.kNEWTAB_OPEN_AS_NEXT_SIBLING:
this.readyToOpenNextSiblingTabNow(aBaseTab);
break;
}
},
onBeforeNewTabCommand : function TSTWindow_onBeforeNewTabCommand(aTabBrowser)
{
var self = this.windowService || this;
if (self._clickEventOnNewTabButtonHandled)
return;
var b = aTabBrowser || this.browser;
this._handleNewTabCommand(b.selectedTab, utils.getTreePref('autoAttach.newTabCommand'));
},
handleNewTabActionOnButton : function TSTWindow_handleNewTabActionOnButton(aEvent)
{
// ignore non new-tab commands (middle click, Ctrl-click)
if (aEvent.button != 1 && (aEvent.button != 0 || !this.isAccelKeyPressed(aEvent)))
return;
var newTabButton = this.getNewTabButtonFromEvent(aEvent);
if (newTabButton) {
this._handleNewTabCommand(this.browser.selectedTab, utils.getTreePref('autoAttach.newTabButton'));
let self = this.windowService || this;
self._clickEventOnNewTabButtonHandled = true;
this.Deferred.next(function() {
self._clickEventOnNewTabButtonHandled = false;
});
}
else if (aEvent.target.id == 'urlbar-go-button' || aEvent.target.id == 'go-button') {
this._handleNewTabCommand(this.browser.selectedTab, utils.getTreePref('autoAttach.goButton'));
}
},
_clickEventOnNewTabButtonHandled : false,
onBeforeTabDuplicate : function TSTWindow_onBeforeTabDuplicate(aTab, aWhere, aDelta)
{
if (aWhere && aWhere.indexOf('tab') != 0)
return;
var b = this.getTabBrowserFromChild(aTab) || this.browser;
var behaviorPref = !aDelta ? 'autoAttach.duplicateTabCommand' :
aDelta < 0 ? 'autoAttach.duplicateTabCommand.back' :
'autoAttach.duplicateTabCommand.forward'
var behavior = utils.getTreePref(behaviorPref);
this._handleNewTabCommand(aTab || b.selectedTab, behavior);
},
onBeforeOpenLink : function TSTWindow_onBeforeOpenLink(aWhere, aOwner)
{
if (aWhere == 'tab' || aWhere == 'tabshifted')
this.readyToOpenChildTab(aOwner);
},
onBeforeOpenLinkWithParams : function TSTWindow_onBeforeOpenLinkWithParams(aParams)
{
if (aParams.linkNode &&
!this.checkToOpenChildTab(aParams.linkNode.ownerDocument.defaultView))
this.readyToOpenChildTab(aParams.linkNode.ownerDocument.defaultView);
},
onBeforeOpenNewTabByThirdParty : function TSTWindow_onBeforeOpenNewTabByThirdParty(aOwner)
{
if (!this.checkToOpenChildTab(aOwner))
this.readyToOpenChildTab(aOwner);
},
onBeforeBrowserAccessOpenURI : function TSTWindow_onBeforeBrowserAccessOpenURI(aOpener, aWhere)
{
if (aOpener && aWhere == Ci.nsIBrowserDOMWindow.OPEN_NEWTAB)
this.readyToOpenChildTab(aOpener);
},
onBeforeViewMedia : function TSTWindow_onBeforeViewMedia(aEvent, aOwner)
{
if (String(this.window.whereToOpenLink(aEvent, false, true)).indexOf('tab') == 0)
this.readyToOpenChildTab(aOwner);
},
onBeforeBrowserSearch : function TSTWindow_onBeforeBrowserSearch(aTerm, aForceNewTab)
{
2012-09-15 08:53:22 -04:00
if ((arguments.length == 1 || aForceNewTab) &&
this.shouldOpenSearchResultAsChild(aTerm))
this.readyToOpenChildTab();
},
2012-08-28 08:53:25 -04:00
/* Tree Style Tabの初期化が行われる前に復元されたセッションについてツリー構造を復元 */
onTabRestored : function TSTWindow_onTabRestored(aEvent)
{
this._restoringTabs.push(aEvent.originalTarget);
},
processRestoredTabs : function TSTWindow_processRestoredTabs()
{
2012-02-04 16:31:03 -05:00
for (let i = 0, maxi = this._restoringTabs.length; i < maxi; i++)
{
2012-02-04 16:31:03 -05:00
let tab = this._restoringTabs[i];
try {
let b = this.getTabBrowserFromChild(aTab);
2011-12-12 13:54:43 -05:00
if (b) b.treeStyleTab.handleRestoredTab(aTab);
}
catch(e) {
}
}
this._restoringTabs = [];
},
handleMutations : function TSTWindow_handleMutations(aMutations, aObserver)
{
aMutations.forEach(function(aMutation) {
switch (aMutation.type)
{
case 'childList':
this.destroyBrowserBottomBoxChildListeners();
this.initBrowserBottomBoxChildListeners();
break;
case 'attributes':
if (aMutation.attributeName == 'hidden' ||
aMutation.attributeName == 'collapsed')
this.browser.treeStyleTab.updateFloatingTabbar(this.kTABBAR_UPDATE_BY_WINDOW_RESIZE);
break;
}
}, this);
},
/* Commands */
setTabbarWidth : function TSTWindow_setTabbarWidth(aWidth, aForceExpanded) /* PUBLIC API */
{
this.browser.treeStyleTab.autoHide.setWidth(aWidth, aForceExpanded);
},
setContentWidth : function TSTWindow_setContentWidth(aWidth, aKeepWindowSize) /* PUBLIC API */
{
2011-05-25 17:23:02 -04:00
var w = this.window;
var treeStyleTab = this.browser.treeStyleTab;
var strip = treeStyleTab.tabStrip;
var tabbarWidth = treeStyleTab.splitterWidth + (treeStyleTab.isVertical ? strip.boxObject.width : 0 );
var contentWidth = this.browser.boxObject.width - tabbarWidth;
if (aKeepWindowSize ||
2011-05-25 17:23:02 -04:00
w.fullScreen ||
w.windowState != Ci.nsIDOMChromeWindow.STATE_NORMAL) {
this.setTabbarWidth(Math.max(10, this.browser.boxObject.width - aWidth));
}
2011-05-25 17:23:02 -04:00
else if (tabbarWidth + aWidth <= w.screen.availWidth) {
w.resizeBy(aWidth - contentWidth, 0);
}
else {
2011-05-25 17:23:02 -04:00
w.resizeBy(w.screen.availWidth - w.outerWidth, 0);
this.setTabbarWidth(this.browser.boxObject.width - aWidth);
}
},
toggleAutoHide : function TSTWindow_toggleAutoHide(aTabBrowser) /* PUBLIC API, for backward compatibility */
{
this.autoHideWindow.toggleMode(aTabBrowser || this.browser);
},
toggleFixed : function TSTWindow_toggleFixed(aTabBrowser) /* PUBLIC API */
{
var b = aTabBrowser || this.browser;
var orient = b.treeStyleTab.isVertical ? 'vertical' : 'horizontal' ;
var newFixed = b.getAttribute(this.kFIXED+'-'+orient) != 'true';
this.setTabbrowserAttribute(this.kFIXED+'-'+orient, newFixed || null, b);
this.setPrefForActiveWindow(function() {
b.treeStyleTab.fixed = newFixed;
utils.setTreePref('tabbar.fixed.'+orient, newFixed);
});
b.treeStyleTab.updateTabbarState();
},
removeTabSubtree : function TSTWindow_removeTabSubtree(aTabOrTabs, aOnlyChildren)
{
var tabs = this.gatherSubtreeMemberTabs(aTabOrTabs, aOnlyChildren);
if (!this.warnAboutClosingTabs(tabs.length))
return;
if (aOnlyChildren)
tabs = this.gatherSubtreeMemberTabs(aTabOrTabs);
2012-02-04 16:31:03 -05:00
var allSubtrees = this.splitTabsToSubtrees(tabs);
for (let i = 0, maxi = allSubtrees.length; i < maxi; i++)
{
2012-02-04 16:31:03 -05:00
let subtreeTabs = allSubtrees[i];
if (!this.fireTabSubtreeClosingEvent(subtreeTabs[0], subtreeTabs))
continue;
let b = this.getTabBrowserFromChild(subtreeTabs[0]);
if (aOnlyChildren)
subtreeTabs = subtreeTabs.slice(1);
if (!subtreeTabs.length)
continue;
this.stopRendering();
this.markAsClosedSet(subtreeTabs);
for (let i = subtreeTabs.length-1; i > -1; i--)
{
b.removeTab(subtreeTabs[i], { animate : true });
}
this.startRendering();
this.fireTabSubtreeClosedEvent(b, subtreeTabs[0], subtreeTabs)
}
},
removeTabSubTree : function() { return this.removeTabSubtree.apply(this, arguments); }, // obsolete, for backward compatibility
fireTabSubtreeClosingEvent : function TSTWindow_fireTabSubtreeClosingEvent(aParentTab, aClosedTabs)
{
var b = this.getTabBrowserFromChild(aParentTab);
var data = {
parent : aParentTab,
tabs : aClosedTabs
};
var canClose = (
/* PUBLIC API */
this.fireDataContainerEvent(this.kEVENT_TYPE_SUBTREE_CLOSING, b, true, true, data) &&
// for backward compatibility
this.fireDataContainerEvent(this.kEVENT_TYPE_SUBTREE_CLOSING.replace(/^nsDOM/, ''), b, true, true, data)
);
return canClose;
},
fireTabSubtreeClosedEvent : function TSTWindow_fireTabSubtreeClosedEvent(aTabBrowser, aParentTab, aClosedTabs)
{
aClosedTabs = aClosedTabs.filter(function(aTab) { return !aTab.parentNode; });
var data = {
parent : aParentTab,
tabs : aClosedTabs
};
/* PUBLIC API */
this.fireDataContainerEvent(this.kEVENT_TYPE_SUBTREE_CLOSED, aTabBrowser, true, false, data);
// for backward compatibility
this.fireDataContainerEvent(this.kEVENT_TYPE_SUBTREE_CLOSED.replace(/^nsDOM/, ''), aTabBrowser, true, false, data);
},
warnAboutClosingTabSubtreeOf : function TSTWindow_warnAboutClosingTabSubtreeOf(aTab)
{
if (!this.shouldCloseTabSubtreeOf(aTab))
return true;
var tabs = [aTab].concat(this.getDescendantTabs(aTab));
return this.warnAboutClosingTabs(tabs.length);
},
warnAboutClosingTabSubTreeOf : function() { return this.warnAboutClosingTabSubtreeOf.apply(this, arguments); }, // obsolete, for backward compatibility
warnAboutClosingTabs : function TSTWindow_warnAboutClosingTabs(aTabsCount)
{
if (
aTabsCount <= 1 ||
!this.getPref('browser.tabs.warnOnClose')
)
return true;
var checked = { value:true };
2011-05-25 17:23:02 -04:00
var w = this.window;
w.focus();
var shouldClose = Services.prompt.confirmEx(w,
this.tabbrowserBundle.getString('tabs.closeWarningTitle'),
this.tabbrowserBundle.getFormattedString('tabs.closeWarningMultipleTabs', [aTabsCount]),
(Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0) +
(Services.prompt.BUTTON_TITLE_CANCEL * Services.prompt.BUTTON_POS_1),
this.tabbrowserBundle.getString('tabs.closeButtonMultiple'),
null, null,
this.tabbrowserBundle.getString('tabs.closeWarningPromptMe'),
checked
) == 0;
if (shouldClose && !checked.value)
this.setPref('browser.tabs.warnOnClose', false);
return shouldClose;
},
reloadTabSubtree : function TSTWindow_reloadTabSubtree(aTabOrTabs, aOnlyChildren)
{
var tabs = this.gatherSubtreeMemberTabs(aTabOrTabs, aOnlyChildren);
var b = this.getTabBrowserFromChild(tabs[0]);
for (var i = tabs.length-1; i > -1; i--)
{
b.reloadTab(tabs[i]);
}
},
reloadTabSubTree : function() { return this.reloadTabSubtree.apply(this, arguments); }, // obsolete, for backward compatibility
createSubtree : function TSTWindow_createSubtree(aTabs)
{
aTabs = this.getRootTabs(aTabs);
if (!aTabs.length) return;
var b = this.getTabBrowserFromChild(aTabs[0]);
var parent = this.getParentTab(aTabs[0]);
var next = aTabs[0];
while (
(next = this.getNextSiblingTab(next)) &&
aTabs.indexOf(next) > -1
);
var root = utils.getTreePref('createSubtree.underParent') ?
b.addTab(this.getGroupTabURI()) :
aTabs.shift() ;
var self = this;
this.Deferred.next(function(self) {
2012-02-04 16:31:03 -05:00
for (let i = 0, maxi = aTabs.length; i < maxi; i++)
{
2012-02-04 16:31:03 -05:00
let tab = aTabs[i];
b.treeStyleTab.attachTabTo(tab, root);
b.treeStyleTab.collapseExpandTab(tab, false);
}
if (parent) {
b.treeStyleTab.attachTabTo(root, parent, {
insertBefore : next
});
}
else if (next) {
b.treeStyleTab.moveTabSubtreeTo(root, next._tPos);
}
2012-02-06 12:13:46 -05:00
}).error(this.defaultDeferredErrorHandler);
},
createSubTree : function() { return this.createSubtree.apply(this, arguments); }, // obsolete, for backward compatibility
canCreateSubtree : function TSTWindow_canCreateSubtree(aTabs)
{
aTabs = this.getRootTabs(aTabs);
if (aTabs.length < 2) return false;
var lastParent = this.getParentTab(aTabs[0]);
for (let i = 1, maxi = aTabs.length-1; i < maxi; i++)
{
let parent = this.getParentTab(aTabs[i]);
if (!lastParent || parent != lastParent) return true;
lastParent = parent;
}
return this.getChildTabs(lastParent).length != aTabs.length;
},
canCreateSubTree : function() { return this.canCreateSubtree.apply(this, arguments); }, // obsolete, for backward compatibility
getRootTabs : function TSTWindow_getRootTabs(aTabs)
{
var roots = [];
if (!aTabs || !aTabs.length) return roots;
aTabs = this.cleanUpTabsArray(aTabs);
2012-02-04 16:31:03 -05:00
for (let i = 0, maxi = aTabs.length; i < maxi; i++)
{
2012-02-04 16:31:03 -05:00
let tab = aTabs[i];
let parent = this.getParentTab(tab);
if (parent && aTabs.indexOf(parent) > -1) continue;
roots.push(tab);
}
return roots;
},
collapseExpandAllSubtree : function TSTWindow_collapseExpandAllSubtree(aCollapse)
{
Services.obs.notifyObservers(
this.window,
this.kTOPIC_COLLAPSE_EXPAND_ALL,
(aCollapse ? 'collapse' : 'open' )
);
},
promoteTab : function TSTWindow_promoteTab(aTab) /* PUBLIC API */
{
var b = this.getTabBrowserFromChild(aTab);
var sv = b.treeStyleTab;
var parent = sv.getParentTab(aTab);
if (!parent) return;
var nextSibling = sv.getNextSiblingTab(parent);
var grandParent = sv.getParentTab(parent);
if (grandParent) {
sv.attachTabTo(aTab, grandParent, {
insertBefore : nextSibling
});
}
else {
sv.detachTab(aTab);
let index = nextSibling ? nextSibling._tPos : b.mTabContainer.childNodes.length ;
if (index > aTab._tPos) index--;
b.moveTabTo(aTab, index);
}
},
promoteCurrentTab : function TSTWindow_promoteCurrentTab() /* PUBLIC API */
{
this.promoteTab(this.browser.selectedTab);
},
demoteTab : function TSTWindow_demoteTab(aTab) /* PUBLIC API */
{
var b = this.getTabBrowserFromChild(aTab);
var sv = b.treeStyleTab;
var previous = this.getPreviousSiblingTab(aTab);
if (previous)
sv.attachTabTo(aTab, previous);
},
demoteCurrentTab : function TSTWindow_demoteCurrentTab() /* PUBLIC API */
{
this.demoteTab(this.browser.selectedTab);
},
expandTreeAfterKeyReleased : function TSTWindow_expandTreeAfterKeyReleased(aTab)
{
if (utils.getTreePref('autoCollapseExpandSubtreeOnSelect.whileFocusMovingByShortcut')) return;
this._tabShouldBeExpandedAfterKeyReleased = aTab || null;
},
_tabShouldBeExpandedAfterKeyReleased : null,
removeAllTabsBut : function TSTWindow_removeAllTabsBut(aTab)
{
var keepTabs = [aTab].concat(this.getDescendantTabs(aTab));
var b = this.getTabBrowserFromChild(aTab);
2012-09-23 02:43:49 -04:00
var closeTabs = this.getTabs(b).filter(function(aTab) {
return keepTabs.indexOf(aTab) < 0 && !aTab.hasAttribute('pinned');
});
if (!this.warnAboutClosingTabs(closeTabs.length))
return;
this.stopRendering();
this.markAsClosedSet(closeTabs);
2012-02-04 16:31:03 -05:00
var tabs = closeTabs.reverse();
for (let i = 0, maxi = tabs.length; i < maxi; i++)
{
2012-02-04 16:31:03 -05:00
b.removeTab(tabs[i]);
}
this.startRendering();
},
2010-11-30 19:26:12 -05:00
// For backward compatibility. You should use DOM event to block TST's focus handling.
registerTabFocusAllowance : function TSTWindow_registerTabFocusAllowance(aProcess) /* PUBLIC API */
{
2010-11-30 05:05:00 -05:00
var listener = {
process : aProcess,
handleEvent : function(aEvent) {
var tab = aEvent.originalTarget;
var b = tab.__treestyletab__linkedTabBrowser;
if (!this.process.call(b.treeStyleTab, b))
2010-11-30 05:05:00 -05:00
aEvent.preventDefault();
}
};
this.window.addEventListener(this.kEVENT_TYPE_FOCUS_NEXT_TAB, listener, false);
2010-11-30 05:05:00 -05:00
this._tabFocusAllowance.push(listener);
},
_tabFocusAllowance : [],
tearOffSubtreeFromRemote : function TSTWindow_tearOffSubtreeFromRemote()
{
2011-05-25 17:23:02 -04:00
var w = this.window;
var remoteTab = w.arguments[0];
var remoteWindow = remoteTab.ownerDocument.defaultView;
var remoteService = remoteWindow.TreeStyleTabService;
var remoteMultipleTabService = remoteWindow.MultipleTabService;
if (remoteService.hasChildTabs(remoteTab) ||
(remoteMultipleTabService && remoteMultipleTabService.isSelected(remoteTab))) {
let remoteBrowser = remoteService.getTabBrowserFromChild(remoteTab);
2010-12-01 19:00:39 -05:00
if (remoteBrowser.treeStyleTab.tabbarDNDObserver.isDraggingAllTabs(remoteTab)) {
2011-05-25 17:23:02 -04:00
w.close();
}
else {
let actionInfo = {
action : remoteTab.__treestyletab__toBeDuplicated ? this.kACTION_DUPLICATE : this.kACTION_IMPORT
};
let b = this.browser;
let blankTab;
this.Deferred
.next(function() {
var blankTab = b.selectedTab;
b.treeStyleTab.tabbarDNDObserver.performDrop(actionInfo, remoteTab);
return blankTab;
})
.next(function(aBlankTab) {
b.removeTab(aBlankTab);
remoteTab = null;
remoteBrowser = null;
remoteWindow = null
remoteService = null;
remoteMultipleTabService = null;
})
2012-02-06 12:13:46 -05:00
.error(this.defaultDeferredErrorHandler);
}
return true;
}
return false;
},
tearOffSubTreeFromRemote : function() { return this.tearOffSubtreeFromRemote.apply(this, arguments); }, // obsolete, for backward compatibility
onPrintPreviewEnter : function TSTWindow_onPrintPreviewEnter()
{
2011-05-25 17:23:02 -04:00
var d = this.document;
var event = d.createEvent('Events');
2010-11-30 05:05:00 -05:00
event.initEvent(this.kEVENT_TYPE_PRINT_PREVIEW_ENTERED, true, false);
2011-05-25 17:23:02 -04:00
d.documentElement.dispatchEvent(event);
// for backward compatibility
2011-05-25 17:23:02 -04:00
event = d.createEvent('Events');
event.initEvent(this.kEVENT_TYPE_PRINT_PREVIEW_ENTERED.replace(/^nsDOM/, ''), true, false);
2011-05-25 17:23:02 -04:00
d.documentElement.dispatchEvent(event);
},
onPrintPreviewExit : function TSTWindow_onPrintPreviewExit()
{
2011-05-25 17:23:02 -04:00
var d = this.document;
var event = d.createEvent('Events');
2010-11-30 05:05:00 -05:00
event.initEvent(this.kEVENT_TYPE_PRINT_PREVIEW_EXITED, true, false);
2011-05-25 17:23:02 -04:00
d.documentElement.dispatchEvent(event);
// for backward compatibility
2011-05-25 17:23:02 -04:00
event = d.createEvent('Events');
event.initEvent(this.kEVENT_TYPE_PRINT_PREVIEW_EXITED.replace(/^nsDOM/, ''), true, false);
2011-05-25 17:23:02 -04:00
d.documentElement.dispatchEvent(event);
},
observe : function TSTWindow_observe(aSubject, aTopic, aData)
{
switch (aTopic)
{
case 'nsPref:changed':
this.onPrefChange(aData);
return;
}
},
get restoringTree() {
if (this._restoringTree || !!this.restoringCount)
return true;
var count = 0;
this.browser.visibleTabs.some(function(aTab) {
if (aTab.linkedBrowser.__treestyletab__toBeRestored)
count++;
return count > 1;
});
return count > 1;
},
2011-12-08 11:39:55 -05:00
set restoringTree(aValue) {
return this._restoringTree = !!aValue;
},
_restoringTree : false,
/* Pref Listener */
domains : [
'extensions.treestyletab',
'browser.ctrlTab.previews'
],
onPrefChange : function TSTWindow_onPrefChange(aPrefName)
{
var value = this.getPref(aPrefName);
switch (aPrefName)
{
case 'extensions.treestyletab.tabbar.autoHide.mode':
// don't set on this time, because appearance of all tabbrowsers are not updated yet.
// this.autoHide.mode = utils.getTreePref('tabbar.autoHide.mode');
case 'extensions.treestyletab.tabbar.autoShow.accelKeyDown':
case 'extensions.treestyletab.tabbar.autoShow.tabSwitch':
case 'extensions.treestyletab.tabbar.autoShow.feedback':
this.autoHideWindow.updateKeyListeners(this.window);
break;
case 'extensions.treestyletab.tabbar.style':
case 'extensions.treestyletab.tabbar.position':
2011-05-26 10:22:42 -04:00
this.themeManager.set(this.getPref('extensions.treestyletab.tabbar.style'), this.position);
break;
case 'browser.ctrlTab.previews':
this.autoHideWindow.updateKeyListeners(this.window);
case 'extensions.treestyletab.autoCollapseExpandSubtreeOnSelect.whileFocusMovingByShortcut':
case 'extensions.treestyletab.autoCollapseExpandSubtreeOnSelect':
if (this.shouldListenKeyEventsForAutoExpandByFocusChange)
this.startListenKeyEventsFor(this.LISTEN_FOR_AUTOEXPAND_BY_FOCUSCHANGE);
else
this.endListenKeyEventsFor(this.LISTEN_FOR_AUTOEXPAND_BY_FOCUSCHANGE);
break;
default:
break;
}
}
};