links.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. window.addEventListener('DOMContentLoaded', () => {
  66. // Display list of files from day folder
  67. // need to parse as `Array`
  68. const inputElement = document.getElementsByName('userId')[0]
  69. const linksList = document.getElementById('links-list')
  70. loadDataList(inputElement, linksList)
  71. inputElement.addEventListener('keyup', event => {
  72. event.preventDefault()
  73. currentElem = event.currentTarget
  74. loadDataList(currentElem, linksList)
  75. for (var element of linksList.children){
  76. element.addEventListener('click', elemClick)
  77. }
  78. })
  79. })