All files / app/component/tabs tabs.component.ts

32.28% Statements 41/127
28.37% Branches 21/74
42.1% Functions 8/19
31.68% Lines 32/101

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197                              9x 9x                   38x   9x         9x 9x 9x     9x   9x     9x 9x 9x       9x 9x       9x         29x         9x 7x                           9x 9x 9x 9x 9x 9x 28x 28x 23x 15x 28x 28x   9x                                           9x                                                                                                                                                                    
import {
  AfterViewInit,
  ChangeDetectorRef,
  Component,
  ContentChildren,
  ElementRef,
  HostBinding,
  HostListener,
  QueryList,
  ChangeDetectionStrategy
} from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { RouterLink } from '@angular/router';
import { defer } from 'lodash-es';
import { ConfigService } from '../../service/config.service';
import { memo, MemoCache } from '../../util/memo';
import { SettingsComponent } from '../settings/settings.component';
 
@Component({
  selector: 'app-tabs',
  templateUrl: './tabs.component.html',
  styleUrl: './tabs.component.scss',
  host: { 'class': 'tabs' },
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [ReactiveFormsModule, SettingsComponent]
})
export class TabsComponent implements AfterViewInit {
 
  @ContentChildren(RouterLink)
  routerLinks!: QueryList<RouterLink>;
  @ContentChildren(RouterLink, { read: ElementRef })
  anchors!: QueryList<ElementRef>;
 
  options: string[] = [];
  map = new Map<string, number>();
  hidden = 0;
 
  @HostBinding('class.measuring')
  measuring = true;
 
  private resizeObserver = window.ResizeObserver && new ResizeObserver(() => this.onResize()) || undefined;
 
  constructor(
    private config: ConfigService,
    private el: ElementRef<HTMLElement>,
    private cd: ChangeDetectorRef,
  ) { }
 
  ngAfterViewInit() {
    this.updateTabs();
    this.anchors.changes.subscribe(value => {
      this.measuring = true;
      this.updateTabs();
    });
    defer(() => this.resizeObserver?.observe(this.el.nativeElement!.parentElement!));
  }
 
  @HostBinding('class.floating-tabs')
  get floatingTabs() {
    return this.config.mini || this.hidden > 0 && this.hidden === this.options.length;
  }
 
  @HostListener('window:resize')
  onResize() {
    if (!this.options.length) return;
    defer(() => {
      if (!document.body.classList.contains('fullscreen')) {
        this.measureVisible();
      }
    });
  }
 
  measureVisible() {
    if (!this.options.length) return;
    this.hidden = this.options.length - this.visible;
    this.hideTabs();
  }
 
  updateTabs() {
    MemoCache.clear(this);
    this.hidden = 0;
    this.options = [];
    this.map.clear();
    const tabs = this.anchors.toArray();
    for (const t of tabs) {
      const el = t.nativeElement as HTMLAnchorElement;
      if (el.tagName !== 'A') continue;
      if (el.classList.contains('logo')) continue;
      const value = el.title || el.innerText;
      this.options.push(value);
      this.map.set(value, tabs.indexOf(t));
    }
    defer(() => this.onResize());
  }
 
  hideTabs() {
    const tabs = this.anchors.toArray();
    let i = this.options.length - 1;
    for (const t of tabs) {
      const el = t.nativeElement as HTMLAnchorElement;
      if (el.tagName !== 'A') continue;
      if (el.classList.contains('logo')) continue;
      if (el.classList.contains('current-tab')) {
        el.style.display = 'inline-block';
      } else {
        el.style.display = i > this.hidden ? 'inline-block' : 'none';
        i--;
      }
    }
    this.measuring = false;
    this.cd.markForCheck();
  }
 
  @memo
  get tabWidths() {
    const result: number[] = [];
    const tabs = this.anchors.toArray();
    for (const t of tabs) {
      const el = t.nativeElement as HTMLAnchorElement;
      if (el.tagName !== 'A') continue;
      if (el.classList.contains('logo')) continue;
      result.push(el.offsetWidth + 8.5);
    }
    return result;
  }
 
  get currentTabWidth() {
    const el = this.el.nativeElement;
    for (let i = 0; i < el.children.length; i++) {
      const e = el.children[i] as HTMLElement;
      if (!e.classList.contains('current-tab')) continue;
      return e.offsetWidth + 8.5;
    }
    return 0;
  }
 
  /**
   * Widths of permanent children including the overflow dropdown.
   */
  get childWidths() {
    const el = this.el.nativeElement;
    const result: number[] = [];
    let mobileSelect = false;
    for (let i = 0; i < el.children.length; i++) {
      const e = el.children[i] as HTMLElement;
      if (e.tagName === 'A' && !e.classList.contains('logo')) continue;
      if (this.config.mobile) {
        if (e.tagName === 'H5') continue;
        if (e.classList.contains('logo')) continue;
      }
      if (e.classList.contains('mobile-tab-select')) mobileSelect = true;
      result.push(e.offsetWidth + (e.classList.contains('settings') ? 0 : 8));
    }
    if (!mobileSelect) {
      result.push(52);
    }
    return result;
  }
 
  /**
   * Number of visible tabs, including the current tab.
   */
  get visible() {
    const current = this.currentTabWidth;
    if (!current) return this.options.length;
    if (this.config.mini) return 0;
    const el = this.el.nativeElement;
    const width = el.offsetWidth - 2;
    let result = 1;
    let childWidth = current + this.childWidths.reduce((a, b) => a + b);
    if (childWidth > width) return 0;
    let skipped = false;
    for (const w of this.tabWidths) {
      if (!skipped && w === current) {
        skipped = true;
        continue;
      }
      childWidth += w;
      if (childWidth + current < width) {
        result++;
      } else {
        return result;
      }
    }
    return this.options.length;
  }
 
  nav(select: HTMLSelectElement) {
    if (select.value && this.map.has(select.value)) {
      this.routerLinks.get(this.map.get(select.value)!)?.onClick(0, false, false, false, false);
    }
    select.selectedIndex = 0;
    this.measureVisible();
  }
 
}