browser/devtools/app-manager/content/index.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Cu = Components.utils;
michael@0 6 Cu.import("resource:///modules/devtools/gDevTools.jsm");
michael@0 7 const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
michael@0 8 const {require} = devtools;
michael@0 9 const {ConnectionManager, Connection} = require("devtools/client/connection-manager");
michael@0 10 const promise = require("devtools/toolkit/deprecated-sync-thenables");
michael@0 11 const prefs = require('sdk/preferences/service');
michael@0 12
michael@0 13
michael@0 14 let UI = {
michael@0 15 _toolboxTabCursor: 0,
michael@0 16 _handledTargets: new Map(),
michael@0 17
michael@0 18 connection: null,
michael@0 19
michael@0 20 init: function() {
michael@0 21 this.onLoad = this.onLoad.bind(this);
michael@0 22 this.onUnload = this.onUnload.bind(this);
michael@0 23 this.onMessage = this.onMessage.bind(this);
michael@0 24 this.onConnected = this.onConnected.bind(this);
michael@0 25 this.onDisconnected = this.onDisconnected.bind(this);
michael@0 26
michael@0 27 window.addEventListener("load", this.onLoad);
michael@0 28 window.addEventListener("unload", this.onUnload);
michael@0 29 window.addEventListener("message", this.onMessage);
michael@0 30 },
michael@0 31
michael@0 32 onLoad: function() {
michael@0 33 window.removeEventListener("load", this.onLoad);
michael@0 34 let defaultPanel = prefs.get("devtools.appmanager.lastTab");
michael@0 35 let panelExists = !!document.querySelector("." + defaultPanel + "-panel");
michael@0 36 this.selectTab(panelExists ? defaultPanel : "projects");
michael@0 37 },
michael@0 38
michael@0 39 onUnload: function() {
michael@0 40 for (let [target, toolbox] of this._handledTargets) {
michael@0 41 toolbox.destroy();
michael@0 42 }
michael@0 43
michael@0 44 window.removeEventListener("unload", this.onUnload);
michael@0 45 window.removeEventListener("message", this.onMessage);
michael@0 46 if (this.connection) {
michael@0 47 this.connection.off(Connection.Status.CONNECTED, this.onConnected);
michael@0 48 this.connection.off(Connection.Status.DISCONNECTED, this.onDisconnected);
michael@0 49 }
michael@0 50 },
michael@0 51
michael@0 52 onMessage: function(event) {
michael@0 53 try {
michael@0 54 let json = JSON.parse(event.data);
michael@0 55 switch (json.name) {
michael@0 56 case "connection":
michael@0 57 let cid = +json.cid;
michael@0 58 for (let c of ConnectionManager.connections) {
michael@0 59 if (c.uid == cid) {
michael@0 60 this.onNewConnection(c);
michael@0 61 break;
michael@0 62 }
michael@0 63 }
michael@0 64 break;
michael@0 65 case "closeHelp":
michael@0 66 this.selectTab("projects");
michael@0 67 break;
michael@0 68 case "toolbox-raise":
michael@0 69 window.top.focus();
michael@0 70 this.selectTab(json.uid);
michael@0 71 break;
michael@0 72 case "toolbox-close":
michael@0 73 this.closeToolboxTab(json.uid);
michael@0 74 break;
michael@0 75 case "toolbox-title":
michael@0 76 // Not implemented
michael@0 77 break;
michael@0 78 default:
michael@0 79 Cu.reportError("Unknown message: " + json.name);
michael@0 80 }
michael@0 81 } catch(e) { Cu.reportError(e); }
michael@0 82
michael@0 83 // Forward message
michael@0 84 let panels = document.querySelectorAll(".panel");
michael@0 85 for (let frame of panels) {
michael@0 86 frame.contentWindow.postMessage(event.data, "*");
michael@0 87 }
michael@0 88 },
michael@0 89
michael@0 90 selectTabFromButton: function(button) {
michael@0 91 if (!button.hasAttribute("panel"))
michael@0 92 return;
michael@0 93 this.selectTab(button.getAttribute("panel"));
michael@0 94 },
michael@0 95
michael@0 96 selectTab: function(panel) {
michael@0 97 let isToolboxTab = false;
michael@0 98 for (let type of ["button", "panel"]) {
michael@0 99 let oldSelection = document.querySelector("." + type + "[selected]");
michael@0 100 let newSelection = document.querySelector("." + panel + "-" + type);
michael@0 101 if (oldSelection) oldSelection.removeAttribute("selected");
michael@0 102 if (newSelection) {
michael@0 103 newSelection.setAttribute("selected", "true");
michael@0 104 if (newSelection.classList.contains("toolbox")) {
michael@0 105 isToolboxTab = true;
michael@0 106 }
michael@0 107 }
michael@0 108 }
michael@0 109 if (!isToolboxTab) {
michael@0 110 prefs.set("devtools.appmanager.lastTab", panel);
michael@0 111 }
michael@0 112 },
michael@0 113
michael@0 114 onNewConnection: function(connection) {
michael@0 115 this.connection = connection;
michael@0 116 this.connection.on(Connection.Status.CONNECTED, this.onConnected);
michael@0 117 this.connection.on(Connection.Status.DISCONNECTED, this.onDisconnected);
michael@0 118 },
michael@0 119
michael@0 120 onConnected: function() {
michael@0 121 document.querySelector("#content").classList.add("connected");
michael@0 122 },
michael@0 123
michael@0 124 onDisconnected: function() {
michael@0 125 for (let [,toolbox] of this._handledTargets) {
michael@0 126 if (toolbox) {
michael@0 127 toolbox.destroy();
michael@0 128 }
michael@0 129 }
michael@0 130 this._handledTargets.clear();
michael@0 131 document.querySelector("#content").classList.remove("connected");
michael@0 132 },
michael@0 133
michael@0 134 createToolboxTab: function(name, iconURL, uid) {
michael@0 135 let button = document.createElement("button");
michael@0 136 button.className = "button toolbox " + uid + "-button";
michael@0 137 button.setAttribute("panel", uid);
michael@0 138 button.textContent = name;
michael@0 139 button.setAttribute("style", "background-image: url(" + iconURL + ")");
michael@0 140 let toolboxTabs = document.querySelector("#toolbox-tabs");
michael@0 141 toolboxTabs.appendChild(button);
michael@0 142 let iframe = document.createElement("iframe");
michael@0 143 iframe.setAttribute("flex", "1");
michael@0 144 iframe.className = "panel toolbox " + uid + "-panel";
michael@0 145 let panels = document.querySelector("#tab-panels");
michael@0 146 panels.appendChild(iframe);
michael@0 147 this.selectTab(uid);
michael@0 148 return iframe;
michael@0 149 },
michael@0 150
michael@0 151 closeToolboxTab: function(uid) {
michael@0 152 let buttonToDestroy = document.querySelector("." + uid + "-button");
michael@0 153 let panelToDestroy = document.querySelector("." + uid + "-panel");
michael@0 154
michael@0 155 if (buttonToDestroy.hasAttribute("selected")) {
michael@0 156 let lastTab = prefs.get("devtools.appmanager.lastTab");
michael@0 157 this.selectTab(lastTab);
michael@0 158 }
michael@0 159
michael@0 160 buttonToDestroy.remove();
michael@0 161 panelToDestroy.remove();
michael@0 162 },
michael@0 163
michael@0 164 openAndShowToolboxForTarget: function(target, name, icon) {
michael@0 165 let host = devtools.Toolbox.HostType.CUSTOM;
michael@0 166 let toolbox = gDevTools.getToolbox(target);
michael@0 167 if (!toolbox) {
michael@0 168 let uid = "uid" + this._toolboxTabCursor++;
michael@0 169 let iframe = this.createToolboxTab(name, icon, uid);
michael@0 170 let options = { customIframe: iframe , uid: uid };
michael@0 171 this._handledTargets.set(target, null);
michael@0 172 return gDevTools.showToolbox(target, null, host, options).then(toolbox => {
michael@0 173 this._handledTargets.set(target, toolbox);
michael@0 174 toolbox.once("destroyed", () => {
michael@0 175 this._handledTargets.delete(target)
michael@0 176 });
michael@0 177 });
michael@0 178 } else {
michael@0 179 return gDevTools.showToolbox(target, null, host);
michael@0 180 }
michael@0 181 }
michael@0 182 }
michael@0 183
michael@0 184 UI.init();

mercurial