Skip to content
Snippets Groups Projects
Commit f43f9cd1 authored by Laura Cappelli's avatar Laura Cappelli
Browse files

Prova eliminazione file .js

parent cef4de16
No related branches found
No related tags found
Loading
//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) {
openFileFromServer(node['url']);
}
});
//bottone per l'importazione locale
var fileInputButton = document.getElementById('myImport');
fileInputButton.onchange = function () {
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);
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment