summaryrefslogtreecommitdiffstats
path: root/devtools/shared/fronts/styleeditor.js
blob: 5feb6df1b6367d9c90ce44359860cd71d28c26ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const { SimpleStringFront } = require("devtools/shared/fronts/string");
const { Front, FrontClassWithSpec } = require("devtools/shared/protocol");
const {
  oldStyleSheetSpec,
  styleEditorSpec
} = require("devtools/shared/specs/styleeditor");
const promise = require("promise");
const defer = require("devtools/shared/defer");
const events = require("sdk/event/core");

/**
 * StyleSheetFront is the client-side counterpart to a StyleSheetActor.
 */
const OldStyleSheetFront = FrontClassWithSpec(oldStyleSheetSpec, {
  initialize: function (conn, form, ctx, detail) {
    Front.prototype.initialize.call(this, conn, form, ctx, detail);

    this._onPropertyChange = this._onPropertyChange.bind(this);
    events.on(this, "property-change", this._onPropertyChange);
  },

  destroy: function () {
    events.off(this, "property-change", this._onPropertyChange);

    Front.prototype.destroy.call(this);
  },

  _onPropertyChange: function (property, value) {
    this._form[property] = value;
  },

  form: function (form, detail) {
    if (detail === "actorid") {
      this.actorID = form;
      return;
    }
    this.actorID = form.actor;
    this._form = form;
  },

  getText: function () {
    let deferred = defer();

    events.once(this, "source-load", (source) => {
      let longStr = new SimpleStringFront(source);
      deferred.resolve(longStr);
    });
    this.fetchSource();

    return deferred.promise;
  },

  getOriginalSources: function () {
    return promise.resolve([]);
  },

  get href() {
    return this._form.href;
  },
  get nodeHref() {
    return this._form.nodeHref;
  },
  get disabled() {
    return !!this._form.disabled;
  },
  get title() {
    return this._form.title;
  },
  get isSystem() {
    return this._form.system;
  },
  get styleSheetIndex() {
    return this._form.styleSheetIndex;
  },
  get ruleCount() {
    return this._form.ruleCount;
  }
});

exports.OldStyleSheetFront = OldStyleSheetFront;

/**
 * The corresponding Front object for the StyleEditorActor.
 */
const StyleEditorFront = FrontClassWithSpec(styleEditorSpec, {
  initialize: function (client, tabForm) {
    Front.prototype.initialize.call(this, client);
    this.actorID = tabForm.styleEditorActor;
    this.manage(this);
  },

  getStyleSheets: function () {
    let deferred = defer();

    events.once(this, "document-load", (styleSheets) => {
      deferred.resolve(styleSheets);
    });
    this.newDocument();

    return deferred.promise;
  },

  addStyleSheet: function (text) {
    return this.newStyleSheet(text);
  }
});

exports.StyleEditorFront = StyleEditorFront;