Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | const {Cu} = require("chrome"); |
michael@0 | 8 | |
michael@0 | 9 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 10 | |
michael@0 | 11 | var {Promise: promise} = require("resource://gre/modules/Promise.jsm"); |
michael@0 | 12 | var EventEmitter = require("devtools/toolkit/event-emitter"); |
michael@0 | 13 | var Telemetry = require("devtools/shared/telemetry"); |
michael@0 | 14 | |
michael@0 | 15 | const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * ToolSidebar provides methods to register tabs in the sidebar. |
michael@0 | 19 | * It's assumed that the sidebar contains a xul:tabbox. |
michael@0 | 20 | * |
michael@0 | 21 | * @param {Node} tabbox |
michael@0 | 22 | * <tabbox> node; |
michael@0 | 23 | * @param {ToolPanel} panel |
michael@0 | 24 | * Related ToolPanel instance; |
michael@0 | 25 | * @param {String} uid |
michael@0 | 26 | * Unique ID |
michael@0 | 27 | * @param {Boolean} showTabstripe |
michael@0 | 28 | * Show the tabs. |
michael@0 | 29 | */ |
michael@0 | 30 | function ToolSidebar(tabbox, panel, uid, showTabstripe=true) |
michael@0 | 31 | { |
michael@0 | 32 | EventEmitter.decorate(this); |
michael@0 | 33 | |
michael@0 | 34 | this._tabbox = tabbox; |
michael@0 | 35 | this._uid = uid; |
michael@0 | 36 | this._panelDoc = this._tabbox.ownerDocument; |
michael@0 | 37 | this._toolPanel = panel; |
michael@0 | 38 | |
michael@0 | 39 | try { |
michael@0 | 40 | this._width = Services.prefs.getIntPref("devtools.toolsidebar-width." + this._uid); |
michael@0 | 41 | } catch(e) {} |
michael@0 | 42 | |
michael@0 | 43 | this._telemetry = new Telemetry(); |
michael@0 | 44 | |
michael@0 | 45 | this._tabbox.tabpanels.addEventListener("select", this, true); |
michael@0 | 46 | |
michael@0 | 47 | this._tabs = new Map(); |
michael@0 | 48 | |
michael@0 | 49 | if (!showTabstripe) { |
michael@0 | 50 | this._tabbox.setAttribute("hidetabs", "true"); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | exports.ToolSidebar = ToolSidebar; |
michael@0 | 55 | |
michael@0 | 56 | ToolSidebar.prototype = { |
michael@0 | 57 | /** |
michael@0 | 58 | * Register a tab. A tab is a document. |
michael@0 | 59 | * The document must have a title, which will be used as the name of the tab. |
michael@0 | 60 | * |
michael@0 | 61 | * @param {string} tab uniq id |
michael@0 | 62 | * @param {string} url |
michael@0 | 63 | */ |
michael@0 | 64 | addTab: function ToolSidebar_addTab(id, url, selected=false) { |
michael@0 | 65 | let iframe = this._panelDoc.createElementNS(XULNS, "iframe"); |
michael@0 | 66 | iframe.className = "iframe-" + id; |
michael@0 | 67 | iframe.setAttribute("flex", "1"); |
michael@0 | 68 | iframe.setAttribute("src", url); |
michael@0 | 69 | iframe.tooltip = "aHTMLTooltip"; |
michael@0 | 70 | |
michael@0 | 71 | let tab = this._tabbox.tabs.appendItem(); |
michael@0 | 72 | tab.setAttribute("label", ""); // Avoid showing "undefined" while the tab is loading |
michael@0 | 73 | |
michael@0 | 74 | let onIFrameLoaded = function() { |
michael@0 | 75 | tab.setAttribute("label", iframe.contentDocument.title); |
michael@0 | 76 | iframe.removeEventListener("load", onIFrameLoaded, true); |
michael@0 | 77 | if ("setPanel" in iframe.contentWindow) { |
michael@0 | 78 | iframe.contentWindow.setPanel(this._toolPanel, iframe); |
michael@0 | 79 | } |
michael@0 | 80 | this.emit(id + "-ready"); |
michael@0 | 81 | }.bind(this); |
michael@0 | 82 | |
michael@0 | 83 | iframe.addEventListener("load", onIFrameLoaded, true); |
michael@0 | 84 | |
michael@0 | 85 | let tabpanel = this._panelDoc.createElementNS(XULNS, "tabpanel"); |
michael@0 | 86 | tabpanel.setAttribute("id", "sidebar-panel-" + id); |
michael@0 | 87 | tabpanel.appendChild(iframe); |
michael@0 | 88 | this._tabbox.tabpanels.appendChild(tabpanel); |
michael@0 | 89 | |
michael@0 | 90 | this._tooltip = this._panelDoc.createElementNS(XULNS, "tooltip"); |
michael@0 | 91 | this._tooltip.id = "aHTMLTooltip"; |
michael@0 | 92 | tabpanel.appendChild(this._tooltip); |
michael@0 | 93 | this._tooltip.page = true; |
michael@0 | 94 | |
michael@0 | 95 | tab.linkedPanel = "sidebar-panel-" + id; |
michael@0 | 96 | |
michael@0 | 97 | // We store the index of this tab. |
michael@0 | 98 | this._tabs.set(id, tab); |
michael@0 | 99 | |
michael@0 | 100 | if (selected) { |
michael@0 | 101 | // For some reason I don't understand, if we call this.select in this |
michael@0 | 102 | // event loop (after inserting the tab), the tab will never get the |
michael@0 | 103 | // the "selected" attribute set to true. |
michael@0 | 104 | this._panelDoc.defaultView.setTimeout(function() { |
michael@0 | 105 | this.select(id); |
michael@0 | 106 | }.bind(this), 10); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | this.emit("new-tab-registered", id); |
michael@0 | 110 | }, |
michael@0 | 111 | |
michael@0 | 112 | /** |
michael@0 | 113 | * Select a specific tab. |
michael@0 | 114 | */ |
michael@0 | 115 | select: function ToolSidebar_select(id) { |
michael@0 | 116 | let tab = this._tabs.get(id); |
michael@0 | 117 | if (tab) { |
michael@0 | 118 | this._tabbox.selectedTab = tab; |
michael@0 | 119 | } |
michael@0 | 120 | }, |
michael@0 | 121 | |
michael@0 | 122 | /** |
michael@0 | 123 | * Return the id of the selected tab. |
michael@0 | 124 | */ |
michael@0 | 125 | getCurrentTabID: function ToolSidebar_getCurrentTabID() { |
michael@0 | 126 | let currentID = null; |
michael@0 | 127 | for (let [id, tab] of this._tabs) { |
michael@0 | 128 | if (this._tabbox.tabs.selectedItem == tab) { |
michael@0 | 129 | currentID = id; |
michael@0 | 130 | break; |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | return currentID; |
michael@0 | 134 | }, |
michael@0 | 135 | |
michael@0 | 136 | /** |
michael@0 | 137 | * Returns the requested tab based on the id. |
michael@0 | 138 | * |
michael@0 | 139 | * @param String id |
michael@0 | 140 | * unique id of the requested tab. |
michael@0 | 141 | */ |
michael@0 | 142 | getTab: function ToolSidebar_getTab(id) { |
michael@0 | 143 | return this._tabbox.tabpanels.querySelector("#sidebar-panel-" + id); |
michael@0 | 144 | }, |
michael@0 | 145 | |
michael@0 | 146 | /** |
michael@0 | 147 | * Event handler. |
michael@0 | 148 | */ |
michael@0 | 149 | handleEvent: function ToolSidebar_eventHandler(event) { |
michael@0 | 150 | if (event.type == "select") { |
michael@0 | 151 | if (this._currentTool == this.getCurrentTabID()) { |
michael@0 | 152 | // Tool hasn't changed. |
michael@0 | 153 | return; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | let previousTool = this._currentTool; |
michael@0 | 157 | this._currentTool = this.getCurrentTabID(); |
michael@0 | 158 | if (previousTool) { |
michael@0 | 159 | this._telemetry.toolClosed(previousTool); |
michael@0 | 160 | this.emit(previousTool + "-unselected"); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | this._telemetry.toolOpened(this._currentTool); |
michael@0 | 164 | this.emit(this._currentTool + "-selected"); |
michael@0 | 165 | this.emit("select", this._currentTool); |
michael@0 | 166 | } |
michael@0 | 167 | }, |
michael@0 | 168 | |
michael@0 | 169 | /** |
michael@0 | 170 | * Toggle sidebar's visibility state. |
michael@0 | 171 | */ |
michael@0 | 172 | toggle: function ToolSidebar_toggle() { |
michael@0 | 173 | if (this._tabbox.hasAttribute("hidden")) { |
michael@0 | 174 | this.show(); |
michael@0 | 175 | } else { |
michael@0 | 176 | this.hide(); |
michael@0 | 177 | } |
michael@0 | 178 | }, |
michael@0 | 179 | |
michael@0 | 180 | /** |
michael@0 | 181 | * Show the sidebar. |
michael@0 | 182 | */ |
michael@0 | 183 | show: function ToolSidebar_show() { |
michael@0 | 184 | if (this._width) { |
michael@0 | 185 | this._tabbox.width = this._width; |
michael@0 | 186 | } |
michael@0 | 187 | this._tabbox.removeAttribute("hidden"); |
michael@0 | 188 | }, |
michael@0 | 189 | |
michael@0 | 190 | /** |
michael@0 | 191 | * Show the sidebar. |
michael@0 | 192 | */ |
michael@0 | 193 | hide: function ToolSidebar_hide() { |
michael@0 | 194 | Services.prefs.setIntPref("devtools.toolsidebar-width." + this._uid, this._tabbox.width); |
michael@0 | 195 | this._tabbox.setAttribute("hidden", "true"); |
michael@0 | 196 | }, |
michael@0 | 197 | |
michael@0 | 198 | /** |
michael@0 | 199 | * Return the window containing the tab content. |
michael@0 | 200 | */ |
michael@0 | 201 | getWindowForTab: function ToolSidebar_getWindowForTab(id) { |
michael@0 | 202 | if (!this._tabs.has(id)) { |
michael@0 | 203 | return null; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | let panel = this._panelDoc.getElementById(this._tabs.get(id).linkedPanel); |
michael@0 | 207 | return panel.firstChild.contentWindow; |
michael@0 | 208 | }, |
michael@0 | 209 | |
michael@0 | 210 | /** |
michael@0 | 211 | * Clean-up. |
michael@0 | 212 | */ |
michael@0 | 213 | destroy: function ToolSidebar_destroy() { |
michael@0 | 214 | if (this._destroyed) { |
michael@0 | 215 | return promise.resolve(null); |
michael@0 | 216 | } |
michael@0 | 217 | this._destroyed = true; |
michael@0 | 218 | |
michael@0 | 219 | Services.prefs.setIntPref("devtools.toolsidebar-width." + this._uid, this._tabbox.width); |
michael@0 | 220 | |
michael@0 | 221 | this._tabbox.tabpanels.removeEventListener("select", this, true); |
michael@0 | 222 | |
michael@0 | 223 | while (this._tabbox.tabpanels.hasChildNodes()) { |
michael@0 | 224 | this._tabbox.tabpanels.removeChild(this._tabbox.tabpanels.firstChild); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | while (this._tabbox.tabs.hasChildNodes()) { |
michael@0 | 228 | this._tabbox.tabs.removeChild(this._tabbox.tabs.firstChild); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | if (this._currentTool) { |
michael@0 | 232 | this._telemetry.toolClosed(this._currentTool); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | this._tabs = null; |
michael@0 | 236 | this._tabbox = null; |
michael@0 | 237 | this._panelDoc = null; |
michael@0 | 238 | this._toolPanel = null; |
michael@0 | 239 | |
michael@0 | 240 | return promise.resolve(null); |
michael@0 | 241 | }, |
michael@0 | 242 | } |