primevue-mirror/app.vue

82 lines
2.5 KiB
Vue
Raw Normal View History

2022-09-06 11:52:18 +00:00
<template>
2022-09-13 11:29:45 +00:00
<NuxtLayout :name="layout">
2022-09-06 11:52:18 +00:00
<NuxtPage />
2022-09-06 13:53:29 +00:00
</NuxtLayout>
2022-09-06 11:52:18 +00:00
</template>
2022-09-06 13:53:29 +00:00
<script>
2022-09-09 10:25:14 +00:00
import EventBus from '@/layouts/AppEventBus';
import NewsService from '@/service/NewsService';
2022-09-09 14:35:31 +00:00
2022-09-09 10:25:14 +00:00
export default {
themeChangeListener: null,
newsActivate: null,
newsService: null,
data() {
return {
2022-09-13 11:29:45 +00:00
storageKey: 'primevue',
layout: 'custom',
}
},
watch: {
$route: {
immediate: true,
handler(to) {
if (to.name === 'index') {
this.layout = 'custom';
}
else {
this.layout = 'default';
}
}
2022-09-09 10:25:14 +00:00
}
},
created() {
this.newsService = new NewsService();
},
mounted() {
this.newsActivate = () => {
this.newsService.fetchNews().then(data => {
this.$appState.announcement = data;
2022-09-06 13:53:29 +00:00
2022-09-09 10:25:14 +00:00
const itemString = localStorage.getItem(this.storageKey);
if (itemString) {
const item = JSON.parse(itemString);
if (item.hiddenNews && item.hiddenNews !== data.id) {
this.$appState.newsActive = true;
}
else this.$appState.newsActive = false;
}
else {
this.$appState.newsActive = true;
}
});
};
this.themeChangeListener = (event) => {
const elementId = 'theme-link';
const linkElement = document.getElementById(elementId);
const cloneLinkElement = linkElement.cloneNode(true);
const newThemeUrl = linkElement.getAttribute('href').replace(this.$appState.theme, event.theme);
cloneLinkElement.setAttribute('id', elementId + '-clone');
cloneLinkElement.setAttribute('href', newThemeUrl);
cloneLinkElement.addEventListener('load', () => {
linkElement.remove();
cloneLinkElement.setAttribute('id', elementId);
});
linkElement.parentNode.insertBefore(cloneLinkElement, linkElement.nextSibling);
this.$appState.theme = event.theme;
this.$appState.darkTheme = event.dark;
};
EventBus.on('theme-change', this.themeChangeListener);
EventBus.on('news-activate', this.newsActivate);
},
beforeUnmount() {
EventBus.off('theme-change', this.themeChangeListener);
EventBus.off('news-activate', this.newsActivate);
}
}
2022-09-07 14:20:55 +00:00
</script>