-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathExtensionManagerDialog.js
More file actions
440 lines (385 loc) · 18.2 KB
/
Copy pathExtensionManagerDialog.js
File metadata and controls
440 lines (385 loc) · 18.2 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2013 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
define(function (require, exports, module) {
var _ = require("thirdparty/lodash"),
Mustache = require("thirdparty/mustache/mustache"),
Dialogs = require("widgets/Dialogs"),
DefaultDialogs = require("widgets/DefaultDialogs"),
Package = require("extensibility/Package"),
Strings = require("strings"),
StringUtils = require("utils/StringUtils"),
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
InstallExtensionDialog = require("extensibility/InstallExtensionDialog"),
ThemeManager = require("view/ThemeManager"),
AppInit = require("utils/AppInit"),
Async = require("utils/Async"),
KeyEvent = require("utils/KeyEvent"),
ExtensionManager = require("extensibility/ExtensionManager"),
ExtensionManagerView = require("extensibility/ExtensionManagerView").ExtensionManagerView,
ExtensionManagerViewModel = require("extensibility/ExtensionManagerViewModel"),
PreferencesManager = require("preferences/PreferencesManager"),
Metrics = require("utils/Metrics");
var dialogTemplate = require("text!htmlContent/extension-manager-dialog.html");
// bootstrap tabs component
require("widgets/bootstrap-tab");
var _activeTabIndex;
/**
* @private
* Triggers changes requested by the dialog UI.
*/
function _performChanges() {
// If an extension was removed or updated, prompt the user to quit Brackets.
var hasRemovedExtensions = ExtensionManager.hasExtensionsToRemove(),
hasUpdatedExtensions = ExtensionManager.hasExtensionsToUpdate(),
hasDisabledExtensions = ExtensionManager.hasExtensionsToDisable();
if (!hasRemovedExtensions && !hasUpdatedExtensions && !hasDisabledExtensions) {
return;
}
var buttonLabel = Strings.CHANGE_AND_RELOAD;
if (hasRemovedExtensions && !hasUpdatedExtensions && !hasDisabledExtensions) {
buttonLabel = Strings.REMOVE_AND_RELOAD;
} else if (hasUpdatedExtensions && !hasRemovedExtensions && !hasDisabledExtensions) {
buttonLabel = Strings.UPDATE_AND_RELOAD;
} else if (hasDisabledExtensions && !hasRemovedExtensions && !hasUpdatedExtensions) {
buttonLabel = Strings.DISABLE_AND_RELOAD;
}
var dlg = Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_CHANGE_EXTENSIONS,
Strings.CHANGE_AND_RELOAD_TITLE,
Strings.CHANGE_AND_RELOAD_MESSAGE,
[
{
className: Dialogs.DIALOG_BTN_CLASS_NORMAL,
id: Dialogs.DIALOG_BTN_CANCEL,
text: Strings.CANCEL
},
{
className: Dialogs.DIALOG_BTN_CLASS_PRIMARY,
id: Dialogs.DIALOG_BTN_OK,
text: buttonLabel
}
],
false
),
$dlg = dlg.getElement();
$dlg.one("buttonClick", function (e, buttonId) {
if (buttonId === Dialogs.DIALOG_BTN_OK) {
// Disable the dialog buttons so the user can't dismiss it,
// and show a message indicating that we're doing the updates,
// in case it takes a long time.
$dlg.find(".dialog-button").prop("disabled", true);
$dlg.find(".close").hide();
$dlg.find(".dialog-message")
.text(Strings.PROCESSING_EXTENSIONS)
.append("<span class='spinner inline spin'/>");
var removeExtensionsPromise,
updateExtensionsPromise,
disableExtensionsPromise,
removeErrors,
updateErrors,
disableErrors;
removeExtensionsPromise = ExtensionManager.removeMarkedExtensions()
.fail(function (errorArray) {
removeErrors = errorArray;
});
updateExtensionsPromise = ExtensionManager.updateExtensions()
.fail(function (errorArray) {
updateErrors = errorArray;
});
disableExtensionsPromise = ExtensionManager.disableMarkedExtensions()
.fail(function (errorArray) {
disableErrors = errorArray;
});
Async.waitForAll([removeExtensionsPromise, updateExtensionsPromise, disableExtensionsPromise], true)
.always(function () {
dlg.close();
})
.done(function () {
CommandManager.execute(Commands.APP_RELOAD);
})
.fail(function () {
var ids = [],
dialogs = [];
function nextDialog() {
var dialog = dialogs.shift();
if (dialog) {
Dialogs.showModalDialog(dialog.dialog, dialog.title, dialog.message)
.done(nextDialog);
} else {
// Even in case of error condition, we still have to reload
CommandManager.execute(Commands.APP_RELOAD);
}
}
if (removeErrors) {
removeErrors.forEach(function (errorObj) {
ids.push(errorObj.item);
});
dialogs.push({
dialog: DefaultDialogs.DIALOG_ID_ERROR,
title: Strings.EXTENSION_MANAGER_REMOVE,
message: StringUtils.format(Strings.EXTENSION_MANAGER_REMOVE_ERROR, ids.join(", "))
});
}
if (updateErrors) {
// This error case should be very uncommon.
// Just let the user know that we couldn't update
// this extension and log the errors to the console.
ids.length = 0;
updateErrors.forEach(function (errorObj) {
ids.push(errorObj.item);
if (errorObj.error && errorObj.error.forEach) {
console.error("Errors for", errorObj.item);
errorObj.error.forEach(function (error) {
console.error(Package.formatError(error));
});
} else {
console.error("Error for", errorObj.item, errorObj);
}
});
dialogs.push({
dialog: DefaultDialogs.DIALOG_ID_ERROR,
title: Strings.EXTENSION_MANAGER_UPDATE,
message: StringUtils.format(Strings.EXTENSION_MANAGER_UPDATE_ERROR, ids.join(", "))
});
}
if (disableErrors) {
ids.length = 0;
disableErrors.forEach(function (errorObj) {
ids.push(errorObj.item);
});
dialogs.push({
dialog: DefaultDialogs.DIALOG_ID_ERROR,
title: Strings.EXTENSION_MANAGER_DISABLE,
message: StringUtils.format(Strings.EXTENSION_MANAGER_DISABLE_ERROR, ids.join(", "))
});
}
nextDialog();
});
} else {
dlg.close();
ExtensionManager.cleanupUpdates();
ExtensionManager.unmarkAllForRemoval();
ExtensionManager.unmarkAllForDisabling();
}
});
}
/**
* @private
* Show a dialog that allows the user to browse and manage extensions.
*/
function _showDialog(which) {
Metrics.countEvent(Metrics.EVENT_TYPE.EXTENSIONS, "dialogue", "shown");
let dialog,
$dlg,
views = [],
$search,
$searchClear,
$modalDlg,
context = { Strings: Strings, showRegistry: !!brackets.config.extension_registry },
models = [],
originalTheme = ThemeManager.getCurrentTheme();
// Load registry only if the registry URL exists
if (context.showRegistry) {
models.push(new ExtensionManagerViewModel.RegistryViewModel());
models.push(new ExtensionManagerViewModel.ThemesViewModel());
}
models.push(new ExtensionManagerViewModel.InstalledViewModel());
models.push(new ExtensionManagerViewModel.DefaultViewModel());
function updateSearchDisabled() {
var model = models[_activeTabIndex],
searchDisabled = ($search.val() === "") &&
(!model.filterSet || model.filterSet.length === 0);
$search.prop("disabled", searchDisabled);
$searchClear.prop("disabled", searchDisabled);
return searchDisabled;
}
function clearSearch() {
$search.val("");
views.forEach(function (view, index) {
view.filter("");
$modalDlg.scrollTop(0);
});
if (!updateSearchDisabled()) {
$search.focus();
}
}
// Open the dialog
dialog = Dialogs.showModalDialogUsingTemplate(Mustache.render(dialogTemplate, context));
// On dialog close: clean up listeners & models, and commit changes
dialog.done(function () {
$(window.document).off(".extensionManager");
models.forEach(function (model) {
model.dispose();
});
_performChanges();
});
// Create the view.
$dlg = dialog.getElement();
$search = $(".search", $dlg);
$searchClear = $(".search-clear", $dlg);
$modalDlg = $(".modal-body", $dlg);
function setActiveTab($tab) {
if (models[_activeTabIndex]) {
models[_activeTabIndex].scrollPos = $modalDlg.scrollTop();
}
$tab.tab("show");
if (models[_activeTabIndex]) {
$modalDlg.scrollTop(models[_activeTabIndex].scrollPos || 0);
clearSearch();
if (_activeTabIndex === 2) {
$(".ext-sort-group").hide();
} else {
$(".ext-sort-group").show();
}
}
}
// Dialog tabs
$dlg.find(".nav-tabs a")
.on("click", function (event) {
setActiveTab($(this));
});
// Navigate through tabs via Ctrl-(Shift)-Tab
// (focus may be on document.body if text in extension listing clicked - see #9511)
$(window.document).on("keyup.extensionManager", function (event) {
if (event.keyCode === KeyEvent.DOM_VK_TAB && event.ctrlKey) {
var $tabs = $(".nav-tabs a", $dlg),
tabIndex = _activeTabIndex;
if (event.shiftKey) {
tabIndex--;
} else {
tabIndex++;
}
tabIndex %= $tabs.length;
setActiveTab($tabs.eq(tabIndex));
}
});
// Update & hide/show the notification overlay on a tab's icon, based on its model's notifyCount
function updateNotificationIcon(index) {
var model = models[index],
$notificationIcon = $dlg.find(".nav-tabs li").eq(index).find(".notification");
if (model.notifyCount) {
$notificationIcon.text(model.notifyCount);
$notificationIcon.show();
} else {
$notificationIcon.hide();
}
}
// Initialize models and create a view for each model
var modelInitPromise = Async.doInParallel(models, function (model, index) {
var view = new ExtensionManagerView(),
promise = view.initialize(model),
lastNotifyCount;
promise.always(function () {
views[index] = view;
lastNotifyCount = model.notifyCount;
updateNotificationIcon(index);
});
model.on("change", function () {
if (lastNotifyCount !== model.notifyCount) {
lastNotifyCount = model.notifyCount;
updateNotificationIcon(index);
}
});
return promise;
}, true);
modelInitPromise.always(function () {
$(".spinner", $dlg).remove();
views.forEach(function (view) {
view.$el.appendTo($modalDlg);
});
// Update search UI before new tab is shown
$("a[data-toggle='tab']", $dlg).each(function (index, tabElement) {
$(tabElement).on("show", function (event) {
_activeTabIndex = index;
// Focus the search input
if (!updateSearchDisabled()) {
$dlg.find(".search").focus();
}
});
});
// Filter the views when the user types in the search field.
var searchTimeoutID;
$dlg.on("input", ".search", function (e) {
clearTimeout(searchTimeoutID);
var query = $(this).val();
searchTimeoutID = setTimeout(function () {
views[_activeTabIndex].filter(query);
$modalDlg.scrollTop(0);
}, 200);
}).on("click", ".search-clear", clearSearch);
// Sort the extension list based on the current selected sorting criteria
$dlg.on("change", ".sort-extensions", function (e) {
var sortBy = $(this).val();
PreferencesManager.set("extensions.sort", sortBy);
models.forEach(function (model, index) {
if (index <= 1) {
model._setSortedExtensionList(ExtensionManager.extensions, index === 1);
views[index].filter($(".search").val());
}
});
});
// Disable the search field when there are no items in the model
models.forEach(function (model, index) {
model.on("change", function () {
if (_activeTabIndex === index) {
updateSearchDisabled();
}
});
});
var $activeTab = $dlg.find(".nav-tabs li.active a");
if ($activeTab.length) { // If there's already a tab selected, show it
$activeTab.parent().removeClass("active"); // workaround for bootstrap-tab
$activeTab.tab("show");
} else if ($("#toolbar-extension-manager").hasClass('updatesAvailable')) {
// Open dialog to Installed tab if extension updates are available
$dlg.find(".nav-tabs a.installed").tab("show");
} else { // Otherwise show the first tab
$dlg.find(".nav-tabs a:first").tab("show");
}
// If activeTab was explicitly selected by user,
// then check for the selection
// Or if there was an update available since activeTab.length would be 0,
// then check for updatesAvailable class in toolbar-extension-manager
if (($activeTab.length && $activeTab.hasClass("installed")) || (!$activeTab.length && $("#toolbar-extension-manager").hasClass('updatesAvailable'))) {
$(".ext-sort-group").hide();
} else {
$(".ext-sort-group").show();
}
});
// Handle the 'Install from URL' button.
$(".extension-manager-dialog .install-from-url")
.click(function () {
Metrics.countEvent(Metrics.EVENT_TYPE.EXTENSIONS, "install", "fromURL");
InstallExtensionDialog.showDialog().done(ExtensionManager.updateFromDownload);
});
if(which === "themes"){
$dlg.find(".nav-tabs a.themes").click();
}
return new $.Deferred().resolve(dialog).promise();
}
CommandManager.register(Strings.CMD_EXTENSION_MANAGER, Commands.FILE_EXTENSION_MANAGER, _showDialog, { supportsDesignMode: true });
AppInit.appReady(function () {
$("#toolbar-extension-manager").click(_showDialog);
});
// Unit tests
exports._performChanges = _performChanges;
});