import {Prop, Component, Ref, Watch} from "vue-property-decorator"; // Interfaces import { INavigationBarLinks,linkType } from "@/types"; // Components import NotificationBadge from "@/components/NotificationBadge/index.vue"; import Icon from "@/components/Icon/index.vue"; import Tooltip from "@/components/Tooltip/index.vue"; import PremiumBadge from "@/components/PremiumBadge/index.vue"; import KeyNavigate from "@/utils/class_components/KeyNavigate"; import NavSubItem from "@/components/NavigationBar/NavSubItem/index.vue"; @Component({ components: { Icon, NotificationBadge, Tooltip, PremiumBadge, NavSubItem, }, }) export default class NavItem extends KeyNavigate { @Prop({ type: [String, Object] }) link!: linkType; @Prop({ type: Array }) readonly subMenu?: INavigationBarLinks[]; @Prop({ type: String }) icon!: string; @Prop({ type: String }) title!: string; @Prop({ type: String }) badge!: string; @Prop({ type: Boolean }) premiumBadge!: boolean; @Prop({ type: Function }) toggleMenu!: () => boolean; @Prop({ type: Boolean }) isCollapsed!: boolean; @Prop({ type: Boolean }) isToggle!: boolean; @Prop({ type: Boolean }) divider!: boolean; @Prop({ type: Function }) clkOccured!: (menuTitle:string) => void; @Prop({ type: String }) curActiveMenu!: string; @Ref() readonly subMenuRef!: HTMLElement; @Prop({ type: Function }) readonly onEnterLink?: (lnk:linkType)=>void; showSub = false; active = false; isFocused = false; isMenuActive = false; befStat = false; created(): void { this.kOrder = ["navSubItem"]; this.kCreated(); } mounted(): void { this.findMenuActive() } toggleSub() { if (this.isCollapsed && !this.isToggle) { this.toggleMenu(); } this.showSub = !this.showSub; } findBadge(subList: any) { const subListAsArray = Object.values(subList); const subListValuesAsArray = Object.values(subListAsArray); const hasBadge = (obj: any): obj is any => obj?.badge !== undefined; const badge = subListValuesAsArray.filter(hasBadge); return badge.length ? badge[0].badge : null; } get subMenuActive() { if(this.showSub){ return false; } if (this.subMenu) { for (let index = 0; index < this.subMenu.length; index += 1) { const link = this.subMenu[index]; if (link.active) { return true; } } } return this.isMenuActive || false; } @Watch('$parent.curActiveMenu') findMenuActive(){ this.isMenuActive = false; if(this.subMenu && !this.showSub && this.curActiveMenu === this.title){ const activSubMenu = this.subMenuRef?.querySelector(".nuxt-link-active"); const activeLnk = this.subMenuRef?.querySelector(".activeLnk"); if(activSubMenu||activeLnk){ this.isMenuActive = true; } } } @Watch('$parent.isToggle') isToggleNav(){ this.$nextTick(()=>{ if(!this.isToggle){ if(this.isCollapsed){ if(this.showSub){ this.befStat = this.showSub; this.showSub = false; } } } else if(this.befStat){ this.showSub = this.befStat; this.befStat = false; } }) } onKeyDown(e: KeyboardEvent) { console.log(e) this.kDoKeyDown(e); } onKeyup() { // eslint-disable-next-line no-console console.log("NavItem onKeyup"); } onFocus(): void { this.isFocused = true; } onBlur(e: KeyboardEvent): void { this.isFocused = false; if (this.showSub) { this.kDoBlurComp(e); this.showSub = false; this.kDestroyKeyUp('onblur navItem'); } } onEnter(e: KeyboardEvent): void { this.isFocused = false; this.toggleSub(); if(this.link){ this.kDoBlurComp(e); } this.$nextTick(() => { this.kDoKeyup(e, "Tab"); if(this.link){ if(this.onEnterLink){ this.onEnterLink(this.link); } else{ this.$router.push(this.link); } } }); } beforeDestroy(): void { this.kDestroyKeyUp('beforeDestroy navItem'); } }