-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
executable file
·363 lines (310 loc) · 12.8 KB
/
main.js
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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, Mustache */
/**
brackets-cardboard Brackets Cardboard Extension
Manage packages/dependencies for your project.
Copyright 2014 Kyle Hornberg
LICENSE Apache 2.0
*/
define(function (require, exports, module) {
'use strict';
// Modules
var CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
WorkspaceManager = brackets.getModule("view/WorkspaceManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
AppInit = brackets.getModule("utils/AppInit"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
_ = brackets.getModule("thirdparty/lodash"),
// Setup Extension
moduleDirectory = ExtensionUtils.getModulePath(module),
managerDirectory = moduleDirectory + "modules/managers",
// Extension modules
Interface = require("modules/Interface"),
Strings = require("strings"),
Result = require("modules/Result"),
Status = require("modules/Status"),
deferredReduce = require("modules/deferredReduce"),
isUri = require("modules/isUri"),
// Extension variables
COMMAND_ID = "brackets-cardboard.cardboardTogglePanel",
$icon = $( "<a href='#' title='" + Strings.EXTENSION_NAME + "' class='brackets-cardboard-icon'></a>" ),
panel = null;
// Load CSS
ExtensionUtils.loadStyleSheet(module, "css/brackets-cardboard.css");
ExtensionUtils.loadStyleSheet(module, "css/font-awesome.min.css");
// UI Methods
/**
* Show the cardboard panel
*/
function cardboardTogglePanel(toggle) {
if(panel.isVisible() || toggle) {
panel.hide();
$icon.removeClass("active");
CommandManager.get(COMMAND_ID).setChecked(false);
}
else {
panel.show();
$icon.addClass("active");
$(".brackets-cardboard-search input").focus();
CommandManager.get(COMMAND_ID).setChecked(true);
}
}
/**
* Adds available managers to the search list
*/
function listManagers() {
$.when.apply($, Interface.getAvailable()).then(function () {
var args = arguments,
managers = [];
for (var i = args.length - 1; i >= 0; i--) {
managers.push(args[i]);
}
var template = require("text!html/managers.html"),
templateData = _.merge({"available" : managers }, Strings),
templateHtml = Mustache.render(template, templateData);
$('#brackets-cardboard-managers').html(templateHtml);
});
}
/**
* Select a manager
*/
function selectManager() {
// Changes the manager dropdown text and makes it look like a select box
var manager = $(this).attr("data-id"),
text = $(this).text();
// Change text
$(this).parent().prev().html(text + ' <span class="caret"></span>');
// Change data-id
$(this).parent().prev().attr('data-id', manager);
}
/**
* Adds results to the table on the cardboard panel
* @param {Object} results Object with key "results" of array of Results
* @param {String} selector jQuery selector of DOM object to update
*/
function addResults(results) {
var template = require("text!html/results.html"),
templateInstallButton = require("text!html/installButton.html"),
templateInstalledButtons = require("text!html/installedButtons.html"),
templateData = _.merge(results, Strings),
templatePartials = { installButton : templateInstallButton, installedButtons : templateInstalledButtons},
templateHtml = Mustache.render(template, templateData, templatePartials);
// $showButton = $('#brackets-cardboard-show');
$(".brackets-cardboard-table tbody").append(templateHtml);
// $showButton.html(Strings.HIDE_INSTALLED);
}
function addStatus(status) {
var template = require("text!html/status.html"),
templateInstallButton = require("text!html/installButton.html"),
templateInstalledButtons = require("text!html/installedButtons.html"),
templateData = _.merge(status, Strings),
templatePartials = { installButton : templateInstallButton, installedButtons : templateInstalledButtons},
templateHtml = Mustache.render(template, templateData, templatePartials);
$(".brackets-cardboard-table tbody").append(templateHtml);
}
/**
* Updates a single result on the cardboard results table
* @param {Array} status Status object
*/
function updateResult(status) {
var template,
templateData = Strings,
templateHtml,
$result = $("tr[data-id='" + status.id + "']");
switch(status.status) {
case "installed":
template = require("text!html/installedButtons.html");
$result.removeClass();
$result.addClass('brackets-cardboard-result-installed');
break;
case "updated":
template = require("text!html/installedButtons.html");
$result.removeClass();
$result.addClass('brackets-cardboard-result-installed');
break;
case "uninstalled":
template = require("text!html/installButton.html");
$result.removeClass();
break;
case "error":
addError(status, $result);
return;
default:
template = require("text!html/installButton.html");
$result.removeClass();
}
templateHtml = Mustache.render(template, templateData);
// Add button(s)
$('td:last-child', $result).html(templateHtml);
}
/**
* Adds error to the cardboard panel
* @param {Status} status Status from manager
* @param {jQuery Object} $result Package row that generated the error
*/
function addError(status, $result) {
var template = require("text!html/error.html"),
templateData = _.merge(status, Strings),
templateHtml = Mustache.render(template, templateData);
if($result.length > 0) {
$result.removeClass();
$result.addClass('brackets-cardboard-result-error');
// Add error row
$($result).after(templateHtml);
} else {
$(".brackets-cardboard-table tbody").append(templateHtml);
}
//Remove loading class
$result.find(".btn-loading").removeClass('btn-loading');
}
/**
* Search for a package
* @param {event} event jQuery event
*/
function search(event) {
var query = $(this).val();
if(event.which === 13 && query !== "") {
var manager = $("#brackets-cardboard-managers .dropdown").attr("data-id"),
uri = isUri(query);
clearPanel();
$('.brackets-cardboard-search input').addClass('brackets-cardboard-spinner'); // start spinner
if (uri && manager !== undefined) {
install(query, manager);
return;
} else if (uri && manager === undefined) {
$('.brackets-cardboard-search input').removeClass('brackets-cardboard-spinner'); //stop spinner
return;
}
if (manager === undefined) {
deferredReduce(Interface.search(query), function (results) {
var obj = { "results" : results };
addResults(obj);
//TODO stops when first manager resolves
$('.brackets-cardboard-search input').removeClass('brackets-cardboard-spinner'); //stop spinner
});
} else {
deferredReduce(Interface.search(manager, query), function (results) {
var obj = { "results" : results };
addResults(obj);
$('.brackets-cardboard-search input').removeClass('brackets-cardboard-spinner'); //stop spinner
});
}
}
}
/**
* Install a package
* @param {string} queryUri Install URI string from search box
* @param {string} uriManager Manager to use to install uri package. From manager list.
*/
function install(queryUri, uriManager) {
var uri = (typeof queryUri === "string") ? queryUri : false,
id = uri || $(this).parents("tr").attr("data-id"),
manager = uriManager || $(this).parents("tr").attr("data-manager");
if (uri) {
deferredReduce(Interface.install(manager, id), function (status) {
if (status[0].status === "installed") {
status[0].button = "installed";
}
addStatus({"statuses": status});
$('.brackets-cardboard-search input').removeClass('brackets-cardboard-spinner'); //stop spinner
});
} else {
$(this).addClass('btn-loading');
deferredReduce(Interface.install(manager, id), function (status) {
updateResult(status[0]);
});
}
}
/**
* Update a package
*/
function update() {
var id = $(this).parents("tr").attr("data-id"),
manager = $(this).parents("tr").attr("data-manager");
deferredReduce(Interface.update(manager, id), function (status) {
updateResult(status[0]);
});
}
/**
* Uninstall a package
*/
function uninstall() {
var id = $(this).parents("tr").attr("data-id"),
manager = $(this).parents("tr").attr("data-manager");
$(this).addClass('btn-loading');
deferredReduce(Interface.uninstall(manager, id), function (status) {
updateResult(status[0]);
});
}
/**
* Shows installed packages
*/
function show() {
var $installed = $(".brackets-cardboard-result-installed, .brackets-cardboard-result-update"),
$showButton = $("#brackets-cardboard-show"),
rows = ($(".brackets-cardboard-table tr").length > 0) ? true : false;
// results are present (eg. a search performed)
if (rows) {
$installed.toggle();
// toggle button
if ($showButton.html() === Strings.HIDE_INSTALLED) {
$(this).html(Strings.SHOW_INSTALLED);
} else {
$(this).html(Strings.HIDE_INSTALLED);
}
} else { // show only the installed packages
deferredReduce(Interface.getInstalled(), function (results) {
var obj = { "results" : results };
addResults(obj);
});
}
}
/**
* Clears panel
*/
function clearPanel() {
$(".brackets-cardboard-table tr").remove();
$('.brackets-cardboard-search input').removeClass('brackets-cardboard-spinner'); //stop spinner
}
/**
* Adds the cardboard panel to brackets
*/
function addPanel() {
var template = require("text!html/panel.html");
var panelHtml = Mustache.render(template, Strings);
panel = WorkspaceManager.createBottomPanel(COMMAND_ID, $(panelHtml), 200);
// Listeners on the panel
var $cardboardPanel = $("#brackets-cardboard");
$cardboardPanel
.on( "click", ".brackets-cardboard-close", function () {
cardboardTogglePanel(false);
})
.on( "click", ".brackets-cardboard-close-error", function () {
$(this).parents("tr").remove();
})
.on( "click", "#brackets-cardboard-show", show)
.on( "keydown", ".brackets-cardboard-search input", search)
.on( "click", ".brackets-cardboard-manager", selectManager)
.on( "click", ".brackets-cardboard-install", install)
.on( "click", ".brackets-cardboard-update", update)
.on( "click", ".brackets-cardboard-uninstall", uninstall)
.on( "click", ".brackets-cardboard-clear", clearPanel);
}
function init () {
addPanel();
listManagers();
}
// Listener for toolbar icon
$icon.click(function () {
CommandManager.execute(COMMAND_ID);
}).appendTo("#main-toolbar .buttons");
// View menu
// TODO remove?
CommandManager.register(Strings.MENU_NAME, COMMAND_ID, cardboardTogglePanel);
var menu = Menus.getMenu(Menus.AppMenuBar.VIEW_MENU);
menu.addMenuItem(COMMAND_ID);
AppInit.appReady(function () {
init();
});
});