browser/devtools/styleeditor/styleeditor-panel.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/styleeditor/styleeditor-panel.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,152 @@
     1.4 +/* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +const {Cc, Ci, Cu, Cr} = require("chrome");
    1.11 +
    1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +
    1.15 +let {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
    1.16 +let EventEmitter = require("devtools/toolkit/event-emitter");
    1.17 +
    1.18 +Cu.import("resource:///modules/devtools/StyleEditorUI.jsm");
    1.19 +Cu.import("resource:///modules/devtools/StyleEditorUtil.jsm");
    1.20 +
    1.21 +loader.lazyGetter(this, "StyleSheetsFront",
    1.22 +  () => require("devtools/server/actors/stylesheets").StyleSheetsFront);
    1.23 +
    1.24 +loader.lazyGetter(this, "StyleEditorFront",
    1.25 +  () => require("devtools/server/actors/styleeditor").StyleEditorFront);
    1.26 +
    1.27 +this.StyleEditorPanel = function StyleEditorPanel(panelWin, toolbox) {
    1.28 +  EventEmitter.decorate(this);
    1.29 +
    1.30 +  this._toolbox = toolbox;
    1.31 +  this._target = toolbox.target;
    1.32 +  this._panelWin = panelWin;
    1.33 +  this._panelDoc = panelWin.document;
    1.34 +
    1.35 +  this.destroy = this.destroy.bind(this);
    1.36 +  this._showError = this._showError.bind(this);
    1.37 +}
    1.38 +
    1.39 +exports.StyleEditorPanel = StyleEditorPanel;
    1.40 +
    1.41 +StyleEditorPanel.prototype = {
    1.42 +  get target() this._toolbox.target,
    1.43 +
    1.44 +  get panelWindow() this._panelWin,
    1.45 +
    1.46 +  /**
    1.47 +   * open is effectively an asynchronous constructor
    1.48 +   */
    1.49 +  open: function() {
    1.50 +    let deferred = promise.defer();
    1.51 +
    1.52 +    let targetPromise;
    1.53 +    // We always interact with the target as if it were remote
    1.54 +    if (!this.target.isRemote) {
    1.55 +      targetPromise = this.target.makeRemote();
    1.56 +    } else {
    1.57 +      targetPromise = promise.resolve(this.target);
    1.58 +    }
    1.59 +
    1.60 +    targetPromise.then(() => {
    1.61 +      this.target.on("close", this.destroy);
    1.62 +
    1.63 +      if (this.target.form.styleSheetsActor) {
    1.64 +        this._debuggee = StyleSheetsFront(this.target.client, this.target.form);
    1.65 +      }
    1.66 +      else {
    1.67 +        /* We're talking to a pre-Firefox 29 server-side */
    1.68 +        this._debuggee = StyleEditorFront(this.target.client, this.target.form);
    1.69 +      }
    1.70 +      this.UI = new StyleEditorUI(this._debuggee, this.target, this._panelDoc);
    1.71 +      this.UI.initialize().then(() => {
    1.72 +        this.UI.on("error", this._showError);
    1.73 +
    1.74 +        this.isReady = true;
    1.75 +
    1.76 +        deferred.resolve(this);
    1.77 +      });
    1.78 +    }, console.error);
    1.79 +
    1.80 +    return deferred.promise;
    1.81 +  },
    1.82 +
    1.83 +  /**
    1.84 +   * Show an error message from the style editor in the toolbox
    1.85 +   * notification box.
    1.86 +   *
    1.87 +   * @param  {string} event
    1.88 +   *         Type of event
    1.89 +   * @param  {string} code
    1.90 +   *         Error code of error to report
    1.91 +   * @param  {string} message
    1.92 +   *         Extra message to append to error message
    1.93 +   */
    1.94 +  _showError: function(event, code, message) {
    1.95 +    if (!this._toolbox) {
    1.96 +      // could get an async error after we've been destroyed
    1.97 +      return;
    1.98 +    }
    1.99 +
   1.100 +    let errorMessage = _(code);
   1.101 +    if (message) {
   1.102 +      errorMessage += " " + message;
   1.103 +    }
   1.104 +
   1.105 +    let notificationBox = this._toolbox.getNotificationBox();
   1.106 +    let notification = notificationBox.getNotificationWithValue("styleeditor-error");
   1.107 +    if (!notification) {
   1.108 +      notificationBox.appendNotification(errorMessage,
   1.109 +        "styleeditor-error", "", notificationBox.PRIORITY_CRITICAL_LOW);
   1.110 +    }
   1.111 +  },
   1.112 +
   1.113 +  /**
   1.114 +   * Select a stylesheet.
   1.115 +   *
   1.116 +   * @param {string} href
   1.117 +   *        Url of stylesheet to find and select in editor
   1.118 +   * @param {number} line
   1.119 +   *        Line number to jump to after selecting. One-indexed
   1.120 +   * @param {number} col
   1.121 +   *        Column number to jump to after selecting. One-indexed
   1.122 +   */
   1.123 +  selectStyleSheet: function(href, line, col) {
   1.124 +    if (!this._debuggee || !this.UI) {
   1.125 +      return;
   1.126 +    }
   1.127 +    this.UI.selectStyleSheet(href, line - 1, col ? col - 1 : 0);
   1.128 +  },
   1.129 +
   1.130 +  /**
   1.131 +   * Destroy the style editor.
   1.132 +   */
   1.133 +  destroy: function() {
   1.134 +    if (!this._destroyed) {
   1.135 +      this._destroyed = true;
   1.136 +
   1.137 +      this._target.off("close", this.destroy);
   1.138 +      this._target = null;
   1.139 +      this._toolbox = null;
   1.140 +      this._panelDoc = null;
   1.141 +      this._debuggee.destroy();
   1.142 +      this._debuggee = null;
   1.143 +
   1.144 +      this.UI.destroy();
   1.145 +    }
   1.146 +
   1.147 +    return promise.resolve(null);
   1.148 +  },
   1.149 +}
   1.150 +
   1.151 +XPCOMUtils.defineLazyGetter(StyleEditorPanel.prototype, "strings",
   1.152 +  function () {
   1.153 +    return Services.strings.createBundle(
   1.154 +            "chrome://browser/locale/devtools/styleeditor.properties");
   1.155 +  });

mercurial