styleeditor-panel.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "use strict";
  5. var Services = require("Services");
  6. var promise = require("promise");
  7. var {Task} = require("devtools/shared/task");
  8. var {XPCOMUtils} = require("resource://gre/modules/XPCOMUtils.jsm");
  9. var EventEmitter = require("devtools/shared/event-emitter");
  10. var {StyleEditorUI} = require("resource://devtools/client/styleeditor/StyleEditorUI.jsm");
  11. var {getString} = require("resource://devtools/client/styleeditor/StyleEditorUtil.jsm");
  12. var {initCssProperties} = require("devtools/shared/fronts/css-properties");
  13. loader.lazyGetter(this, "StyleSheetsFront",
  14. () => require("devtools/shared/fronts/stylesheets").StyleSheetsFront);
  15. loader.lazyGetter(this, "StyleEditorFront",
  16. () => require("devtools/shared/fronts/styleeditor").StyleEditorFront);
  17. var StyleEditorPanel = function StyleEditorPanel(panelWin, toolbox) {
  18. EventEmitter.decorate(this);
  19. this._toolbox = toolbox;
  20. this._target = toolbox.target;
  21. this._panelWin = panelWin;
  22. this._panelDoc = panelWin.document;
  23. this.destroy = this.destroy.bind(this);
  24. this._showError = this._showError.bind(this);
  25. };
  26. exports.StyleEditorPanel = StyleEditorPanel;
  27. StyleEditorPanel.prototype = {
  28. get target() {
  29. return this._toolbox.target;
  30. },
  31. get panelWindow() {
  32. return this._panelWin;
  33. },
  34. /**
  35. * open is effectively an asynchronous constructor
  36. */
  37. open: Task.async(function* () {
  38. // We always interact with the target as if it were remote
  39. if (!this.target.isRemote) {
  40. yield this.target.makeRemote();
  41. }
  42. this.target.on("close", this.destroy);
  43. if (this.target.form.styleSheetsActor) {
  44. this._debuggee = StyleSheetsFront(this.target.client, this.target.form);
  45. } else {
  46. /* We're talking to a pre-Firefox 29 server-side */
  47. this._debuggee = StyleEditorFront(this.target.client, this.target.form);
  48. }
  49. // Initialize the CSS properties database.
  50. const {cssProperties} = yield initCssProperties(this._toolbox);
  51. // Initialize the UI
  52. this.UI = new StyleEditorUI(this._debuggee, this.target, this._panelDoc,
  53. cssProperties);
  54. this.UI.on("error", this._showError);
  55. yield this.UI.initialize();
  56. this.isReady = true;
  57. return this;
  58. }),
  59. /**
  60. * Show an error message from the style editor in the toolbox
  61. * notification box.
  62. *
  63. * @param {string} event
  64. * Type of event
  65. * @param {string} data
  66. * The parameters to customize the error message
  67. */
  68. _showError: function (event, data) {
  69. if (!this._toolbox) {
  70. // could get an async error after we've been destroyed
  71. return;
  72. }
  73. let errorMessage = getString(data.key);
  74. if (data.append) {
  75. errorMessage += " " + data.append;
  76. }
  77. let notificationBox = this._toolbox.getNotificationBox();
  78. let notification =
  79. notificationBox.getNotificationWithValue("styleeditor-error");
  80. let level = (data.level === "info") ?
  81. notificationBox.PRIORITY_INFO_LOW :
  82. notificationBox.PRIORITY_CRITICAL_LOW;
  83. if (!notification) {
  84. notificationBox.appendNotification(errorMessage, "styleeditor-error",
  85. "", level);
  86. }
  87. },
  88. /**
  89. * Select a stylesheet.
  90. *
  91. * @param {string} href
  92. * Url of stylesheet to find and select in editor
  93. * @param {number} line
  94. * Line number to jump to after selecting. One-indexed
  95. * @param {number} col
  96. * Column number to jump to after selecting. One-indexed
  97. * @return {Promise}
  98. * Promise that will resolve when the editor is selected and ready
  99. * to be used.
  100. */
  101. selectStyleSheet: function (href, line, col) {
  102. if (!this._debuggee || !this.UI) {
  103. return null;
  104. }
  105. return this.UI.selectStyleSheet(href, line - 1, col ? col - 1 : 0);
  106. },
  107. /**
  108. * Destroy the style editor.
  109. */
  110. destroy: function () {
  111. if (!this._destroyed) {
  112. this._destroyed = true;
  113. this._target.off("close", this.destroy);
  114. this._target = null;
  115. this._toolbox = null;
  116. this._panelWin = null;
  117. this._panelDoc = null;
  118. this._debuggee.destroy();
  119. this._debuggee = null;
  120. this.UI.destroy();
  121. this.UI = null;
  122. }
  123. return promise.resolve(null);
  124. },
  125. };
  126. XPCOMUtils.defineLazyGetter(StyleEditorPanel.prototype, "strings",
  127. function () {
  128. return Services.strings.createBundle(
  129. "chrome://devtools/locale/styleeditor.properties");
  130. });