-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.js
338 lines (286 loc) · 11.6 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
/*!
* Brackets Todo 0.5.3
* Display all todo comments in current document or project.
*
* @author Mikael Jorhult
* @license http://mikaeljorhult.mit-license.org MIT
*/
define( function( require, exports, module ) {
'use strict';
// Get dependencies.
var Async = brackets.getModule( 'utils/Async' ),
Menus = brackets.getModule( 'command/Menus' ),
CommandManager = brackets.getModule( 'command/CommandManager' ),
Commands = brackets.getModule( 'command/Commands' ),
PreferencesManager = brackets.getModule( 'preferences/PreferencesManager' ),
ProjectManager = brackets.getModule( 'project/ProjectManager' ),
EditorManager = brackets.getModule( 'editor/EditorManager' ),
DocumentManager = brackets.getModule( 'document/DocumentManager' ),
WorkspaceManager = brackets.getModule( 'view/WorkspaceManager' ),
Resizer = brackets.getModule( 'utils/Resizer' ),
AppInit = brackets.getModule( 'utils/AppInit' ),
FileUtils = brackets.getModule( 'file/FileUtils' ),
FileSystem = brackets.getModule( 'filesystem/FileSystem' ),
ExtensionUtils = brackets.getModule( 'utils/ExtensionUtils' ),
NodeDomain = brackets.getModule("utils/NodeDomain"),
// Extension basics.
COMMAND_ID = 'bigeyex.bracketsSFTPUpload.enable',
COMMAND_ID_UPLOAD = 'bigeyex.bracketsSFTPUpload.upload',
COMMAND_ID_UPLOAD_ALL = 'bigeyex.bracketsSFTPUpload.uploadAll',
Strings = require( 'modules/Strings' ),
dataStorage = require( 'modules/DataStorageManager' ),
settingsDialog = require( 'modules/SettingsDialog' ),
// Preferences.
preferences = PreferencesManager.getExtensionPrefs( 'bigeyex.bracketsSFTPUpload' ),
// Mustache templates.
todoPanelTemplate = require( 'text!html/panel.html' ),
todoRowTemplate = require( 'text!html/row.html' ),
// Setup extension.
serverInfo, //sftp username/password etc;
$todoPanel,
projectUrl,
$todoIcon = $( '<a href="#" title="' + Strings.EXTENSION_NAME + '" id="brackets-sftp-upload-icon"></a>' ),
// Get view menu.
menu = Menus.getMenu( Menus.AppMenuBar.VIEW_MENU ),
contextMenu = Menus.getContextMenu(Menus.ContextMenuIds.PROJECT_MENU);
// Define preferences.
preferences.definePreference( 'enabled', 'boolean', false );
// Get Node module domain
var _domainPath = ExtensionUtils.getModulePath(module, "node/SftpUploadDomain");
var _nodeDomain = new NodeDomain("sftpUpload", _domainPath);
// Register extension.
CommandManager.register( Strings.EXTENSION_NAME, COMMAND_ID, togglePanel );
CommandManager.register( Strings.UPLOAD_MENU_NAME, COMMAND_ID_UPLOAD, uploadMenuAction );
CommandManager.register( Strings.UPLOAD_ALL, COMMAND_ID_UPLOAD_ALL, uploadAllItems );
// Add command to menu.
if ( menu !== undefined ) {
menu.addMenuDivider();
menu.addMenuItem( COMMAND_ID, 'Ctrl-Alt-Shift-U' );
menu.addMenuItem( COMMAND_ID_UPLOAD, 'Ctrl-Alt-U' );
menu.addMenuItem( COMMAND_ID_UPLOAD_ALL, 'Ctrl-Shift-U' );
}
if ( contextMenu !== undefined ) {
contextMenu.addMenuDivider();
contextMenu.addMenuItem( COMMAND_ID_UPLOAD );
}
// Load stylesheet.
ExtensionUtils.loadStyleSheet( module, 'todo.css' );
/**
* Set state of extension.
*/
// this is a menu item
function togglePanel() {
var enabled = preferences.get( 'enabled' );
enablePanel( !enabled );
}
function uploadMenuAction(){
var item = ProjectManager.getSelectedItem();
var projectUrl = ProjectManager.getProjectRoot().fullPath;
var remotePath = item.fullPath.replace(projectUrl, '');
if(item.isFile){
uploadItem(item.fullPath, remotePath);
}
else{
uploadDirectory(item.fullPath, remotePath);
}
}
/**
* Initialize extension.
*/
function enablePanel( enabled ) {
if ( enabled ) {
loadSettings( function() {
// Show panel.
Resizer.show( $todoPanel );
} );
// Set active class on icon.
$todoIcon.addClass( 'active' );
} else {
// Hide panel.
Resizer.hide( $todoPanel );
// Remove active class from icon.
$todoIcon.removeClass( 'active' );
}
// Save enabled state.
preferences.set( 'enabled', enabled );
preferences.save()
// Mark menu item as enabled/disabled.
CommandManager.get( COMMAND_ID ).setChecked( enabled );
}
// this is called every time the panel opens.
function loadSettings( callback ) {
var changedFiles = dataStorage.get('changed_files');
var files = [];
var projectUrl = ProjectManager.getProjectRoot().fullPath;
for(var filepath in changedFiles){
files.push({
path: filepath,
file: filepath.replace(projectUrl, '')
});
}
$('#sftp-upload-tbody').empty().append(Mustache.render( todoRowTemplate, {
strings: Strings,
files: files
} ));
$('#sftp-upload-tbody tr').off().on('click', function(){
var fullPath = $(this).attr('x-file');
CommandManager.execute( Commands.FILE_OPEN, { fullPath: fullPath } );
});
$('#sftp-upload-tbody .upload-button').off().on('click', function(e){
uploadItem($(this).attr('x-file'), $(this).attr('r-file'));
e.stopPropagation();
});
$('#sftp-upload-tbody .skip-button').off().on('click', function(e){
skipItem($(this).attr('x-file'));
e.stopPropagation();
});
if ( callback ) { callback(); }
}
function showUploadingIconStatus(status){
if(status){
$todoIcon.addClass( 'uploading' );
}
else{
$todoIcon.removeClass( 'uploading' );
}
}
// upload ONE file to the server
function uploadItem(localPath, remotePath){
var serverInfo = dataStorage.get('server_info');
showUploadingIconStatus(true);
_nodeDomain.exec('upload', localPath, remotePath, serverInfo).fail(function(err){
showUploadingIconStatus(false);
updateStatus(err);
});
}
function uploadDirectory(localPath, remotePath){
var serverInfo = dataStorage.get('server_info');
showUploadingIconStatus(true);
_nodeDomain.exec('uploadDirectory', localPath, remotePath, serverInfo).fail(function(err){
showUploadingIconStatus(false);
updateStatus(err);
});
}
// upload all files in the panel to the server
function uploadAllItems(){
var serverInfo = dataStorage.get('server_info');
var trs = $('#brackets-sftp-upload tr .upload-button');
var filelist = [];
for(var i=0;i<trs.length;i++){
var arg = {
localPath: $(trs[i]).attr('x-file'),
remotePath: $(trs[i]).attr('r-file')
};
filelist.push(arg);
}
showUploadingIconStatus(true);
_nodeDomain.exec('uploadAll', filelist, serverInfo).fail(function(err){
showUploadingIconStatus(false);
updateStatus(err);
});
}
function skipItem(path) {
var changedFiles = dataStorage.get('changed_files') || {};
$('#brackets-sftp-upload tr[x-file="'+path+'"]').remove();
if(path in changedFiles){
delete changedFiles[path];
dataStorage.set('changed_files', changedFiles);
}
}
function skipAllItems(){
$('#brackets-sftp-upload tr').remove();
dataStorage.set('changed_files', {});
}
function updateStatus(status){
$('#brackets-sftp-upload .status-stab').text(status);
}
/**
* Listen for save or refresh and look for todos when needed.
*/
function registerListeners() {
var $documentManager = $( DocumentManager ),
$projectManager = $( ProjectManager );
// Listeners bound to Brackets modules.
$documentManager
.on( 'documentSaved.todo', function( event, document ) {
//TODO: add current document to change list
var path = document.file.fullPath;
var changedFiles = dataStorage.get('changed_files') || {};
if(changedFiles === null){
changedFiles = {};
}
var projectUrl = ProjectManager.getProjectRoot().fullPath;
var serverInfo = dataStorage.get('server_info');
if(serverInfo != null && serverInfo.uploadOnSave){
uploadItem(path, path.replace(projectUrl, ''));
return;
}
if(!(path in changedFiles)){
changedFiles[path]=1;
dataStorage.set('changed_files', changedFiles);
$('#sftp-upload-tbody').append(Mustache.render( todoRowTemplate, {
strings: Strings,
files: [{
path: path,
file: path.replace(projectUrl, '')
}]
}));
$('#sftp-upload-tbody .upload-button').off().on('click', function(e){
uploadItem($(this).attr('x-file'), $(this).attr('r-file'));
e.stopPropagation();
});
$('#sftp-upload-tbody .skip-button').off().on('click', function(e){
skipItem($(this).attr('x-file'));
e.stopPropagation();
});
}
} );
}
// Register panel and setup event listeners.
AppInit.appReady( function() {
var panelHTML = Mustache.render( todoPanelTemplate, {
strings: Strings
} );
// Create and cache todo panel.
WorkspaceManager.createBottomPanel( 'bigeyex.bracketsSFTPUpload.panel', $( panelHTML ), 100 );
$todoPanel = $( '#brackets-sftp-upload' );
// Close panel when close button is clicked.
$todoPanel
.on( 'click', '.close', function() {
enablePanel( false );
} );
// Setup listeners.
registerListeners();
// Add listener for toolbar icon..
$todoIcon.click( function() {
CommandManager.execute( COMMAND_ID );
} ).appendTo( '#main-toolbar .buttons' );
$todoPanel.on('click', '.btn-server-setup',function(){
settingsDialog.showDialog();
});
$todoPanel.on('click', '.btn-upload-all',function(){
uploadAllItems();
});
$todoPanel.on('click', '.btn-skip-all',function(){
skipAllItems();
});
// Enable extension if loaded last time.
if ( preferences.get( 'enabled' ) ) {
enablePanel( true );
}
$(_nodeDomain).on('uploading', function(err, msg){
updateStatus('Uploading: '+msg);
});
$(_nodeDomain).on('uploaded', function(err, msg){
var projectUrl = ProjectManager.getProjectRoot().fullPath;
skipItem(projectUrl+msg);
updateStatus('Finished: '+msg);
});
$(_nodeDomain).on('error', function(err, msg){
updateStatus('Error: '+msg);
});
$(_nodeDomain).on('jobCompleted', function(err, msg){
showUploadingIconStatus(false);
});
} );
} );