main.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict'
  2. import { marshalActionButtonsJsonToHtml, marshalLogsJsonToHtml } from './js/marshaller.js'
  3. function showSection (name) {
  4. for (const otherName of ['Actions', 'Logs']) {
  5. document.getElementById('show' + otherName).classList.remove('activeSection')
  6. document.getElementById('content' + otherName).hidden = true
  7. }
  8. document.getElementById('show' + name).classList.add('activeSection')
  9. document.getElementById('content' + name).hidden = false
  10. }
  11. function setupSections () {
  12. document.getElementById('showActions').onclick = () => { showSection('Actions') }
  13. document.getElementById('showLogs').onclick = () => { showSection('Logs') }
  14. showSection('Actions')
  15. }
  16. function fetchGetDashboardComponents () {
  17. window.fetch(window.restBaseUrl + 'GetDashboardComponents', {
  18. cors: 'cors'
  19. }).then(res => {
  20. return res.json()
  21. }).then(res => {
  22. marshalActionButtonsJsonToHtml(res)
  23. }).catch(err => {
  24. window.showBigError('fetch-buttons', 'getting buttons', err, 'blat')
  25. })
  26. }
  27. function fetchGetLogs () {
  28. window.fetch(window.restBaseUrl + 'GetLogs', {
  29. cors: 'cors'
  30. }).then(res => {
  31. return res.json()
  32. }).then(res => {
  33. marshalLogsJsonToHtml(res)
  34. }).catch(err => {
  35. window.showBigError('fetch-buttons', 'getting buttons', err, 'blat')
  36. })
  37. }
  38. function processWebuiSettingsJson (settings) {
  39. window.restBaseUrl = settings.Rest
  40. if (settings.ThemeName) {
  41. const themeCss = document.createElement('link')
  42. themeCss.setAttribute('rel', 'stylesheet')
  43. themeCss.setAttribute('type', 'text/css')
  44. themeCss.setAttribute('href', '/themes/' + settings.ThemeName + '/theme.css')
  45. document.head.appendChild(themeCss)
  46. }
  47. document.querySelector('#currentVersion').innerText = 'Version: ' + settings.CurrentVersion
  48. if (settings.ShowNewVersions && settings.AvailableVersion !== 'none') {
  49. document.querySelector('#available-version').innerText = 'New Version Available: ' + settings.AvailableVersion
  50. document.querySelector('#available-version').hidden = false
  51. }
  52. document.querySelector('#section-switcher').hidden = !settings.ShowNavigation
  53. document.querySelector('footer[title="footer"]').hidden = !settings.ShowFooter
  54. if (settings.PageTitle) {
  55. document.title = settings.PageTitle
  56. const titleElem = document.querySelector('#page-title')
  57. if (titleElem) titleElem.innerText = settings.PageTitle
  58. }
  59. }
  60. function main () {
  61. setupSections()
  62. window.fetch('webUiSettings.json').then(res => {
  63. return res.json()
  64. }).then(res => {
  65. processWebuiSettingsJson(res)
  66. fetchGetDashboardComponents()
  67. fetchGetLogs()
  68. window.buttonInterval = setInterval(fetchGetDashboardComponents, 3000)
  69. }).catch(err => {
  70. window.showBigError('fetch-webui-settings', 'getting webui settings', err)
  71. })
  72. }
  73. main() // call self