treestyletab/modules/pseudoTreeBuilder.js

177 lines
6.2 KiB
JavaScript
Raw Normal View History

2014-09-30 07:09:27 -04: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.
*
* The Initial Developer of the Original Code is YUKI "Piro" Hiroshi.
* Portions created by the Initial Developer are Copyright (C) 2011-2016
2014-09-30 07:09:27 -04:00
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): YUKI "Piro" Hiroshi <piro.outsider.reflex@gmail.com>
* Tetsuharu OHZEKI <https://github.com/saneyuki>
*
* 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 ******/
var EXPORTED_SYMBOLS = ['PseudoTreeBuilder'];
2014-09-30 07:09:27 -04:00
const Cc = Components.classes;
const Ci = Components.interfaces;
Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'TreeStyleTabBase', 'resource://treestyletab-modules/base.js');
var PseudoTreeBuilder = {
2016-02-15 05:49:09 -05:00
XHTMLNS : 'http://www.w3.org/1999/xhtml',
2014-09-30 07:09:27 -04:00
kFAVICON : 'treestyletab-pseudo-tree-favicon',
2016-02-15 06:26:14 -05:00
kROOT : 'treestyletab-pseudo-tree-root',
2014-09-30 07:09:27 -04:00
kROOTITEM : 'treestyletab-pseudo-tree-root-item',
kTREEITEM : 'treestyletab-pseudo-tree-item',
kTREEROW : 'treestyletab-pseudo-tree-row',
kTREECHILDREN : 'treestyletab-pseudo-tree-children',
kTAB_LINK_CLICK : 'nsDOMTSTPseudoTreeItemClick',
build : function TB_build(aTab)
{
if (!aTab)
return null;
var tree = this.createTabItem(aTab);
2016-02-15 06:26:14 -05:00
tree.className = this.kROOT;
2014-09-30 07:09:27 -04:00
2016-02-15 05:49:09 -05:00
var row = tree.querySelector("*|*."+this.kTREEROW);
2014-09-30 07:09:27 -04:00
if (!row)
return;
row.className += " "+this.kROOTITEM;
tree.setAttribute('onclick',
('var doc = event.target.ownerDocument;\n' +
'var label = doc.evaluate(\n' +
' "ancestor-or-self::*[local-name()=\'label\' and contains(@class, \'text-link\')][1]",\n' +
' event.target,\n' +
' null,\n' +
' Components.interfaces.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE,\n' +
' null\n' +
' ).singleNodeValue;\n' +
'if (label) {\n' +
' var customEvent = new doc.defaultView.CustomEvent(%EVENT_TYPE%, {\n' +
' bubbles : true,\n' +
' cancelable : true,\n' +
' detail : {\n' +
' id : label.getAttribute("tab-id"),\n' +
' sourceEvent : event\n' +
' }\n' +
' });\n' +
' event.target.dispatchEvent(customEvent);\n' +
'}').replace('%EVENT_TYPE%', JSON.stringify(this.kTAB_LINK_CLICK)));
2014-09-30 07:09:27 -04:00
return tree;
},
createTabItem : function TB_createTabItem(aTab)
{
var doc = aTab.ownerDocument;
var w = doc.defaultView;
2016-02-15 05:49:09 -05:00
var item = doc.createElementNS(this.XHTMLNS, 'div');
2016-02-15 05:18:42 -05:00
item.setAttribute('class', this.kTREEITEM);
2014-09-30 07:09:27 -04:00
2016-02-15 05:49:09 -05:00
var favicon = item.appendChild(doc.createElementNS(this.XHTMLNS, 'img'));
2014-09-30 07:09:27 -04:00
favicon.setAttribute('src', aTab.getAttribute('image') || 'chrome://mozapps/skin/places/defaultFavicon.png');
favicon.setAttribute('class', this.kFAVICON);
var label = item.appendChild(doc.createElement('label'));
2016-02-15 05:18:42 -05:00
label.setAttribute('flex', 1);
label.textContent = aTab.label;
2014-09-30 07:09:27 -04:00
var tooltip = aTab.label;
var uri = aTab.linkedBrowser.currentURI.spec;
if (w.isBlankPageURL ? !w.isBlankPageURL(uri) : (uri != 'about:blank')) tooltip += '\n' + uri;
label.setAttribute('tooltiptext', tooltip);
2016-02-15 05:18:42 -05:00
label.setAttribute('class', 'text-link');
2014-09-30 07:09:27 -04:00
label.setAttribute('tab-id', TreeStyleTabBase.getTabValue(aTab, TreeStyleTabBase.kID));
2016-02-15 05:49:09 -05:00
var row = doc.createElementNS(this.XHTMLNS, 'div');
2016-02-15 05:18:42 -05:00
row.setAttribute('class', this.kTREEROW);
row.appendChild(item);
2014-09-30 07:09:27 -04:00
var children = this.createTabChildren(aTab);
if (children) {
2016-02-15 05:49:09 -05:00
let container = doc.createElementNS(this.XHTMLNS, 'div');
2016-02-15 05:18:42 -05:00
container.appendChild(row);
2014-09-30 07:09:27 -04:00
container.appendChild(children);
return container;
}
else {
2016-02-15 05:18:42 -05:00
return row;
2014-09-30 07:09:27 -04:00
}
},
createTabChildren : function TB_createTabChildren(aTab)
{
var doc = aTab.ownerDocument;
var children = TreeStyleTabBase.getChildTabs(aTab);
if (!children.length)
return null;
2016-02-15 05:49:09 -05:00
var container = doc.createElementNS(this.XHTMLNS, 'div');
2016-02-15 05:18:42 -05:00
container.setAttribute('class', this.kTREECHILDREN);
2014-09-30 07:09:27 -04:00
for (let i = 0, maxi = children.length; i < maxi; i++)
{
container.appendChild(this.createTabItem(children[i]));
}
return container;
},
layoutTree : function TB_layoutTree(aTree, aContainer)
{
aContainer = aContainer || aTree.parentNode;
var style = aTree.style;
var height = aTree.clientHeight * (aTree.columnCount || 1);
if (height > aContainer.boxObject.height &&
aContainer.boxObject.height < aContainer.boxObject.width) {
aTree.columnCount = style.columnCount = style.MozColumnCount = 2;
var maxWidth = aContainer.boxObject.width;
style.columnWidth = style.MozColumnWidth = Math.floor(maxWidth * 0.45)+'px';
style.columnGap = style.MozColumnGap = Math.floor(maxWidth * 0.05)+'px';
}
else {
aTree.columnCount = 1;
style.columnCount = style.MozColumnCount =
style.columnWidth = style.MozColumnWidth =
style.columnGap = style.MozColumnGap = '';
}
var items = aTree.querySelectorAll('*|*.' + this.kTREEITEM);
Array.forEach(items, function(aItem) {
aItem.style.minHeight = (aItem.lastChild.boxObject.height + 1) + 'px';
}, this);
2014-09-30 07:09:27 -04:00
}
};