Skip to content

Commit 45fa4af

Browse files
committed
tree contents dynamically loaded
1 parent 1d0bfeb commit 45fa4af

1 file changed

Lines changed: 49 additions & 28 deletions

File tree

js/tree.js

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,49 @@ function sort_by_title(a,b) {
2828
//gets the contents of a folder
2929
function tree_folder(id, callback){
3030
if(cloud_use === "drive"){
31-
var ret = [];
3231
//gets all the files
32+
var ret = 0;
3333
drive.retrieveAllFilesInFolder(id, function(data, _root){
3434
var goal = data.length; //how many files total
35+
if(goal === 0){
36+
tree_prime(_root);
37+
callback();
38+
}
3539
for(var i = 0; i < data.length; i++){
3640
var id_id = data[i].id;
3741
//add placeholder element
3842
tree_placeholder(id_id, _root);
3943

4044
//for each of the files, gets the information
4145
drive.getFile(id_id, function(resp){
42-
var to_push = {
43-
title: resp.title,
44-
id: resp.id,
45-
folder: (resp.mimeType === "application/vnd.google-apps.folder") //is it a folder?
46-
};
4746
if(resp.explicitlyTrashed === true){ //if trashed, don't count
4847
goal--;
4948
}
5049
else{
51-
ret.push(to_push);
50+
//add it here
51+
ret++;
52+
if(ret === 1){
53+
tree_prime(_root);
54+
}
55+
treeInsert(resp.title, resp.id, (resp.mimeType === "application/vnd.google-apps.folder"), false, _root);
5256
}
53-
if(ret.length === goal){
54-
callback(ret.sort(sort_by_title));
57+
if(ret === goal){
58+
callback(); //this is the finisher
5559
}
5660
});
5761
}
5862
});
5963
}
6064
else if(cloud_use === "sky"){
61-
var ret = [];
6265
sky.retrieveAllFilesInFolder(id, function(data, _root){
6366
for(var i = 0; i < data.length; i++){
64-
var to_push = {
65-
title: data[i].name,
66-
id: data[i].id,
67-
folder: (data[i].id.indexOf("folder") === 0)
68-
};
69-
ret.push(to_push);
67+
//open it up!
68+
tree_prime(_root);
69+
70+
//add it here
71+
treeInsert(data[i].name, data[i].id, (data[i].id.indexOf("folder") === 0), true, _root);
7072
}
71-
callback(ret.sort(sort_by_title));
73+
callback(); //this is the finisher
7274
});
7375
}
7476
}
@@ -80,6 +82,23 @@ function tree_placeholder(id, root){
8082
$("[data-tree-ul='"+root+"']").append(to_push);
8183
}
8284

85+
function treeInsert(title, the_id, folder, sky, root){
86+
var html = tree_insert({
87+
title: title,
88+
id: the_id,
89+
folder: folder
90+
});
91+
92+
if(sky === false){
93+
//google drive
94+
$("center[data-placeholder='"+the_id+"']").replaceWith(html);
95+
}
96+
else{
97+
//skydrive
98+
$("[data-tree-ul='"+root+"']").append(html);
99+
}
100+
}
101+
83102
function tree_insert(resp){
84103
var ret = "";
85104
var title = resp.title;
@@ -99,24 +118,26 @@ function tree_insert(resp){
99118
return ret;
100119
}
101120

121+
function tree_prime(id){
122+
//data loading
123+
//opens the folder
124+
$("[data-tree-ul='"+id+"']").slideDown();
125+
}
126+
127+
function tree_finish(id){
128+
//data done loading
129+
}
130+
102131
//sets the tree contents for a folder
103132
function get_tree(id){
104133
//gets the array of files/folders, passes to callback
105134
$("[data-tree-ul='"+id+"']").html("");
106-
tree_folder(id, function(data){
107-
var ret = ""; //the html that will be returned
108-
for(var i = 0; i < data.length; i++){
109-
ret += tree_insert(data[i]);
110-
}
111-
//sets the html
112-
$("[data-tree-ul='"+id+"']").append(ret);
113-
//opens the folder
114-
$("[data-tree-ul='"+id+"']").slideDown();
135+
tree_folder(id, function(){
115136
//removes the loading icon
116137
$("[data-tree-li='"+id+"']>span>i").removeClass("fa-folder");
117-
$("[data-tree-li='"+id+"']>span>i").removeClass("fa-circle-o-notch");
138+
$("[data-tree-li='"+id+"']>span>i").removeClass("fa-circle-o-notch");
118139
$("[data-tree-li='"+id+"']>span>i").removeClass("fa-spin");
119-
$("[data-tree-li='"+id+"']>span>i").addClass("fa-folder-open");
140+
$("[data-tree-li='"+id+"']>span>i").addClass("fa-folder-open");
120141
});
121142
}
122143
//what happens when you click on a folder

0 commit comments

Comments
 (0)