import { Transition, computed, defineComponent, nextTick, ref, watch, } from 'vue'; import { useTranslation } from '../../translations'; import { BCMSButton, BCMSIcon, BCMSLink } from '@ui/components'; import { BCMSJwtRoleName } from '@becomes/cms-sdk/types'; const component = defineComponent({ setup() { const translations = computed(() => { return useTranslation(); }); const store = window.bcms.vue.store; const userMe = computed(() => store.getters.user_me); const isAdmin = computed(() => { return userMe.value?.roles[0].name === BCMSJwtRoleName.ADMIN; }); const canSeeMedia = computed(() => { return isAdmin.value || userMe.value?.customPool.policy.media.get; }); const templates = computed(() => { return store.getters.template_items; }); const showCreateNewDropdown = ref(false); const showEntriesDropdown = ref(false); const showAllEntries = ref(false); const entriesDropdownDOM = ref(); const filteredTemplates = computed(() => { return isAdmin.value ? templates.value : templates.value.filter( (e) => userMe.value?.customPool.policy.templates.find( (j) => j.get && j._id === e._id, ) && !e.singleEntry, ); }); const createNewOptions = computed(() => [ { label: translations.value.page.home.newOptions.template, icon: '/administration/template', href: '/dashboard/t', show: isAdmin.value, }, { label: translations.value.page.home.newOptions.widget, icon: '/administration/widget', href: '/dashboard/w', show: isAdmin.value, }, { label: translations.value.page.home.newOptions.group, icon: '/administration/group', href: '/dashboard/g', show: isAdmin.value, }, { label: translations.value.page.home.newOptions.fileUpload, icon: '/administration/media', href: '/dashboard/media', show: canSeeMedia.value, }, { label: translations.value.page.home.newOptions.entry.label, icon: '/file', show: isAdmin.value || filteredTemplates.value.length > 0, onClick() { showEntriesDropdown.value = !showEntriesDropdown.value; }, }, ]); const filteredNewOptions = computed(() => { return createNewOptions.value.filter((e) => e.show); }); // TODO: Extract to a directive or similar const checkIfDropdownIsOverflowing = () => { nextTick(() => { if (entriesDropdownDOM.value) { const rect = entriesDropdownDOM.value.getBoundingClientRect(); if ( rect.bottom > (window.innerHeight || document.documentElement.clientHeight) ) { // Bottom is out of viewport entriesDropdownDOM.value.style.bottom = `${ rect.bottom - window.innerHeight + 80 }px`; } if ( rect.right > (window.innerWidth || document.documentElement.clientWidth) ) { // Right side is out of viewport entriesDropdownDOM.value.style.right = `${ rect.right - window.innerWidth }px`; } } }); }; watch(showCreateNewDropdown, (newVal) => { if (newVal) { showEntriesDropdown.value = false; } }); watch(showEntriesDropdown, (newVal) => { if (newVal) { checkIfDropdownIsOverflowing(); } }); return () => { return ( userMe.value && (
{translations.value.page.home.greeting.title( userMe.value?.customPool.personal.firstName, )}
{translations.value.page.home.greeting.wish}
{ showCreateNewDropdown.value = !showCreateNewDropdown.value; }} > {translations.value.page.home.newOptions.title} {showCreateNewDropdown.value && (
{ showCreateNewDropdown.value = false; }} class="absolute -bottom-2 right-0 w-[320px] max-w-[100vw] translate-y-full pt-5 pb-2.5 rounded-2.5 shadow-cardLg bg-white dark:bg-[#504F54]" >
{translations.value.page.home.newOptions.title}
{filteredNewOptions.value.map((e, index) => { return e.href ? ( {e.label} ) : ( ); })} {showEntriesDropdown.value && (
{ showEntriesDropdown.value = false; }} ref={entriesDropdownDOM} class="absolute w-[320px] max-w-[100vw] translate-x-full translate-y-full pt-5 pb-2.5 rounded-2.5 shadow-cardLg bg-white dark:bg-[#504F54]" style="right: -8px; bottom: 80px" >
{ translations.value.page.home.newOptions .entry.dropdown.title }
{filteredTemplates.value .slice( 0, showAllEntries.value ? filteredTemplates.value.length : 4, ) .map((template, index) => { return ( {template.label} ); })}
)}
)}
) ); }; }, }); export default component;