1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/app-manager/content/index.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,184 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const Cu = Components.utils; 1.9 +Cu.import("resource:///modules/devtools/gDevTools.jsm"); 1.10 +const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}); 1.11 +const {require} = devtools; 1.12 +const {ConnectionManager, Connection} = require("devtools/client/connection-manager"); 1.13 +const promise = require("devtools/toolkit/deprecated-sync-thenables"); 1.14 +const prefs = require('sdk/preferences/service'); 1.15 + 1.16 + 1.17 +let UI = { 1.18 + _toolboxTabCursor: 0, 1.19 + _handledTargets: new Map(), 1.20 + 1.21 + connection: null, 1.22 + 1.23 + init: function() { 1.24 + this.onLoad = this.onLoad.bind(this); 1.25 + this.onUnload = this.onUnload.bind(this); 1.26 + this.onMessage = this.onMessage.bind(this); 1.27 + this.onConnected = this.onConnected.bind(this); 1.28 + this.onDisconnected = this.onDisconnected.bind(this); 1.29 + 1.30 + window.addEventListener("load", this.onLoad); 1.31 + window.addEventListener("unload", this.onUnload); 1.32 + window.addEventListener("message", this.onMessage); 1.33 + }, 1.34 + 1.35 + onLoad: function() { 1.36 + window.removeEventListener("load", this.onLoad); 1.37 + let defaultPanel = prefs.get("devtools.appmanager.lastTab"); 1.38 + let panelExists = !!document.querySelector("." + defaultPanel + "-panel"); 1.39 + this.selectTab(panelExists ? defaultPanel : "projects"); 1.40 + }, 1.41 + 1.42 + onUnload: function() { 1.43 + for (let [target, toolbox] of this._handledTargets) { 1.44 + toolbox.destroy(); 1.45 + } 1.46 + 1.47 + window.removeEventListener("unload", this.onUnload); 1.48 + window.removeEventListener("message", this.onMessage); 1.49 + if (this.connection) { 1.50 + this.connection.off(Connection.Status.CONNECTED, this.onConnected); 1.51 + this.connection.off(Connection.Status.DISCONNECTED, this.onDisconnected); 1.52 + } 1.53 + }, 1.54 + 1.55 + onMessage: function(event) { 1.56 + try { 1.57 + let json = JSON.parse(event.data); 1.58 + switch (json.name) { 1.59 + case "connection": 1.60 + let cid = +json.cid; 1.61 + for (let c of ConnectionManager.connections) { 1.62 + if (c.uid == cid) { 1.63 + this.onNewConnection(c); 1.64 + break; 1.65 + } 1.66 + } 1.67 + break; 1.68 + case "closeHelp": 1.69 + this.selectTab("projects"); 1.70 + break; 1.71 + case "toolbox-raise": 1.72 + window.top.focus(); 1.73 + this.selectTab(json.uid); 1.74 + break; 1.75 + case "toolbox-close": 1.76 + this.closeToolboxTab(json.uid); 1.77 + break; 1.78 + case "toolbox-title": 1.79 + // Not implemented 1.80 + break; 1.81 + default: 1.82 + Cu.reportError("Unknown message: " + json.name); 1.83 + } 1.84 + } catch(e) { Cu.reportError(e); } 1.85 + 1.86 + // Forward message 1.87 + let panels = document.querySelectorAll(".panel"); 1.88 + for (let frame of panels) { 1.89 + frame.contentWindow.postMessage(event.data, "*"); 1.90 + } 1.91 + }, 1.92 + 1.93 + selectTabFromButton: function(button) { 1.94 + if (!button.hasAttribute("panel")) 1.95 + return; 1.96 + this.selectTab(button.getAttribute("panel")); 1.97 + }, 1.98 + 1.99 + selectTab: function(panel) { 1.100 + let isToolboxTab = false; 1.101 + for (let type of ["button", "panel"]) { 1.102 + let oldSelection = document.querySelector("." + type + "[selected]"); 1.103 + let newSelection = document.querySelector("." + panel + "-" + type); 1.104 + if (oldSelection) oldSelection.removeAttribute("selected"); 1.105 + if (newSelection) { 1.106 + newSelection.setAttribute("selected", "true"); 1.107 + if (newSelection.classList.contains("toolbox")) { 1.108 + isToolboxTab = true; 1.109 + } 1.110 + } 1.111 + } 1.112 + if (!isToolboxTab) { 1.113 + prefs.set("devtools.appmanager.lastTab", panel); 1.114 + } 1.115 + }, 1.116 + 1.117 + onNewConnection: function(connection) { 1.118 + this.connection = connection; 1.119 + this.connection.on(Connection.Status.CONNECTED, this.onConnected); 1.120 + this.connection.on(Connection.Status.DISCONNECTED, this.onDisconnected); 1.121 + }, 1.122 + 1.123 + onConnected: function() { 1.124 + document.querySelector("#content").classList.add("connected"); 1.125 + }, 1.126 + 1.127 + onDisconnected: function() { 1.128 + for (let [,toolbox] of this._handledTargets) { 1.129 + if (toolbox) { 1.130 + toolbox.destroy(); 1.131 + } 1.132 + } 1.133 + this._handledTargets.clear(); 1.134 + document.querySelector("#content").classList.remove("connected"); 1.135 + }, 1.136 + 1.137 + createToolboxTab: function(name, iconURL, uid) { 1.138 + let button = document.createElement("button"); 1.139 + button.className = "button toolbox " + uid + "-button"; 1.140 + button.setAttribute("panel", uid); 1.141 + button.textContent = name; 1.142 + button.setAttribute("style", "background-image: url(" + iconURL + ")"); 1.143 + let toolboxTabs = document.querySelector("#toolbox-tabs"); 1.144 + toolboxTabs.appendChild(button); 1.145 + let iframe = document.createElement("iframe"); 1.146 + iframe.setAttribute("flex", "1"); 1.147 + iframe.className = "panel toolbox " + uid + "-panel"; 1.148 + let panels = document.querySelector("#tab-panels"); 1.149 + panels.appendChild(iframe); 1.150 + this.selectTab(uid); 1.151 + return iframe; 1.152 + }, 1.153 + 1.154 + closeToolboxTab: function(uid) { 1.155 + let buttonToDestroy = document.querySelector("." + uid + "-button"); 1.156 + let panelToDestroy = document.querySelector("." + uid + "-panel"); 1.157 + 1.158 + if (buttonToDestroy.hasAttribute("selected")) { 1.159 + let lastTab = prefs.get("devtools.appmanager.lastTab"); 1.160 + this.selectTab(lastTab); 1.161 + } 1.162 + 1.163 + buttonToDestroy.remove(); 1.164 + panelToDestroy.remove(); 1.165 + }, 1.166 + 1.167 + openAndShowToolboxForTarget: function(target, name, icon) { 1.168 + let host = devtools.Toolbox.HostType.CUSTOM; 1.169 + let toolbox = gDevTools.getToolbox(target); 1.170 + if (!toolbox) { 1.171 + let uid = "uid" + this._toolboxTabCursor++; 1.172 + let iframe = this.createToolboxTab(name, icon, uid); 1.173 + let options = { customIframe: iframe , uid: uid }; 1.174 + this._handledTargets.set(target, null); 1.175 + return gDevTools.showToolbox(target, null, host, options).then(toolbox => { 1.176 + this._handledTargets.set(target, toolbox); 1.177 + toolbox.once("destroyed", () => { 1.178 + this._handledTargets.delete(target) 1.179 + }); 1.180 + }); 1.181 + } else { 1.182 + return gDevTools.showToolbox(target, null, host); 1.183 + } 1.184 + } 1.185 +} 1.186 + 1.187 +UI.init();