All files / app/component/nav nav.component.ts

61.42% Statements 43/70
33.33% Branches 22/66
58.33% Functions 7/12
64.91% Lines 37/57

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 12068x                 68x 68x               68x 1x     1x   1x   1x   1x   1x         1x 1x 1x 1x 1x 1x 1x       1x 1x 1x                                       1x 1x       1x     1x 1x 1x 1x   1x     1x 1x     1x 1x 1x       2x       1x 1x 1x                                        
import { Component, ElementRef, Input, OnDestroy, OnInit } from '@angular/core';
import { RouterLink } from '@angular/router';
import { Subject, takeUntil } from 'rxjs';
import { AdminService } from '../../service/admin.service';
import { RefService } from '../../service/api/ref.service';
import { TaggingService } from '../../service/api/tagging.service';
import { ConfigService } from '../../service/config.service';
import { EditorService } from '../../service/editor.service';
import { VisibilityService } from '../../service/visibility.service';
import { getPath, parseBookmarkParams } from '../../util/http';
import { hasPrefix } from '../../util/tag';
 
@Component({
  selector: 'app-nav',
  templateUrl: './nav.component.html',
  styleUrls: ['./nav.component.scss'],
  imports: [RouterLink]
})
export class NavComponent implements OnInit, OnDestroy {
  private destroy$ = new Subject<void>();
 
  @Input()
  url: string = '';
  @Input()
  title = '';
  @Input()
  text = '';
  @Input()
  css = '';
  @Input()
  external = false;
 
  nav?: (string|number)[];
 
  constructor(
    private config: ConfigService,
    private admin: AdminService,
    private refs: RefService,
    private ts: TaggingService,
    private editor: EditorService,
    private vis: VisibilityService,
    private el: ElementRef,
  ) { }
 
  ngOnInit() {
    if (this.localUrl) {
      this.nav = this.getNav();
      Iif (this.nav[0] === '/tag' && !this.external && !this.hasText) {
        this.editor.getTagPreview(this.nav[1] as string)
          .pipe(takeUntil(this.destroy$))
          .subscribe(x => {
            this.text = x?.name || this.text || x?.tag || '';
            this.title ||= x?.tag || '';
          });
      }
    E} else if (!this.external) {
      this.vis.notifyVisible(this.el, () => {
        this.refs.exists(this.url).pipe(takeUntil(this.destroy$)).subscribe(exists => {
          if (exists) {
            this.nav = ['/ref', this.url];
          }
        });
      });
    }
  }
 
  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
 
  getNav() {
    Iif (this.url.toLowerCase().startsWith('tag:/')) {
      return ['/tag', getPath(this.url.substring('tag:'.length))!.substring(1)];
    }
    let path = getPath(this.url) || '';
    const basePath = getPath(this.config.base)!;
    Eif (path.startsWith(basePath)) {
      path = path.substring(basePath.length);
    }
    Iif (path.startsWith('/')) {
      path = path.substring(1);
    }
    const parts = path.split('/');
    Iif (path.startsWith('ref/e/')) {
      return ['/ref', + decodeURIComponent(parts[2])];
    }
    const route = parts[0];
    parts.splice(0, 1);
    return ['/' + route, parts.join('/')];
  }
 
  get query() {
    return parseBookmarkParams(this.url);
  }
 
  get localUrl() {
    Iif (this.url.toLowerCase().startsWith('tag:/'))return true
    Iif (this.url.startsWith(this.config.base)) return true
    Eif (this.url.startsWith(getPath(this.config.base)!)) return true;
    if (this.url.startsWith('/')) return true;
    return false;
  }
 
  get hasText() {
    if (!this.text || hasPrefix(this.text, 'user') || hasPrefix(this.text, 'plugin')) return false;
    if (this.url.startsWith('/tag/') || this.url.toLowerCase().startsWith('tag:/')) {
      if (this.text === '#' + this.url.substring(5)) return false;
    }
    return this.text != this.url;
  }
 
  markRead(event: MouseEvent) {
    if (!this.admin.getPlugin('plugin/user/read')) return;
    if (event.button !== 0 && event.button !== 1) return;
    this.ts.createResponse('plugin/user/read', this.url).subscribe();
  }
 
}