Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

4.1 KB
Raw Blame
import { Component, DOCUMENT, HostListener, inject, OnDestroy, OnInit, Renderer2 } from '@angular/core'
import { NavigationStart, Router, RouterOutlet } from '@angular/router'
import { filter, Subscription } from 'rxjs'
import { themeLight } from './layout.interfaces'
import { LayoutService } from './layout.service'
import { NavBarComponent } from './navbar/navbar.component'
import { SideBarLeftComponent } from './sidebar/sidebar.left.component'
import { SideBarRightComponent } from './sidebar/sidebar.right.component'

@Component({
  selector: 'app-layout',
  templateUrl: 'layout.component.html',
  imports: [RouterOutlet, NavBarComponent, SideBarLeftComponent, SideBarRightComponent]
})
export class LayoutComponent implements OnDestroy, OnInit {
  protected themeMode = themeLight
  private readonly document = inject<Document>(DOCUMENT)
  private readonly layout = inject(LayoutService)
  private readonly renderer = inject(Renderer2)
  private readonly router = inject(Router)
  private rightSideBarClass = 'control-sidebar-open'
  private leftSideBarCollapsedClass = 'sidebar-collapse'
  private leftSideBarOpenedClass = 'sidebar-open'
  private isSmallerThanMediumScreen = false
  private subscriptions: Subscription[] = []

  constructor() {
    this.subscriptions.push(this.layout.switchTheme.subscribe((theme: string) => this.setTheme(theme)))
    this.subscriptions.push(this.layout.toggleRightSideBar.subscribe((status: boolean) => this.toggleRightSideBar(status)))
    this.subscriptions.push(this.layout.toggleLeftSideBar.subscribe((status) => this.toggleLeftSideBar(status)))
    this.subscriptions.push(
      this.router.events.pipe(filter((event) => event instanceof NavigationStart)).subscribe(() => this.layout.closeContextMenu())
    )
  }

  ngOnInit() {
    this.isSmallerThanMediumScreen = this.layout.isSmallerMediumScreen()
    this.checkLeftSideBarCollapse()
  }

  ngOnDestroy() {
    this.subscriptions.forEach((s) => s.unsubscribe())
  }

  @HostListener('window:resize')
  onResize() {
    this.layout.resizeEvent.next()
    const isSmallerThanMediumScreen = this.layout.isSmallerMediumScreen()
    if (this.isSmallerThanMediumScreen !== isSmallerThanMediumScreen) {
      this.isSmallerThanMediumScreen = isSmallerThanMediumScreen
      this.checkLeftSideBarCollapse()
    }
  }

  @HostListener('dragover', ['$event'])
  onDragOver(event: DragEvent) {
    event.preventDefault()
  }

  @HostListener('drop', ['$event'])
  onDrop(event: DragEvent) {
    event.preventDefault()
  }

  toggleLeftSideBar(status: number) {
    // 1: open / 2: collapse / 3: toggle
    if (status === 1) {
      this.openLeftSideBar()
      this.layout.leftSideBarIsOpen.next(true)
    } else if (status === 2) {
      this.collapseLeftSideBar()
      this.layout.leftSideBarIsOpen.next(false)
    } else if (this.document.body.classList.contains(this.leftSideBarOpenedClass)) {
      this.collapseLeftSideBar()
      this.layout.saveLeftSideBarIsOpen.next(false)
      this.layout.leftSideBarIsOpen.next(false)
    } else {
      this.openLeftSideBar()
      this.layout.saveLeftSideBarIsOpen.next(true)
      this.layout.leftSideBarIsOpen.next(true)
    }
  }

  toggleRightSideBar(show: boolean) {
    if (show) {
      this.renderer.addClass(this.document.body, this.rightSideBarClass)
    } else {
      this.renderer.removeClass(this.document.body, this.rightSideBarClass)
    }
  }

  private openLeftSideBar() {
    this.renderer.removeClass(this.document.body, this.leftSideBarCollapsedClass)
    this.renderer.addClass(this.document.body, this.leftSideBarOpenedClass)
  }

  private collapseLeftSideBar() {
    this.renderer.removeClass(this.document.body, this.leftSideBarOpenedClass)
    this.renderer.addClass(this.document.body, this.leftSideBarCollapsedClass)
  }

  private checkLeftSideBarCollapse() {
    if (this.isSmallerThanMediumScreen) {
      this.toggleLeftSideBar(2)
    } else {
      this.layout.saveLeftSideBarIsOpen.next(true)
      this.toggleLeftSideBar(1)
    }
  }

  private setTheme(theme: string) {
    this.renderer.removeClass(this.document.body, this.themeMode)
    this.themeMode = theme
    this.renderer.addClass(this.document.body, this.themeMode)
  }
}