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.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 let { Cu } = require("chrome");
8 let EventEmitter = require("devtools/toolkit/event-emitter");
10 Cu.import("resource:///modules/devtools/SideMenuWidget.jsm");
11 Cu.import("resource:///modules/devtools/ViewHelpers.jsm");
13 const {
14 PROFILE_IDLE,
15 PROFILE_COMPLETED,
16 PROFILE_RUNNING,
17 L10N_BUNDLE
18 } = require("devtools/profiler/consts");
20 loader.lazyGetter(this, "L10N", () => new ViewHelpers.L10N(L10N_BUNDLE));
22 let stopProfilingString = L10N.getStr("profiler.stopProfilerString");
23 let startProfilingString = L10N.getStr("profiler.startProfilerString");
25 function Sidebar(el) {
26 EventEmitter.decorate(this);
28 this.document = el.ownerDocument;
29 this.widget = new SideMenuWidget(el, { showArrows: true });
30 this.emptyText = L10N.getStr("profiler.sidebarNotice");
32 this.widget.addEventListener("select", (ev) => {
33 if (!ev.detail)
34 return;
36 this.emit("select", parseInt(ev.detail.value, 10));
37 });
38 }
40 Sidebar.prototype = Heritage.extend(WidgetMethods, {
41 /**
42 * Adds a new item for a profile to the sidebar. Markup
43 * example:
44 *
45 * <vbox id="profile-1" class="profiler-sidebar-item">
46 * <h3>Profile 1</h3>
47 * <hbox>
48 * <span flex="1">Completed</span>
49 * <a>Save</a>
50 * </hbox>
51 * </vbox>
52 *
53 */
54 addProfile: function (profile) {
55 let doc = this.document;
56 let vbox = doc.createElement("vbox");
57 let hbox = doc.createElement("hbox");
58 let h3 = doc.createElement("h3");
59 let span = doc.createElement("span");
60 let save = doc.createElement("a");
62 vbox.id = "profile-" + profile.uid;
63 vbox.className = "profiler-sidebar-item";
65 h3.textContent = profile.name;
66 span.setAttribute("flex", 1);
67 span.textContent = L10N.getStr("profiler.stateIdle");
69 save.textContent = L10N.getStr("profiler.save");
70 save.addEventListener("click", (ev) => {
71 ev.preventDefault();
72 this.emit("save", profile.uid);
73 });
75 hbox.appendChild(span);
76 hbox.appendChild(save);
78 vbox.appendChild(h3);
79 vbox.appendChild(hbox);
81 this.push([vbox, profile.uid], {
82 attachment: {
83 name: profile.name,
84 state: PROFILE_IDLE
85 }
86 });
87 },
89 getElementByProfile: function (profile) {
90 return this.document.querySelector("#profile-" + profile.uid);
91 },
93 getItemByProfile: function (profile) {
94 return this.getItemByValue(profile.uid.toString());
95 },
97 setProfileState: function (profile, state) {
98 let item = this.getItemByProfile(profile);
99 let doc = this.document;
100 let label = item.target.querySelector(".profiler-sidebar-item > hbox > span");
101 let toggleButton = doc.getElementById("profiler-start");
103 switch (state) {
104 case PROFILE_IDLE:
105 item.target.setAttribute("state", "idle");
106 label.textContent = L10N.getStr("profiler.stateIdle");
107 break;
108 case PROFILE_RUNNING:
109 item.target.setAttribute("state", "running");
110 label.textContent = L10N.getStr("profiler.stateRunning");
111 toggleButton.setAttribute("tooltiptext",stopProfilingString);
112 break;
113 case PROFILE_COMPLETED:
114 item.target.setAttribute("state", "completed");
115 label.textContent = L10N.getStr("profiler.stateCompleted");
116 toggleButton.setAttribute("tooltiptext",startProfilingString);
117 break;
118 default: // Wrong state, do nothing.
119 return;
120 }
122 item.attachment.state = state;
123 this.emit("stateChanged", item);
124 }
125 });
127 module.exports = Sidebar;