summaryrefslogtreecommitdiff
path: root/devtools/client/projecteditor/lib/project.js
blob: 8e0a8802d6cff021eb4c576c2b40e3bf9a36f31c (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* 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/. */

const { Cu } = require("chrome");
const { Class } = require("sdk/core/heritage");
const { EventTarget } = require("sdk/event/target");
const { emit } = require("sdk/event/core");
const { scope, on, forget } = require("devtools/client/projecteditor/lib/helpers/event");
const prefs = require("sdk/preferences/service");
const { LocalStore } = require("devtools/client/projecteditor/lib/stores/local");
const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {});
const { Task } = require("devtools/shared/task");
const promise = require("promise");
const { TextEncoder, TextDecoder } = require("sdk/io/buffer");
const url = require("sdk/url");

const gDecoder = new TextDecoder();
const gEncoder = new TextEncoder();

/**
 * A Project keeps track of the opened folders using LocalStore
 * objects.  Resources are generally requested from the project,
 * even though the Store is actually keeping track of them.
 *
 *
 * This object emits the following events:
 *   - "refresh-complete": After all stores have been refreshed from disk.
 *   - "store-added": When a store has been added to the project.
 *   - "store-removed": When a store has been removed from the project.
 *   - "resource-added": When a resource has been added to one of the stores.
 *   - "resource-removed": When a resource has been removed from one of the stores.
 */
var Project = Class({
  extends: EventTarget,

  /**
   * Intialize the Project.
   *
   * @param Object options
   *               Options to be passed into Project.load function
   */
  initialize: function (options) {
    this.localStores = new Map();

    this.load(options);
  },

  destroy: function () {
    // We are removing the store because the project never gets persisted.
    // There may need to be separate destroy functionality that doesn't remove
    // from project if this is saved to DB.
    this.removeAllStores();
  },

  toString: function () {
    return "[Project] " + this.name;
  },

  /**
   * Load a project given metadata about it.
   *
   * @param Object options
   *               Information about the project, containing:
   *                id: An ID (currently unused, but could be used for saving)
   *                name: The display name of the project
   *                directories: An array of path strings to load
   */
  load: function (options) {
    this.id = options.id;
    this.name = options.name || "Untitled";

    let paths = new Set(options.directories.map(name => OS.Path.normalize(name)));

    for (let [path, store] of this.localStores) {
      if (!paths.has(path)) {
        this.removePath(path);
      }
    }

    for (let path of paths) {
      this.addPath(path);
    }
  },

  /**
   * Refresh all project stores from disk
   *
   * @returns Promise
   *          A promise that resolves when everything has been refreshed.
   */
  refresh: function () {
    return Task.spawn(function* () {
      for (let [path, store] of this.localStores) {
        yield store.refresh();
      }
      emit(this, "refresh-complete");
    }.bind(this));
  },


  /**
   * Fetch a resource from the backing storage system for the store.
   *
   * @param string path
   *               The path to fetch
   * @param Object options
   *               "create": bool indicating whether to create a file if it does not exist.
   * @returns Promise
   *          A promise that resolves with the Resource.
   */
  resourceFor: function (path, options) {
    let store = this.storeContaining(path);
    return store.resourceFor(path, options);
  },

  /**
   * Get every resource used inside of the project.
   *
   * @returns Array<Resource>
   *          A list of all Resources in all Stores.
   */
  allResources: function () {
    let resources = [];
    for (let store of this.allStores()) {
      resources = resources.concat(store.allResources());
    }
    return resources;
  },

  /**
   * Get every Path used inside of the project.
   *
   * @returns generator-iterator<Store>
   *          A list of all Stores
   */
  allStores: function* () {
    for (let [path, store] of this.localStores) {
      yield store;
    }
  },

  /**
   * Get every file path used inside of the project.
   *
   * @returns Array<string>
   *          A list of all file paths
   */
  allPaths: function () {
    return [...this.localStores.keys()];
  },

  /**
   * Get the store that contains a path.
   *
   * @returns Store
   *          The store, if any.  Will return null if no store
   *          contains the given path.
   */
  storeContaining: function (path) {
    let containingStore = null;
    for (let store of this.allStores()) {
      if (store.contains(path)) {
        // With nested projects, the final containing store will be returned.
        containingStore = store;
      }
    }
    return containingStore;
  },

  /**
   * Add a store at the current path.  If a store already exists
   * for this path, then return it.
   *
   * @param string path
   * @returns LocalStore
   */
  addPath: function (path) {
    if (!this.localStores.has(path)) {
      this.addLocalStore(new LocalStore(path));
    }
    return this.localStores.get(path);
  },

  /**
   * Remove a store for a given path.
   *
   * @param string path
   */
  removePath: function (path) {
    this.removeLocalStore(this.localStores.get(path));
  },


  /**
   * Add the given Store to the project.
   * Fires a 'store-added' event on the project.
   *
   * @param Store store
   */
  addLocalStore: function (store) {
    store.canPair = true;
    this.localStores.set(store.path, store);

    // Originally StoreCollection.addStore
    on(this, store, "resource-added", (resource) => {
      emit(this, "resource-added", resource);
    });
    on(this, store, "resource-removed", (resource) => {
      emit(this, "resource-removed", resource);
    });

    emit(this, "store-added", store);
  },


  /**
   * Remove all of the Stores belonging to the project.
   */
  removeAllStores: function () {
    for (let store of this.allStores()) {
      this.removeLocalStore(store);
    }
  },

  /**
   * Remove the given Store from the project.
   * Fires a 'store-removed' event on the project.
   *
   * @param Store store
   */
  removeLocalStore: function (store) {
    // XXX: tree selection should be reset if active element is affected by
    // the store being removed
    if (store) {
      this.localStores.delete(store.path);
      forget(this, store);
      emit(this, "store-removed", store);
      store.destroy();
    }
  }
});

exports.Project = Project;