tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

panel.js (4272B)


      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/. */
      4 
      5 "use strict";
      6 
      7 var EventEmitter = require("resource://devtools/shared/event-emitter.js");
      8 
      9 var { StyleEditorUI } = ChromeUtils.importESModule(
     10  "resource://devtools/client/styleeditor/StyleEditorUI.sys.mjs"
     11 );
     12 var { getString } = ChromeUtils.importESModule(
     13  "resource://devtools/client/styleeditor/StyleEditorUtil.sys.mjs"
     14 );
     15 
     16 class StyleEditorPanel extends EventEmitter {
     17  constructor(panelWin, toolbox, commands) {
     18    super();
     19 
     20    this._toolbox = toolbox;
     21    this._commands = commands;
     22    this._panelWin = panelWin;
     23    this._panelDoc = panelWin.document;
     24 
     25    this._showError = this._showError.bind(this);
     26  }
     27  get panelWindow() {
     28    return this._panelWin;
     29  }
     30 
     31  /**
     32   * open is effectively an asynchronous constructor
     33   */
     34  async open(options) {
     35    // Initialize the CSS properties database.
     36    const { cssProperties } =
     37      await this._toolbox.target.getFront("cssProperties");
     38 
     39    // Initialize the UI
     40    this.UI = new StyleEditorUI(
     41      this._toolbox,
     42      this._commands,
     43      this._panelDoc,
     44      cssProperties
     45    );
     46    this.UI.on("error", this._showError);
     47    this.UI.on("reloaded", () => this.emit("reloaded"));
     48    await this.UI.initialize(options);
     49 
     50    return this;
     51  }
     52 
     53  /**
     54   * Show an error message from the style editor in the toolbox
     55   * notification box.
     56   *
     57   * @param  {string} data
     58   *         The parameters to customize the error message
     59   */
     60  _showError(data) {
     61    if (!this._toolbox) {
     62      // could get an async error after we've been destroyed
     63      return;
     64    }
     65 
     66    let errorMessage = getString(data.key);
     67    if (data.append) {
     68      errorMessage += " " + data.append;
     69    }
     70 
     71    const notificationBox = this._toolbox.getNotificationBox();
     72    const notification =
     73      notificationBox.getNotificationWithValue("styleeditor-error");
     74 
     75    let level = notificationBox.PRIORITY_CRITICAL_LOW;
     76    if (data.level === "info") {
     77      level = notificationBox.PRIORITY_INFO_LOW;
     78    } else if (data.level === "warning") {
     79      level = notificationBox.PRIORITY_WARNING_LOW;
     80    }
     81 
     82    if (!notification) {
     83      notificationBox.appendNotification(
     84        errorMessage,
     85        "styleeditor-error",
     86        "",
     87        level
     88      );
     89    }
     90  }
     91 
     92  /**
     93   * Select a stylesheet.
     94   *
     95   * @param {StyleSheetResource} stylesheet
     96   *        The resource for the stylesheet to find and select in editor.
     97   * @param {number} line
     98   *        Line number to jump to after selecting. One-indexed
     99   * @param {number} col
    100   *        Column number to jump to after selecting. One-indexed
    101   * @return {Promise}
    102   *         Promise that will resolve when the editor is selected and ready
    103   *         to be used.
    104   */
    105  selectStyleSheet(stylesheet, line, col) {
    106    if (!this.UI) {
    107      return null;
    108    }
    109 
    110    return this.UI.selectStyleSheet(stylesheet, line - 1, col ? col - 1 : 0);
    111  }
    112 
    113  /**
    114   * Given a location in an original file, open that file in the editor.
    115   *
    116   * @param {string} originalId
    117   *        The original "sourceId" returned from the sourcemap worker.
    118   * @param {number} line
    119   *        Line number to jump to after selecting. One-indexed
    120   * @param {number} col
    121   *        Column number to jump to after selecting. One-indexed
    122   * @return {Promise}
    123   *         Promise that will resolve when the editor is selected and ready
    124   *         to be used.
    125   */
    126  selectOriginalSheet(originalId, line, col) {
    127    if (!this.UI) {
    128      return null;
    129    }
    130 
    131    const originalSheet = this.UI.getOriginalSourceSheet(originalId);
    132    return this.UI.selectStyleSheet(originalSheet, line - 1, col ? col - 1 : 0);
    133  }
    134 
    135  getStylesheetResourceForGeneratedURL(url) {
    136    if (!this.UI) {
    137      return null;
    138    }
    139 
    140    return this.UI.getStylesheetResourceForGeneratedURL(url);
    141  }
    142 
    143  /**
    144   * Destroy the style editor.
    145   */
    146  destroy() {
    147    if (this._destroyed) {
    148      return;
    149    }
    150    this._destroyed = true;
    151 
    152    this._toolbox = null;
    153    this._panelWin = null;
    154    this._panelDoc = null;
    155 
    156    this.UI.destroy();
    157    this.UI = null;
    158  }
    159 }
    160 
    161 exports.StyleEditorPanel = StyleEditorPanel;