Commit cf17d813 by Dányi Bence

one: cloud.js dev documentation

parent b179dcf0
...@@ -74,23 +74,61 @@ $(function() { ...@@ -74,23 +74,61 @@ $(function() {
return false; return false;
}) })
function convert(n, skip) { /**
* Convert bytes to human readable format
*/
function convert(n, skip, precision) {
skip = skip | 0; skip = skip | 0;
precision = precision|2;
var suffix = 'B KB MB GB'.split(' '); var suffix = 'B KB MB GB'.split(' ');
for(var i = skip; n > 1024; i++) { for(var i = skip; n > 1024; i++) {
n /= 1024; n /= 1024;
} }
return n.toFixed(2) + ' ' + suffix[i]; return n.toFixed(precision) + ' ' + suffix[i];
} }
function Model() { function Model() {
//alias for this
var self = this; var self = this;
var uploadURLRequestInProgress=false; var uploadURLRequestInProgress=false;
//currently displayed files
self.files = ko.observableArray(); self.files = ko.observableArray();
//all fetched files
self.allFiles = []; self.allFiles = [];
//false, if you are in /
self.notInRoot = ko.observable(false); self.notInRoot = ko.observable(false);
//default file limit
self.fileLimit = 5; self.fileLimit = 5;
//defalut path to display
self.currentPath = ko.observable('/');
//default upload url (invalid)
self.uploadURL = ko.observable('/');
self.newFolderName = ko.observable();
self.uploadProgress = ko.observable('0%');
self.quota = {
rawUsed: ko.observable(0),
rawSoft: ko.observable(0),
rawHard: ko.observable(0)
};
self.quota.used = ko.computed(function() {
return convert(self.quota.rawUsed(),1);
});
self.quota.hard = ko.computed(function() {
return convert(self.quota.rawHard(),1);
});
self.quota.soft = ko.computed(function() {
return convert(self.quota.rawSoft(),1);
});
self.quota.usedBar = ko.computed(function() {
return(self.quota.rawUsed() / self.quota.rawHard() * 100).toFixed(0) + '%';
}, self);
self.quota.softPos = ko.computed(function() {
return(self.quota.rawSoft() / self.quota.rawHard() * 100).toFixed(0) + '%';
}, self)
/**
* Returns throttled function
*/
function throttle(f) { function throttle(f) {
var disabled = false; var disabled = false;
return function() { return function() {
...@@ -104,10 +142,18 @@ $(function() { ...@@ -104,10 +142,18 @@ $(function() {
f.apply(null, arguments); f.apply(null, arguments);
} }
} }
/**
* Loads the parent folder
*/
self.jumpUp = function() { self.jumpUp = function() {
var s = self.currentPath(); var s = self.currentPath();
loadFolder(s.substr(0, s.substr(0, s.length - 1).lastIndexOf('/') + 1)); loadFolder(s.substr(0, s.substr(0, s.length - 1).lastIndexOf('/') + 1));
} }
/**
* Loads the specified folder
*/
var loadFolder = throttle(function(path) { var loadFolder = throttle(function(path) {
self.currentPath(path); self.currentPath(path);
self.fileLimit = 5; self.fileLimit = 5;
...@@ -135,6 +181,9 @@ $(function() { ...@@ -135,6 +181,9 @@ $(function() {
}) })
}) })
/**
* After loadFolder completes, this function updates the UI
*/
function loadFolderDone(data) { function loadFolderDone(data) {
var viewData = []; var viewData = [];
var added = 0; var added = 0;
...@@ -147,6 +196,9 @@ $(function() { ...@@ -147,6 +196,9 @@ $(function() {
} }
} }
/**
* Add file to the displayed files list
*/
function addFile(d) { function addFile(d) {
var viewData; var viewData;
if(d.TYPE === 'D') { if(d.TYPE === 'D') {
...@@ -175,10 +227,17 @@ $(function() { ...@@ -175,10 +227,17 @@ $(function() {
} }
self.files.push(viewData); self.files.push(viewData);
} }
/**
* After 'addFile', this function animates the new item
*/
self.fadeIn = function(e) { self.fadeIn = function(e) {
$(e).hide().slideDown(500); $(e).hide().slideDown(500);
} }
self.currentPath = ko.observable('/');
/**
* Shows 5 more files (in the current folder)
*/
self.showMore = function() { self.showMore = function() {
for(var i = self.fileLimit; i < self.fileLimit + 5; i++) { for(var i = self.fileLimit; i < self.fileLimit + 5; i++) {
if(self.allFiles[i] === undefined) break; if(self.allFiles[i] === undefined) break;
...@@ -186,6 +245,10 @@ $(function() { ...@@ -186,6 +245,10 @@ $(function() {
} }
self.fileLimit += 5; self.fileLimit += 5;
} }
/**
* Downloads the specified file (or folder zipped)
*/
self.download = function(item) { self.download = function(item) {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
...@@ -197,6 +260,10 @@ $(function() { ...@@ -197,6 +260,10 @@ $(function() {
} }
}) })
} }
/**
* Deletes the specified file (or folder)
*/
self.delete = function(item) { self.delete = function(item) {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
...@@ -208,6 +275,10 @@ $(function() { ...@@ -208,6 +275,10 @@ $(function() {
} }
}) })
} }
/**
* Renames the specified file
*/
self.rename = function(item, e) { self.rename = function(item, e) {
$(e.target).parent().parent().parent().unbind('click'); $(e.target).parent().parent().parent().unbind('click');
$(e.target).parent().parent().parent().find('.name').html('<input type="text" value="' + item.originalName + '" />\ $(e.target).parent().parent().parent().find('.name').html('<input type="text" value="' + item.originalName + '" />\
...@@ -226,7 +297,10 @@ $(function() { ...@@ -226,7 +297,10 @@ $(function() {
}) })
}) })
} }
self.uploadURL = ko.observable('/');
/**
* Requests a new upload link from the store server
*/
self.getUploadURL = function() { self.getUploadURL = function() {
uploadURLRequestInProgress=true; uploadURLRequestInProgress=true;
$.ajax({ $.ajax({
...@@ -240,7 +314,10 @@ $(function() { ...@@ -240,7 +314,10 @@ $(function() {
} }
}) })
} }
self.newFolderName = ko.observable();
/**
* Creates a new folder (and then reloads the current folder)
*/
self.newFolder = throttle(function(i, e) { self.newFolder = throttle(function(i, e) {
$(e.target).parent().parent().parent().removeClass('opened'); $(e.target).parent().parent().parent().removeClass('opened');
$.ajax({ $.ajax({
...@@ -253,7 +330,10 @@ $(function() { ...@@ -253,7 +330,10 @@ $(function() {
} }
}) })
}); });
self.uploadProgress = ko.observable('0%');
/**
* Drag'n'drop tests
*/
var tests = { var tests = {
filereader: typeof FileReader != 'undefined', filereader: typeof FileReader != 'undefined',
dnd: 'draggable' in document.createElement('span'), dnd: 'draggable' in document.createElement('span'),
...@@ -261,6 +341,9 @@ $(function() { ...@@ -261,6 +341,9 @@ $(function() {
progress: "upload" in new XMLHttpRequest progress: "upload" in new XMLHttpRequest
}; };
/**
* Uploads the specified file(s)
*/
function readfiles(files) { function readfiles(files) {
var formData = tests.formdata ? new FormData() : null; var formData = tests.formdata ? new FormData() : null;
for(var i = 0; i < files.length; i++) { for(var i = 0; i < files.length; i++) {
...@@ -314,6 +397,10 @@ $(function() { ...@@ -314,6 +397,10 @@ $(function() {
xhr.send(formData); xhr.send(formData);
} }
} }
/**
* Drag'n'drop listeners
*/
document.addEventListener('drop', function(e) { document.addEventListener('drop', function(e) {
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
...@@ -328,27 +415,10 @@ $(function() { ...@@ -328,27 +415,10 @@ $(function() {
e.preventDefault(); e.preventDefault();
return false; return false;
}); });
self.quota = {
rawUsed: ko.observable(0),
rawSoft: ko.observable(0),
rawHard: ko.observable(0)
};
self.quota.used = ko.computed(function() {
return convert(self.quota.rawUsed(),1);
});
self.quota.hard = ko.computed(function() {
return convert(self.quota.rawHard(),1);
});
self.quota.soft = ko.computed(function() {
return convert(self.quota.rawSoft(),1);
});
self.quota.usedBar = ko.computed(function() {
return(self.quota.rawUsed() / self.quota.rawHard() * 100).toFixed(0) + '%';
}, self);
self.quota.softPos = ko.computed(function() {
return(self.quota.rawSoft() / self.quota.rawHard() * 100).toFixed(0) + '%';
}, self)
/**
* Fetch quota information
*/
function refreshQuota() { function refreshQuota() {
$.ajax({ $.ajax({
'type': 'GET', 'type': 'GET',
...@@ -361,6 +431,8 @@ $(function() { ...@@ -361,6 +431,8 @@ $(function() {
} }
}) })
} }
//initialization
refreshQuota(); refreshQuota();
loadFolder(self.currentPath()); loadFolder(self.currentPath());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment