2011-12-11 09:09:53 -05: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.
|
|
|
|
*
|
|
|
|
* 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.
|
2016-02-15 10:29:30 -05:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010-2016
|
2011-12-11 09:09:53 -05:00
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
2012-10-13 14:31:06 -04:00
|
|
|
* Contributor(s): YUKI "Piro" Hiroshi <piro.outsider.reflex@gmail.com>
|
2013-11-09 14:28:07 -05:00
|
|
|
* Tetsuharu OHZEKI <https://github.com/saneyuki>
|
2011-12-11 09:09:53 -05: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 ******/
|
|
|
|
|
2015-10-10 23:40:17 -04:00
|
|
|
var EXPORTED_SYMBOLS = ['GroupTab'];
|
2011-12-11 09:09:53 -05:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2016-02-15 11:32:56 -05:00
|
|
|
Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
|
|
|
|
|
2014-04-02 06:25:51 -04:00
|
|
|
Components.utils.import('resource://treestyletab-modules/lib/inherit.jsm');
|
2012-09-27 02:42:50 -04:00
|
|
|
Components.utils.import('resource://treestyletab-modules/base.js');
|
2011-12-11 09:09:53 -05:00
|
|
|
Components.utils.import('resource://treestyletab-modules/pseudoTreeBuilder.js');
|
2014-11-11 05:26:14 -05:00
|
|
|
Components.utils.import('resource://treestyletab-modules/tabAttributesObserver.js');
|
2011-12-11 09:09:53 -05:00
|
|
|
|
2016-02-15 11:32:56 -05:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, 'utils', 'resource://treestyletab-modules/utils.js', 'TreeStyleTabUtils');
|
|
|
|
|
2011-12-11 09:09:53 -05:00
|
|
|
function GroupTab(aWindow)
|
|
|
|
{
|
|
|
|
this.window = aWindow;
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
2014-04-02 06:25:51 -04:00
|
|
|
GroupTab.prototype = inherit(TreeStyleTabBase, {
|
2011-12-11 09:09:53 -05:00
|
|
|
|
|
|
|
initialized : false,
|
|
|
|
shouldUpdate : false,
|
|
|
|
|
|
|
|
window : null,
|
|
|
|
get document()
|
|
|
|
{
|
|
|
|
return this.window && this.window.document;
|
|
|
|
},
|
|
|
|
get location()
|
|
|
|
{
|
|
|
|
return this.window && this.window.location;
|
|
|
|
},
|
2013-08-21 00:00:38 -04:00
|
|
|
get locationSearch()
|
|
|
|
{
|
|
|
|
return this.location.href.replace(/^[^\?]+/, '');
|
|
|
|
},
|
2011-12-11 09:09:53 -05:00
|
|
|
|
|
|
|
get label()
|
|
|
|
{
|
|
|
|
return this.document.getElementById('label');
|
|
|
|
},
|
|
|
|
get editor()
|
|
|
|
{
|
|
|
|
return this.document.getElementById('editor');
|
|
|
|
},
|
|
|
|
get deck()
|
|
|
|
{
|
|
|
|
return this.document.getElementById('deck');
|
|
|
|
},
|
2013-08-21 00:51:03 -04:00
|
|
|
get temporaryCheck()
|
|
|
|
{
|
|
|
|
return this.document.getElementById('temporary');
|
|
|
|
},
|
2011-12-11 09:09:53 -05:00
|
|
|
|
|
|
|
get title()
|
|
|
|
{
|
|
|
|
if (this._title === null) {
|
2013-08-21 00:00:38 -04:00
|
|
|
let locationSearch = this.locationSearch;
|
|
|
|
let title = locationSearch.match(/(?:^|[\?&;])title=([^&;]*)/i);
|
2013-08-20 23:39:58 -04:00
|
|
|
if (title)
|
|
|
|
title = title[1];
|
|
|
|
// for old style URIs
|
2013-08-21 00:00:38 -04:00
|
|
|
if (!title && !/(?:^|[\?&;])temporary=/i.test(locationSearch))
|
|
|
|
title = locationSearch.replace(/^\?/, '');
|
2013-08-20 23:28:47 -04:00
|
|
|
this._title = (title) ?
|
2011-12-11 09:09:53 -05:00
|
|
|
this.trim(decodeURIComponent(title)) :
|
|
|
|
'' ;
|
|
|
|
}
|
|
|
|
return this._title;
|
|
|
|
},
|
2013-08-20 23:46:17 -04:00
|
|
|
set title(aValue) {
|
|
|
|
this._title = aValue;
|
2013-08-21 00:49:31 -04:00
|
|
|
this.document.title = this.label.value = aValue;
|
|
|
|
this.label.setAttribute('tooltiptext', aValue);
|
|
|
|
this.document.documentElement.setAttribute('title', aValue);
|
2013-08-20 23:46:17 -04:00
|
|
|
this._updateURI();
|
|
|
|
return aValue;
|
|
|
|
},
|
2011-12-11 09:09:53 -05:00
|
|
|
_title : null,
|
|
|
|
|
2013-08-20 23:39:58 -04:00
|
|
|
get temporary() {
|
2013-08-21 00:00:38 -04:00
|
|
|
return /(?:^|[\?&;])temporary=(?:1|yes|true)/i.test(this.locationSearch);
|
2013-08-20 23:39:58 -04:00
|
|
|
},
|
2013-08-20 23:46:17 -04:00
|
|
|
set temporary(aValue) {
|
2013-08-21 00:51:03 -04:00
|
|
|
aValue = !!aValue;
|
|
|
|
this._updateURI({ temporary: aValue });
|
|
|
|
this.temporaryCheck.checked = aValue;
|
2013-08-20 23:46:17 -04:00
|
|
|
return aValue;
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateURI : function GT_updateURI(aOptions) {
|
|
|
|
aOptions = aOptions || {};
|
|
|
|
var temporary = this.temporary;
|
|
|
|
if ('temporary' in aOptions)
|
|
|
|
temporary = aOptions.temporary;
|
|
|
|
this.location.replace(
|
|
|
|
this.location.href.split('?')[0] + '?' +
|
2013-08-21 04:36:12 -04:00
|
|
|
'title=' + encodeURIComponent(this.title) + '&' +
|
2013-08-20 23:46:17 -04:00
|
|
|
'temporary=' + temporary
|
|
|
|
);
|
|
|
|
},
|
2013-08-20 23:39:58 -04:00
|
|
|
|
2011-12-11 09:09:53 -05:00
|
|
|
get browser()
|
|
|
|
{
|
|
|
|
var ownerWindow = this.window
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShell)
|
|
|
|
.QueryInterface(Ci.nsIDocShellTreeItem)
|
|
|
|
.parent;
|
|
|
|
if (ownerWindow) {
|
|
|
|
ownerWindow = ownerWindow
|
|
|
|
.QueryInterface(Ci.nsIWebNavigation)
|
|
|
|
.document
|
|
|
|
.defaultView;
|
|
|
|
return ownerWindow.gBrowser;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
getOwnerTab : function GT_getOwnerTab(aWindow)
|
|
|
|
{
|
|
|
|
var b = this.browser;
|
|
|
|
return b &&
|
|
|
|
b.treeStyleTab &&
|
|
|
|
b.treeStyleTab.getTabFromFrame(aWindow || this.window);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
init : function GT_init()
|
|
|
|
{
|
|
|
|
var title = this.title;
|
|
|
|
if (title) {
|
|
|
|
this.document.title = title;
|
|
|
|
this.document.documentElement.setAttribute('title', title);
|
|
|
|
this.label.value = title;
|
|
|
|
this.label.setAttribute('tooltiptext', title);
|
|
|
|
this.editor.value = title;
|
|
|
|
}
|
|
|
|
|
2013-08-21 00:51:03 -04:00
|
|
|
this.temporaryCheck.checked = this.temporary;
|
|
|
|
|
2011-12-11 09:09:53 -05:00
|
|
|
this.window.addEventListener('load', this, false);
|
|
|
|
|
|
|
|
this.window.groupTab = this;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2012-01-02 15:13:39 -05:00
|
|
|
trim : function GT_trim(aString)
|
2011-12-11 09:09:53 -05:00
|
|
|
{
|
|
|
|
return aString.replace(/^\s+|\s+$/g, '');
|
|
|
|
},
|
|
|
|
|
|
|
|
getTabById : function GT_getTabById(aId)
|
|
|
|
{
|
|
|
|
var b = this.browser;
|
|
|
|
if (aId && b)
|
|
|
|
return b.treeStyleTab.getTabById(aId);
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
enterEdit : function GT_enterEdit()
|
|
|
|
{
|
|
|
|
if (this.deck.selectedIndex == 1) return;
|
|
|
|
|
|
|
|
this.editor.value = this.label.value;
|
|
|
|
this.deck.selectedIndex = 1;
|
|
|
|
this.editor.focus();
|
|
|
|
this.editor.select();
|
|
|
|
},
|
|
|
|
|
|
|
|
exitEdit : function GT_exitEdit()
|
|
|
|
{
|
|
|
|
if (this.deck.selectedIndex == 0) return;
|
|
|
|
|
|
|
|
var old = this.trim(this.label.value);
|
|
|
|
var value = this.trim(this.editor.value);
|
|
|
|
|
2013-08-20 23:46:17 -04:00
|
|
|
if (value != old)
|
|
|
|
this.title = value;
|
2011-12-11 09:09:53 -05:00
|
|
|
|
|
|
|
this.editor.blur();
|
|
|
|
this.deck.selectedIndex = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
updateTree : function GT_updateTree()
|
|
|
|
{
|
|
|
|
if (!this.window || this.window.closed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var tree = this.document.getElementById('tree');
|
|
|
|
|
|
|
|
var range = this.document.createRange();
|
|
|
|
range.selectNodeContents(tree);
|
|
|
|
range.deleteContents();
|
|
|
|
range.detach();
|
|
|
|
|
|
|
|
var contents = PseudoTreeBuilder.build(this.getOwnerTab());
|
|
|
|
if (contents)
|
|
|
|
tree.appendChild(contents);
|
2016-02-15 05:49:09 -05:00
|
|
|
|
|
|
|
this.onResize();
|
2011-12-11 09:09:53 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
checkUpdateTreeNow : function GT_checkUpdateTreeNow()
|
|
|
|
{
|
|
|
|
if (this.getOwnerTab().selected)
|
|
|
|
this.window.setTimeout(function(aSelf) {
|
|
|
|
if (aSelf.window && !aSelf.window.closed)
|
|
|
|
aSelf.onTabSelect();
|
|
|
|
}, 0, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
handleEvent : function GT_handleEvent(aEvent)
|
|
|
|
{
|
|
|
|
switch (aEvent.type)
|
|
|
|
{
|
|
|
|
case 'load':
|
|
|
|
if (aEvent.currentTarget == this.window)
|
|
|
|
this.onLoad(aEvent);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'unload':
|
|
|
|
case 'TabClose':
|
|
|
|
return this.onUnload(aEvent);
|
|
|
|
|
|
|
|
case 'click':
|
|
|
|
return this.onClick(aEvent);
|
|
|
|
case 'dblclick':
|
|
|
|
return this.onDblClick(aEvent);
|
|
|
|
case 'keypress':
|
|
|
|
return this.onKeyPress(aEvent);
|
|
|
|
|
|
|
|
case PseudoTreeBuilder.kTAB_LINK_CLICK:
|
|
|
|
return this.onItemClick(aEvent);
|
|
|
|
|
|
|
|
case 'TabSelect':
|
|
|
|
return this.onTabSelect(aEvent);
|
2013-11-20 12:48:55 -05:00
|
|
|
|
2016-02-15 05:49:09 -05:00
|
|
|
case 'resize':
|
|
|
|
return this.onResize();
|
2016-02-15 05:18:42 -05:00
|
|
|
|
2011-12-11 09:09:53 -05:00
|
|
|
case this.kEVENT_TYPE_ATTACHED:
|
|
|
|
return this.onTabAttached(aEvent);
|
|
|
|
case this.kEVENT_TYPE_DETACHED:
|
|
|
|
return this.onTabDetached(aEvent);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onLoad : function GT_onLoad(aEvent)
|
|
|
|
{
|
|
|
|
this.window.removeEventListener('load', this, false);
|
|
|
|
|
|
|
|
if (this.initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var tab = this.getOwnerTab();
|
|
|
|
|
|
|
|
this.updateTree();
|
|
|
|
|
|
|
|
this.window.addEventListener('unload', this, false);
|
|
|
|
this.window.addEventListener('click', this, false);
|
|
|
|
this.window.addEventListener('dblclick', this, false);
|
2016-02-15 05:49:09 -05:00
|
|
|
this.window.addEventListener('resize', this, false);
|
2011-12-11 09:09:53 -05:00
|
|
|
|
|
|
|
tab.addEventListener('TabSelect', this, false);
|
|
|
|
tab.addEventListener('TabClose', this, false);
|
|
|
|
tab.parentNode.addEventListener(this.kEVENT_TYPE_ATTACHED, this, false);
|
|
|
|
tab.parentNode.addEventListener(this.kEVENT_TYPE_DETACHED, this, false);
|
|
|
|
|
2016-02-12 05:56:57 -05:00
|
|
|
this.tabsObserver = new TabAttributesObserver({
|
|
|
|
container : this.browser.tabContainer,
|
|
|
|
attributes : 'label,visibleLabel,image',
|
|
|
|
callback : (function(aTab) {
|
|
|
|
this.onTabModified(aTab);
|
|
|
|
}).bind(this)
|
|
|
|
});
|
2011-12-11 09:09:53 -05:00
|
|
|
|
|
|
|
this.editor.addEventListener('keypress', this, false);
|
|
|
|
|
|
|
|
this.document.getElementById('tree')
|
|
|
|
.addEventListener(PseudoTreeBuilder.kTAB_LINK_CLICK, this, false);
|
|
|
|
|
|
|
|
this.initialized = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
onUnload : function GT_onUnload(aEvent)
|
|
|
|
{
|
|
|
|
var tab = this.getOwnerTab();
|
|
|
|
if (!this.initialized || !tab)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.window.removeEventListener('unload', this, false);
|
|
|
|
this.window.removeEventListener('click', this, false);
|
|
|
|
this.window.removeEventListener('dblclick', this, false);
|
2016-02-15 05:49:09 -05:00
|
|
|
this.window.removeEventListener('resize', this, false);
|
2011-12-11 09:09:53 -05:00
|
|
|
|
|
|
|
tab.removeEventListener('TabSelect', this, false);
|
|
|
|
tab.removeEventListener('TabClose', this, false);
|
|
|
|
tab.parentNode.removeEventListener(this.kEVENT_TYPE_ATTACHED, this, false);
|
|
|
|
tab.parentNode.removeEventListener(this.kEVENT_TYPE_DETACHED, this, false);
|
|
|
|
|
2014-11-11 05:26:14 -05:00
|
|
|
this.tabsObserver.destroy();
|
|
|
|
delete this.tabsObserver;
|
2011-12-11 09:09:53 -05:00
|
|
|
|
|
|
|
this.editor.removeEventListener('keypress', this, false);
|
|
|
|
|
|
|
|
this.document.getElementById('tree')
|
|
|
|
.removeEventListener(PseudoTreeBuilder.kTAB_LINK_CLICK, this, false);
|
|
|
|
|
|
|
|
delete this.window.groupTab;
|
|
|
|
delete this.window;
|
|
|
|
},
|
|
|
|
|
|
|
|
onClick : function GT_onClick(aEvent)
|
|
|
|
{
|
|
|
|
if (aEvent.target == this.editor) return;
|
|
|
|
if (aEvent.target == this.label)
|
|
|
|
this.enterEdit();
|
|
|
|
else
|
|
|
|
this.exitEdit();
|
|
|
|
},
|
|
|
|
|
|
|
|
onDblClick : function GT_onDblClick(aEvent)
|
|
|
|
{
|
|
|
|
if (aEvent.target == this.editor) return;
|
|
|
|
if (this.deck.selectedIndex == 0)
|
|
|
|
this.enterEdit();
|
|
|
|
else
|
|
|
|
this.exitEdit();
|
|
|
|
},
|
|
|
|
|
|
|
|
onKeyPress : function GT_onKeyPress(aEvent)
|
|
|
|
{
|
|
|
|
if (aEvent.keyCode == aEvent.DOM_VK_ENTER ||
|
|
|
|
aEvent.keyCode == aEvent.DOM_VK_RETURN ||
|
|
|
|
aEvent.keyCode == aEvent.DOM_VK_ESCAPE)
|
|
|
|
this.exitEdit();
|
|
|
|
},
|
|
|
|
|
|
|
|
onItemClick : function GT_onItemClick(aEvent)
|
|
|
|
{
|
|
|
|
var b = this.browser;
|
|
|
|
if (!b)
|
|
|
|
return;
|
|
|
|
|
2014-03-11 12:50:40 -04:00
|
|
|
var tab = this.getTabById(aEvent.detail.id);
|
2011-12-11 09:09:53 -05:00
|
|
|
if (!tab)
|
|
|
|
return;
|
|
|
|
|
2014-03-11 12:50:40 -04:00
|
|
|
var event = aEvent.detail.sourceEvent;
|
2011-12-11 09:09:53 -05:00
|
|
|
if (event.button == 1 ||
|
|
|
|
(event.button == 0 && this.isAccelKeyPressed(event)))
|
|
|
|
b.removeTab(tab);
|
|
|
|
else if (event.button != 2)
|
|
|
|
b.selectedTab = tab;
|
|
|
|
},
|
|
|
|
|
|
|
|
onTabSelect : function GT_onTabSelect(aEvent)
|
|
|
|
{
|
|
|
|
if (this.shouldUpdate)
|
|
|
|
this.updateTree();
|
|
|
|
|
|
|
|
this.shouldUpdate = false;
|
|
|
|
},
|
|
|
|
|
2016-02-15 05:49:09 -05:00
|
|
|
onResize : function GT_onResize()
|
2016-02-15 05:18:42 -05:00
|
|
|
{
|
2016-02-15 11:32:56 -05:00
|
|
|
if (!utils.getTreePref('groupTab.columnize'))
|
|
|
|
return;
|
2016-02-15 06:26:14 -05:00
|
|
|
var container = this.document.getElementById('tree');
|
2016-02-15 11:24:36 -05:00
|
|
|
var tree = container.firstChild;
|
2016-03-05 00:47:55 -05:00
|
|
|
PseudoTreeBuilder.columnizeTree(tree, {
|
|
|
|
columnWidth : utils.getTreePref('groupTab.columnize.width')
|
|
|
|
});
|
2016-02-15 05:18:42 -05:00
|
|
|
},
|
|
|
|
|
2011-12-11 09:09:53 -05:00
|
|
|
onTabAttached : function GT_onTabAttached(aEvent)
|
|
|
|
{
|
2014-03-11 12:50:40 -04:00
|
|
|
var tab = aEvent.detail.parentTab;
|
2014-07-01 15:44:43 -04:00
|
|
|
var id = tab.getAttribute(this.kID);
|
2011-12-11 09:09:53 -05:00
|
|
|
if (tab == this.getOwnerTab() ||
|
|
|
|
this.document.getElementsByAttribute('tab-id', id).length)
|
|
|
|
this.shouldUpdate = true;
|
|
|
|
|
|
|
|
this.checkUpdateTreeNow();
|
|
|
|
},
|
|
|
|
|
|
|
|
onTabDetached : function GT_onTabDetached(aEvent)
|
|
|
|
{
|
|
|
|
var tab = aEvent.originalTarget;
|
2014-07-01 15:44:43 -04:00
|
|
|
var id = tab.getAttribute(this.kID);
|
2011-12-11 09:09:53 -05:00
|
|
|
if (this.document.getElementsByAttribute('tab-id', id).length)
|
|
|
|
this.shouldUpdate = true;
|
|
|
|
|
|
|
|
this.checkUpdateTreeNow();
|
|
|
|
},
|
|
|
|
|
2014-11-11 05:26:14 -05:00
|
|
|
onTabModified : function GT_onTabModified(aTab)
|
2011-12-11 09:09:53 -05:00
|
|
|
{
|
2014-11-11 05:26:14 -05:00
|
|
|
if (aTab) {
|
|
|
|
let id = aTab.getAttribute(this.kID);
|
2011-12-11 09:09:53 -05:00
|
|
|
if (this.document.getElementsByAttribute('tab-id', id).length)
|
|
|
|
this.shouldUpdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.checkUpdateTreeNow();
|
|
|
|
}
|
2016-09-07 02:53:01 -04:00
|
|
|
}, Object);
|