main.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. setupSectionNavigation(settings.SectionNavigationStyle)
  77. window.restBaseUrl = settings.Rest
  78. document.querySelector('#currentVersion').innerText = settings.CurrentVersion
  79. if (settings.ShowNewVersions && settings.AvailableVersion !== 'none') {
  80. document.querySelector('#available-version').innerText = 'New Version Available: ' + settings.AvailableVersion
  81. document.querySelector('#available-version').hidden = false
  82. }
  83. if (!settings.ShowNavigation) {
  84. document.querySelector('header').style.display = 'none'
  85. }
  86. if (!settings.ShowFooter) {
  87. document.querySelector('footer[title="footer"]').style.display = 'none'
  88. }
  89. if (settings.EnableCustomJs) {
  90. const script = document.createElement('script')
  91. script.src = './custom-webui/custom.js'
  92. document.head.appendChild(script)
  93. }
  94. window.pageTitle = 'OliveTin'
  95. if (settings.PageTitle) {
  96. window.pageTitle = settings.PageTitle
  97. document.title = window.pageTitle
  98. const titleElem = document.querySelector('#page-title')
  99. if (titleElem) titleElem.innerText = window.pageTitle
  100. }
  101. processAdditionalLinks(settings.AdditionalLinks)
  102. const loginForm = new LoginForm()
  103. loginForm.setup()
  104. loginForm.processOAuth2Providers(settings.AuthOAuth2Providers)
  105. loginForm.processLocalLogin(settings.AuthLocalLogin)
  106. document.getElementsByTagName('main')[0].appendChild(loginForm)
  107. window.settings = settings
  108. }
  109. function processAdditionalLinks (links) {
  110. if (links === null) {
  111. return
  112. }
  113. if (links.length > 0) {
  114. for (const link of links) {
  115. const linkA = document.createElement('a')
  116. linkA.href = link.Url
  117. linkA.innerText = link.Title
  118. if (link.Target === '') {
  119. linkA.target = '_blank'
  120. } else {
  121. linkA.target = link.Target
  122. }
  123. const linkLi = document.createElement('li')
  124. linkLi.appendChild(linkA)
  125. document.getElementById('supplemental-links').prepend(linkLi)
  126. }
  127. }
  128. }
  129. function main () {
  130. initMarshaller()
  131. setupLogSearchBox()
  132. window.addEventListener('EventConfigChanged', fetchGetDashboardComponents)
  133. window.addEventListener('EventEntityChanged', fetchGetDashboardComponents)
  134. window.fetch('webUiSettings.json').then(res => {
  135. return res.json()
  136. }).then(res => {
  137. processWebuiSettingsJson(res)
  138. window.restAvailable = true
  139. window.refreshLoop = refreshLoop
  140. window.refreshLoop()
  141. setInterval(refreshLoop, 3000)
  142. }).catch(err => {
  143. window.showBigError('fetch-webui-settings', 'getting webui settings', err)
  144. })
  145. }
  146. main() // call self