main.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 (let 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 fetchGetButtons() {
  25. window.fetch(window.restBaseUrl + 'GetButtons', {
  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. var 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. }
  56. setupSections();
  57. window.fetch('webUiSettings.json').then(res => {
  58. return res.json()
  59. }).then(res => {
  60. processWebuiSettingsJson(res)
  61. fetchGetButtons()
  62. fetchGetLogs()
  63. window.buttonInterval = setInterval(fetchGetButtons, 3000);
  64. }).catch(err => {
  65. showBigError('fetch-webui-settings', 'getting webui settings', err)
  66. })