This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathExtensionManagerView.js
More file actions
295 lines (258 loc) · 12.1 KB
/
ExtensionManagerView.js
File metadata and controls
295 lines (258 loc) · 12.1 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
/*global define, window, $, brackets, Mustache */
/*unittests: ExtensionManager*/
define(function (require, exports, module) {
"use strict";
var Strings = require("strings"),
NativeApp = require("utils/NativeApp"),
ExtensionManager = require("extensibility/ExtensionManager"),
registry_utils = require("extensibility/registry_utils"),
InstallExtensionDialog = require("extensibility/InstallExtensionDialog"),
CommandManager = require("command/CommandManager"),
Commands = require("command/Commands"),
itemTemplate = require("text!htmlContent/extension-manager-view-item.html");
/**
* @constructor
* Creates a view enabling the user to install and manage extensions. Must be initialized
* with initialize(). When the view is closed, dispose() must be called.
*/
function ExtensionManagerView() {
}
/**
* Initializes the view to show a set of extensions.
* @param {ExtensionManagerViewModel} model Model object containing extension data to view
* @return {$.Promise} a promise that's resolved once the view has been initialized. Never
* rejected.
*/
ExtensionManagerView.prototype.initialize = function (model) {
var self = this,
result = new $.Deferred();
this.model = model;
this._itemTemplate = Mustache.compile(itemTemplate);
this._itemViews = {};
this.$el = $("<div class='extension-list tab-pane' id='" + this.model.source + "'/>");
this._$emptyMessage = $("<div class='empty-message'/>")
.appendTo(this.$el);
this._$infoMessage = $("<div class='info-message'/>")
.appendTo(this.$el).html(this.model.infoMessage);
this._$table = $("<table class='table'/>").appendTo(this.$el);
this.model.initialize().done(function () {
self._setupEventHandlers();
}).always(function () {
self._render();
result.resolve();
});
return result.promise();
};
/**
* @type {jQueryObject}
* The root of the view's DOM tree.
*/
ExtensionManagerView.prototype.$el = null;
/**
* @type {Model}
* The view's model. Handles sorting and filtering of items in the view.
*/
ExtensionManagerView.prototype.model = null;
/**
* @type {jQueryObject}
* Element showing a message when there are no extensions.
*/
ExtensionManagerView.prototype._$emptyMessage = null;
/**
* @private
* @type {jQueryObject}
* The root of the table inside the view.
*/
ExtensionManagerView.prototype._$table = null;
/**
* @private
* @type {function} The compiled template we use for rendering items in the extension list.
*/
ExtensionManagerView.prototype._itemTemplate = null;
/**
* @private
* @type {Object.<string, jQueryObject>}
* The individual views for each item, keyed by the extension ID.
*/
ExtensionManagerView.prototype._itemViews = null;
/**
* @private
* Attaches our event handlers. We wait to do this until we've fully fetched the extension list.
*/
ExtensionManagerView.prototype._setupEventHandlers = function () {
var self = this;
// Listen for model data and filter changes.
$(this.model)
.on("filter", function () {
self._render();
})
.on("change", function (e, id) {
var extensions = self.model.extensions,
$oldItem = self._itemViews[id];
self._updateMessage();
if (self.model.filterSet.indexOf(id) === -1) {
// This extension is not in the filter set. Remove it from the view if we
// were rendering it previously.
if ($oldItem) {
$oldItem.remove();
delete self._itemViews[id];
}
} else {
// Render the item, replacing the old item if we had previously rendered it.
var $newItem = self._renderItem(extensions[id], self.model._getEntry(id));
if ($oldItem) {
$oldItem.replaceWith($newItem);
self._itemViews[id] = $newItem;
}
}
});
// UI event handlers
this.$el
.on("click", "a", function (e) {
var $target = $(e.target);
if ($target.hasClass("undo-remove")) {
ExtensionManager.markForRemoval($target.attr("data-extension-id"), false);
} else if ($target.hasClass("remove")) {
ExtensionManager.markForRemoval($target.attr("data-extension-id"), true);
} else if ($target.hasClass("undo-update")) {
ExtensionManager.removeUpdate($target.attr("data-extension-id"));
}
})
.on("click", "button.install", function (e) {
self._installUsingDialog($(e.target).attr("data-extension-id"));
})
.on("click", "button.update", function (e) {
self._installUsingDialog($(e.target).attr("data-extension-id"), true);
})
.on("click", "button.remove", function (e) {
ExtensionManager.markForRemoval($(e.target).attr("data-extension-id"), true);
});
};
/**
* @private
* Renders the view for a single extension entry.
* @param {Object} entry The extension entry to render.
* @param {Object} info The extension's metadata.
* @return {jQueryObject} The rendered node as a jQuery object.
*/
ExtensionManagerView.prototype._renderItem = function (entry, info) {
// Create a Mustache context object containing the entry data and our helper functions.
// Start with the basic info from the given entry, either the installation info or the
// registry info depending on what we're listing.
var context = $.extend({}, info);
// Normally we would merge the strings into the context we're passing into the template,
// but since we're instantiating the template for every item, it seems wrong to take the hit
// of copying all the strings into the context, so we just make it a subfield.
context.Strings = Strings;
// Calculate various bools, since Mustache doesn't let you use expressions and interprets
// arrays as iteration contexts.
context.isInstalled = !!entry.installInfo;
context.failedToStart = (entry.installInfo && entry.installInfo.status === ExtensionManager.START_FAILED);
context.hasVersionInfo = !!info.versions;
var compatInfo = ExtensionManager.getCompatibilityInfo(info, brackets.metadata.apiVersion);
context.isCompatible = compatInfo.isCompatible;
context.isMarkedForRemoval = ExtensionManager.isMarkedForRemoval(info.metadata.name);
context.isMarkedForUpdate = ExtensionManager.isMarkedForUpdate(info.metadata.name);
context.requiresNewer = compatInfo.requiresNewer;
context.showInstallButton = (this.model.source === this.model.SOURCE_REGISTRY) && !context.updateAvailable;
context.showUpdateButton = context.updateAvailable && !context.isMarkedForUpdate && !context.isMarkedForRemoval;
context.allowInstall = context.isCompatible && !context.isInstalled;
context.allowRemove = (entry.installInfo && entry.installInfo.locationType === ExtensionManager.LOCATION_USER);
context.allowUpdate = context.isCompatible && context.isInstalled &&
!context.isMarkedForUpdate && !context.isMarkedForRemoval;
context.removalAllowed = this.model.source === "installed" &&
!context.failedToStart && !context.isMarkedForUpdate && !context.isMarkedForRemoval;
// Copy over helper functions that we share with the registry app.
["lastVersionDate", "authorInfo"].forEach(function (helper) {
context[helper] = registry_utils[helper];
});
return $(this._itemTemplate(context));
};
/**
* @private
* Display an optional message (hiding the extension list if displayed)
* @return {boolean} Returns true if a message is displayed
*/
ExtensionManagerView.prototype._updateMessage = function () {
if (this.model.message) {
this._$emptyMessage.css("display", "block");
this._$emptyMessage.html(this.model.message);
this._$infoMessage.css("display", "none");
this._$table.css("display", "none");
return true;
} else {
this._$emptyMessage.css("display", "none");
this._$infoMessage.css("display", this.model.infoMessage ? "block" : "none");
this._$table.css("display", "");
return false;
}
};
/**
* @private
* Renders the extension entry table based on the model's current filter set. Will create
* new items for entries that haven't yet been rendered, but will not re-render existing items.
*/
ExtensionManagerView.prototype._render = function () {
var self = this,
$item;
this._$table.empty();
this._updateMessage();
this.model.filterSet.forEach(function (id) {
var $item = self._itemViews[id];
if (!$item) {
$item = self._renderItem(self.model.extensions[id], self.model._getEntry(id));
self._itemViews[id] = $item;
}
$item.appendTo(self._$table);
});
$(this).triggerHandler("render");
};
/**
* @private
* Install the extension with the given ID using the install dialog.
* @param {string} id ID of the extension to install.
*/
ExtensionManagerView.prototype._installUsingDialog = function (id, _isUpdate) {
var entry = this.model.extensions[id];
if (entry && entry.registryInfo) {
var url = ExtensionManager.getExtensionURL(id, entry.registryInfo.metadata.version);
// TODO: this should set .done on the returned promise
if (_isUpdate) {
InstallExtensionDialog.updateUsingDialog(url).done(ExtensionManager.updateFromDownload);
} else {
InstallExtensionDialog.installUsingDialog(url);
}
}
};
/**
* Filters the contents of the view.
* @param {string} query The query to filter by.
*/
ExtensionManagerView.prototype.filter = function (query) {
this.model.filter(query);
};
exports.ExtensionManagerView = ExtensionManagerView;
});