App.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <template>
  2. <AppShell class="layout-fixed-header">
  3. <template #header>
  4. <Header
  5. :title="pageTitle"
  6. :logo-url="logoUrl"
  7. :breadcrumbs="false"
  8. :sidebar-enabled="sidebarEnabled"
  9. :top-bar-enabled="topbarEnabled"
  10. :navigation="navigation"
  11. :top-bar-navigation="navigation"
  12. :username="headerUsername"
  13. :login-route="headerLoginRoute"
  14. @toggle-sidebar="toggleSidebar"
  15. @user-click="goToUserControlPanel"
  16. >
  17. <template #toolbar>
  18. <QuickSearch
  19. v-if="!loginRequired && headerSearchEnabled"
  20. :items="searchIndexItems"
  21. :auto-import-routes="false"
  22. :search-fields="['title', 'description', 'category']"
  23. :max-results="15"
  24. placeholder="Search actions, dashboards, entities…"
  25. />
  26. </template>
  27. </Header>
  28. </template>
  29. <template #sidebar>
  30. <Navigation ref="navigation">
  31. <Sidebar
  32. v-if="sidebarEnabled && showNavigation"
  33. id="mainnav"
  34. ref="sidebar"
  35. />
  36. </Navigation>
  37. </template>
  38. <template #banners>
  39. <NotificationBlock
  40. v-if="bannerMessage"
  41. id="banner"
  42. type="info"
  43. tabindex="0"
  44. :message="bannerMessage"
  45. :style="bannerCss"
  46. />
  47. <ConnectionBanner />
  48. </template>
  49. <router-view :key="routerViewKey" />
  50. <template
  51. v-if="showFooter"
  52. #footer
  53. >
  54. <p>
  55. <img
  56. title="application icon"
  57. :src="logoUrl"
  58. alt="OliveTin logo"
  59. style="height: 1em;"
  60. class="logo"
  61. >
  62. OliveTin <span v-if="showVersionNumber">{{ currentVersion }}</span>
  63. </p>
  64. <p>
  65. <span>
  66. <a
  67. href="https://docs.olivetin.app"
  68. target="_new"
  69. >{{ t('docs') }}</a>
  70. </span>
  71. <span>
  72. <a
  73. href="https://github.com/OliveTin/OliveTin/issues/new/choose"
  74. target="_new"
  75. >{{ t('raise-issue') }}</a>
  76. </span>
  77. <span>
  78. <a
  79. href="#"
  80. @click.prevent="openLanguageDialog"
  81. >{{ currentLanguageName }}</a>
  82. </span>
  83. <span v-if="availableThemes.length > 1">
  84. <a
  85. href="#"
  86. @click.prevent="openThemeDialog"
  87. >{{ currentThemeName }}</a>
  88. </span>
  89. </p>
  90. <p v-if="showVersionNumber">
  91. <a
  92. id="available-version"
  93. href="http://olivetin.app"
  94. target="_blank"
  95. hidden
  96. >?</a>
  97. </p>
  98. </template>
  99. </AppShell>
  100. <dialog
  101. ref="languageDialog"
  102. class="language-dialog"
  103. @click="handleLanguageDialogClick"
  104. >
  105. <div
  106. class="dialog-content"
  107. @click.stop
  108. >
  109. <h2>{{ t('language-dialog.title') }}</h2>
  110. <select
  111. v-model="selectedLanguage"
  112. class="language-select"
  113. @change="changeLanguage"
  114. >
  115. <option
  116. v-for="(name, code) in availableLanguages"
  117. :key="code"
  118. :value="code"
  119. >
  120. {{ code === 'auto' ? name : `${name} (${code})` }}
  121. </option>
  122. </select>
  123. <p class="browser-languages">
  124. {{ t('language-dialog.browser-languages') }}:
  125. <span v-if="browserLanguages.length > 0">{{ browserLanguages.join(', ') }}</span>
  126. <span v-else>{{ t('language-dialog.not-available') }}</span>
  127. </p>
  128. <div class="dialog-buttons">
  129. <button @click="closeLanguageDialog">
  130. {{ t('language-dialog.close') }}
  131. </button>
  132. </div>
  133. </div>
  134. </dialog>
  135. <dialog
  136. ref="themeDialog"
  137. class="theme-dialog"
  138. @click="handleThemeDialogClick"
  139. >
  140. <div
  141. class="dialog-content"
  142. @click.stop
  143. >
  144. <h2>{{ t('theme-dialog.title') }}</h2>
  145. <select
  146. v-model="selectedTheme"
  147. class="language-select"
  148. @change="changeTheme"
  149. >
  150. <option value="">
  151. {{ t('theme-dialog.default') }}
  152. </option>
  153. <option
  154. v-for="theme in availableThemes"
  155. :key="theme"
  156. :value="theme"
  157. >
  158. {{ theme }}
  159. </option>
  160. </select>
  161. <div class="dialog-buttons">
  162. <button @click="closeThemeDialog">
  163. {{ t('theme-dialog.close') }}
  164. </button>
  165. </div>
  166. </div>
  167. </dialog>
  168. </template>
  169. <script setup>
  170. import { ref, onMounted, computed } from 'vue'
  171. import { useRoute, useRouter } from 'vue-router'
  172. import Sidebar from 'picocrank/vue/components/Sidebar.vue'
  173. import Navigation from 'picocrank/vue/components/Navigation.vue'
  174. import Header from 'picocrank/vue/components/Header.vue'
  175. import AppShell from 'picocrank/vue/components/AppShell.vue'
  176. import NotificationBlock from 'picocrank/vue/components/NotificationBlock.vue'
  177. import QuickSearch from 'picocrank/vue/components/QuickSearch.vue'
  178. import ConnectionBanner from './components/ConnectionBanner.vue'
  179. import { connectEventStreamIfNeeded, stopEventStream } from '../../js/websocket.js'
  180. import { DashboardSquare01Icon } from '@hugeicons/core-free-icons'
  181. import logoUrl from '../../OliveTinLogo.png'
  182. import { useI18n } from 'vue-i18n'
  183. import { ensureLocaleMessages, resolveBrowserLocale } from './i18n.js'
  184. import { searchIndexItems, clearSearchIndex, indexSystemNavigation, indexSearchHints, indexRootDashboardEntries } from './stores/searchIndex.js'
  185. import { applyThemeStyles } from './utils/themeLoader.js'
  186. const { t } = useI18n()
  187. const router = useRouter()
  188. const route = useRoute()
  189. // Views that keep their own state in the query (e.g. the calendar month) opt out of remounting when only the query changes.
  190. const routerViewKey = computed(() => route.meta.keepOnQueryChange ? route.path : route.fullPath)
  191. const sidebar = ref(null)
  192. const navigation = ref(null)
  193. const username = ref('')
  194. const isLoggedIn = ref(false)
  195. const currentVersion = ref('?')
  196. const pageTitle = ref('OliveTin')
  197. const bannerMessage = ref('')
  198. const bannerCss = ref('')
  199. const showFooter = ref(true)
  200. const showNavigation = ref(true)
  201. const showLogs = ref(true)
  202. const showDiagnostics = ref(true)
  203. const showVersionNumber = ref(true)
  204. const showLoginLink = ref(true)
  205. const sectionNavigationStyle = ref('sidebar')
  206. const loginRequired = ref(false)
  207. const headerSearchEnabled = ref(false)
  208. const languageDialog = ref(null)
  209. const browserLanguages = ref([])
  210. const initialLanguagePreference = typeof window !== 'undefined' ? localStorage.getItem('olivetin-language') : null
  211. const languagePreference = ref(initialLanguagePreference || 'auto')
  212. const selectedLanguage = ref(languagePreference.value)
  213. let latestLanguageChange = 0
  214. const themeDialog = ref(null)
  215. const availableThemes = ref([])
  216. const initialThemePreference = typeof window !== 'undefined' ? localStorage.getItem('olivetin-theme') : null
  217. const themePreference = ref(initialThemePreference || '')
  218. const selectedTheme = ref(themePreference.value)
  219. // Available languages with display names
  220. const availableLanguages = {
  221. auto: 'Browser Language',
  222. en: 'English',
  223. 'de-DE': 'Deutsch',
  224. 'es-ES': 'Español',
  225. 'it-IT': 'Italiano',
  226. 'zh-Hans-CN': '简体中文',
  227. 'zh-Hant-TW': '繁體中文'
  228. }
  229. // Computed property to get current language display name
  230. const currentLanguageName = computed(() => {
  231. if (languagePreference.value === 'auto') {
  232. return availableLanguages.auto
  233. }
  234. return availableLanguages[languagePreference.value] || languagePreference.value
  235. })
  236. // Computed property to get current theme display name
  237. const currentThemeName = computed(() => {
  238. if (!themePreference.value || themePreference.value === '') {
  239. return t('theme-dialog.default')
  240. }
  241. return themePreference.value
  242. })
  243. // Computed properties for navigation style
  244. const topbarEnabled = computed(() => {
  245. return sectionNavigationStyle.value === 'topbar' && showNavigation.value
  246. })
  247. const sidebarEnabled = computed(() => {
  248. return sectionNavigationStyle.value !== 'topbar' && showNavigation.value
  249. })
  250. const headerUsername = computed(() => {
  251. return isLoggedIn.value ? username.value : ''
  252. })
  253. const headerLoginRoute = computed(() => {
  254. return showLoginLink.value ? { name: 'Login' } : null
  255. })
  256. function toggleSidebar () {
  257. if (sidebar.value && showNavigation.value) {
  258. sidebar.value.toggle()
  259. }
  260. }
  261. function goToUserControlPanel () {
  262. router.push({ name: 'UserInformation' })
  263. }
  264. async function updateHeaderFromInit () {
  265. if (!window.initResponse) {
  266. return
  267. }
  268. // Rebuild the in-memory search index from this Init; never persist it.
  269. clearSearchIndex()
  270. const authenticatedUser = window.initResponse.authenticatedUser
  271. loginRequired.value = !!window.initResponse.loginRequired
  272. headerSearchEnabled.value = !!window.initResponse.features?.headerSearch
  273. // The server filters stream events by the identity the stream connected with.
  274. if (username.value !== authenticatedUser) {
  275. stopEventStream()
  276. }
  277. username.value = authenticatedUser
  278. isLoggedIn.value = authenticatedUser !== '' && authenticatedUser !== 'guest'
  279. currentVersion.value = window.initResponse.currentVersion
  280. pageTitle.value = window.initResponse.pageTitle || 'OliveTin'
  281. bannerMessage.value = window.initResponse.bannerMessage || ''
  282. bannerCss.value = window.initResponse.bannerCss || ''
  283. showFooter.value = window.initResponse.showFooter
  284. showNavigation.value = !!window.initResponse.showNavigation && !loginRequired.value
  285. showLogs.value = window.initResponse.showLogList
  286. showDiagnostics.value = window.initResponse.showDiagnostics
  287. showVersionNumber.value = window.initResponse.effectivePolicy?.showVersionNumber ?? true
  288. sectionNavigationStyle.value = window.initResponse.sectionNavigationStyle || 'sidebar'
  289. availableThemes.value = window.initResponse.availableThemes || []
  290. if (!window.initResponse.authLocalLogin && window.initResponse.oAuth2Providers.length === 0) {
  291. showLoginLink.value = false
  292. }
  293. applyStyleMods()
  294. loadCustomJsIfEnabled()
  295. applyTheme().catch((err) => {
  296. console.warn('Failed to load theme CSS:', err)
  297. })
  298. if (loginRequired.value) {
  299. connectEventStreamIfNeeded()
  300. router.push('/login')
  301. return
  302. }
  303. renderNavigation()
  304. if (headerSearchEnabled.value) {
  305. indexSearchHints(window.initResponse.searchHints)
  306. indexRootDashboardEntries(getRootDashboardEntries())
  307. }
  308. connectEventStreamIfNeeded()
  309. }
  310. function getRootDashboardEntries () {
  311. const entries = window.initResponse?.rootDashboardEntries
  312. if (entries && entries.length > 0) {
  313. return entries.map((entry) => ({
  314. title: entry.title,
  315. category: entry.category || ''
  316. }))
  317. }
  318. return (window.initResponse?.rootDashboards || []).map((title) => ({
  319. title,
  320. category: ''
  321. }))
  322. }
  323. function addDashboardNavLink (title) {
  324. navigation.value.addNavigationLink({
  325. id: title,
  326. name: title,
  327. title,
  328. path: title === 'Actions' ? '/' : `/dashboards/${title}`,
  329. icon: DashboardSquare01Icon
  330. })
  331. }
  332. function addCategorizedDashboardLinks (entries) {
  333. const categoryOrder = []
  334. const byCategory = new Map()
  335. for (const entry of entries) {
  336. const category = entry.category.trim()
  337. if (!byCategory.has(category)) {
  338. byCategory.set(category, [])
  339. categoryOrder.push(category)
  340. }
  341. byCategory.get(category).push(entry.title)
  342. }
  343. for (const category of categoryOrder) {
  344. navigation.value.addSection(category)
  345. for (const title of byCategory.get(category)) {
  346. addDashboardNavLink(title)
  347. }
  348. }
  349. }
  350. function renderNavigation () {
  351. if (!navigation.value) {
  352. return
  353. }
  354. if (typeof navigation.value.clearNavigationLinks === 'function') {
  355. navigation.value.clearNavigationLinks()
  356. }
  357. const entries = getRootDashboardEntries()
  358. const uncategorized = entries.filter((entry) => !entry.category.trim())
  359. const categorized = entries.filter((entry) => entry.category.trim())
  360. for (const entry of uncategorized) {
  361. addDashboardNavLink(entry.title)
  362. }
  363. addCategorizedDashboardLinks(categorized)
  364. addSystemNavLinks()
  365. }
  366. function addSystemNavLinks () {
  367. const systemLinks = []
  368. systemLinks.push({
  369. routeName: 'Entities',
  370. title: t('nav.entities')
  371. })
  372. if (showLogs.value) {
  373. systemLinks.push({
  374. routeName: 'Logs',
  375. title: t('nav.logs')
  376. })
  377. }
  378. if (showDiagnostics.value) {
  379. const issueCount = window.initResponse?.configIssueCount || 0
  380. systemLinks.push({
  381. routeName: 'Diagnostics',
  382. title: t('nav.diagnostics'),
  383. options: { count: issueCount }
  384. })
  385. }
  386. if (!loginRequired.value && headerSearchEnabled.value) {
  387. indexRootDashboardEntries(getRootDashboardEntries())
  388. indexSystemNavigation({
  389. showLogs: showLogs.value,
  390. showDiagnostics: showDiagnostics.value,
  391. entitiesTitle: t('nav.entities'),
  392. logsTitle: t('nav.logs'),
  393. diagnosticsTitle: t('nav.diagnostics')
  394. })
  395. }
  396. if (systemLinks.length === 0) {
  397. return
  398. }
  399. navigation.value.addSection(t('nav.system'))
  400. for (const link of systemLinks) {
  401. addSystemRouterNavLink(link.routeName, link.title, link.options || {})
  402. }
  403. }
  404. function addSystemRouterNavLink (routeName, title, options = {}) {
  405. const foundRoute = router.getRoutes().find((candidate) => candidate.name === routeName)
  406. if (!foundRoute) {
  407. console.warn(`Route "${routeName}" not found`)
  408. return
  409. }
  410. const navLink = {
  411. name: routeName,
  412. title,
  413. path: foundRoute.path,
  414. to: foundRoute.path,
  415. icon: foundRoute.meta?.icon || DashboardSquare01Icon,
  416. type: 'route'
  417. }
  418. if (options.count != null && options.count > 0) {
  419. navLink.count = options.count
  420. }
  421. navigation.value.addNavigationLink(navLink)
  422. }
  423. function openLanguageDialog () {
  424. selectedLanguage.value = languagePreference.value
  425. if (typeof navigator !== 'undefined' && Array.isArray(navigator.languages)) {
  426. browserLanguages.value = navigator.languages
  427. } else {
  428. browserLanguages.value = []
  429. }
  430. if (languageDialog.value) {
  431. languageDialog.value.showModal()
  432. }
  433. }
  434. function closeLanguageDialog () {
  435. if (languageDialog.value) {
  436. languageDialog.value.close()
  437. }
  438. }
  439. async function changeLanguage () {
  440. if (!window.i18n || !selectedLanguage.value) {
  441. return
  442. }
  443. const languageChange = ++latestLanguageChange
  444. const requestedPreference = selectedLanguage.value
  445. const requestedLocale = requestedPreference === 'auto' ? resolveBrowserLocale() : requestedPreference
  446. await ensureLocaleMessages(window.i18n, requestedLocale)
  447. if (languageChange !== latestLanguageChange) {
  448. return
  449. }
  450. window.i18n.locale.value = requestedLocale
  451. if (requestedPreference === 'auto') {
  452. localStorage.removeItem('olivetin-language')
  453. languagePreference.value = 'auto'
  454. } else {
  455. localStorage.setItem('olivetin-language', requestedPreference)
  456. languagePreference.value = requestedPreference
  457. }
  458. // Update navigation with new translations
  459. if (navigation.value) {
  460. renderNavigation()
  461. }
  462. closeLanguageDialog()
  463. }
  464. function handleLanguageDialogClick (event) {
  465. // Close dialog when clicking on the backdrop
  466. if (event.target === languageDialog.value) {
  467. closeLanguageDialog()
  468. }
  469. }
  470. function openThemeDialog () {
  471. selectedTheme.value = themePreference.value || ''
  472. if (themeDialog.value) {
  473. themeDialog.value.showModal()
  474. }
  475. }
  476. function closeThemeDialog () {
  477. if (themeDialog.value) {
  478. themeDialog.value.close()
  479. }
  480. }
  481. async function changeTheme () {
  482. if (!selectedTheme.value || selectedTheme.value === '') {
  483. localStorage.removeItem('olivetin-theme')
  484. themePreference.value = ''
  485. } else {
  486. localStorage.setItem('olivetin-theme', selectedTheme.value)
  487. themePreference.value = selectedTheme.value
  488. }
  489. await applyTheme()
  490. closeThemeDialog()
  491. }
  492. async function applyTheme () {
  493. try {
  494. await applyThemeStyles(themePreference.value)
  495. } catch (err) {
  496. console.warn('Failed to load theme CSS:', err)
  497. localStorage.removeItem('olivetin-theme')
  498. themePreference.value = ''
  499. selectedTheme.value = ''
  500. try {
  501. await applyThemeStyles('')
  502. } catch (fallbackErr) {
  503. console.warn('Failed to load default theme CSS:', fallbackErr)
  504. const themeStyle = document.getElementById('theme-style')
  505. if (themeStyle) {
  506. themeStyle.textContent = ''
  507. }
  508. document.body.removeAttribute('loaded-theme')
  509. }
  510. }
  511. }
  512. function loadCustomJsIfEnabled () {
  513. if (!window.initResponse?.enableCustomJs || document.getElementById('olivetin-custom-js')) {
  514. return
  515. }
  516. const script = document.createElement('script')
  517. script.src = '/custom-webui/custom.js'
  518. script.async = true
  519. script.id = 'olivetin-custom-js'
  520. document.head.appendChild(script)
  521. }
  522. function applyStyleMods () {
  523. if (!window.initResponse || !window.initResponse.styleMods) {
  524. return
  525. }
  526. for (const styleMod of window.initResponse.styleMods) {
  527. if (styleMod) {
  528. document.body.classList.add(styleMod)
  529. }
  530. }
  531. }
  532. function handleThemeDialogClick (event) {
  533. if (event.target === themeDialog.value) {
  534. closeThemeDialog()
  535. }
  536. }
  537. window.updateHeaderFromInit = updateHeaderFromInit
  538. onMounted(async () => {
  539. await updateHeaderFromInit()
  540. // Initialize selected language from stored preference
  541. selectedLanguage.value = languagePreference.value
  542. // Initialize selected theme from stored preference
  543. selectedTheme.value = themePreference.value || ''
  544. if (typeof navigator !== 'undefined' && Array.isArray(navigator.languages)) {
  545. browserLanguages.value = navigator.languages
  546. }
  547. })
  548. </script>
  549. <style scoped>
  550. /* The banner area is sticky, so a long configured message must not fill a short viewport. */
  551. #banner {
  552. max-height: 25vh;
  553. overflow-y: auto;
  554. }
  555. .language-dialog,
  556. .theme-dialog {
  557. border: 1px solid var(--border-color, #ccc);
  558. border-radius: 0.5rem;
  559. padding: 0;
  560. max-width: 400px;
  561. width: 90%;
  562. }
  563. .language-dialog::backdrop,
  564. .theme-dialog::backdrop {
  565. background-color: rgba(0, 0, 0, 0.5);
  566. }
  567. .dialog-content {
  568. padding: 1.5rem;
  569. }
  570. .dialog-content h2 {
  571. margin-top: 0;
  572. margin-bottom: 1rem;
  573. }
  574. .language-select {
  575. width: 100%;
  576. padding: 0.5rem;
  577. margin-bottom: 1rem;
  578. font-size: 1rem;
  579. border: 1px solid var(--border-color, #ccc);
  580. border-radius: 0.25rem;
  581. }
  582. .dialog-buttons {
  583. display: flex;
  584. justify-content: flex-end;
  585. gap: 0.5rem;
  586. }
  587. .dialog-buttons button {
  588. padding: 0.5rem 1rem;
  589. cursor: pointer;
  590. }
  591. .browser-languages {
  592. font-size: 0.875rem;
  593. color: var(--fg2, #555);
  594. margin-bottom: 1rem;
  595. }
  596. </style>