styleeditor.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. const { SimpleStringFront } = require("devtools/shared/fronts/string");
  6. const { Front, FrontClassWithSpec } = require("devtools/shared/protocol");
  7. const {
  8. oldStyleSheetSpec,
  9. styleEditorSpec
  10. } = require("devtools/shared/specs/styleeditor");
  11. const promise = require("promise");
  12. const defer = require("devtools/shared/defer");
  13. const events = require("sdk/event/core");
  14. /**
  15. * StyleSheetFront is the client-side counterpart to a StyleSheetActor.
  16. */
  17. const OldStyleSheetFront = FrontClassWithSpec(oldStyleSheetSpec, {
  18. initialize: function (conn, form, ctx, detail) {
  19. Front.prototype.initialize.call(this, conn, form, ctx, detail);
  20. this._onPropertyChange = this._onPropertyChange.bind(this);
  21. events.on(this, "property-change", this._onPropertyChange);
  22. },
  23. destroy: function () {
  24. events.off(this, "property-change", this._onPropertyChange);
  25. Front.prototype.destroy.call(this);
  26. },
  27. _onPropertyChange: function (property, value) {
  28. this._form[property] = value;
  29. },
  30. form: function (form, detail) {
  31. if (detail === "actorid") {
  32. this.actorID = form;
  33. return;
  34. }
  35. this.actorID = form.actor;
  36. this._form = form;
  37. },
  38. getText: function () {
  39. let deferred = defer();
  40. events.once(this, "source-load", (source) => {
  41. let longStr = new SimpleStringFront(source);
  42. deferred.resolve(longStr);
  43. });
  44. this.fetchSource();
  45. return deferred.promise;
  46. },
  47. getOriginalSources: function () {
  48. return promise.resolve([]);
  49. },
  50. get href() {
  51. return this._form.href;
  52. },
  53. get nodeHref() {
  54. return this._form.nodeHref;
  55. },
  56. get disabled() {
  57. return !!this._form.disabled;
  58. },
  59. get title() {
  60. return this._form.title;
  61. },
  62. get isSystem() {
  63. return this._form.system;
  64. },
  65. get styleSheetIndex() {
  66. return this._form.styleSheetIndex;
  67. },
  68. get ruleCount() {
  69. return this._form.ruleCount;
  70. }
  71. });
  72. exports.OldStyleSheetFront = OldStyleSheetFront;
  73. /**
  74. * The corresponding Front object for the StyleEditorActor.
  75. */
  76. const StyleEditorFront = FrontClassWithSpec(styleEditorSpec, {
  77. initialize: function (client, tabForm) {
  78. Front.prototype.initialize.call(this, client);
  79. this.actorID = tabForm.styleEditorActor;
  80. this.manage(this);
  81. },
  82. getStyleSheets: function () {
  83. let deferred = defer();
  84. events.once(this, "document-load", (styleSheets) => {
  85. deferred.resolve(styleSheets);
  86. });
  87. this.newDocument();
  88. return deferred.promise;
  89. },
  90. addStyleSheet: function (text) {
  91. return this.newStyleSheet(text);
  92. }
  93. });
  94. exports.StyleEditorFront = StyleEditorFront;