main.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict'
  2. import { marshalActionButtonsJsonToHtml, marshalLogsJsonToHtml } from './js/marshaller.js'
  3. import { checkWebsocketConnection } from './js/websocket.js'
  4. function showSection (name) {
  5. for (const otherName of ['Actions', 'Logs']) {
  6. document.getElementById('show' + otherName).classList.remove('activeSection')
  7. document.getElementById('content' + otherName).hidden = true
  8. }
  9. document.getElementById('show' + name).classList.add('activeSection')
  10. document.getElementById('content' + name).hidden = false
  11. document.getElementById('hide-sidebar-checkbox').checked = true
  12. }
  13. function setupSections () {
  14. document.getElementById('showActions').onclick = () => { showSection('Actions') }
  15. document.getElementById('showLogs').onclick = () => { showSection('Logs') }
  16. showSection('Actions')
  17. }
  18. function refreshLoop () {
  19. if (window.websocketAvailable) {
  20. // Websocket updates are streamed live, not updated on a loop.
  21. } else if (window.restAvailable) {
  22. // Fallback to rest, but try to reconnect the websocket anyway.
  23. fetchGetDashboardComponents()
  24. fetchGetLogs()
  25. checkWebsocketConnection()
  26. } else {
  27. // Still try to fetch the dashboard, if successfull window.restAvailable = true
  28. fetchGetDashboardComponents()
  29. }
  30. refreshServerConnectionLabel()
  31. }
  32. function refreshServerConnectionLabel () {
  33. if (window.restAvailable) {
  34. document.querySelector('#serverConnectionRest').classList.remove('error')
  35. } else {
  36. document.querySelector('#serverConnectionRest').classList.add('error')
  37. }
  38. if (window.websocketAvailable) {
  39. document.querySelector('#serverConnectionWebSocket').classList.remove('error')
  40. } else {
  41. document.querySelector('#serverConnectionWebSocket').classList.add('error')
  42. }
  43. }
  44. function fetchGetDashboardComponents () {
  45. window.fetch(window.restBaseUrl + 'GetDashboardComponents', {
  46. cors: 'cors'
  47. }).then(res => {
  48. return res.json()
  49. }).then(res => {
  50. if (!window.restAvailable) {
  51. window.clearBigErrors('fetch-buttons')
  52. }
  53. window.restAvailable = true
  54. marshalActionButtonsJsonToHtml(res)
  55. refreshServerConnectionLabel() // in-case it changed, update the label quicker
  56. }).catch((err) => { // err is 1st arg
  57. window.restAvailable = false
  58. window.showBigError('fetch-buttons', 'getting buttons', err, 'blat')
  59. })
  60. }
  61. function fetchGetLogs () {
  62. window.fetch(window.restBaseUrl + 'GetLogs', {
  63. cors: 'cors'
  64. }).then(res => {
  65. return res.json()
  66. }).then(res => {
  67. marshalLogsJsonToHtml(res)
  68. }).catch(err => {
  69. window.showBigError('fetch-buttons', 'getting buttons', err, 'blat')
  70. })
  71. }
  72. function processWebuiSettingsJson (settings) {
  73. window.restBaseUrl = settings.Rest
  74. if (settings.ThemeName) {
  75. const themeCss = document.createElement('link')
  76. themeCss.setAttribute('rel', 'stylesheet')
  77. themeCss.setAttribute('type', 'text/css')
  78. themeCss.setAttribute('href', '/themes/' + settings.ThemeName + '/theme.css')
  79. document.head.appendChild(themeCss)
  80. }
  81. document.querySelector('#currentVersion').innerText = settings.CurrentVersion
  82. if (settings.ShowNewVersions && settings.AvailableVersion !== 'none') {
  83. document.querySelector('#available-version').innerText = 'New Version Available: ' + settings.AvailableVersion
  84. document.querySelector('#available-version').hidden = false
  85. }
  86. document.querySelector('#perma-widget').hidden = !settings.ShowNavigation
  87. document.querySelector('footer[title="footer"]').hidden = !settings.ShowFooter
  88. if (settings.PageTitle) {
  89. document.title = settings.PageTitle
  90. const titleElem = document.querySelector('#page-title')
  91. if (titleElem) titleElem.innerText = settings.PageTitle
  92. }
  93. }
  94. function main () {
  95. setupSections()
  96. window.fetch('webUiSettings.json').then(res => {
  97. return res.json()
  98. }).then(res => {
  99. processWebuiSettingsJson(res)
  100. window.restAvailable = true
  101. window.refreshLoop = refreshLoop
  102. window.refreshLoop()
  103. setInterval(refreshLoop, 3000)
  104. }).catch(err => {
  105. window.showBigError('fetch-webui-settings', 'getting webui settings', err)
  106. })
  107. }
  108. main() // call self