main.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict'
  2. import {
  3. initMarshaller,
  4. setupSectionNavigation,
  5. marshalDashboardComponentsJsonToHtml,
  6. marshalLogsJsonToHtml,
  7. refreshServerConnectionLabel,
  8. refreshDiagnostics
  9. } from './js/marshaller.js'
  10. import { checkWebsocketConnection } from './js/websocket.js'
  11. import { LoginForm } from './js/LoginForm.js'
  12. function searchLogs (e) {
  13. document.getElementById('searchLogsClear').disabled = false
  14. const searchText = e.target.value.toLowerCase()
  15. for (const row of document.querySelectorAll('tr.log-row')) {
  16. const actionTitle = row.getAttribute('title').toLowerCase()
  17. row.hidden = !actionTitle.includes(searchText)
  18. }
  19. }
  20. function searchLogsClear () {
  21. for (const row of document.querySelectorAll('tr.log-row')) {
  22. row.hidden = false
  23. }
  24. document.getElementById('searchLogsClear').disabled = true
  25. document.getElementById('logSearchBox').value = ''
  26. }
  27. function setupLogSearchBox () {
  28. document.getElementById('logSearchBox').oninput = searchLogs
  29. document.getElementById('searchLogsClear').onclick = searchLogsClear
  30. }
  31. function refreshLoop () {
  32. if (window.websocketAvailable) {
  33. // Websocket updates are streamed live, not updated on a loop.
  34. } else if (window.restAvailable) {
  35. // Fallback to rest, but try to reconnect the websocket anyway.
  36. fetchGetDashboardComponents()
  37. fetchGetLogs()
  38. checkWebsocketConnection()
  39. } else {
  40. // Still try to fetch the dashboard, if successfull window.restAvailable = true
  41. fetchGetDashboardComponents()
  42. }
  43. refreshServerConnectionLabel()
  44. }
  45. function fetchGetDashboardComponents () {
  46. window.fetch(window.restBaseUrl + 'GetDashboardComponents', {
  47. cors: 'cors'
  48. }).then(res => {
  49. if (!res.ok && res.status === 403) {
  50. return null
  51. }
  52. return res.json()
  53. }).then(res => {
  54. if (!window.restAvailable) {
  55. window.clearBigErrors()
  56. }
  57. window.restAvailable = true
  58. marshalDashboardComponentsJsonToHtml(res)
  59. refreshServerConnectionLabel() // in-case it changed, update the label quicker
  60. }).catch((err) => { // err is 1st arg
  61. window.restAvailable = false
  62. window.showBigError('fetch-buttons', 'getting buttons', err, false)
  63. })
  64. }
  65. function fetchGetLogs () {
  66. window.fetch(window.restBaseUrl + 'GetLogs', {
  67. cors: 'cors'
  68. }).then(res => {
  69. return res.json()
  70. }).then(res => {
  71. marshalLogsJsonToHtml(res)
  72. }).catch(err => {
  73. window.showBigError('fetch-buttons', 'getting buttons', err, false)
  74. })
  75. }
  76. function processWebuiSettingsJson (settings) {
  77. setupSectionNavigation(settings.SectionNavigationStyle)
  78. window.restBaseUrl = settings.Rest
  79. document.querySelector('#currentVersion').innerText = settings.CurrentVersion
  80. if (settings.ShowNewVersions && settings.AvailableVersion !== 'none') {
  81. document.querySelector('#available-version').innerText = 'New Version Available: ' + settings.AvailableVersion
  82. document.querySelector('#available-version').hidden = false
  83. }
  84. if (!settings.ShowNavigation) {
  85. document.querySelector('header').style.display = 'none'
  86. }
  87. if (!settings.ShowFooter) {
  88. document.querySelector('footer[title="footer"]').style.display = 'none'
  89. }
  90. if (settings.EnableCustomJs) {
  91. const script = document.createElement('script')
  92. script.src = './custom-webui/custom.js'
  93. document.head.appendChild(script)
  94. }
  95. window.pageTitle = 'OliveTin'
  96. if (settings.PageTitle) {
  97. window.pageTitle = settings.PageTitle
  98. document.title = window.pageTitle
  99. const titleElem = document.querySelector('#page-title')
  100. if (titleElem) titleElem.innerText = window.pageTitle
  101. }
  102. processAdditionalLinks(settings.AdditionalLinks)
  103. const loginForm = new LoginForm()
  104. loginForm.setup()
  105. loginForm.processOAuth2Providers(settings.AuthOAuth2Providers)
  106. loginForm.processLocalLogin(settings.AuthLocalLogin)
  107. document.getElementsByTagName('main')[0].appendChild(loginForm)
  108. window.settings = settings
  109. refreshDiagnostics()
  110. }
  111. function processAdditionalLinks (links) {
  112. if (links === null) {
  113. return
  114. }
  115. if (links.length > 0) {
  116. for (const link of links) {
  117. const linkA = document.createElement('a')
  118. linkA.href = link.Url
  119. linkA.innerText = link.Title
  120. if (link.Target === '') {
  121. linkA.target = '_blank'
  122. } else {
  123. linkA.target = link.Target
  124. }
  125. const linkLi = document.createElement('li')
  126. linkLi.appendChild(linkA)
  127. document.getElementById('supplemental-links').prepend(linkLi)
  128. }
  129. }
  130. }
  131. function main () {
  132. initMarshaller()
  133. setupLogSearchBox()
  134. window.addEventListener('EventConfigChanged', fetchGetDashboardComponents)
  135. window.addEventListener('EventEntityChanged', fetchGetDashboardComponents)
  136. window.fetch('webUiSettings.json').then(res => {
  137. return res.json()
  138. }).then(res => {
  139. processWebuiSettingsJson(res)
  140. window.restAvailable = true
  141. window.refreshLoop = refreshLoop
  142. window.refreshLoop()
  143. setInterval(refreshLoop, 3000)
  144. }).catch(err => {
  145. window.showBigError('fetch-webui-settings', 'getting webui settings', err)
  146. })
  147. }
  148. main() // call self