Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Add sidebar service #18

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions apps/nestjs/src/shared/services/nav/sidebar.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { ISidebar, ISidebarMenu, ISidebarMenuItem, Sidebar } from './sidebar'

export class SidebarService {
public sidebar!: ISidebar

constructor() {
this.newSidebar(new Sidebar())
}

/**
* Create new sidebar
*
* @param sidebar ISidebar
* @return void
*/
public newSidebar(sidebar: ISidebar = new Sidebar()): SidebarService {
this.sidebar = sidebar

return this
}

/**
* Get sidebar
*
* @returns ISidebar
*/
public getSidebar(): ISidebar {
return this.sidebar
}

/**
* Add sidebar menu item
*
* @param menu ISidebarMenu
* @returns SidebarService
*/
public addMenu(menu: ISidebarMenu): SidebarService {
this.sidebar.items.push(menu)

return this
}

/**
* Append child menu item to parent menu
*
* @param menu ISidebarMenu
* @param child ISidebarMenuItem
* @returns SidebarService
*/
public appendChild(
menu: ISidebarMenu,
child: ISidebarMenuItem,
): SidebarService {
this.sidebar.items
.find((item: ISidebarMenu) => item === menu)
?.children?.push(child)

return this
}
}
61 changes: 61 additions & 0 deletions apps/nestjs/src/shared/services/nav/sidebar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export interface ISidebarMenuItem {
title?: string
icon?: string
route?: string[]
hasPermAccess?: boolean
children?: ISidebarMenuItem[]
}

export class SidebarMenuItem implements ISidebarMenuItem {
public constructor(props?: Partial<ISidebarMenuItem>) {
Object.assign(this, props)
}

title?: string
icon?: string
route?: string[]
hasPermAccess?: boolean
children?: ISidebarMenuItem[]
}

export interface ISidebarMenu {
title?: string
icon?: string
route?: string[]
isDivider?: boolean
hasRoleAccess?: boolean
children?: ISidebarMenuItem[]
loadChildren: (self: ISidebarMenu) => void
}

export class SidebarMenu implements ISidebarMenu {
public constructor(props?: Partial<ISidebarMenu>) {
Object.assign(this, props)

// eslint-disable-next-line antfu/if-newline
if (this.loadChildren) this.loadChildren(this)
}

title?: string
icon?: string
route?: string[]
isDivider?: boolean
hasRoleAccess?: boolean
children?: ISidebarMenuItem[]
loadChildren: ((self: ISidebarMenu) => void) | any
}

export interface ISidebar {
items: ISidebarMenu[]

version?: string | number
}

export class Sidebar implements ISidebar {
public constructor(props?: Partial<ISidebar>) {
Object.assign(this, props)
}

items: ISidebarMenu[] = []
version?: string | number
}