-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
164 lines (124 loc) · 5.62 KB
/
main.js
File metadata and controls
164 lines (124 loc) · 5.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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets */
/** Simple extension that adds a "File > Hello World" menu item. Inserts "Hello, world!" at cursor pos. */
define(function (require, exports, module) {
'use strict';
var AppInit = brackets.getModule('utils/AppInit'),
Menus = brackets.getModule('command/Menus'),
CommandManager = brackets.getModule('command/CommandManager'),
ProjectManager = brackets.getModule('project/ProjectManager'),
FileSystem = brackets.getModule('filesystem/FileSystem'),
DocumentManager = brackets.getModule('document/DocumentManager'),
ExtensionUtils = brackets.getModule('utils/ExtensionUtils'),
NodeConnection = brackets.getModule('utils/NodeConnection'),
PanelManager = brackets.getModule('view/PanelManager'),
Dialogs = brackets.getModule("widgets/Dialogs");
// Mustache templates
var azEncPanelTemplate = require('text!html/panel.html'),
azEncRowTemplate = require('text!html/rows.html');
// Setting var for extension
var files = [],
$azPanel;
var nodeConnection, azBkPanel;
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);
});
}
}
function convertFile() {
var $this = $(this),
convertPromise;
//$this.html('Verifying...');
// var readPromise = DocumentManager.getDocumentForPath($this.data('file'));
// readPromise.fail(function() {
convertPromise = nodeConnection.domains.azenc.convertFileEncoding($this.data('file'));
convertPromise.fail(function () {
console.log('[Az-Enc] failed to convert the file');
});
convertPromise.done(function(newFilePath) {
console.log('[Az-Enc] converted');
$this.html('Converted');
});
// });
//readPromise.done(function() {
// $this.html('Is ready to use');
//});
}
function showRows(files) {
var rowsHtml = Mustache.render(azEncRowTemplate, {'arquivos': files});
$azPanel.find('.rows-container').empty().append(rowsHtml);
}
function showPanel(files) {
azBkPanel.show();
showRows(files);
}
function detectEncoding() {
var encodingPromise = nodeConnection.domains.azenc.getFilesEncoding(ProjectManager.getSelectedItem()._path.toString()),
files = [],
readPromise,
i;
encodingPromise.fail(function (err) {
console.error('[Az-Enc] failed to detect encoding of files', err);
});
encodingPromise.done(function (data) {
showPanel(data.files);
});
return encodingPromise;
}
// Function to run when the menu item is clicked
function handleDetectEncoding() {
console.log(ProjectManager.getSelectedItem());
if (ProjectManager.getSelectedItem()._isDirectory) {
chain(detectEncoding);
} else {
Dialogs.showModalDialog('', 'Az-Enc', 'You must select a <b>directory</b> for detect encodings.<br />This extension doesn\'t work with a single files.');
}
}
AppInit.appReady(function() {
nodeConnection = new NodeConnection();
function connect() {
var connectionPromise = nodeConnection.connect(true);
connectionPromise.fail(function () {
console.error('[Az-Enc] failed to connect to node');
});
return connectionPromise;
}
function loadAzEncDomain() {
var path = ExtensionUtils.getModulePath(module, 'node/EncDomain');
var loadPromise = nodeConnection.loadDomains([path], true);
loadPromise.fail(function () {
console.log('[Az-Enc] failed to load domain');
});
loadPromise.done(function () {
console.log('[Az-Enc] loaded');
});
return loadPromise;
}
chain(connect, loadAzEncDomain);
ExtensionUtils.loadStyleSheet(module, "css/az.css");
});
var panelHtml = Mustache.render(azEncPanelTemplate, '');
azBkPanel = PanelManager.createBottomPanel('azenc.encoding.listfiles', $(panelHtml), 200);
$azPanel = $('#brackets-azenc');
$azPanel
.on('click', '.close', function () {
azBkPanel.hide();
})
.on('click', '.btnConvert', convertFile);
$(ProjectManager).on("beforeProjectClose", function () {
azBkPanel.hide();
});
// First, register a command - a UI-less object associating an id to a handler
var MY_COMMAND_ID = 'azenc.detectEncoding'; // package-style naming to avoid collisions
CommandManager.register('Detect Encoding', MY_COMMAND_ID, handleDetectEncoding);
// Then create a menu item bound to the command
// The label of the menu item is the name we gave the command (see above)
var menu = Menus.getContextMenu(Menus.ContextMenuIds.PROJECT_MENU);
menu.addMenuItem(MY_COMMAND_ID);
exports.handleDetectEncoding = handleDetectEncoding;
});