From 4b5eb09d9e08f55e9f15c99a46bfb95eb540308a Mon Sep 17 00:00:00 2001 From: Francesco Giacomini <giaco at cnaf dot infn dot it> Date: Thu, 2 Nov 2017 17:26:46 +0100 Subject: [PATCH] waiting for a sane deployment model, push also js files --- loaddir.js | 222 ++++++++++++++++++++++++----------------------------- start.js | 29 +++---- 2 files changed, 117 insertions(+), 134 deletions(-) diff --git a/loaddir.js b/loaddir.js index e69cf36..8f066d2 100644 --- a/loaddir.js +++ b/loaddir.js @@ -2,23 +2,24 @@ //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 xmlDoc; 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.open("PROPFIND", "CHNET/", false); + xmlListingFile.setRequestHeader("Depth", "infinity"); xmlListingFile.onreadystatechange = function () { if (xmlListingFile.readyState === 4) { - if (xmlListingFile.status === 200) { - jsonResponse = JSON.parse(xmlListingFile.responseText); + if (xmlListingFile.status === 207) { + var parser = new DOMParser(); + xmlDoc = parser.parseFromString(xmlListingFile.responseText, "text/xml"); } } }; xmlListingFile.send(null); //ora genero l'albero e definisco l'evento in caso di selezione di un nodo - $('#FileTreeview').treeview({ data: generateTree(jsonResponse) }); + $('#FileTreeview').treeview({ data: generateTree(xmlDoc) }); $('#FileTreeview').on('nodeSelected', function (e, node) { if (node['url'] != undefined) { $("#load-spinner").css("display", "inline"); - $("#wrapper").css("opacity", "0.25"); openFileFromServer(node['url']); } }); @@ -26,7 +27,6 @@ function setImportFile() { 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); @@ -37,140 +37,122 @@ function setImportFile() { }; } //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") { +function generateTree(xmlDoc) { + var tree = []; + var first = true; + var oldFolderParent = ""; + var oldFolder = ""; + //inizio leggendo tutti gli elementi da inserire nell'albero (caratterizzati + //dall'avere un url di riferimento) + var entry = xmlDoc.getElementsByTagName("D:href"); + //per ogni elemento controllo se si tratta di una cartella o di un documento + for (var i = 0; i < entry.length; i++) { + var path = entry[i].childNodes[0].nodeValue.split(""); + //cartella, creo l'oggetto corrsipondente + if (path[path.length - 1] == "/") { + var folderName = entry[i].childNodes[0].nodeValue.split("/"); var Folder = { - text: jsonDoc[i].name, - relativePath: jsonDoc[i].path, + text: folderName[folderName.length - 2], nodes: [] }; - insertOBJinFS(Folder, tree); + //posiziono la radice del file system, ne memorizzo il path e il padre + if (first) { + tree.push(Folder); + first = false; + oldFolder = entry[i].childNodes[0].nodeValue; + oldFolderParent = + oldFolder.slice(0, oldFolder.lastIndexOf(folderName[folderName.length - 2])); + //per ogni cartella determino la relazione con la cartella precedente + } + else { + var newFolder = entry[i].childNodes[0].nodeValue; + var newFolderParent = newFolder.slice(0, newFolder.lastIndexOf(folderName[folderName.length - 2])); + //cartella sorella con quella memorizzata + if (newFolderParent == oldFolderParent) { + oldFolder = newFolder; + insertOBJinFS(Folder, tree, folderName, 0); + //cartella figlia di quella memorizzata + } + else if (newFolderParent == oldFolder) { + oldFolder = newFolder; + oldFolderParent = newFolderParent; + insertOBJinFS(Folder, tree, folderName, 0); + //nessuno dei casi precedenti + } + else { + //arretro nell'albero fino a trovare lo stesso padre. Per fare questo + //tolgo al padre memorizzato in precedenza prima "/" poi il nome dell' + //ultima cartella + while (newFolderParent != oldFolderParent) { + oldFolderParent = oldFolderParent.slice(0, oldFolderParent.length - 1); + oldFolderParent = oldFolderParent.slice(0, (oldFolderParent.lastIndexOf("/") + 1)); + } + oldFolder = newFolder; + insertOBJinFS(Folder, tree, folderName, 0); + } + } + //documento, creo l'oggetto corrispondente e lo inserisco nell'albero } else { + var fileName = entry[i].childNodes[0].nodeValue.split("/"); + var filePath = entry[i].childNodes[0].nodeValue; var File = { - text: jsonDoc[i].name, - relativePath: jsonDoc[i].path, + text: fileName[fileName.length - 1], icon: "glyphicon glyphicon-file", selectedIcon: "glyphicon glyphicon-file", - url: jsonDoc[i].path + url: filePath }; - //inserisco il file nella giusta posizione - insertOBJinFS(File, tree); + insertOBJinFS(File, tree, fileName, 1); } } 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; - } +function insertOBJinFS(objfs, tree, objfsName, type) { + //determino la profondità dell'oggetto (se è un file devo aggiungere 1 a causa di "/") + var depth = objfsName.length; + if (type) + depth++; + //in base alla profondità determino a quale oggetto agganciare quello in input + var treePosition; + var l = tree.length - 1; + switch (depth) { + case 6: + treePosition = tree; + break; + case 7: + treePosition = tree[l].nodes; + break; + case 8: + treePosition = + tree[l].nodes[tree[l].nodes.length - 1].nodes; + break; + case 9: + treePosition = + tree[l].nodes[tree[l].nodes.length - 1].nodes[tree[l].nodes[tree[l].nodes.length - 1].nodes.length - 1].nodes; + break; + case 10: + treePosition = + tree[l].nodes[tree[l].nodes.length - 1].nodes[tree[l].nodes[tree[l].nodes.length - 1].nodes.length - 1].nodes[tree[l].nodes[tree[l].nodes.length - 1].nodes[tree[l].nodes[tree[l].nodes.length - 1].nodes.length - 1].nodes.length - 1].nodes; + break; + default: break; } + treePosition[treePosition.length - 1].nodes.push(objfs); } //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))); + var fileName = url.split("/"); + console.log("Try to open " + fileName[fileName.length - 1] + " ..."); + var txtFile = new XMLHttpRequest(); + txtFile.open("GET", url, true); + txtFile.onreadystatechange = function () { + if (txtFile.readyState === 4) { + if (txtFile.status === 200) { + readData((txtFile.responseText)); } } }; - jsonContentFile.send(null); + txtFile.send(null); } diff --git a/start.js b/start.js index def0d4a..f09dfdc 100644 --- a/start.js +++ b/start.js @@ -16,10 +16,11 @@ var channelDepth = round3(((depth + 1) * a - b) / 1000); //profondità massima i var globalxMinRange = 0, globalxMaxRange = channelDepth; //INIZIO DELLO SCRIPT $(document).ready(function () { + //abilito tooltip $('[data-toggle="tooltip"]').tooltip(); //creazione dell'albero e gestione barre laterali setImportFile(); - compressingSidenavFS(); + compressingSidenav(); //abilitazione drag&drop var droppableArea = document.querySelector('.droppable'); makeDroppable(droppableArea, callback); @@ -28,24 +29,26 @@ $(document).ready(function () { zPixel2 = { xp: 0, yp: 0 }; }); //funzione per la compressione della sidenav sx -function compressingSidenavFS() { - var fsLabel = $('.fs-label'); +function compressingSidenav() { + var fsLabel = $('.sidebar-label'); var isClosedfs = false; fsLabel.click(function () { fsLabel_cross(); }); function fsLabel_cross() { if (isClosedfs == true) { isClosedfs = false; - fsLabel.removeClass('is-open'); - fsLabel.addClass('is-closed'); - $('#mySidenavfs').css('width', "0"); - $('#fsbtn').css('marginLeft', "-2px"); + $('.w3-bar-block').css('width', "65px"); + $('.text-sidenav').css('display', 'none'); + $('#collapse-symbol').attr('title', 'Open Sidebar'); + $("#collapse-symbol").removeClass("fa-angle-double-left"); + $("#collapse-symbol").addClass("fa-angle-double-right"); } else { isClosedfs = true; - fsLabel.removeClass('is-closed'); - fsLabel.addClass('is-open'); - $('#mySidenavfs').css('width', "250px"); - $('#fsbtn').css('marginLeft', "248px"); + $('.w3-bar-block').css('width', "200px"); + $('.text-sidenav').css('display', 'inline'); + $('#collapse-symbol').attr('title', 'Close Sidebar'); + $("#collapse-symbol").removeClass("fa-angle-double-right"); + $("#collapse-symbol").addClass("fa-angle-double-left"); } } } @@ -82,7 +85,6 @@ function makeDroppable(droppableArea, callback) { //della sua lettura e del passaggio del suo contenuto alla funzione readData() function callback(files) { $("#load-spinner").css("display", "inline"); - $("#wrapper").css("opacity", "0.25"); console.log("Try to open " + files[files.length - 1].name + " ..."); var readerObject = new FileReader(); readerObject.readAsBinaryString(files[files.length - 1]); @@ -372,7 +374,6 @@ function readData(fileString) { drawImg({ xp: 0, yp: 0 }, { xp: xDim - 1, yp: yDim - 1 }, 0, channelDepth); drawChart({ xp: 0, yp: 0 }, { xp: xDim - 1, yp: yDim - 1 }, 0, channelDepth); $("#load-spinner").css("display", "none"); - $("#wrapper").css("opacity", "1"); } //la funzione disegna il rettangolo durante la selezione di un'area della mappa function zoomRect() { @@ -557,7 +558,7 @@ function drawImg(pixel1, pixel2, xMinRange, xMaxRange) { $("#TrasparencySlider").mouseup(function () { drawImg(pixel1, pixel2, xMinRange, xMaxRange); }); - $(".reset").click(function () { + $("#reset").click(function () { newOrigin = { xp: 0, yp: 0 }; rePrint = false; calibrated = false; -- GitLab