| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- 'use strict'
- import {
- initMarshaller,
- setupSectionNavigation,
- marshalDashboardComponentsJsonToHtml,
- marshalLogsJsonToHtml,
- refreshServerConnectionLabel
- } from './js/marshaller.js'
- import { checkWebsocketConnection } from './js/websocket.js'
- import { LoginForm } from './js/LoginForm.js'
- function searchLogs (e) {
- document.getElementById('searchLogsClear').disabled = false
- const searchText = e.target.value.toLowerCase()
- for (const row of document.querySelectorAll('tr.log-row')) {
- const actionTitle = row.getAttribute('title').toLowerCase()
- row.hidden = !actionTitle.includes(searchText)
- }
- }
- function searchLogsClear () {
- for (const row of document.querySelectorAll('tr.log-row')) {
- row.hidden = false
- }
- document.getElementById('searchLogsClear').disabled = true
- document.getElementById('logSearchBox').value = ''
- }
- function setupLogSearchBox () {
- document.getElementById('logSearchBox').oninput = searchLogs
- document.getElementById('searchLogsClear').onclick = searchLogsClear
- }
- function refreshLoop () {
- if (window.websocketAvailable) {
- // Websocket updates are streamed live, not updated on a loop.
- } else if (window.restAvailable) {
- // Fallback to rest, but try to reconnect the websocket anyway.
- fetchGetDashboardComponents()
- fetchGetLogs()
- checkWebsocketConnection()
- } else {
- // Still try to fetch the dashboard, if successfull window.restAvailable = true
- fetchGetDashboardComponents()
- }
- refreshServerConnectionLabel()
- }
- function fetchGetDashboardComponents () {
- window.fetch(window.restBaseUrl + 'GetDashboardComponents', {
- cors: 'cors'
- }).then(res => {
- if (!res.ok && res.status === 403) {
- return null
- }
- return res.json()
- }).then(res => {
- if (!window.restAvailable) {
- window.clearBigErrors()
- }
- window.restAvailable = true
- marshalDashboardComponentsJsonToHtml(res)
- refreshServerConnectionLabel() // in-case it changed, update the label quicker
- }).catch((err) => { // err is 1st arg
- window.restAvailable = false
- window.showBigError('fetch-buttons', 'getting buttons', err, false)
- })
- }
- function fetchGetLogs () {
- window.fetch(window.restBaseUrl + 'GetLogs', {
- cors: 'cors'
- }).then(res => {
- return res.json()
- }).then(res => {
- marshalLogsJsonToHtml(res)
- }).catch(err => {
- window.showBigError('fetch-buttons', 'getting buttons', err, false)
- })
- }
- function processWebuiSettingsJson (settings) {
- window.settings = settings
- setupSectionNavigation(settings.SectionNavigationStyle)
- window.restBaseUrl = settings.Rest
- document.querySelector('#currentVersion').innerText = settings.CurrentVersion
- if (settings.ShowNewVersions && settings.AvailableVersion !== 'none') {
- document.querySelector('#available-version').innerText = 'New Version Available: ' + settings.AvailableVersion
- document.querySelector('#available-version').hidden = false
- }
- if (!settings.ShowNavigation) {
- document.querySelector('header').style.display = 'none'
- }
- if (!settings.ShowFooter) {
- document.querySelector('footer[title="footer"]').style.display = 'none'
- }
- if (settings.StyleMods != null) {
- for (const style of settings.StyleMods) {
- document.body.classList.add(style)
- }
- }
- if (settings.EnableCustomJs) {
- const script = document.createElement('script')
- script.src = './custom-webui/custom.js'
- document.head.appendChild(script)
- }
- window.pageTitle = 'OliveTin'
- if (settings.PageTitle) {
- window.pageTitle = settings.PageTitle
- document.title = window.pageTitle
- const titleElem = document.querySelector('#page-title')
- if (titleElem) titleElem.innerText = window.pageTitle
- }
- processAdditionalLinks(settings.AdditionalLinks)
- const loginForm = new LoginForm()
- loginForm.setup()
- loginForm.processOAuth2Providers(settings.AuthOAuth2Providers)
- loginForm.processLocalLogin(settings.AuthLocalLogin)
- document.getElementsByTagName('main')[0].appendChild(loginForm)
- }
- function processAdditionalLinks (links) {
- if (links === null) {
- return
- }
- if (links.length > 0) {
- for (const link of links) {
- const linkA = document.createElement('a')
- linkA.href = link.Url
- linkA.innerText = link.Title
- if (link.Target === '') {
- linkA.target = '_blank'
- } else {
- linkA.target = link.Target
- }
- const linkLi = document.createElement('li')
- linkLi.appendChild(linkA)
- document.getElementById('supplemental-links').prepend(linkLi)
- }
- }
- }
- function main () {
- initMarshaller()
- setupLogSearchBox()
- window.addEventListener('EventConfigChanged', fetchGetDashboardComponents)
- window.addEventListener('EventEntityChanged', fetchGetDashboardComponents)
- window.fetch('webUiSettings.json').then(res => {
- return res.json()
- }).then(res => {
- processWebuiSettingsJson(res)
- window.restAvailable = true
- window.refreshLoop = refreshLoop
- window.refreshLoop()
- setInterval(refreshLoop, 3000)
- }).catch(err => {
- window.showBigError('fetch-webui-settings', 'getting webui settings', err)
- })
- }
- main() // call self
|