home.js 3.71 KB
$(document).ready(function() {

/********** Tableau **********/

  var tableNodes = $('#nodes-table').DataTable( {
      select: {
          style: 'multi'
      }
  } );

  var nodeSet = new Set();

  tableNodes.on('select', function (e, dt, type, indexes) {
    var rowData = tableNodes.rows(indexes).data().toArray()[0];
    nodeSet.add(rowData[0]);
  } );

  tableNodes.on('deselect', function (e, dt, type, indexes) {
    var rowData = tableNodes.rows(indexes).data().toArray()[0];
    nodeSet.delete(rowData[0]);
  } );

/********** Drop Zone **********/

  var formData = new FormData();
  var dropZone = document.querySelector("#drop-zone");
  dropZone.style.cursor = "pointer";
  var file = document.getElementById("filesExplorer");
  var fileProgress = document.getElementById("file-progress");
  var readyToSend = false;

  dropZone.addEventListener('click', function() {
    delete formData;
    formData = new FormData();
    file.click();
  })

  file.addEventListener('change', function() {
    dropZone.innerHTML = this.files[0].name;
    formData.append("file", this.files[0]);
    formData.append("filename", this.files[0].name);
    readyToSend = true;
  }, false)

  dropZone.addEventListener('drop', function(e) {
    e.preventDefault();
    delete formData;
    formData = new FormData();
    dropZone.style.borderWidth = '2px';
    var files = e.dataTransfer.files;

    if(files.length > 1) {
      $("#warningFilesNumber").modal()
      return;
    }
    dropZone.innerHTML = files[0].name;
    formData.append("file", files[0]);
    formData.append("filename", files[0].name);
    readyToSend = true;
  }, false)

  dropZone.addEventListener('dragenter', function(e) {
      console.log("Enter");
      dropZone.style.borderWidth = '5px';
  }, false);

  dropZone.addEventListener('dragleave', function(e) {
    console.log("Leave");
    dropZone.style.borderWidth = '2px';
  }, false);

  dropZone.addEventListener('dragover', function(e) {
      e.preventDefault();
  }, false);

  document.getElementById("sendButton").addEventListener('click', function() {
    var modalContent = document.getElementById("modal-content");
    var modalTitle = document.getElementById("modal-title");
    if(readyToSend && nodeSet.size > 0) {
      formData.append("nodes", Array.from(nodeSet).join(';'));
      var request = new XMLHttpRequest();
      request.open("POST", "file.php");

      request.upload.onloadstart = function(e) {
        fileProgress.style.display = 'block';
        fileProgress.style.width = 0 + "px"
        fileProgress.innerHTML = "0%";
      }

      request.upload.onprogress = function(e) {
        var p = 100 - ((e.total - e.loaded) / e.total * 100);
        fileProgress.style.width = Math.ceil(p) + "%"
        fileProgress.innerHTML = Math.ceil(p) + "%";
      }

      request.upload.onloadend = function(e) {
        fileProgress.style.width = 100 + "%"
        fileProgress.innerHTML = "100%";
        var modalButton = document.getElementById("modal-button");

        modalButton.addEventListener('click', function(e) {
          location.reload();
        })

        modalContent.innerHTML = "Upload terminé !";
        modalTitle.innerHTML = "Félicitations";
        $("#warningFilesNumber").modal()
      }

      request.send(formData);

      delete formData;
      formData = new FormData();
    }
    else {
      if(readyToSend) {
        modalContent.innerHTML = "Veuillez choisir un ou plusieurs noeuds !";
      }
      else {
        modalContent.innerHTML = "Veuillez choisir un fichier !";
      }
      modalTitle.innerHTML = "Attention";
      $("#warningFilesNumber").modal()
    }
  })

} );