All files / app/component/notebook notebook.component.ts

54.28% Statements 38/70
28.84% Branches 15/52
36.36% Functions 8/22
55% Lines 33/60

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 17067x                   67x 67x 67x 67x                         67x 1x             1x   1x   1x   1x   1x   1x   1x   1x       1x   1x     1x 1x         1x     1x 1x 1x 1x               2x                                               2x 2x             2x 2x                           4x                                     1x           1x 1x                                      
import { Component, Input, OnDestroy, OnInit, QueryList, ViewChildren } from '@angular/core';
import { Router } from '@angular/router';
import { catchError, forkJoin, Observable, of, Subject, takeUntil } from 'rxjs';
import { HasChanges } from '../../guard/pending-changes.guard';
import { Ext } from '../../model/ext';
import { Page } from '../../model/page';
import { Ref } from '../../model/ref';
import { AccountService } from '../../service/account.service';
import { RefService } from '../../service/api/ref.service';
import { Store } from '../../store/store';
import { LoadingComponent } from '../loading/loading.component';
import { PageControlsComponent } from '../page-controls/page-controls.component';
import { RefComponent } from '../ref/ref.component';
import { NoteComponent } from './note/note.component';
 
@Component({
  selector: 'app-notebook',
  templateUrl: './notebook.component.html',
  styleUrl: './notebook.component.scss',
  host: { 'class': 'notebook ext' },
  imports: [
    NoteComponent,
    PageControlsComponent,
    LoadingComponent,
  ],
})
export class NotebookComponent implements OnInit, OnDestroy, HasChanges {
  private destroy$ = new Subject<void>();
 
  @Input()
  hide?: number[];
  @Input()
  plugins?: string[];
  @Input()
  showPageLast = true;
  @Input()
  showAlarm = true;
  @Input()
  pageControls = true;
  @Input()
  emptyMessage = 'No results found';
  @Input()
  showToggle = true;
  @Input()
  expandInline = false;
  @Input()
  showVotes = false;
  @Input()
  hideNewZeroVoteScores = true;
  @Input()
  newRefs$?: Observable<Ref | undefined>;
  @Input()
  showPrev = true;
 
  @ViewChildren(RefComponent)
  list?: QueryList<RefComponent>;
 
  pinned: Ref[] = [];
  newRefs: Ref[] = [];
 
  private _page?: Page<Ref>;
  private _ext?: Ext;
  private _expanded?: boolean;
  private _cols = 0;
 
  constructor(
    private accounts: AccountService,
    private router: Router,
    private store: Store,
    private refs: RefService,
  ) { }
 
  saveChanges() {
    return !this.list?.find(r => !r.saveChanges());
  }
 
  get ext() {
    return this._ext;
  }
 
  @Input()
  set ext(value: Ext | undefined) {
    this._ext = value;
    if (!value?.config?.pinned?.length) {
      this.pinned = [];
    } else {
      forkJoin((value.config.pinned as string[])
        .map(pin => this.refs.getCurrent(pin).pipe(
          catchError(err => of({ url: pin })),
          takeUntil(this.destroy$),
        )))
        .subscribe(pinned => this.pinned = pinned);
    }
  }
 
  @Input()
  set cols(value: number | undefined) {
    this._cols = value || 0;
  }
 
  get colStyle() {
    if (!this.cols) {
      return '';
    } else E{
      return ' 1fr'.repeat(this.cols);
    }
  }
 
  get cols() {
    Iif (this._cols) return this._cols;
    return this.ext?.config?.defaultCols;
  }
 
  get expanded(): boolean {
    if (this._expanded === undefined) return this._ext?.config?.defaultExpanded;
    return this._expanded;
  }
 
  @Input()
  set expanded(value: boolean) {
    this._expanded = value;
  }
 
  get page(): Page<Ref> | undefined {
    return this._page;
  }
 
  @Input()
  set page(value: Page<Ref> | undefined) {
    this._page = value;
    if (this._page) {
      if (this._page.page.number > 0 && this._page.page.number >= this._page.page.totalPages) {
        this.router.navigate([], {
          queryParams: {
            pageNumber: this._page.page.totalPages - 1,
          },
          queryParamsHandling: 'merge',
        });
      }
    }
  }
 
  ngOnInit(): void {
    this.newRefs$?.pipe(
      takeUntil(this.destroy$),
    ).subscribe(ref => ref && this.addNewRef(ref));
  }
 
  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
 
  addNewRef(ref: Ref) {
    // TODO: verify read before clearing?
    this.accounts.clearNotificationsIfNone(ref.modified);
    if (!this.page?.content.find(r => r.url === ref.url)) {
      const index = this.newRefs.findIndex(r => r.url === ref.url);
      if (index !== -1) {
        this.newRefs[index] = ref;
      } else {
        this.newRefs = [ref, ...this.newRefs];
        return;
      }
    }
    this.store.eventBus.refresh(ref);
    this.store.eventBus.reset();
  }
}