forked from brackets-userland/brackets-git
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFtp.js
More file actions
190 lines (165 loc) · 7.62 KB
/
Ftp.js
File metadata and controls
190 lines (165 loc) · 7.62 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
define(function (require) {
// Brackets modules
var _ = brackets.getModule("thirdparty/lodash"),
DefaultDialogs = brackets.getModule("widgets/DefaultDialogs"),
Dialogs = brackets.getModule("widgets/Dialogs"),
StringUtils = brackets.getModule("utils/StringUtils");
// Local modules
var ErrorHandler = require("src/ErrorHandler"),
Events = require("src/Events"),
EventEmitter = require("src/EventEmitter"),
Preferences = require("src/Preferences"),
Strings = require("strings"),
Utils = require("src/Utils"),
GitFtp = require("./GitFtp");
// Module variables
var ftpScopesTemplate = require("text!src/Ftp/templates/remotes-picker.html"),
$gitPanel = null,
$remotesDropdown = null;
// Implementation
var attachEvents = _.once(function () {
$gitPanel
.on("click", ".gitftp-remote-new", handleGitFtpScopeCreation)
.on("click", ".gitftp-remove-remote", function () { handleGitFtpScopeRemove($(this)); })
.on("click", ".gitftp-init-remote", function () { handleGitFtpInitScope($(this)); })
.on("click", ".gitftp-push", handleGitFtpPush);
});
function initVariables() {
$gitPanel = $("#git-panel");
$remotesDropdown = $gitPanel.find(".git-remotes-dropdown");
if (Preferences.get("useGitFtp")) {
attachEvents();
}
}
function handleGitFtpPush() {
var gitFtpScope = $gitPanel.find(".git-remote-selected").text().trim();
$gitPanel.find(".gitftp-push").prop("disabled", true).addClass("btn-loading");
GitFtp.push(gitFtpScope).done(function (result) {
Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_INFO,
Strings.GITFTP_PUSH_RESPONSE, // title
result // message
);
}).catch(function (err) {
ErrorHandler.showError(err, "Failed push to Git-FTP remote.");
}).finally(function () {
$gitPanel.find(".gitftp-push")
.prop("disabled", false)
.removeClass("btn-loading");
});
}
function handleGitFtpScopeCreation() {
$gitPanel.find(".git-remotes")
.addClass("btn-loading")
.prop("disabled", true);
return Utils.askQuestion(Strings.CREATE_GITFTP_NEW_SCOPE, Strings.ENTER_GITFTP_SCOPE_NAME)
.then(function (name) {
return Utils.askQuestion(
Strings.CREATE_GITFTP_NEW_SCOPE,
Strings.ENTER_GITFTP_SCOPE_URL,
{defaultValue: "ftp://user:passwd@example.org/folder"}
)
.then(function (url) {
return GitFtp.addScope(name, url).then(function () {
// Render the list element of the new remote
// TODO: replace this part with a way to call `Remotes.refreshRemotesPicker()`
var $newScope = $("<li/>")
.addClass("gitftp-remote")
.append("<a/>")
.find("a")
.attr({href: "#", "data-remote-name": name, "data-type": "ftp"})
.addClass("remote-name")
.append("<span/>")
.find("span")
.addClass("trash-icon gitftp-remove-remote")
.html("×")
.end()
.append("<span/>")
.find("span:nth-child(2)")
.addClass("change-remote")
.text(name)
.end()
.end();
$gitPanel.find(".git-remotes-dropdown .ftp-remotes-header").after($newScope);
}).catch(function (err) {
ErrorHandler.showError(err, "Git-FTP remote creation failed");
});
});
})
.finally(function () {
$gitPanel.find(".git-remotes")
.removeClass("btn-loading")
.prop("disabled", false);
});
}
function handleGitFtpScopeRemove($this) {
$gitPanel.find(".git-remotes")
.addClass("btn-loading")
.prop("disabled", true);
var $selectedElement = $this.closest(".remote-name"),
$currentScope = $gitPanel.find(".git-remote-selected"),
scopeName = $selectedElement.data("remote-name");
return Utils.askQuestion(
Strings.DELETE_SCOPE,
StringUtils.format(Strings.DELETE_SCOPE_NAME, scopeName),
{booleanResponse: true}
).then(function (response) {
if (response) {
return GitFtp.removeScope(scopeName).then(function () {
$selectedElement.parent().remove();
var newScope = $gitPanel.find(".git-remotes-dropdown .remote").first().find("a").data("remote-name");
$currentScope.data("remote-name", newScope).html(newScope);
}).catch(function (err) {
ErrorHandler.showError(err, "Remove scope failed");
});
}
}).finally(function () {
$gitPanel.find(".git-remotes")
.removeClass("btn-loading")
.prop("disabled", false);
});
}
function handleGitFtpInitScope($this) {
$gitPanel.find(".git-remotes")
.addClass("btn-loading")
.prop("disabled", true);
var $selectedElement = $this.closest(".remote-name"),
scopeName = $selectedElement.data("remote-name");
return Utils.askQuestion(
Strings.INIT_GITFTP_SCOPE,
StringUtils.format(Strings.INIT_GITFTP_SCOPE_NAME, scopeName),
{booleanResponse: true}
).then(function (response) {
if (response) {
return GitFtp.init(scopeName).then(function () {
}).catch(function (err) {
ErrorHandler.showError(err, "Init scope failed");
});
}
}).finally(function () {
$gitPanel.find(".git-remotes")
.removeClass("btn-loading")
.prop("disabled", false);
});
}
function addFtpScopesToPicker() {
GitFtp.getScopes().then(function (ftpScopes) {
// Pass to Mustache the needed data
var compiledTemplate = Mustache.render(ftpScopesTemplate, {
Strings: Strings,
ftpScopes: ftpScopes,
hasFtpScopes: ftpScopes.length > 0
});
$remotesDropdown.prepend(compiledTemplate);
}).catch(function (err) {
ErrorHandler.showError(err, "Getting FTP remotes failed!");
});
}
// Event subscriptions
EventEmitter.on(Events.GIT_ENABLED, function () {
initVariables();
});
EventEmitter.on(Events.REMOTES_REFRESH_PICKER, function () {
addFtpScopesToPicker();
});
});