Skip to content

Commit

Permalink
Announce document title changes handled by apps
Browse files Browse the repository at this point in the history
... and not the runtime
  • Loading branch information
dschmidt committed Oct 13, 2022
1 parent dc86cff commit a40b46c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
21 changes: 17 additions & 4 deletions packages/web-pkg/src/composables/appDefaults/useDocumentTitle.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import { watch, Ref, unref } from '@vue/composition-api'
import { useStore } from '../store'
import { Store } from 'vuex'
import { useEventBus } from '../eventBus/useEventBus'
import { EventBus } from '../../services'

interface DocumentTitleOptions {
document: Document
titleSegments: Ref<string[]>
store?: Store<any>
eventBus?: EventBus
}

export function useDocumentTitle({ document, titleSegments, store }: DocumentTitleOptions): void {
export function useDocumentTitle({
document,
titleSegments,
store,
eventBus
}: DocumentTitleOptions): void {
store = store || useStore()
eventBus = eventBus || useEventBus()

watch(
titleSegments,
(newTitleSegments) => {
const titleSegments = unref(newTitleSegments)

// TODO: announce navigation
const glue = ' - '
const generalName = store.getters['configuration'].currentTheme.general.name
const payload = {
shortDocumentTitle: titleSegments.join(glue),
fullDocumentTitle: [...titleSegments, generalName].join(glue)
}

titleSegments.push(store.getters['configuration'].currentTheme.general.name)
document.title = titleSegments.join(' - ')
eventBus.publish('runtime.documentTitle.changed', payload)
},
{ immediate: true }
)
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions packages/web-pkg/src/composables/eventBus/useEventBus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { eventBus } from '../../services'

export const useEventBus = () => {
return eventBus
}
39 changes: 23 additions & 16 deletions packages/web-runtime/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { getBackendVersion, getWebVersion } from './container/versions'
import { defineComponent } from '@vue/composition-api'
import { isPublicLinkContext, isUserContext, isAuthenticationRequired } from './router'
import { additionalTranslations } from './helpers/additionalTranslations' // eslint-disable-line
import { eventBus } from 'web-pkg/src/services'
export default defineComponent({
components: {
Expand Down Expand Up @@ -100,12 +101,13 @@ export default defineComponent({
$route: {
immediate: true,
handler: function (to) {
const title = this.extractPageTitleFromRoute(to)
if (!title) {
const extracted = this.extractPageTitleFromRoute(to)
if (!extracted) {
return
}
this.announceRouteChange(to)
document.title = title
const { shortDocumentTitle, fullDocumentTitle } = extracted
this.announceRouteChange(shortDocumentTitle)
document.title = fullDocumentTitle
}
},
capabilities: {
Expand Down Expand Up @@ -145,7 +147,15 @@ export default defineComponent({
}
}
},
mounted() {
eventBus.subscribe(
'runtime.documentTitle.changed',
({ shortDocumentTitle, fullDocumentTitle }) => {
document.title = fullDocumentTitle
this.announceRouteChange(shortDocumentTitle)
}
)
},
destroyed() {
if (this.$_notificationsInterval) {
clearInterval(this.$_notificationsInterval)
Expand Down Expand Up @@ -183,26 +193,23 @@ export default defineComponent({
})
},
announceRouteChange(route) {
const pageTitle = this.extractPageTitleFromRoute(route, false)
announceRouteChange(pageTitle) {
const translated = this.$gettext('Navigated to %{ pageTitle }')
this.announcement = this.$gettextInterpolate(translated, { pageTitle })
},
extractPageTitleFromRoute(route, includeGeneralName = true) {
const titleSegments = []
extractPageTitleFromRoute(route) {
const routeTitle = route.meta.title ? this.$gettext(route.meta.title) : undefined
if (!routeTitle) {
return
}
titleSegments.push(routeTitle)
if (includeGeneralName) {
titleSegments.push(this.configuration.currentTheme.general.name)
const glue = ' - '
const titleSegments = [routeTitle]
const generalName = this.configuration.currentTheme.general.name
return {
shortDocumentTitle: titleSegments.join(glue),
fullDocumentTitle: [...titleSegments, generalName].join(glue)
}
return titleSegments.join(' - ')
}
}
})
Expand Down

0 comments on commit a40b46c

Please sign in to comment.