results.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. function toggle(elem) {
  2. if (elem.style.display === "none") {
  3. elem.style.display = "block";
  4. } else {
  5. elem.style.display = "none";
  6. }
  7. }
  8. function toggleClass(elem, class1, class2) {
  9. if (elem.className === class1) {
  10. elem.className = class2;
  11. } else {
  12. elem.className = class1;
  13. }
  14. }
  15. // use for call route to dowload content (as post request)
  16. function downloadContent(path){
  17. const csrfToken = document.querySelectorAll('[name=csrfmiddlewaretoken]')[0].value
  18. var xhttp = new XMLHttpRequest();
  19. xhttp.open("POST", "/admin/download", true);
  20. xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  21. xhttp.setRequestHeader("X-CSRFToken", csrfToken);
  22. xhttp.onreadystatechange = function (){
  23. if (xhttp.readyState == 4 && xhttp.status == 200) {
  24. // Try to find out the filename from the content disposition `filename` value
  25. var disposition = xhttp.getResponseHeader('content-disposition');
  26. // expe is find from django
  27. var filename = expe_name + "_" + disposition.split('=')[1]
  28. var blob = new Blob([xhttp.response], {type: "octet/stream"});
  29. saveAs(blob, filename);
  30. }
  31. };
  32. xhttp.responseType = "arraybuffer";
  33. xhttp.send("path=" + path);
  34. }
  35. window.onload = function () {
  36. // Display list of files from day folder
  37. elems = document.getElementsByClassName('date-folder-list')
  38. for (let item of elems) {
  39. item.onclick = function(event){
  40. event.preventDefault()
  41. currentElem = event.currentTarget
  42. // get list element
  43. list = currentElem.parentElement.nextElementSibling
  44. // display or hide list elements
  45. toggle(list)
  46. // toggle arrow class for display effect
  47. iconElem = currentElem.children[0]
  48. toggleClass(iconElem, 'fas fa-arrow-circle-right', 'fas fa-arrow-circle-down')
  49. }
  50. }
  51. elems = document.getElementsByClassName('download-list')
  52. for (let downloadElem of elems) {
  53. downloadElem.onclick = function(event){
  54. event.preventDefault()
  55. currentElem = event.currentTarget
  56. pathDownload = currentElem.getAttribute('data-download-path')
  57. downloadContent(pathDownload)
  58. }
  59. }
  60. }