results.js 2.3 KB

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