results.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // create
  2. const toggleVisible = ele => ele.style.display = ele.style.display === 'none' ? 'block' : 'none'
  3. const toggleClass = (elem, class1, class2) => elem.className = elem.className === class1 ? class2 : class1
  4. // Download endpoint response as a file using a POST request
  5. const downloadContent = path => {
  6. const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value
  7. if (BASE == ''){
  8. var downloadUrl = '/admin/download'
  9. }else{
  10. var downloadUrl = '/' + BASE + '/admin/download'
  11. }
  12. fetch(downloadUrl, {
  13. method: 'POST',
  14. body: `path=${path}`,
  15. headers: {
  16. 'Content-type': 'application/x-www-form-urlencoded',
  17. 'X-CSRFToken': csrfToken
  18. }
  19. }).then(async res => {
  20. console.log(res)
  21. if (res.status === 200) {
  22. // Try to find out the filename from the content disposition `filename` value
  23. const disposition = res.headers.get('Content-Disposition')
  24. // expe is find from django
  25. const filename = `${expe_name}_${disposition.split('=')[1]}`
  26. const blob = await res.blob()
  27. // use of `FileSaver`
  28. saveAs(blob, filename)
  29. }
  30. })
  31. }
  32. window.addEventListener('DOMContentLoaded', () => {
  33. // Display list of files from day folder
  34. // need to parse as `Array`
  35. Array.from(document.getElementsByClassName('date-folder-list')).forEach(item => {
  36. item.addEventListener('click', event => {
  37. event.preventDefault()
  38. currentElem = event.currentTarget
  39. // get list element
  40. list = currentElem.parentElement.nextElementSibling
  41. // display or hide list elements
  42. toggleVisible(list)
  43. // toggle arrow class for display effect
  44. iconElem = currentElem.children[0]
  45. toggleClass(iconElem, 'fas fa-arrow-circle-right', 'fas fa-arrow-circle-down')
  46. })
  47. })
  48. // need to parse as `Array`
  49. Array.from(document.getElementsByClassName('download-list')).forEach(downloadElem => {
  50. downloadElem.addEventListener('click', event => {
  51. event.preventDefault()
  52. currentElem = event.currentTarget
  53. pathDownload = currentElem.getAttribute('data-download-path')
  54. downloadContent(pathDownload)
  55. })
  56. })
  57. })