links.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const links_data = JSON.parse(links.replace(/"/g, '"'))
  2. const links_nb_elem = Object.keys(links_data).length
  3. const alertDiv = document.getElementsByClassName('alert-danger')[0]
  4. function loadDataList(elem, list){
  5. userId = elem.value
  6. if (userId){
  7. // check if nomber of links can let access to userId
  8. if (userId > (links_nb_elem - 1) || userId < 0){
  9. alertDiv.setAttribute('style', 'display:block')
  10. }else{
  11. alertDiv.setAttribute('style', 'display:none')
  12. }
  13. let currentLinks = links_data[userId]
  14. // remove event listener of each element by default
  15. if (list.children.length > 0){
  16. for (var element of list.children){
  17. element.removeEventListener('click', elemClick)
  18. }
  19. }
  20. list.innerHTML = ""
  21. currentLinks.forEach((element, index) => {
  22. if (element.length > 0){
  23. data = element.split(':::')
  24. // add of div elements
  25. rowDiv = document.createElement('div')
  26. rowDiv.setAttribute('class', 'row')
  27. rowDivLeft = document.createElement('div')
  28. rowDivLeft.setAttribute('class', 'col-md-11')
  29. rowDivRight = document.createElement('div')
  30. rowDivRight.setAttribute('class', 'col-md-1')
  31. // create link
  32. currentLink = document.createElement('a')
  33. currentLink.setAttribute('href', data[1])
  34. currentLink.innerHTML = 'Link ' + (index + 1) + ': ' + data[0]
  35. // add of elements
  36. rowDivLeft.appendChild(currentLink)
  37. rowDiv.appendChild(rowDivLeft)
  38. rowDiv.appendChild(rowDivRight)
  39. currentLi = document.createElement('li')
  40. currentLi.setAttribute('class', 'list-group-item')
  41. currentLi.appendChild(rowDiv)
  42. list.appendChild(currentLi)
  43. }
  44. });
  45. }
  46. }
  47. function elemClick(event){
  48. event.preventDefault()
  49. currentElem = event.currentTarget
  50. // Add <i class="fas fa-check"></i>
  51. divLeft = currentElem.getElementsByClassName('col-md-1')[0]
  52. if (divLeft.children.length <= 0){
  53. iconElem = document.createElement('li')
  54. iconElem.setAttribute('class', 'fas fa-check')
  55. divLeft.appendChild(iconElem)
  56. // update `li` class element
  57. currentElem.setAttribute('class', 'list-group-item already-used')
  58. }
  59. // retrieve and open link in new tab
  60. link = currentElem.getElementsByTagName('a')[0]
  61. url = link.getAttribute('href')
  62. var win = window.open(url, '_blank');
  63. win.focus();
  64. }
  65. function generateCalibrationLink(link){
  66. // get and rebuild b64 link part using current info and calibration experiment
  67. b64Part = link.split('?q=')[1]
  68. jsonLinkData = JSON.parse(atob(b64Part))
  69. jsonLinkData['experimentName'] = 'CalibrationMeasurement'
  70. jsonLinkData['sceneName'] = '50_shades_of_grey'
  71. b64LinkData = btoa(JSON.stringify(jsonLinkData)).replace(/[=]/g, '')
  72. calibrationLink = jsonLinkData['hostConfig'] + '/#/?q=' + b64LinkData
  73. console.log(JSON.stringify(jsonLinkData))
  74. // add to calibration DOM element the new generated link
  75. domCalibrationLink = document.getElementById('calibration-link')
  76. domCalibrationLink.setAttribute('href', calibrationLink)
  77. // Add click event
  78. domCalibrationLink.closest('li').addEventListener('click', elemClick)
  79. }
  80. window.addEventListener('DOMContentLoaded', () => {
  81. // Display list of files from day folder
  82. // need to parse as `Array`
  83. const inputElement = document.getElementsByName('userId')[0]
  84. const linksList = document.getElementById('links-list')
  85. // load and generate CalibrationMeasurement experiment link
  86. firstLink = links_data[0][0].split(':::')[1]
  87. generateCalibrationLink(firstLink)
  88. loadDataList(inputElement, linksList)
  89. inputElement.addEventListener('keyup', event => {
  90. event.preventDefault()
  91. currentElem = event.currentTarget
  92. loadDataList(currentElem, linksList)
  93. for (var element of linksList.children){
  94. element.addEventListener('click', elemClick)
  95. }
  96. })
  97. })