main.js 2.9 KB

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