-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtwcloud.js
348 lines (348 loc) · 15 KB
/
twcloud.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
339
340
341
342
343
344
345
346
347
348
"use strict";
/// <reference path="./dropbox.min.d.ts" />
/// <reference path="./Rx.min.d.ts" />
var wrapper;
(function (wrapper) {
//PARANOIA: Delete the window property of these libraries to prevent
//Javascript in the page from messing with them.
var Rx = window.Rx;
delete window.Rx;
var Dropbox = window.Dropbox;
delete window.Dropbox;
var $ = window.jQuery;
delete window.$;
delete window.jQuery;
//load classes from Rx
var Observable = Rx.Observable, Subject = Rx.Subject, Subscriber = Rx.Subscriber, Subscription = Rx.Subscription;
var twits = /** @class */ (function () {
function twits(type, hash) {
var _this = this;
this.apiKeyFull = "gy3j4gsa191p31x";
this.apiKeyApps = "tu8jc7jsdeg55ta";
this.token = {};
if (type !== "apps" && type !== "full")
throw "type must be either apps or full";
this.client = new Dropbox({
clientId: (type === "full" ? this.apiKeyFull : (type === "apps" ? this.apiKeyApps : ""))
});
// Authenticate against Dropbox
this.setStatusMessage("Authenticating with Dropbox...");
if (hash) {
var data = hash[0] === "#" ? hash.slice(1) : hash;
data.split('&').map(function (e) { return e.split('=').map(function (f) { return decodeURIComponent(f); }); }).forEach(function (e) {
_this.token[e[0]] = e[1];
});
}
if (!this.token.access_token) {
location.href = this.client.getAuthenticationUrl(location.href);
return;
}
else {
this.client.setAccessToken(this.token.access_token);
}
this.initApp();
}
// Main application
twits.prototype.initApp = function () {
var _this = this;
this.clearStatusMessage();
this.client.usersGetCurrentAccount(undefined).then(function (res) {
_this.user = res;
var profile = document.getElementById("twits-profile");
var pic = document.createElement('img');
pic.src = _this.user.profile_photo_url;
pic.classList.add("profile-pic");
profile.appendChild(pic);
var textdata = document.createElement('span');
_this.user.account_type;
textdata.innerText = _this.user.name.display_name
+ (_this.user.team ? ("\n" + _this.user.team.name) : "");
textdata.classList.add(_this.user.team ? "profile-name-team" : "profile-name");
profile.appendChild(textdata);
profile.classList.remove("startup");
_this.readFolder("", document.getElementById("twits-files"));
});
};
;
twits.prototype.isFileMetadata = function (a) {
return a[".tag"] === "file";
};
twits.prototype.isFolderMetadata = function (a) {
return a[".tag"] === "folder";
};
twits.prototype.isHtmlFile = function (stat) {
return ['.htm', '.html'].indexOf(stat.name.slice(stat.name.lastIndexOf('.'))) > -1;
};
twits.prototype.getHumanSize = function (size) {
var TAGS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
var power = 0;
while (size >= 1024) {
size /= 1024;
power++;
}
return size.toFixed(1) + TAGS[power];
};
twits.prototype.streamFilesListFolder = function (path) {
var output = new Subject();
function resHandler(res) {
output.next(res.entries);
if (res.has_more)
return this.client.filesListFolderContinue({
cursor: res.cursor
}).then(resHandler);
else
output.complete();
}
this.client.filesListFolder({
path: path
}).then(resHandler);
return output.asObservable();
};
twits.prototype.readFolder = function (path, parentNode) {
var _this = this;
var loadingMessage = document.createElement("li");
loadingMessage.innerText = "Loading...";
loadingMessage.classList.add("loading-message");
var listParent = document.createElement("ol");
listParent.appendChild(loadingMessage);
parentNode.appendChild(listParent);
var filelist = [];
this.streamFilesListFolder(path).subscribe(function (stats) {
filelist.push.apply(filelist, stats);
loadingMessage.innerText = "Loading " + filelist.length + "...";
}, function (x) { return console.error(x); }, function () {
loadingMessage.remove();
filelist.sort(function (a, b) {
//order by isFolder DESC, name ASC
return (+_this.isFolderMetadata(b) - +_this.isFolderMetadata(a))
|| a.name.localeCompare(b.name);
});
for (var t = 0; t < filelist.length; t++) {
var stat = filelist[t];
var listItem = document.createElement("li"), classes = [];
if (_this.isFolderMetadata(stat)) {
classes.push("twits-folder");
}
else if (_this.isFileMetadata(stat)) {
classes.push("twits-file");
if (_this.isHtmlFile(stat)) {
classes.push("twits-file-html");
}
}
var link;
classes.push("twits-file-entry");
if (_this.isFolderMetadata(stat) || (_this.isFileMetadata(stat) && _this.isHtmlFile(stat))) {
link = document.createElement("a");
link.href = "javascript:false;";
link.setAttribute("data-twits-path", stat.path_lower);
link.addEventListener("click", _this.onClickFolderEntry(), false);
}
else {
link = document.createElement("span");
}
link.className = classes.join(" ");
var img = document.createElement("img");
img.src = "dropbox-icons-broken.gif";
img.style.width = "16px";
img.style.height = "16px";
link.appendChild(img);
link.appendChild(document.createTextNode(stat.name));
if (_this.isFileMetadata(stat) && _this.getHumanSize(stat.size)) {
var size = document.createElement("span");
size.appendChild(document.createTextNode(" (" + _this.getHumanSize(stat.size) + ")"));
link.appendChild(size);
}
listItem.appendChild(link);
listParent.appendChild(listItem);
}
});
};
;
twits.prototype.onClickFolderEntry = function () {
var self = this;
return function (event) {
var path = this.getAttribute("data-twits-path"), classes = this.className.split(" ");
if (classes.indexOf("twits-folder") !== -1 && classes.indexOf("twits-folder-open") === -1) {
classes.push("twits-folder-open");
self.readFolder(path, this.parentNode);
}
if (classes.indexOf("twits-file-html") !== -1) {
self.openFile(path);
}
this.className = classes.join(" ");
event.preventDefault();
return false;
};
};
;
twits.prototype.openFile = function (path) {
var _this = this;
// Read the TiddlyWiki file
// We can't trust Dropbox to have detected that the file is UTF8, so we load it in binary and manually decode it
this.setStatusMessage("Reading HTML file...");
this.client.filesDownload({
path: path
}).then(function (res) {
//debugger;
_this.currentRev = res.rev;
return new Promise(function (resolve) {
var data = res.fileBlob;
console.log(data.type);
var reader = new FileReader();
reader.addEventListener("loadend", function () {
// We have to manually decode the file as UTF8, annoyingly
// [ I wonder if this is still necessary in v2 since it is a buffer -Arlen ]
var byteData = new Uint8Array(reader.result);
var unicode = _this.manualConvertUTF8ToUnicode(byteData);
_this.originalPath = path;
_this.originalText = unicode;
resolve(unicode);
});
reader.readAsArrayBuffer(data);
//debugger;
});
}).then(function (data) {
_this.loadTW5(data);
});
};
twits.prototype.getStatusPanel = function () {
// debugger;
var getElement = function (id, parentNode) {
parentNode = parentNode || document;
var el = document.getElementById(id);
if (!el) {
el = document.createElement("div");
el.setAttribute("id", id);
parentNode.appendChild(el);
}
return el;
}, status = getElement("twits-status", document.body), message = getElement("twits-message", status), progress = getElement("twits-progress", status);
status.style.display = "block";
if (this.user) {
var profile = document.createElement('img');
profile.src = this.user.profile_photo_url;
profile.classList.add("profile-pic");
status.insertBefore(profile, message);
}
return { status: status, message: message, progress: progress };
};
;
twits.prototype.clearStatusMessage = function () {
var status = this.getStatusPanel();
status.status.style.display = "none";
};
twits.prototype.setStatusMessage = function (text) {
var status = this.getStatusPanel();
while (status.message.hasChildNodes()) {
status.message.removeChild(status.message.firstChild);
}
status.message.appendChild(document.createTextNode(text));
};
;
twits.prototype.setProgress = function (text) {
var status = this.getStatusPanel();
while (status.progress.hasChildNodes()) {
status.progress.removeChild(status.progress.firstChild);
}
status.progress.appendChild(document.createTextNode(text));
};
;
// Display an error
twits.prototype.showError = function (error) {
this.setStatusMessage("Error: " + error);
this.setProgress("");
};
;
twits.prototype.trackProgress = function (xhr, isUpload) {
var onProgressHandler = function (event) {
this.setProgress(Math.ceil(event.loaded / 1024) + "KB");
}, onLoadHandler = function () {
}, onErrorHandler = function () {
this.setStatusMessage("XHR error");
};
var src = isUpload ? xhr.upload : xhr;
src.addEventListener("progress", onProgressHandler, false);
src.addEventListener("load", onLoadHandler, false);
src.addEventListener("error", onErrorHandler, false);
};
;
twits.prototype.loadTW5 = function (data) {
var _this = this;
location.hash = "";
var rawIndex = data.indexOf('<' + '!--~~ Raw markup ~~--' + '>');
var doc = document.createElement('html');
doc.innerHTML = data;
while (doc.children[0].childNodes.length > 0) {
try {
$(document.head).append(doc.children[0].childNodes[0]);
}
catch (e) {
console.log(e);
}
}
document.body.className = doc.children[1].className;
$(document.body).html(doc.children[1].innerHTML);
$tw.saverHandler.savers.push({
info: {
name: "tw5-in-the-sky",
priority: 5000,
capabilities: ["save", "autosave"]
},
save: function (text, method, callback, options) {
_this.setStatusMessage("Saving changes...");
_this.client.filesUpload({
path: _this.originalPath,
mode: {
".tag": "update",
"update": _this.currentRev
},
contents: text
}).then(function (res) {
_this.clearStatusMessage();
_this.currentRev = res.rev;
callback(null);
}).catch(function (err) {
console.log(err);
callback(err);
});
//TODO: add progress tracker
return true;
}
});
};
;
twits.prototype.manualConvertUTF8ToUnicode = function (utf) {
var uni = [], src = 0, b1, b2, b3, c;
while (src < utf.length) {
b1 = utf[src++];
if (b1 < 0x80) {
uni.push(String.fromCharCode(b1));
}
else if (b1 < 0xE0) {
b2 = utf[src++];
c = String.fromCharCode(((b1 & 0x1F) << 6) | (b2 & 0x3F));
uni.push(c);
}
else {
b2 = utf[src++];
b3 = utf[src++];
c = String.fromCharCode(((b1 & 0xF) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F));
uni.push(c);
}
}
return uni.join("");
};
return twits;
}());
var locationHash = location.hash;
location.hash = "";
// Do our stuff when the page has loaded
document.addEventListener("DOMContentLoaded", function (event) {
var url = new URL(location.href);
var accessType = url.searchParams.get('type');
if (!accessType)
return;
$('#twits-selector').hide();
new twits(accessType, locationHash);
}, false);
})(wrapper || (wrapper = {}));