/
utilityFunctions.js
272 lines (232 loc) · 7.33 KB
/
utilityFunctions.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
var pathElements = [];
var dirResults = {};
var displayError = function (error) {
Modal.error({
title: "Error",
message: error
});
}
var displayDropboxError = function (error) {
switch (error.status) {
case Dropbox.ApiError.INVALID_TOKEN:
// If you're using dropbox.js, the only cause behind this error is that
// the user token expired.
// Get the user through the authentication flow again.
// Rajat : currently requesting user to refresh.
displayError("Looks like you token expired. Please refresh.");
break;
case Dropbox.ApiError.NOT_FOUND:
// The file or folder you tried to access is not in the user's Dropbox.
// Handling this error is specific to your application.
displayError("Looks like the file/folder you are trying to access is not available.");
break;
case Dropbox.ApiError.OVER_QUOTA:
// The user is over their Dropbox quota.
// Tell them their Dropbox is full. Refreshing the page won't help.
displayError("Your Dropbox is full. Clear some space and try again.");
break;
case Dropbox.ApiError.RATE_LIMITED:
// Too many API requests. Tell the user to try again later.
// Long-term, optimize your code to use fewer API calls.
displayError("Please try again later.");
break;
case Dropbox.ApiError.NETWORK_ERROR:
// An error occurred at the XMLHttpRequest layer.
// Most likely, the user's network connection is down.
// API calls will not succeed until the user gets back online.
displayError("Ensure that you are connected to the Internet and try again!");
break;
case Dropbox.ApiError.INVALID_PARAM:
case Dropbox.ApiError.OAUTH_ERROR:
case Dropbox.ApiError.INVALID_METHOD:
default:
// Caused by a bug in dropbox.js, in your application, or in Dropbox.
// Tell the user an error occurred, ask them to refresh the page.
displayError("Some bug seems to have caused a problem. Please refresh the page and try again.");
}
};
var pathString = function () {
return "/" + pathElements.join("/");
}
var listDir = function (client, path, func) {
var operateOnList = function (files, func) {
for (var i = 0; i < files.length; i++) {
func(client, files[i]);
}
}
console.log(dirResults);
if (dirResults[path] !== undefined) {
operateOnList(dirResults[path], func);
doneLoading();
}
else {
client.readdir(path, function (error, results, dirStat, contentStats) {
// Check for error.
for (var i = 0; i < results.length; i++) {
contentStats[i].name = results[i];
}
dirResults[path] = contentStats;
operateOnList(contentStats, func);
doneLoading();
});
}
}
var inLoadingState = function () {
$('#loadingInfo').show();
}
var doneLoading = function () {
$('#loadingInfo').hide();
}
var addRowToFileBrowser = function (elements) {
var row = document.createElement("tr");
row.innerHTML = "";
for (var i = 0; i < elements.length; i++) {
row.innerHTML += "<td>" + elements[i] + "</td>";
}
$("#file_browser").append(row);
}
var listDirInHTML = function (client, path) {
$("#file_browser").children().remove();
inLoadingState();
listDir(client, path, addFileDetailsToTable);
}
var addPathElement = function (subDirectory, client) {
li = $(document.createElement("li"));
li.html(subDirectory + "<span class='divider'>/</span>");
pathElements.push(subDirectory);
finalPath = pathString();
li.attr("path", finalPath);
li.attr("class", "breadcrumbEntry");
$('#navigation').append(li);
$('#parentDirectory').show();
$('#parentDirectory').unbind('click');
$('#parentDirectory').click(function () {
removePathElement(client);
});
}
var addFileDetailsToTable = function (client, file) {
if (file.isRemoved)
return;
var row = $(document.createElement("tr"));
var nameElement = $(document.createElement("td"));
var typeElement = $(document.createElement("td"));
var dateElement = $(document.createElement("td"));
nameElement.html("<a href='#'>" + file.name + "</a>");
typeElement.text(file.mimeType);
dateElement.text(" " + file.modifiedAt);
if (file.isFolder) {
nameElement.click(function () {
folderClicked(client, file.path);
});
}
else {
nameElement.click(function () {
fileClicked(client, file.path);
});
}
row.append(nameElement);
row.append(typeElement);
row.append(dateElement);
$("#file_browser").append(row);
}
var folderClicked = function (client, path) {
console.log("folder clicked");
addPathElement(path.split("/").pop(), client);
listDirInHTML(client, path);
}
var getFileNameFromPath = function(path){
var partsOfPath = path.split("/");
return partsOfPath[partsOfPath.length - 1];
}
var fileClicked = function (client, path) {
password = Modal.prompt({
title: "Enter password",
input_type:"password",
optional:{
affirmative_label: "Decrypt"
},
success:function(password){
inLoadingState();
client.readFile(path, function (error, result) {
if (error !== null){
displayError(error);
}
try{
var decryptedText = sjcl.decrypt(password, result);
} catch(exception){
displayError("Either the password entered is incorrect or the data has been corrupted. Please try again.");
doneLoading();
return;
}
doneLoading();
setEditorValues(getFileNameFromPath(path), decryptedText);
showEditor();
});
}
});
}
var hideEditor = function(){
$('.zen-editor').hide();
$('.container-fluid').show();
}
var showEditor = function(){
$('.zen-editor').show();
$('.container-fluid').hide();
}
var removePathElement = function (client) {
var nav = $('#navigation');
console.log("Entered");
if (nav.children().length > 1) {
nav.children().last().remove(); // This changes the length
}
if (nav.children().length === 2) {
$('#parentDirectory').hide();
}
pathElements.pop();
console.log(pathString());
listDirInHTML(client, pathString());
}
var createDirectory = function (client, parentDir, dirName) {
client.mkdir(parentDir + "/" + dirName, function (error, result) {
if (error) {
displayError($.parseJSON(error.responseText).error);
}
else {
// Clear the result already stored.
delete dirResults[parentDir];
listDirInHTML(client, parentDir);
}
});
}
var createFile = function (client, path, fileName, content, key) {
var encryptedContent = sjcl.encrypt(key, content);
console.log(client, path, fileName, content, key);
client.writeFile(path + "/" + fileName, encryptedContent, function (error, result) {
if (error) {
displayDropboxError(error);
}
else {
Modal.info({
title: "File saved",
message: "'" + fileName + "' has been saved. Please remember your password. There is no way to recover the file if the password is lost/forgotten."
});
}
});
}
var getEditorTitleEntity = function(){
return $('#newFileName');
}
var getEditorContentEntity = function(){
return $('#fileContent');
}
var setEditorValues = function(title, content){
var titleEntity = getEditorTitleEntity();
var contentEntity = getEditorContentEntity();
titleEntity.text(title);
contentEntity.html(content);
}
var resetEditor = function(){
setEditorValues("Name of the file", "Content of the file");
$('#key').val("");
$('#verify_key').val("");
}