forked from cfjedimaster/brackets-jsdownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
182 lines (141 loc) · 5.42 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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4,
maxerr: 50, browser: true */
/*global $, define, brackets */
define(function (require, exports, module) {
"use strict";
var downloaderTemplate = require("text!htmlContent/downloader.html"),
libraryListTemplate = require("text!htmlContent/librarylist.html");
var Commands = brackets.getModule("command/Commands"),
CommandManager = brackets.getModule("command/CommandManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
Menus = brackets.getModule("command/Menus"),
Dialogs = brackets.getModule("widgets/Dialogs"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
ProjectManager = brackets.getModule("project/ProjectManager"),
FileUtils = brackets.getModule("file/FileUtils"),
NativeFileSystem = brackets.getModule("file/NativeFileSystem"),
AppInit = brackets.getModule("utils/AppInit"),
NodeConnection = brackets.getModule("utils/NodeConnection");
var nodeConnection;
require('jsdownloader');
var LAUNCH_JSDOWNLOADER = "jsdownloader.run";
var libraryOb = {};
var pathToUse;
function _handleShowJSDownloader() {
//Do we have our cached index
if(!JSDownloader.hasCachedIndex()) {
$(".jsdownloader-dialog div.modal-body").html("<img src='" + require.toUrl("img/ajax-loader.gif") + "' style='float:right'><i>Caching list of libraries.<br/>Please stand by for awesome.</i>");
Dialogs.showModalDialog("jsdownloader-dialog");
JSDownloader.loadIndex(require.toUrl("./"),function() {
_displayIndex();
});
} else {
Dialogs.showModalDialog("jsdownloader-dialog");
_displayIndex();
}
}
function _handleLibClick(e) {
e.preventDefault();
var lib = $(this).data("library");
//Fire off an async process to do the download
//First, get the related span
var $span = $(this).next("span");
$span.html("<i>Downloading...</i>");
/*
Ok, now to create the deferreds for our downloads. Note that we don't yet
support N files for a library, but I'm going to build it later. So for now
we just assume.
*/
var library = libraryOb[lib];
var filesToGet;
if(library.file) {
filesToGet = [library.file];
} else {
filesToGet = library.files;
}
console.dir(filesToGet);
var suPromise = nodeConnection.domains.downloader.fetchStuff(filesToGet,pathToUse);
suPromise.done(function(port) {
$span.html("<i>Done!</i>");
ProjectManager.refreshFileTree();
});
/*
var deferreds = [];
filesToGet.forEach(function(f) {
var deferred = $.get(f, {}, function(res,code) {
var pathToSave = pathToUse + f.split("/").pop();
console.log('about to save '+pathToSave);
var fileEntry = new NativeFileSystem.NativeFileSystem.FileEntry(pathToSave);
FileUtils.writeText(fileEntry, res);
});
deferreds.push(deferred);
});
$.when.apply(null, deferreds).then(function() {
$span.html("<i>Done!</i>");
});
*/
}
function _displayIndex() {
pathToUse = "";
var selectedItem = ProjectManager.getSelectedItem();
if(selectedItem && selectedItem.isDirectory) {
pathToUse = selectedItem.fullPath;
} else {
pathToUse = ProjectManager.getProjectRoot().fullPath;
}
var libraries = JSDownloader.getIndex();
//create a hash we can use on this side for click event
for(var i=0, len=libraries.length;i<len; i++) {
libraryOb[libraries[i].key] = libraries[i];
}
var html = Mustache.render(libraryListTemplate, {library: libraries, path:pathToUse});
$(".jsdownloader-dialog div.modal-body").html(html);
}
function chain() {
var functions = Array.prototype.slice.call(arguments, 0);
if (functions.length > 0) {
var firstFunction = functions.shift();
var firstPromise = firstFunction.call();
firstPromise.done(function () {
chain.apply(null, functions);
});
}
}
AppInit.appReady(function() {
nodeConnection = new NodeConnection();
function connect() {
var connectionPromise = nodeConnection.connect(true);
connectionPromise.fail(function () {
console.error("[brackets-jsdownloader] failed to connect to node");
});
return connectionPromise;
}
// Helper function that loads our domain into the node server
function loadMyDomain() {
var path = ExtensionUtils.getModulePath(module, "node/downloader");
var loadPromise = nodeConnection.loadDomains([path], true);
loadPromise.fail(function (e) {
console.log(JSON.stringify(e));
console.log("[brackets-jsdownloader] failed to load domain");
});
loadPromise.done(function(e) {
ready();
});
return loadPromise;
}
function ready() {
/* At this point we are ready to add to the UI */
CommandManager.register("Run JSDownloader", LAUNCH_JSDOWNLOADER, _handleShowJSDownloader);
var menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
menu.addMenuItem(LAUNCH_JSDOWNLOADER, "", Menus.AFTER);
var context_menu = Menus.getContextMenu(Menus.ContextMenuIds.PROJECT_MENU);
context_menu.addMenuDivider();
context_menu.addMenuItem(LAUNCH_JSDOWNLOADER);
$('body').append(downloaderTemplate);
$(document).on("click",".jsdownloader-dialog div.modal-body a", _handleLibClick);
console.log("[brackets-jsdownloader] Ready for ludicrous speed!");
}
chain(connect, loadMyDomain);
});
});