Newer
Older
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
//funzione che definisce tutti gli elementi responsabili dell'apertura di un file.
//Sono definiti quindi l'albero e il bottone per l'importazione da locale
function setImportFile() {
//genero e leggo il contenuto della directory "filesystem"
var jsonResponse;
var xmlListingFile = new XMLHttpRequest();
xmlListingFile.open("GET", "https://baltig.infn.it/api/v4/projects/819/repository/tree?recursive=1&path=XRF-File-System", false);
xmlListingFile.onreadystatechange = function () {
if (xmlListingFile.readyState === 4) {
if (xmlListingFile.status === 200) {
jsonResponse = JSON.parse(xmlListingFile.responseText);
}
}
};
xmlListingFile.send(null);
//ora genero l'albero e definisco l'evento in caso di selezione di un nodo
$('#FileTreeview').treeview({ data: generateTree(jsonResponse) });
$('#FileTreeview').on('nodeSelected', function (e, node) {
if (node['url'] != undefined) {
$("#load-spinner").css("display", "inline");
$("#wrapper").css("opacity", "0.25");
openFileFromServer(node['url']);
}
});
//bottone per l'importazione locale
var fileInputButton = document.getElementById('myImport');
fileInputButton.onchange = function () {
$("#load-spinner").css("display", "inline");
$("#wrapper").css("opacity", "0.25");
var fileName = fileInputButton.files[0];
var readerObject = new FileReader();
readerObject.readAsBinaryString(fileName);
readerObject.onload = function () {
var fileString = readerObject.result;
readData(fileString);
};
};
}
//funzione che genera automaticamente l'albero
function generateTree(jsonDoc) {
var tree = [{
text: "XRF-File-System",
nodes: []
}];
//scorro ogni elemento dell'albero
for (var i = 0; i < jsonDoc.length; i++) {
if (jsonDoc[i].type == "tree") {
var Folder = {
text: jsonDoc[i].name,
relativePath: jsonDoc[i].path,
nodes: []
};
insertOBJinFS(Folder, tree);
}
else {
var File = {
text: jsonDoc[i].name,
relativePath: jsonDoc[i].path,
icon: "glyphicon glyphicon-file",
selectedIcon: "glyphicon glyphicon-file",
url: jsonDoc[i].path
};
//inserisco il file nella giusta posizione
insertOBJinFS(File, tree);
}
}
return tree;
}
//funzione che posiziona l'oggetto passato in input nell'albero
function insertOBJinFS(objfs, tree) {
//tree[0].nodes.push(objfs);
var parentPath = objfs.relativePath.slice(0, objfs.relativePath.lastIndexOf("/"));
//se l'oggetto è figlio della radice lo colloco subito
if (parentPath == tree[0].text) {
tree[0].nodes.push(objfs);
}
else {
//ricavo la profondità e posiziono l'oggetto
var splitPath = parentPath.split("/");
var depth = splitPath.length;
switch (depth) {
case 2:
var partialParentPath = splitPath[0] + "/" + splitPath[1];
for (var i = 0; i < tree[0].nodes.length; i++) {
if (partialParentPath == tree[0].nodes[i].relativePath) {
tree[0].nodes[i].nodes.push(objfs);
break;
}
}
break;
case 3:
var partialParentPath = splitPath[0] + "/" + splitPath[1];
for (var i = 0; i < tree[0].nodes.length; i++) {
if (partialParentPath == tree[0].nodes[i].relativePath) {
partialParentPath = splitPath[0] + "/" + splitPath[1] + "/" + splitPath[2];
for (var j = 0; j < tree[0].nodes[i].nodes.length; j++) {
if (partialParentPath == tree[0].nodes[i].nodes[j].relativePath) {
tree[0].nodes[i].nodes[j].nodes.push(objfs);
break;
}
}
break;
}
}
break;
case 4:
var partialParentPath = splitPath[0] + "/" + splitPath[1];
for (var i = 0; i < tree[0].nodes.length; i++) {
if (partialParentPath == tree[0].nodes[i].relativePath) {
partialParentPath = splitPath[0] + "/" + splitPath[1] + "/" + splitPath[2];
for (var j = 0; j < tree[0].nodes[i].nodes.length; j++) {
if (partialParentPath == tree[0].nodes[i].nodes[j].relativePath) {
partialParentPath = splitPath[0] + "/" + splitPath[1] + "/" + splitPath[2] + "/" + splitPath[3];
for (var k = 0; k < tree[0].nodes[i].nodes[j].nodes.length; k++) {
if (partialParentPath == tree[0].nodes[i].nodes[j].nodes[k].relativePath) {
tree[0].nodes[i].nodes[j].nodes[k].nodes.push(objfs);
break;
}
}
break;
}
}
break;
}
}
break;
case 5:
var partialParentPath = splitPath[0] + "/" + splitPath[1];
for (var i = 0; i < tree[0].nodes.length; i++) {
if (partialParentPath == tree[0].nodes[i].relativePath) {
partialParentPath = splitPath[0] + "/" + splitPath[1] + "/" + splitPath[2];
for (var j = 0; j < tree[0].nodes[i].nodes.length; j++) {
if (partialParentPath == tree[0].nodes[i].nodes[j].relativePath) {
partialParentPath = splitPath[0] + "/" + splitPath[1] + "/" + splitPath[2] + "/" + splitPath[3];
for (var k = 0; k < tree[0].nodes[i].nodes[j].nodes.length; k++) {
if (partialParentPath == tree[0].nodes[i].nodes[j].nodes[k].relativePath) {
partialParentPath = splitPath[0] + "/" + splitPath[1] + "/" + splitPath[2] + "/" + splitPath[3] + "/" + splitPath[4];
for (var l = 0; l < tree[0].nodes[i].nodes[j].nodes[k].nodes.length; l++) {
if (partialParentPath == tree[0].nodes[i].nodes[j].nodes[k].nodes[l].relativePath) {
tree[0].nodes[i].nodes[j].nodes[k].nodes[l].nodes.push(objfs);
break;
}
}
break;
}
}
break;
}
}
break;
}
}
break;
}
}
}
//funzione che dato l'url di un file, lo apre e lo legge passandone il contenuto
//alla funzione readData(). Questa funzione è invocata quando viene selezionato
//un file dall'albero
function openFileFromServer(url) {
console.log("Try to open " + url.slice(url.lastIndexOf("/") + 1, url.length));
url = "https://baltig.infn.it/api/v4/projects/819/repository/files/" + encodeURIComponent(url) + "?ref=master";
var jsonContentFile = new XMLHttpRequest();
jsonContentFile.open("GET", url, false);
jsonContentFile.onreadystatechange = function () {
if (jsonContentFile.readyState === 4) {
if (jsonContentFile.status === 200) {
var jsonResponse = jsonContentFile.responseText;
var startPoint = jsonResponse.indexOf("content") + 10;
var endPoint = jsonResponse.indexOf("\", :ref");
readData(atob(jsonResponse.slice(startPoint, endPoint)));
}
}
};
jsonContentFile.send(null);
}