flaschengeist-frontend/src/layouts/MainLayout.vue

207 lines
5.9 KiB
Vue
Raw Normal View History

2020-10-02 07:13:14 +00:00
<template>
2020-10-09 16:04:32 +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>
<q-btn
v-if="!leftDrawerOpen"
2020-10-02 07:13:14 +00:00
dense
2020-10-09 16:04:32 +00:00
flat
2020-10-02 07:13:14 +00:00
round
icon="menu"
@click="leftDrawerOpen = !leftDrawerOpen"
/>
<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 -->
2021-03-29 05:35:23 +00:00
<q-btn icon="message" flat dense
><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
2020-10-09 16:04:32 +00:00
v-model="leftDrawerOpen"
show-if-above
2020-10-09 16:04:32 +00:00
side="left"
2020-10-02 07:13:14 +00:00
bordered
:mini="leftDrawerMini"
@click.capture="leftDrawerClicker"
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-09 16:04:32 +00:00
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>
<div class="q-mini-drawer-hide absolute" style="top: 15px; right: -11px">
<q-btn
size="sm"
dense
round
unelevated
color="secondary"
icon="chevron_left"
@click="leftDrawerMini = true"
/>
</div>
2020-10-13 21:13:42 +00:00
<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');
const leftDrawer = ref(false);
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
onBeforeMount(() => {
polling.value = window.setInterval(() => pollNotification(), config.pollingInterval);
pollNotification();
});
onBeforeUnmount(() => window.clearInterval(polling.value));
const leftDrawerOpen = computed({
get: () => (leftDrawer.value || Screen.gt.sm ? true : false),
set: (val: boolean) => (leftDrawer.value = val),
});
const subLinks = computed(() => {
const matched = router.currentRoute.value.matched[1];
return flaschengeist?.menuLinks.find((link) => matched.name == link.link)?.children;
});
2020-10-31 18:33:05 +00:00
function leftDrawerClicker() {
if (leftDrawerMini.value) {
leftDrawerMini.value = false;
}
}
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,
2020-10-13 09:27:27 +00:00
leftDrawerOpen,
leftDrawerMini,
leftDrawerClicker,
logout,
mainLinks,
2021-03-29 05:35:23 +00:00
notifications,
noPermission,
remove,
requestPermission,
shortcuts,
subLinks,
useNative,
2020-10-13 09:27:27 +00:00
};
},
2020-10-02 07:13:14 +00:00
});
</script>