main.js 5.1 KB

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