michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: let { Cu } = require("chrome"); michael@0: let EventEmitter = require("devtools/toolkit/event-emitter"); michael@0: michael@0: Cu.import("resource:///modules/devtools/SideMenuWidget.jsm"); michael@0: Cu.import("resource:///modules/devtools/ViewHelpers.jsm"); michael@0: michael@0: const { michael@0: PROFILE_IDLE, michael@0: PROFILE_COMPLETED, michael@0: PROFILE_RUNNING, michael@0: L10N_BUNDLE michael@0: } = require("devtools/profiler/consts"); michael@0: michael@0: loader.lazyGetter(this, "L10N", () => new ViewHelpers.L10N(L10N_BUNDLE)); michael@0: michael@0: let stopProfilingString = L10N.getStr("profiler.stopProfilerString"); michael@0: let startProfilingString = L10N.getStr("profiler.startProfilerString"); michael@0: michael@0: function Sidebar(el) { michael@0: EventEmitter.decorate(this); michael@0: michael@0: this.document = el.ownerDocument; michael@0: this.widget = new SideMenuWidget(el, { showArrows: true }); michael@0: this.emptyText = L10N.getStr("profiler.sidebarNotice"); michael@0: michael@0: this.widget.addEventListener("select", (ev) => { michael@0: if (!ev.detail) michael@0: return; michael@0: michael@0: this.emit("select", parseInt(ev.detail.value, 10)); michael@0: }); michael@0: } michael@0: michael@0: Sidebar.prototype = Heritage.extend(WidgetMethods, { michael@0: /** michael@0: * Adds a new item for a profile to the sidebar. Markup michael@0: * example: michael@0: * michael@0: * michael@0: *

Profile 1

michael@0: * michael@0: * Completed michael@0: * Save michael@0: * michael@0: *
michael@0: * michael@0: */ michael@0: addProfile: function (profile) { michael@0: let doc = this.document; michael@0: let vbox = doc.createElement("vbox"); michael@0: let hbox = doc.createElement("hbox"); michael@0: let h3 = doc.createElement("h3"); michael@0: let span = doc.createElement("span"); michael@0: let save = doc.createElement("a"); michael@0: michael@0: vbox.id = "profile-" + profile.uid; michael@0: vbox.className = "profiler-sidebar-item"; michael@0: michael@0: h3.textContent = profile.name; michael@0: span.setAttribute("flex", 1); michael@0: span.textContent = L10N.getStr("profiler.stateIdle"); michael@0: michael@0: save.textContent = L10N.getStr("profiler.save"); michael@0: save.addEventListener("click", (ev) => { michael@0: ev.preventDefault(); michael@0: this.emit("save", profile.uid); michael@0: }); michael@0: michael@0: hbox.appendChild(span); michael@0: hbox.appendChild(save); michael@0: michael@0: vbox.appendChild(h3); michael@0: vbox.appendChild(hbox); michael@0: michael@0: this.push([vbox, profile.uid], { michael@0: attachment: { michael@0: name: profile.name, michael@0: state: PROFILE_IDLE michael@0: } michael@0: }); michael@0: }, michael@0: michael@0: getElementByProfile: function (profile) { michael@0: return this.document.querySelector("#profile-" + profile.uid); michael@0: }, michael@0: michael@0: getItemByProfile: function (profile) { michael@0: return this.getItemByValue(profile.uid.toString()); michael@0: }, michael@0: michael@0: setProfileState: function (profile, state) { michael@0: let item = this.getItemByProfile(profile); michael@0: let doc = this.document; michael@0: let label = item.target.querySelector(".profiler-sidebar-item > hbox > span"); michael@0: let toggleButton = doc.getElementById("profiler-start"); michael@0: michael@0: switch (state) { michael@0: case PROFILE_IDLE: michael@0: item.target.setAttribute("state", "idle"); michael@0: label.textContent = L10N.getStr("profiler.stateIdle"); michael@0: break; michael@0: case PROFILE_RUNNING: michael@0: item.target.setAttribute("state", "running"); michael@0: label.textContent = L10N.getStr("profiler.stateRunning"); michael@0: toggleButton.setAttribute("tooltiptext",stopProfilingString); michael@0: break; michael@0: case PROFILE_COMPLETED: michael@0: item.target.setAttribute("state", "completed"); michael@0: label.textContent = L10N.getStr("profiler.stateCompleted"); michael@0: toggleButton.setAttribute("tooltiptext",startProfilingString); michael@0: break; michael@0: default: // Wrong state, do nothing. michael@0: return; michael@0: } michael@0: michael@0: item.attachment.state = state; michael@0: this.emit("stateChanged", item); michael@0: } michael@0: }); michael@0: michael@0: module.exports = Sidebar;