This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
75 lines (59 loc) · 2.57 KB
/
main.js
File metadata and controls
75 lines (59 loc) · 2.57 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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets */
/** Simple extension that shows the file size in KB on save or when switching to another document */
define(function (require, exports, module) {
"use strict";
var DocumentManager = brackets.getModule("document/DocumentManager"),
NodeDomain = brackets.getModule("utils/NodeDomain"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
EditorManager = brackets.getModule("editor/EditorManager");
var Units = {
BYTES: "Bytes",
KILOBYTES: "KB"
},
spaceDelimiter = " ";
//node domain for fetching file stats
var fsAction = new NodeDomain("fileInfo", ExtensionUtils.getModulePath(module, "node/FileInfo"));
//adding the filesize-status indicator in status bar
$("#status-indicators").prepend('<div id="filesize-status" style="text-align: right;"></div>');
var indicator = $("#filesize-status");
//update the status indicator
function updateStatusIndicator(fileSize) {
indicator.text(fileSize);
}
//handler for status update callback
function handleStatusUpdateCallback(event, returnText) {
var fileSize = returnText;
if (fileSize < 1024) {
fileSize += spaceDelimiter + Units.BYTES;
} else {
fileSize = (fileSize/1024).toFixed(2) + spaceDelimiter + Units.KILOBYTES;
}
updateStatusIndicator(fileSize);
}
fsAction.on("statusUpdate", handleStatusUpdateCallback);
function getFileInfo(filePath) {
fsAction.exec("getFileInfo", filePath).fail(function(err) {
console.log("Error occured during file size calculation");
console.log(err);
});
}
//handler for on document save
function handleDocumentSaved(event, doc) {
var absolutePath = doc.file.fullPath;
getFileInfo(absolutePath);
}
//handler for active editor change
function handleActiveEditorChange(event, newEditor) {
if (newEditor.document.isUntitled()) {
updateStatusIndicator("");
} else {
var currentFilePath = newEditor.document.file.fullPath;
getFileInfo(currentFilePath);
}
}
//register for document saved event
DocumentManager.on("documentSaved", handleDocumentSaved);
//register for active editor changed event
EditorManager.on("activeEditorChange", handleActiveEditorChange);
});