browser/devtools/styleeditor/styleeditor-panel.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 const {Cc, Ci, Cu, Cr} = require("chrome");
michael@0 8
michael@0 9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 10 Cu.import("resource://gre/modules/Services.jsm");
michael@0 11
michael@0 12 let {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
michael@0 13 let EventEmitter = require("devtools/toolkit/event-emitter");
michael@0 14
michael@0 15 Cu.import("resource:///modules/devtools/StyleEditorUI.jsm");
michael@0 16 Cu.import("resource:///modules/devtools/StyleEditorUtil.jsm");
michael@0 17
michael@0 18 loader.lazyGetter(this, "StyleSheetsFront",
michael@0 19 () => require("devtools/server/actors/stylesheets").StyleSheetsFront);
michael@0 20
michael@0 21 loader.lazyGetter(this, "StyleEditorFront",
michael@0 22 () => require("devtools/server/actors/styleeditor").StyleEditorFront);
michael@0 23
michael@0 24 this.StyleEditorPanel = function StyleEditorPanel(panelWin, toolbox) {
michael@0 25 EventEmitter.decorate(this);
michael@0 26
michael@0 27 this._toolbox = toolbox;
michael@0 28 this._target = toolbox.target;
michael@0 29 this._panelWin = panelWin;
michael@0 30 this._panelDoc = panelWin.document;
michael@0 31
michael@0 32 this.destroy = this.destroy.bind(this);
michael@0 33 this._showError = this._showError.bind(this);
michael@0 34 }
michael@0 35
michael@0 36 exports.StyleEditorPanel = StyleEditorPanel;
michael@0 37
michael@0 38 StyleEditorPanel.prototype = {
michael@0 39 get target() this._toolbox.target,
michael@0 40
michael@0 41 get panelWindow() this._panelWin,
michael@0 42
michael@0 43 /**
michael@0 44 * open is effectively an asynchronous constructor
michael@0 45 */
michael@0 46 open: function() {
michael@0 47 let deferred = promise.defer();
michael@0 48
michael@0 49 let targetPromise;
michael@0 50 // We always interact with the target as if it were remote
michael@0 51 if (!this.target.isRemote) {
michael@0 52 targetPromise = this.target.makeRemote();
michael@0 53 } else {
michael@0 54 targetPromise = promise.resolve(this.target);
michael@0 55 }
michael@0 56
michael@0 57 targetPromise.then(() => {
michael@0 58 this.target.on("close", this.destroy);
michael@0 59
michael@0 60 if (this.target.form.styleSheetsActor) {
michael@0 61 this._debuggee = StyleSheetsFront(this.target.client, this.target.form);
michael@0 62 }
michael@0 63 else {
michael@0 64 /* We're talking to a pre-Firefox 29 server-side */
michael@0 65 this._debuggee = StyleEditorFront(this.target.client, this.target.form);
michael@0 66 }
michael@0 67 this.UI = new StyleEditorUI(this._debuggee, this.target, this._panelDoc);
michael@0 68 this.UI.initialize().then(() => {
michael@0 69 this.UI.on("error", this._showError);
michael@0 70
michael@0 71 this.isReady = true;
michael@0 72
michael@0 73 deferred.resolve(this);
michael@0 74 });
michael@0 75 }, console.error);
michael@0 76
michael@0 77 return deferred.promise;
michael@0 78 },
michael@0 79
michael@0 80 /**
michael@0 81 * Show an error message from the style editor in the toolbox
michael@0 82 * notification box.
michael@0 83 *
michael@0 84 * @param {string} event
michael@0 85 * Type of event
michael@0 86 * @param {string} code
michael@0 87 * Error code of error to report
michael@0 88 * @param {string} message
michael@0 89 * Extra message to append to error message
michael@0 90 */
michael@0 91 _showError: function(event, code, message) {
michael@0 92 if (!this._toolbox) {
michael@0 93 // could get an async error after we've been destroyed
michael@0 94 return;
michael@0 95 }
michael@0 96
michael@0 97 let errorMessage = _(code);
michael@0 98 if (message) {
michael@0 99 errorMessage += " " + message;
michael@0 100 }
michael@0 101
michael@0 102 let notificationBox = this._toolbox.getNotificationBox();
michael@0 103 let notification = notificationBox.getNotificationWithValue("styleeditor-error");
michael@0 104 if (!notification) {
michael@0 105 notificationBox.appendNotification(errorMessage,
michael@0 106 "styleeditor-error", "", notificationBox.PRIORITY_CRITICAL_LOW);
michael@0 107 }
michael@0 108 },
michael@0 109
michael@0 110 /**
michael@0 111 * Select a stylesheet.
michael@0 112 *
michael@0 113 * @param {string} href
michael@0 114 * Url of stylesheet to find and select in editor
michael@0 115 * @param {number} line
michael@0 116 * Line number to jump to after selecting. One-indexed
michael@0 117 * @param {number} col
michael@0 118 * Column number to jump to after selecting. One-indexed
michael@0 119 */
michael@0 120 selectStyleSheet: function(href, line, col) {
michael@0 121 if (!this._debuggee || !this.UI) {
michael@0 122 return;
michael@0 123 }
michael@0 124 this.UI.selectStyleSheet(href, line - 1, col ? col - 1 : 0);
michael@0 125 },
michael@0 126
michael@0 127 /**
michael@0 128 * Destroy the style editor.
michael@0 129 */
michael@0 130 destroy: function() {
michael@0 131 if (!this._destroyed) {
michael@0 132 this._destroyed = true;
michael@0 133
michael@0 134 this._target.off("close", this.destroy);
michael@0 135 this._target = null;
michael@0 136 this._toolbox = null;
michael@0 137 this._panelDoc = null;
michael@0 138 this._debuggee.destroy();
michael@0 139 this._debuggee = null;
michael@0 140
michael@0 141 this.UI.destroy();
michael@0 142 }
michael@0 143
michael@0 144 return promise.resolve(null);
michael@0 145 },
michael@0 146 }
michael@0 147
michael@0 148 XPCOMUtils.defineLazyGetter(StyleEditorPanel.prototype, "strings",
michael@0 149 function () {
michael@0 150 return Services.strings.createBundle(
michael@0 151 "chrome://browser/locale/devtools/styleeditor.properties");
michael@0 152 });

mercurial