All files / app/component/ref/ref-list ref-list.component.ts

56.79% Statements 46/81
39.68% Branches 25/63
45.83% Functions 11/24
57.74% Lines 41/71

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 19067x               67x       67x 67x 67x                         67x 17x             17x   17x   17x   17x   17x   17x   17x   17x       17x   17x   17x     17x 17x         17x     17x 17x 17x 17x               96x         2x 2x 2x                         2x       96x 96x             96x 96x                           112x         16x 16x 3x                         17x           17x 17x                                                                      
import { Component, forwardRef, Input, OnDestroy, OnInit, QueryList, ViewChildren } from '@angular/core';
import { Router } from '@angular/router';
import { DateTime } from 'luxon';
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 { score } from '../../../mods/vote';
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.component';
 
@Component({
  selector: 'app-ref-list',
  templateUrl: './ref-list.component.html',
  styleUrls: ['./ref-list.component.scss'],
  host: { 'class': 'ref-list' },
  imports: [
    forwardRef(() => RefComponent),
    PageControlsComponent,
    LoadingComponent,
  ],
})
export class RefListComponent 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 = $localize`No results found`;
  @Input()
  showToggle = true;
  @Input()
  expandInline = false;
  @Input()
  showVotes = false;
  @Input()
  hideNewZeroVoteScores = true;
  @Input()
  newRefs$?: Observable<Ref | undefined>;
  @Input()
  insertNewAtTop = false;
  @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 E{
      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) {
      Iif (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',
          replaceUrl: true,
        });
      }
    }
  }
 
  ngOnInit(): void {
    this.newRefs$?.pipe(
      takeUntil(this.destroy$),
    ).subscribe(ref => ref && this.addNewRef(ref));
  }
 
  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
 
  getNumber(i: number) {
    if (this.showVotes) {
      const votes = score(this.page!.content[i]);
      if (votes < 100 &&
        this.hideNewZeroVoteScores &&
        DateTime.now().diff(this.page!.content[i].created!, 'minutes').minutes < 5) {
        return '•';
      }
      return votes;
    }
    return i + this.page!.page.number * this.page!.page.size + 1;
  }
 
  addNewRef(ref: Ref) {
    // TODO: verify read before clearing?
    this.accounts.clearNotificationsIfNone(ref.modified);
    if (ref.url !== this.store.view.url && !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 if (this.insertNewAtTop) {
        this.newRefs = [ref, ...this.newRefs];
        return;
      } else {
        this.newRefs = [...this.newRefs, ref];
        return;
      }
    }
    this.store.eventBus.refresh(ref);
    this.store.eventBus.reset();
  }
}