Untitled

mail@pastecode.io avatar
unknown
typescript
a month ago
4.8 kB
4
Indexable
Never
import { ElementRef, Injectable, Renderer2, RendererFactory2 } from '@angular/core';
import { fromEvent } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class ResponsivityService {
  private initState: boolean = false;
  private renderer: Renderer2;
  originalElement: ElementRef;
  children: ElementRef[] = [];
  savedChildren: ElementRef[] = [];
  noResponsiveElements: ElementRef[] = [];
  burgerChildren: ElementRef[] = [];
  burgerElement: ElementRef = null;
  burgerHidden: boolean = true;
  previousWidth: number = 0;

  constructor(rendererFactory: RendererFactory2) {
    this.renderer = rendererFactory.createRenderer(null, null);
  }

  initResponsivity(headerElement: ElementRef, children: ElementRef[], burgerChildren: ElementRef[], noResponsiveElements: ElementRef[], burgerElement: ElementRef) {
    if (!this.initState) {
      console.log(children);
      this.settingInitialValues(headerElement, children, burgerChildren, noResponsiveElements, burgerElement);
      this.setVisibilityOfBurger(false);
      this.saveChildrenReferences();
      this.checkElementVisibility();
      this.checkWindowResize();
      this.initState = true;
    }
  }

  checkElementVisibility(): void {
    this.hideChildren(this.getChildrenToHide());
  }

  private settingInitialValues(headerElement: ElementRef, children: ElementRef[], burgerChildren: ElementRef[], noResponsiveElements: ElementRef[], burgerElement: ElementRef) {
    this.originalElement = headerElement;
    this.children = children;
    this.burgerChildren = burgerChildren;
    this.noResponsiveElements = noResponsiveElements;
    this.burgerElement = burgerElement;
  }

  private checkWindowResize() {
    fromEvent(window, 'resize').pipe(
      debounceTime(500)
    ).subscribe((event: Event) => {
      this.checkElementVisibility();
    });
  }

  private cloneElement(element: ElementRef): ElementRef {
    const clonedElement = element.nativeElement.cloneNode(true);
    return new ElementRef(clonedElement);
  }

  private saveChildrenReferences() {
    this.children.forEach((child: ElementRef) => {
      this.savedChildren.push(this.cloneElement(child));
    });
  }

  private getParentWidth() {
    const burgerWidth = this.burgerHidden ? 0 : this.burgerElement.nativeElement.offsetWidth;
    let noResponsiveChildrenWidth = 0;
    this.noResponsiveElements.forEach((noResponsiveElement: ElementRef) => {
      noResponsiveChildrenWidth += noResponsiveElement.nativeElement.offsetWidth;
    });
    return this.originalElement.nativeElement.offsetWidth - noResponsiveChildrenWidth - burgerWidth;
  }

  private getChildrenToHide(): ElementRef[] {
    const elementsToHide = [];
    const parentWidth = this.getParentWidth();
    let totalChildrenWidth = 0;

    for (let i = 0; i < this.children.length; i++) {
      const child = this.children[i];
      totalChildrenWidth += child.nativeElement.offsetWidth;
      if (totalChildrenWidth > parentWidth) {
        elementsToHide.push(child.nativeElement.nodeName.toLowerCase());
      }
    }
    return elementsToHide;
  }

  private hideChildren(elementsToHide: ElementRef[]) {
    elementsToHide.forEach(elementId => {
      const element = this.children.find(child => child.nativeElement.nodeName.toLowerCase() === elementId);
      if (element) {
        element.nativeElement.style.display = 'none'; // Skryje element
      }
    });

    if (elementsToHide.length > 0) {
      this.setVisibilityOfBurger(true);
    } else {
      this.setVisibilityOfBurger(false);
    }
  }

  private getChildrenToShow(): ElementRef[] {
    const elementsToShow = [];
    const parentWidth = this.getParentWidth();
    let totalSavedChildrenWidth = 0;

    for (let i = 0; i < this.savedChildren.length; i++) {
      const child = this.savedChildren[i];
      totalSavedChildrenWidth += child.nativeElement.offsetWidth;
      if (totalSavedChildrenWidth <= parentWidth) {
        elementsToShow.push(child.nativeElement.nodeName.toLowerCase());
      }
    }
    return elementsToShow;
  }

  private showChildren(elementsToShow: ElementRef[]) {
    elementsToShow.forEach(elementId => {
      const element = this.children.find(child => child.nativeElement.nodeName.toLowerCase() === elementId);
      const savedElement = this.savedChildren.find(child => child.nativeElement.nodeName.toLowerCase() === elementId);
      if (element) {
        element.nativeElement.style.display = savedElement.nativeElement.style.display; // Skryje element
      }
    });
  }

  private setVisibilityOfBurger(show: boolean) {
    if (show) {
      this.burgerElement.nativeElement.style.display = 'flex';
      this.burgerHidden = false;
    } else {
      this.burgerElement.nativeElement.style.display = 'none';
      this.burgerHidden = true;
    }
  }
}
Leave a Comment