links.js 5.2 KB

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