jws-frontend/src/layouts/ProfileMenu.vue

494 lines
14 KiB
Vue
Raw Normal View History

<script lang="ts" setup>
2024-07-01 07:50:30 +00:00
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
2024-06-25 07:13:13 +00:00
import { useQuasar } from 'quasar';
2024-08-09 15:06:41 +07:00
// import useOption from 'stores/option';
// const optionStore = useOption();
import { getName, getRealm, getRole, isLoggedIn } from 'src/services/keycloak';
2024-06-25 07:13:13 +00:00
const $q = useQuasar();
const filterRole = ref<string[]>();
2024-07-01 07:50:30 +00:00
2024-08-07 14:16:27 +07:00
defineProps<{
userImage?: string;
2024-08-20 18:04:42 +07:00
gender?: string;
2024-08-07 14:16:27 +07:00
}>();
const inputFile = document.createElement('input');
inputFile.type = 'file';
inputFile.accept = 'image/*';
// const profileFile = ref<File | undefined>(undefined);
2024-07-01 06:51:33 +00:00
const currentTheme = ref();
const options = ref([
{
icon: 'mdi-account',
2024-08-26 16:24:08 +07:00
label: 'menu.profile.editPersonalInfo',
value: 'op1',
color: 'grey',
},
{
icon: 'mdi-signature-freehand',
2024-08-26 16:24:08 +07:00
label: 'menu.profile.signature',
value: 'op2',
color: 'grey',
},
2024-06-25 07:13:13 +00:00
{
icon: 'mdi-brightness-6',
2024-08-26 16:24:08 +07:00
label: 'menu.profile.mode',
2024-06-25 07:13:13 +00:00
value: 'op3',
color: 'grey',
},
]);
const themeMode = ref([
{
label: 'light',
value: 'light',
isActive: false,
},
{
label: 'dark',
value: 'dark',
isActive: false,
},
{
label: 'baseOnDevice',
value: 'baseOnDevice',
isActive: false,
},
]);
// inputFile.addEventListener('change', async (e) => {
// profileFile.value = await (e.currentTarget as HTMLInputElement).files?.[0];
// if (profileFile.value) {
// await storageStore.uploadProfile(profileFile.value);
// }
// userImage.value = (await storageStore.getProfile()) ?? '';
// });
2024-07-01 06:51:33 +00:00
function changeMode(mode: string) {
if (mode === 'light') {
localStorage.setItem('currentTheme', 'light');
themeMode.value[0].isActive = true;
themeMode.value[1].isActive = false;
themeMode.value[2].isActive = false;
2024-07-02 07:52:09 +00:00
currentTheme.value = 'light';
2024-07-01 06:51:33 +00:00
$q.dark.set(false);
return;
}
if (mode === 'dark') {
localStorage.setItem('currentTheme', 'dark');
themeMode.value[0].isActive = false;
themeMode.value[1].isActive = true;
themeMode.value[2].isActive = false;
2024-07-02 07:52:09 +00:00
currentTheme.value = 'dark';
2024-07-01 06:51:33 +00:00
$q.dark.set(true);
return;
}
if (mode === 'baseOnDevice') {
localStorage.setItem('currentTheme', 'baseOnDevice');
themeMode.value[0].isActive = false;
themeMode.value[1].isActive = false;
themeMode.value[2].isActive = true;
2024-07-02 07:52:09 +00:00
currentTheme.value = 'baseOnDevice';
2024-07-01 06:51:33 +00:00
if (
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
$q.dark.set(true);
2024-06-25 07:13:13 +00:00
} else {
2024-07-01 06:51:33 +00:00
$q.dark.set(false);
2024-06-25 07:13:13 +00:00
}
2024-07-01 06:51:33 +00:00
return;
}
}
2024-07-01 07:50:30 +00:00
function themeChange() {
if (themeMode.value[2].isActive) changeMode('baseOnDevice');
}
onUnmounted(() => {
window
.matchMedia('(prefers-color-scheme: dark)')
.removeEventListener('change', themeChange);
});
onMounted(async () => {
2024-07-01 07:50:30 +00:00
window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', themeChange);
// if (isLoggedIn()) {
// userImage.value = (await storageStore.getProfile()) ?? '';
// }
2024-07-01 06:51:33 +00:00
currentTheme.value = localStorage.getItem('currentTheme');
2024-07-01 07:50:30 +00:00
if (
currentTheme.value === 'light' ||
currentTheme.value === 'dark' ||
currentTheme.value === 'baseOnDevice'
) {
changeMode(currentTheme.value);
} else {
changeMode('light');
}
const userRoles = getRole();
if (userRoles) {
filterRole.value = userRoles
.filter(
(role) =>
2024-09-16 10:32:06 +07:00
!role.includes('default-roles') &&
role !== 'offline_access' &&
role !== 'uma_authorization',
)
.map((role) =>
2024-09-03 10:48:00 +07:00
role
.replace(/_/g, ' ')
.toLowerCase()
.split(' ')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' '),
);
}
});
</script>
<template>
<div>
2024-07-03 06:45:12 +00:00
<q-btn
rounded
dense
flat
no-caps
color="dark"
class="q-pa-none account-menu-down dropdown-menu"
>
2024-07-03 06:45:12 +00:00
<div class="row items-center">
<div class="q-pa-none">
<q-avatar
class="surface-1 bordered"
:size="$q.screen.lt.sm ? '30px' : '40px'"
>
2024-08-20 18:04:42 +07:00
<q-img
:ratio="1"
class="text-center"
:src="userImage"
v-if="userImage"
>
<template #error>
<div
class="no-padding full-width full-height flex items-center justify-center"
style="
background: linear-gradient(
135deg,
rgba(43, 137, 223, 1) 0%,
rgba(230, 51, 81, 1) 100%
);
"
>
<q-img
v-if="gender"
:ratio="1"
:src="`/no-img-${gender === 'female' ? 'female' : 'man'}.png`"
></q-img>
<q-icon
v-else
name="mdi-account-outline"
color="white"
:size="$q.screen.lt.sm ? 'xs' : 'sm'"
/>
</div>
</template>
</q-img>
2024-07-03 06:45:12 +00:00
<q-icon
v-else
2024-08-20 18:04:42 +07:00
name="mdi-account-outline"
2024-07-03 06:45:12 +00:00
:size="$q.screen.lt.sm ? 'xs' : 'sm'"
/>
</q-avatar>
</div>
<div
class="text-left q-px-md"
v-if="$q.screen.gt.sm"
style="color: var(--foreground)"
>
<div class="text-caption column">
<span
v-if="isLoggedIn()"
class="text-weight-bold ellipsis"
style="max-width: 9vw"
>
{{ getName() }}
<q-tooltip>
{{ getName() }}
2024-07-03 06:45:12 +00:00
</q-tooltip>
</span>
<span
v-else
class="text-weight-bold q-pb-xs ellipsis"
style="max-width: 9vw"
>
{{ 'Guest' }}
<q-tooltip>
{{ 'Guest' }}
2024-07-03 06:45:12 +00:00
</q-tooltip>
</span>
<div style="font-size: 11px">
{{ filterRole?.join(' | ') }}
</div>
</div>
</div>
<div v-if="$q.screen.gt.sm" class="text-right">
<q-icon name="mdi-chevron-down" />
</div>
</div>
2024-08-20 18:04:42 +07:00
<q-menu :offset="[5, 10]" max-width="300px">
2024-07-03 06:45:12 +00:00
<div
no-padding
class="row justify-center bordered rounded"
style="overflow: hidden"
>
<div class="column col-12 items-center">
<div
class="full-width row justify-center"
style="position: relative"
>
<div
class="full-width account-cover"
:class="{ dark: $q.dark.isActive }"
></div>
<div class="avartar-border">
<q-avatar
id="changeAvatar"
size="72px"
class="surface-1 bordered"
:class="{ 'hover-profile': isLoggedIn() }"
@click="
() => {
isLoggedIn() ? inputFile.click() : '';
}
"
>
2024-08-20 18:04:42 +07:00
<q-img
:ratio="1"
class="text-center"
:src="userImage"
v-if="userImage"
>
<template #error>
<div
class="no-padding full-width full-height flex items-center justify-center"
style="
background: linear-gradient(
135deg,
rgba(43, 137, 223, 1) 0%,
rgba(230, 51, 81, 1) 100%
);
"
>
<q-img
v-if="gender"
:ratio="1"
:src="`/no-img-${gender === 'female' ? 'female' : 'man'}.png`"
></q-img>
<q-icon
v-else
name="mdi-account-outline"
color="white"
/>
</div>
</template>
</q-img>
<q-icon name="mdi-account-outline" v-else />
2024-07-03 06:45:12 +00:00
</q-avatar>
</div>
2024-07-03 06:45:12 +00:00
</div>
<div
2024-08-20 18:04:42 +07:00
class="text-subtitle2 q-mb-xs text-center full-width ellipsis q-px-md"
2024-07-03 06:45:12 +00:00
style="margin-top: 58px"
>
<span v-if="isLoggedIn()">
{{ getName() }}
</span>
<span v-else>{{ 'Guest' }}</span>
2024-08-20 18:04:42 +07:00
<q-tooltip>
<span v-if="isLoggedIn()">
{{ getName() }}
</span>
<span v-else>{{ 'Guest' }}</span>
</q-tooltip>
2024-07-03 06:45:12 +00:00
</div>
<div
class="badge q-px-sm q-mb-md text-caption"
:class="{ dark: $q.dark.isActive }"
2024-07-03 06:45:12 +00:00
>
{{ filterRole?.join(' | ') }}
</div>
</div>
2024-07-03 06:45:12 +00:00
<div class="column col-12">
<q-separator />
<div class="column justify-center">
2024-09-18 14:10:03 +07:00
<q-list
:dense="true"
v-for="op in options"
:key="op.label"
:id="op.label"
>
2024-07-03 06:45:12 +00:00
<q-item
2024-08-26 16:24:08 +07:00
v-if="op.label !== 'menu.profile.mode'"
2024-07-03 06:45:12 +00:00
clickable
:id="`btn-${op.label}`"
@click="$emit(op.label)"
>
<q-item-section avatar>
<q-icon :name="op.icon" :color="op.color" size="20px" />
</q-item-section>
<q-item-section class="q-py-sm">
{{ $t(op.label) }}
</q-item-section>
</q-item>
2024-06-25 07:13:13 +00:00
2024-08-26 16:24:08 +07:00
<q-separator v-if="op.label === 'menu.profile.mode'" />
2024-07-03 06:45:12 +00:00
<q-item
2024-08-26 16:24:08 +07:00
v-if="op.label === 'menu.profile.mode'"
2024-07-03 06:45:12 +00:00
clickable
:id="`btn-${op.label}`"
@click="$emit(op.label)"
>
2024-07-03 06:45:12 +00:00
<q-item-section avatar>
<q-icon :name="op.icon" :color="op.color" size="20px" />
</q-item-section>
<q-item-section class="q-py-sm">
<div class="row justify-between">
<span>
{{ $t(op.label) }}
</span>
<span class="app-text-muted-2">
2024-08-26 16:24:08 +07:00
{{ $t(`general.${currentTheme}`) }}
2024-07-03 06:45:12 +00:00
<q-icon name="mdi-chevron-right" />
</span>
</div>
</q-item-section>
<q-menu
class="bordered rounded"
anchor="top right"
self="top left"
max-width="200"
:offset="[10, 0]"
style="width: 160px"
>
<div v-for="(mode, index) in themeMode" :key="index">
<q-item
clickable
@click="
() => {
changeMode(mode.value);
}
"
>
<q-item-section>
<div class="row justify-between">
<span>
2024-08-26 16:24:08 +07:00
{{ $t(`general.${mode.label}`) }}
2024-07-03 06:45:12 +00:00
</span>
<q-icon v-if="mode.isActive" name="mdi-check" />
</div>
</q-item-section>
</q-item>
</div>
</q-menu>
</q-item>
</q-list>
</div>
<q-separator />
<q-btn
v-if="isLoggedIn()"
no-caps
dense
unelevated
class="q-ma-md app-text-negative"
:class="{ dark: $q.dark.isActive }"
2024-08-26 16:24:08 +07:00
:label="$t('general.logout')"
2024-07-03 06:45:12 +00:00
@click="$emit('logout')"
id="btn-logout"
style="background-color: hsla(var(--negative-bg) / 0.1)"
v-close-popup
/>
<q-btn
v-else
no-caps
dense
color="primary"
unelevated
class="q-ma-md app-text-negative"
2024-08-26 16:24:08 +07:00
:label="$t('general.login')"
2024-07-03 06:45:12 +00:00
@click="$emit('login')"
id="btn-login"
v-close-popup
/>
</div>
</div>
2024-07-03 06:45:12 +00:00
</q-menu>
</q-btn>
</div>
</template>
2024-06-25 07:13:13 +00:00
<style scoped lang="scss">
.account-menu-down {
2024-08-20 18:04:42 +07:00
& ::before:not(.q-icon) {
color: var(--foreground);
}
}
.avartar-border {
margin-top: 24px;
border: 5px solid var(--surface-1);
border-radius: 50%;
position: absolute;
}
.account-cover {
height: 65px;
background-color: var(--indigo-0);
&.dark {
background-color: var(--surface-3);
}
}
.badge {
display: inline-block;
border-radius: var(--radius-6);
background-color: var(--indigo-0);
text-wrap: nowrap;
&.dark {
background-color: var(--surface-3);
}
}
.logout-btn {
color: hsl(var(--negative-bg));
background-color: hsl(var(--stone-3-hsl));
&.dark {
background-color: transparent;
border: 1px solid hsl(var(--negative-bg));
}
}
.hover-profile:hover {
opacity: 0.7;
cursor: pointer;
}
</style>