|
| 1 | +import { CommonModule } from '@angular/common'; |
| 2 | +import { AfterViewInit, Component, ElementRef, HostListener, Input, ViewChild } from '@angular/core'; |
| 3 | + |
| 4 | +@Component({ |
| 5 | + selector: 'om-scrollbar', |
| 6 | + standalone: true, |
| 7 | + imports: [CommonModule], |
| 8 | + templateUrl: "./ngx-scrollbar.component.html", |
| 9 | + styleUrl: "./ngx-scrollbar.component.scss", |
| 10 | +}) |
| 11 | +export class NgxScrollbarComponent implements AfterViewInit { |
| 12 | + @ViewChild('OmScrollbarContainer') sidebarRef!: ElementRef<HTMLElement>; |
| 13 | + @ViewChild('OmScrollbarWrapper') wrapperRef!: ElementRef<HTMLElement>; |
| 14 | + @ViewChild('OmScrollbarContent') contentRef!: ElementRef<HTMLElement>; |
| 15 | + @ViewChild('OmScrollbar') scrollbarRef!: ElementRef<HTMLElement>; |
| 16 | + |
| 17 | + @Input('styleClass') |
| 18 | + styleClass?: string; |
| 19 | + |
| 20 | + style: any = {}; |
| 21 | + |
| 22 | + private scrollPercent = 0; |
| 23 | + mouseDown = false; |
| 24 | + private lastY?: number; |
| 25 | + |
| 26 | + @HostListener('mousedown', ['$event']) |
| 27 | + onMouseDown(event: MouseEvent) { |
| 28 | + const target = event.target as HTMLElement; |
| 29 | + |
| 30 | + if (target.classList.contains('om-scrollbar-bar')) { |
| 31 | + event.preventDefault(); |
| 32 | + this.mouseDown = true; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @HostListener('mouseup') |
| 37 | + onMouseUp() { |
| 38 | + this.lastY = undefined; |
| 39 | + this.mouseDown = false; |
| 40 | + } |
| 41 | + |
| 42 | + ngAfterViewInit(): void { |
| 43 | + window.addEventListener('mouseup', () => { |
| 44 | + this.lastY = undefined; |
| 45 | + this.mouseDown = false; |
| 46 | + }); |
| 47 | + window.addEventListener('mousemove', (event) => this.onDrag(event)); |
| 48 | + |
| 49 | + this.calcScrollbar(); |
| 50 | + } |
| 51 | + |
| 52 | + calcScrollbar(): void { |
| 53 | + const sideBarHeight = this.sidebarRef.nativeElement.clientHeight; |
| 54 | + const contentHeight = this.contentRef.nativeElement.clientHeight; |
| 55 | + const scrollBarHeight = Math.floor(sideBarHeight / contentHeight * 100); |
| 56 | + |
| 57 | + this.style['--om-scrollbar-height'] = `${scrollBarHeight}%`; |
| 58 | + } |
| 59 | + |
| 60 | + onScroll(event: Event): void { |
| 61 | + const target = event.target as HTMLElement; |
| 62 | + const contentHeight = this.contentRef.nativeElement.clientHeight; |
| 63 | + const scrollTop = target.scrollTop; |
| 64 | + |
| 65 | + this.scrollPercent = Math.floor(scrollTop / contentHeight * 100); |
| 66 | + |
| 67 | + this.style['--om-scrollbar-top'] = `${this.scrollPercent}%`; |
| 68 | + } |
| 69 | + |
| 70 | + onDrag(event: MouseEvent): void { |
| 71 | + if (!this.mouseDown) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + event.preventDefault(); |
| 76 | + |
| 77 | + const offsetY = event.clientY; |
| 78 | + |
| 79 | + const scrollPx = offsetY - (this.lastY ?? offsetY); |
| 80 | + this.lastY = offsetY; |
| 81 | + |
| 82 | + const contentHeight = this.sidebarRef.nativeElement.clientHeight; |
| 83 | + const scrollPercent = (scrollPx) / (contentHeight); |
| 84 | + |
| 85 | + const scrollContentPx = this.contentRef.nativeElement.clientHeight * scrollPercent; |
| 86 | + |
| 87 | + this.wrapperRef.nativeElement.scrollBy(0, scrollContentPx); |
| 88 | + } |
| 89 | +} |
0 commit comments