flaschengeist-frontend/src/layouts/MainLayout.vue

186 lines
5.6 KiB
Vue
Raw Normal View History

2020-10-02 07:13:14 +00:00
<template>
2021-04-03 11:24:19 +00:00
<q-layout view="hHh Lpr lff">
<q-header elevated class="bg-primary text-white">
2020-10-02 07:13:14 +00:00
<q-toolbar>
2021-04-03 11:24:19 +00:00
<q-btn dense flat round icon="mdi-menu" @click="openMenu" />
2020-10-02 07:13:14 +00:00
<q-toolbar-title>
<router-link :to="{ name: 'dashboard' }" style="text-decoration: none; color: inherit">
2021-01-29 15:24:43 +00:00
<q-avatar rounded>
<img src="flaschengeist-logo-white.svg" />
</q-avatar>
<span class="gt-xs"> Flaschengeist </span>
</router-link>
2020-10-02 07:13:14 +00:00
</q-toolbar-title>
<!-- Hier kommen die Shortlinks hin -->
<q-btn icon="mdi-message-bulleted" flat dense
2021-03-29 05:35:23 +00:00
><q-badge color="negative" floating>{{ notifications.length }}</q-badge>
<q-menu style="max-height: 400px; overflow: auto">
<q-btn
v-if="useNative && noPermission"
2021-03-29 05:35:23 +00:00
label="Benachrichtigungen erlauben"
@click="requestPermission"
/>
<template v-if="notifications.length > 0">
<Notification
v-for="(notification, index) in notifications"
:key="index"
:model-value="notification"
@remove="remove"
/>
</template>
<div v-else class="q-pa-sm">Keine neuen Benachrichtigungen</div>
</q-menu>
</q-btn>
<shortcut-link
v-for="(shortcut, index) in shortcuts"
:key="'shortcut' + index"
:shortcut="shortcut"
/>
<q-btn flat round dense icon="mdi-exit-to-app" @click="logout()" />
2020-10-02 07:13:14 +00:00
</q-toolbar>
</q-header>
<q-drawer
2021-04-03 11:24:19 +00:00
v-model="leftDrawer"
2020-10-09 16:04:32 +00:00
side="left"
2020-10-02 07:13:14 +00:00
bordered
:mini="leftDrawerMini"
2021-04-03 11:24:19 +00:00
@click.capture="openMenu"
2020-10-02 07:13:14 +00:00
>
<!-- Plugins -->
<q-list>
<essential-link
v-for="(entry, index) in mainLinks"
2020-10-13 09:27:27 +00:00
:key="'plugin' + index"
:entry="entry"
/>
<q-separator />
<!-- Plugin functions -->
2020-10-13 09:27:27 +00:00
<essential-link
v-for="(entry, index) in subLinks"
2020-10-13 09:27:27 +00:00
:key="'childPlugin' + index"
:entry="entry"
2020-10-13 09:27:27 +00:00
/>
</q-list>
<q-separator />
2020-10-13 21:13:42 +00:00
<essential-link
v-for="(entry, index) in essentials"
:key="'essential' + index"
:entry="entry"
2020-10-13 21:13:42 +00:00
/>
2020-10-02 07:13:14 +00:00
</q-drawer>
<q-page-container>
<router-view />
2020-10-02 07:13:14 +00:00
</q-page-container>
</q-layout>
</template>
<script lang="ts">
import EssentialLink from 'src/components/navigation/EssentialLink.vue';
import ShortcutLink from 'src/components/navigation/ShortcutLink.vue';
2021-03-29 05:35:23 +00:00
import Notification from 'src/components/Notification.vue';
import { defineComponent, ref, inject, computed, onBeforeMount, onBeforeUnmount } from 'vue';
import { useMainStore } from 'src/stores';
import { FG_Plugin } from 'src/plugins';
import { useRouter } from 'vue-router';
import { Screen } from 'quasar';
import config from 'src/config';
2020-10-02 07:13:14 +00:00
const essentials: FG_Plugin.MenuLink[] = [
2020-10-13 09:27:27 +00:00
{
2020-10-13 21:13:42 +00:00
title: 'Über Flaschengeist',
2020-10-13 09:27:27 +00:00
link: 'about',
icon: 'mdi-information',
},
2020-10-09 16:04:32 +00:00
];
2020-10-02 07:13:14 +00:00
export default defineComponent({
name: 'MainLayout',
2021-03-29 05:35:23 +00:00
components: { EssentialLink, ShortcutLink, Notification },
setup() {
const router = useRouter();
const mainStore = useMainStore();
2021-02-04 01:42:49 +00:00
const flaschengeist = inject<FG_Plugin.Flaschengeist>('flaschengeist');
2021-04-03 11:24:19 +00:00
const leftDrawer = ref(true);
const leftDrawerMini = ref(false);
const shortcuts = flaschengeist?.shortcuts || [];
const mainLinks = flaschengeist?.menuLinks || [];
2021-03-29 05:35:23 +00:00
const notifications = computed(() => mainStore.notifications.slice().reverse());
const polling = ref(NaN);
const useNative = 'Notification' in window && window.Notification !== undefined;
const noPermission = ref(!useNative || window.Notification.permission !== 'granted');
2021-03-29 05:35:23 +00:00
2021-04-03 11:24:19 +00:00
const subLinks = computed(() => {
const matched = router.currentRoute.value.matched[1];
return flaschengeist?.menuLinks.find((link) => matched.name == link.link)?.children;
});
onBeforeMount(() => {
polling.value = window.setInterval(() => pollNotification(), config.pollingInterval);
pollNotification();
});
onBeforeUnmount(() => window.clearInterval(polling.value));
2021-04-03 11:24:19 +00:00
function openMenu(event: { target: HTMLInputElement }) {
if (event.target.nodeName === 'DIV') leftDrawerMini.value = false;
else {
if (!leftDrawer.value || leftDrawerMini.value) {
leftDrawer.value = true;
leftDrawerMini.value = false;
} else {
leftDrawerMini.value = Screen.gt.sm && !leftDrawerMini.value;
leftDrawer.value = leftDrawerMini.value;
}
}
}
2020-10-02 07:13:14 +00:00
function logout() {
void router.push({ name: 'login', params: { logout: 'logout' } });
void mainStore.logout();
}
2021-03-29 05:35:23 +00:00
async function remove(id: number) {
await mainStore.removeNotification(id);
}
function requestPermission() {
void window.Notification.requestPermission().then(
(p) => (noPermission.value = p !== 'granted')
);
}
function pollNotification() {
void mainStore
.loadNotifications(<FG_Plugin.Flaschengeist>flaschengeist)
.then((notifications) => {
if (useNative && !noPermission.value)
notifications.forEach(
(notif) =>
new window.Notification(notif.text, {
timestamp: notif.time.getTime(),
})
);
});
}
2020-10-13 09:27:27 +00:00
return {
essentials,
2021-04-03 11:24:19 +00:00
leftDrawer,
2020-10-13 09:27:27 +00:00
leftDrawerMini,
logout,
mainLinks,
2021-03-29 05:35:23 +00:00
notifications,
noPermission,
2021-04-03 11:24:19 +00:00
openMenu,
2021-03-29 05:35:23 +00:00
remove,
requestPermission,
shortcuts,
subLinks,
useNative,
2020-10-13 09:27:27 +00:00
};
},
2020-10-02 07:13:14 +00:00
});
</script>