browser/devtools/profiler/sidebar.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/profiler/sidebar.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     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 +"use strict";
     1.9 +
    1.10 +let { Cu } = require("chrome");
    1.11 +let EventEmitter = require("devtools/toolkit/event-emitter");
    1.12 +
    1.13 +Cu.import("resource:///modules/devtools/SideMenuWidget.jsm");
    1.14 +Cu.import("resource:///modules/devtools/ViewHelpers.jsm");
    1.15 +
    1.16 +const {
    1.17 +  PROFILE_IDLE,
    1.18 +  PROFILE_COMPLETED,
    1.19 +  PROFILE_RUNNING,
    1.20 +  L10N_BUNDLE
    1.21 +} = require("devtools/profiler/consts");
    1.22 +
    1.23 +loader.lazyGetter(this, "L10N", () => new ViewHelpers.L10N(L10N_BUNDLE));
    1.24 +
    1.25 +let stopProfilingString = L10N.getStr("profiler.stopProfilerString");
    1.26 +let startProfilingString = L10N.getStr("profiler.startProfilerString");
    1.27 +
    1.28 +function Sidebar(el) {
    1.29 +  EventEmitter.decorate(this);
    1.30 +
    1.31 +  this.document = el.ownerDocument;
    1.32 +  this.widget = new SideMenuWidget(el, { showArrows: true });
    1.33 +  this.emptyText = L10N.getStr("profiler.sidebarNotice");
    1.34 +
    1.35 +  this.widget.addEventListener("select", (ev) => {
    1.36 +    if (!ev.detail)
    1.37 +      return;
    1.38 +
    1.39 +    this.emit("select", parseInt(ev.detail.value, 10));
    1.40 +  });
    1.41 +}
    1.42 +
    1.43 +Sidebar.prototype = Heritage.extend(WidgetMethods, {
    1.44 +  /**
    1.45 +   * Adds a new item for a profile to the sidebar. Markup
    1.46 +   * example:
    1.47 +   *
    1.48 +   *   <vbox id="profile-1" class="profiler-sidebar-item">
    1.49 +   *     <h3>Profile 1</h3>
    1.50 +   *     <hbox>
    1.51 +   *       <span flex="1">Completed</span>
    1.52 +   *       <a>Save</a>
    1.53 +   *     </hbox>
    1.54 +   *   </vbox>
    1.55 +   *
    1.56 +   */
    1.57 +  addProfile: function (profile) {
    1.58 +    let doc  = this.document;
    1.59 +    let vbox = doc.createElement("vbox");
    1.60 +    let hbox = doc.createElement("hbox");
    1.61 +    let h3   = doc.createElement("h3");
    1.62 +    let span = doc.createElement("span");
    1.63 +    let save = doc.createElement("a");
    1.64 +
    1.65 +    vbox.id = "profile-" + profile.uid;
    1.66 +    vbox.className = "profiler-sidebar-item";
    1.67 +
    1.68 +    h3.textContent = profile.name;
    1.69 +    span.setAttribute("flex", 1);
    1.70 +    span.textContent = L10N.getStr("profiler.stateIdle");
    1.71 +
    1.72 +    save.textContent = L10N.getStr("profiler.save");
    1.73 +    save.addEventListener("click", (ev) => {
    1.74 +      ev.preventDefault();
    1.75 +      this.emit("save", profile.uid);
    1.76 +    });
    1.77 +
    1.78 +    hbox.appendChild(span);
    1.79 +    hbox.appendChild(save);
    1.80 +
    1.81 +    vbox.appendChild(h3);
    1.82 +    vbox.appendChild(hbox);
    1.83 +
    1.84 +    this.push([vbox, profile.uid], {
    1.85 +      attachment: {
    1.86 +        name:  profile.name,
    1.87 +        state: PROFILE_IDLE
    1.88 +      }
    1.89 +    });
    1.90 +  },
    1.91 +
    1.92 +  getElementByProfile: function (profile) {
    1.93 +    return this.document.querySelector("#profile-" + profile.uid);
    1.94 +  },
    1.95 +
    1.96 +  getItemByProfile: function (profile) {
    1.97 +    return this.getItemByValue(profile.uid.toString());
    1.98 +  },
    1.99 +
   1.100 +  setProfileState: function (profile, state) {
   1.101 +    let item = this.getItemByProfile(profile);
   1.102 +    let doc = this.document;
   1.103 +    let label = item.target.querySelector(".profiler-sidebar-item > hbox > span");
   1.104 +    let toggleButton = doc.getElementById("profiler-start");
   1.105 +
   1.106 +    switch (state) {
   1.107 +      case PROFILE_IDLE:
   1.108 +        item.target.setAttribute("state", "idle");
   1.109 +        label.textContent = L10N.getStr("profiler.stateIdle");
   1.110 +        break;
   1.111 +      case PROFILE_RUNNING:
   1.112 +        item.target.setAttribute("state", "running");
   1.113 +        label.textContent = L10N.getStr("profiler.stateRunning");
   1.114 +        toggleButton.setAttribute("tooltiptext",stopProfilingString);
   1.115 +        break;
   1.116 +      case PROFILE_COMPLETED:
   1.117 +        item.target.setAttribute("state", "completed");
   1.118 +        label.textContent = L10N.getStr("profiler.stateCompleted");
   1.119 +        toggleButton.setAttribute("tooltiptext",startProfilingString);
   1.120 +        break;
   1.121 +      default: // Wrong state, do nothing.
   1.122 +        return;
   1.123 +    }
   1.124 +
   1.125 +    item.attachment.state = state;
   1.126 +    this.emit("stateChanged", item);
   1.127 +  }
   1.128 +});
   1.129 +
   1.130 +module.exports = Sidebar;

mercurial